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")
Related
I'm new to python and programming in general and am working on a final project for a python centric class. One of the requirements that I cant seem to figure out how to make work is to integrate recursion into our code in order to show a working knowledge. I've worked up a simple "bullet hell" style game using pygame.
My goal is that when contact is made between a bullet and an enemy, that a series of bullet sets will be launched from the player position as a sort of short-term modifier.
This code runs in the main loop whenever a bullet hits an enemy:
for i in reversed(range(len(bullets))):
for j in reversed(range(len(enemies))):
if bullets[i].collided(enemies[j].rect):
del enemies[j]
del bullets[I]
s.global_score += 100
more_bullets(10)
#print("Hit!")
#print(s.global_score)
break
The "more_bullets" function is the focus of my recursion, and calls this:
def more_bullets(n):
if(n > 0):
spawnx = sq.rect.x+10 + sq.rect.width/2 - 10
b = Square(s.red, spawnx,sq.rect.y, 10,30)
b.direction = 'N'
b.player_speed = 10
bullets.append(b)
spawnx = sq.rect.x-10 + sq.rect.width/2 - 10
b = Square(s.red, spawnx,sq.rect.y, 10,30)
b.direction = 'N'
b.player_speed = 10
bullets.append(b)
pygame.display.update()
more_bullets(n-1)
print(f"Fired x {n}")
The outcome currently is that my debug does print 10 times making me think that the recursion is functioning correctly, however only one set of bullets is firing when the collision occurs. I'm thinking that all 10 bullets are firing faster than I can register it and just stacking on the screen.
Is there an easy-to-use function that might slow down the firing of the bullets? or have messed up something more fundamentally here?
I'm thinking that all 10 bullets are firing faster than I can register it and just stacking on the screen.
You're correct.
I don't believe there's an "easy way" to do what you're asking the way you think. The recursion is immediate, meaning that the function runs 10 times right away when it's called. For it to send out a burst of staggered bullets, you'd need some kind of timer or queue or something along those lines that runs alongside your main loop, and recursion isn't really a natural fit for that. The same will go for any kind of game logic function that plays out over a period of time.
This isn't what's being asked, but here's an idea of what you could do, even though it's kind of a redundant use of recursion: add a parameter to that function that dictates the angle the bullet is being shot. Then, add a little bit to that parameter on every recursive call. That way an arc of bullets will be shot at the same time.
Trying to create a text-based adventure that involves interacting with a computer. So I found a way to get text to appear as though it's being typed onto the screen but it was too slow initially, so I bumped up the speed but now it's lagging a bit and several characters are appearing at a time instead of one at a time like on a computer.
I tried adding in the fps = 60, time_delta = 1./fps and upping the fps but it didn't do anything.
import time
fps = 60
time_delta = 1./fps
def delay_print(s):
for c in s:
sys.stdout.write(c)
sys.stdout.flush()
time.sleep (0.05)
You tried to change the values of fps and time_delta, but you didn't use them in your command
time.sleep (0.05)
You probably wanted
time.sleep(time_delta)
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
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?
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!