I'm creating a game and at the "start" screen I want it to pop up a picture after 45 seconds of not starting the game saying "Are you not going to play?"
However, I am completely lost at what to do, so if anyone have any clue on how to help that would be really appreciated.
You probably have a timer for your game, like this:
pygame.time.Clock.tick(fps)
Each time your main loop runs, it ticks your fps, so your game could run smoothly.
Now, just add a variable, called, say, tick_counter
Now, in your code, do something like this:
fps = 25
tick_counter = 0
while RUNNING:
#Do stuff, check for if close window, etc
pygame.time.Clock.tick(fps)
tick_counter += 1
if tick_counter >= 1125: #45 seconds if you are doing 25 fps. If your fps is different, just calculate it: 45 seconds = 45*fps
#Pop up the picture!
You can set a timer and an event on the event queue. This answer shows how to do that. How can I detect if the user has double-clicked in pygame?
Related
I took a computer science course this year (grade 10) and the final project is to make a game. For my game I want to add a hunger element where you start with 0 hunger and every minute you hunger goes up by 1 (in the game you buy food items to make your hunger go back down, but I will add that later). If your hunger reaches all the way to 10 (this would be after 11 minutes). You “die” and lose all your game progress - the program crashes (pygame.quit())
Can anyone help me with this doing this, I’m not really sure as I am extremely new to coding.
Thanks!
Have you tried generating any code for it yet? It is not really fair to ask people to give you code for your assessment without doing some real research and trying to get some code working for yourself first. Maybe update your question with anything you have attempted towards this question, to give a better explanation for what you are really needing help with? If you have not started yet, here is some guidance to get you started:
First of all you will need something that would generate a time counter for you, so you know when a minute passes. Check out: Countdown timer in Pygame for guidance on generating a timer and how to get how many seconds have passed. There is also a link in that question to the pygame timer documentation that would be very helpful.
Then, you just need to set up a "hunger" variable that gets added to every time 60 seconds have been reached. And once it hits 10, then have the game over method initiated.
Check out
http://www.pygame.org/docs/ref/time.html#pygame.time.get_ticks
Heres some code to get you going..
import pygame
run_app = True
time_passed = 0
COUNTER = 60 # Set to 60 seconds / 1 minute.
pygame.init()
start_ticks=pygame.time.get_ticks()
# Begin game loop.
while run_app:
# Convert to seconds.
time = (pygame.time.get_ticks()) / 1000
# True every 60 seconds / 1 minute.
if time > (time_passed + COUNTER):
time_passed = time
print("Do something here regarding hunger, Increase by 1")
I'm trying to make a countdown timer like little script. I want it to look like this:
0 years left until your event.
0 months left until your event.
10 days left until your event.
234 hours left until your event.
14020 minutes left until your event.
841166 seconds left until your event.
As you can see it's not a "normal" countdown. I want it to be like this, total amount of seconds, total amount of minutes left etc. Now I'm running into trouble trying to make it actually count-down. Just the seconds are working fine, I'm using this piece of code for that:
def secondsLeft(eventDate):
while secondsUntil:
toPrint = "%s seconds left until your event." %(secondsUntil)
print(toPrint, end='\r')
time.sleep(1)
secondsUntil -= 1
However, when trying to do the same for the minutes, replacing with time.sleep(60) it's not printing out the seconds anymore!
I think it has to do with the while loop. However, I don't know what to do about it... Any help in the right direction is appreciated!!
If you wait a minute the whole program will be stopped for a minute. It can't decrease the seconds. What you want to do is, keep waiting only a second per while interation and add this line mins, secs = divmod(secondsUntil, 60). You can print mins for the minutesUntil
How do you make the code stop for a moment before checking for something else? (I'm new to code)
if BonusTime==True and Drop==True:
if event.type == pygame.MOUSEBUTTONDOWN:
window.blit(Fired,(mouseX-12,mouseY-12))
Cankill=True
#I want it to delay here
Cankill=False
There is a cross hair that follows the mouse and when I click it, it fires. The problem is, you can just hold the mouse down and leave the cross hair in one place. Whenever an enemy walks into the cross hair, they die. I need it so even when you hold it will only fire once. I plan to make it delay the if statement, to set "Cankill" to true and then wait a second, after waiting, it should set "Cankill" to false. I've already looked through a lot of other people's questions similar to this and haven't found something I can understand. So if you could please help me find out how to delay it in the way I need.
"Pausing" is not what you want - if you do that, your game will just freeze, and nothing will move (since you are usign the OS mouse pointer, maybe it could move).
Anyway, the function to "pause" inside a game is pygame.time.wait(1000) - the number is in miliseconds.
Waht you actually need is to mark down the time the click was made, continue with the game, and when 1 second has passed, reset the variable back to the other state.
Something along:
last_trigger = 0
while True:
# game updating code goes here (getting events, and such)
...
if Cankill and pygame.time.get_ticks() - last_trigger > 1000:
Cankill = False
if event.type == pygame.MOUSEBUTTONDOWN:
window.blit(Fired,(mouseX-12,mouseY-12))
Cankill=True
last_trigger = pygame.time.get_ticks()
The "get_ticks" call returns the number of miliseconds that passed since pygame was initialized - and is usefull for this time of timers.
This is a really simple question, and I don't know why I haven't got the answer to it, but does anyone know how to correctly add a timer in pygame for Python 3.4.1?
Here's what I have so far:
texta = font.render("Time:"+str(time), True, black)
screen.blit(texta, [500,100])
I have read solutions using loops and many others, but none have worked so far. I want a timer to be displayed on the screen and count the seconds it takes for the user to perform a certain task.
Here is a timer you can use
import pygame, sys
from pygame.locals import *
clock = pygame.time.Clock()
time = 0 #In Seconds
#GameLoop
while True:
milli = clock.tick() #clock.tick() returns how many milliseconds passed since the last time it was called
#So it tells you how long the while loop took
seconds = milli/1000.
time += seconds
print time #So you can see that this works
You can use jues velarde's code above and just print the time variable onto to your pygame surface using
font = pygame.font.Font(YourFont, YourSizeOfText)
text = font.render(str(time), 1, (YourColour)) # The time variable being in jues's code
DISPLAYSURF.blit(text, (YourXValue, YourYValue)
and you would just call this code in a loop when you want too, in your case, when you want to begin timing the user's task, But remember to run jues's code in your program as well so this will work.
Hope i helped!
I want to have a delay between firing bullets for my character. I used to do this before in Java like:
if (System.currentTimeMillis() - lastFire < 500) {
return;
}
lastFire = System.currentTimeMillis();
bullets.add(...);
However, how can I do this in pygame way, in order to get that sys currentTimeMillis.This is how my run (game loop) method looks like:
time_passed = 0
while not done:
# Process events (keystrokes, mouse clicks, etc)
done = game.process_events()
# Update object positions check for collisions...
game.update()
# Render the current frame
game.render(screen)
# Pause for the next frame
clock.tick(30)
# time passed since game started
time_passed += clock.get_time()
As you can see in the previous code, I have created time passed, but I'm not sure if that is correct way of code order, and what else im missing.
Your code is fine, if game.process_events and game.update works as expected.
Edit:
Use pygame.time.get_ticks instead of the clock I mentioned earlier. It was using python's time module, so that clock and clock in your code meant different clocks. This is much better way.
#this should be done only once in your code *anywhere* before while loop starts
newtime=pygame.time.get_ticks()
# when you fire, inside while loop
# should be ideally inside update method
oldtime=newtime
newtime=pygame.time.get_ticks()
if newtime-oldtime<500: #in milliseconds
fire()
Let me explain you what pygame.time.get_ticks() returns:
On first call, it returns time since pygame.init()
On all later calls, it returns time (in milliseconds) from the first call to get_ticks.
So, we store the oldtime and substract it from newtime to get time diff.
Or, even simpler, you can use pygame.time.set_timer
Before your loop:
firing_event = pygame.USEREVENT + 1
pygame.time.set_timer(firing_event, 500)
Then, an event of type firing_event will be posted onto the queue every 500 milliseconds. You can use this event to signal when to fire.