Football pygame, need help on timer [closed]

2024/9/22 17:31:27

I want to make a football game and I want to make it so that the player only gets 10 seconds to get to the end or else the game ends.

How can I do that?

Answer

I would suggest using pygame.time.set_timer() and pygames event mechanics since you already likely have an event processing loop in your game. See the set_timer docs here.

You do something like this:

pygame.time.set_timer(pygame.USEREVENT, 10000)

to start the a 10 second timer that will trigger a pygame.USEREVENT when it goes off. You would watch for that event in the event loop by adding a test like this:

if event.type == pygame.USEREVENT:# Countdown expired

I doubt that you require it, but if you need multiple timers and need to be able to tell them apart, you can create an event with an attribute that you can set to different values to track them. Something like this:

my_event = pygame.event.Event(pygame.USEREVENT, {"tracker": "gameover"})
pygame.time.set_timer(my_event , 2000)

[edit]

For other events you just create more of them and change what is set in the "tracker", like this:

my_other_event = pygame.event.Event(pygame.USEREVENT, {"tracker": "something_else"})

Then in your event loop you look for them. Something like this:

        for event in pygame.event.get():...# You found your event. Do something about itif event == my_event:...# You found your other event. Do something about itif event == my_other_event:...

Hope that clarifies things.

https://en.xdnf.cn/q/119109.html

Related Q&A

Unable to Install GDAL Using PIP on Python

Im trying to install gdal in python3.8.8 (Windows 10) and im getting below error I have install Visual Studio Build Tools 2019 and reboot my PC Downgrade my Python from 3.9.5 to 3.8.8 C:\Program Files…

Append text to the last line of file with python

First, I use echo hello, >> a.txt to create a new file with one line looks like that. And I know \n is at the last of the line.Then I get some data from python, for example "world", I w…

Python Caesar Cipher [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 2…

Indent Expected? [duplicate]

This question already has answers here:Im getting an IndentationError (or a TabError). How do I fix it?(6 answers)Closed 7 months ago.Im sort of new to python and working on a small text adventure its…

Convert QueryDict to key-value pair dictionary

I have a QueryDict that I get from request.POST in this format: <QueryDict: {name: [John], urls: [google.com/\r\nbing.com/\r\naskjeeves.com/], user_email: [[email protected]]}>Why are the values …

How to test items with each other in 2 dimensional list?

We have a 2 dimensional list (for this example we have it populated with unique 6 nodes and 3 masks)myList = [[node1, mask1], [node2, mask1], [node3, mask1], [node4, mask2], [node5, mask2], [node6, mas…

InsecureRequestWarning + MarkupResemblesLocatorWarning:

Id like to scrape a site for my office work. I am learning each day. I need your support guys. Here is the Code: url = https://www.eprocure.gov.bd/partner/ViewTenderPaymentDetails.jsp?payId=33767442&a…

Fetching images from URL and saving on server and/or Table (ImageField)

Im not seeing much documentation on this. Im trying to get an image uploaded onto server from a URL. Ideally Id like to make things simple but Im in two minds as to whether using an ImageField is the b…

Comparing list with a list of lists

I have a list string_array = [1, 2, 3, 4, 5, 6] and a list of lists multi_list = [[1, 2], [2, 3], [2, 4], [4, 5], [5, 6]]The first element of each sub-list in multi_list will have an associated entry …

Cannot save data to database Python

I have a table called category TABLES["category"] = ("""CREATE TABLE category (category_id INTEGER NOT NULL AUTO_INCREMENT,category_name VARCHAR(120) NOT NULL,PRIMARY KEY (cate…