Code works only when i use print() in pygame library - python

This chunk of code only works when i declare the print("abc"), otherwise it just won't work at all for no aparent reason
Im using pygame for a Minesweeper project that im doing
works:
for Sprite in self.CellsSprites:
if Sprite.rect.colliderect(self.rect):
print("abc")
if time.time() - self.time > 0.1 and self.block == False:
self.block = True
self.time = time.time()
Cell_Hold((Sprite.rect.x,Sprite.rect.y),{self.CellsSprites})
break
doesn't work:
for Sprite in self.CellsSprites:
if Sprite.rect.colliderect(self.rect):
if time.time() - self.time > 0.1 and self.block == False:
self.block = True
self.time = time.time()
Cell_Hold((Sprite.rect.x,Sprite.rect.y),{self.CellsSprites})
break
What this does is that it looks for every sprite that collides with another one and draws another sprite on top of it

time.time() - self.time > 0.1
Maybe the print("abc") line slows the execution time just enough for this if clause to be true? Whereas without the print line, the code runs too fast so that this clause is false? Try lowering the float number a bit and see if you notice a difference. Or else pause the execution for a millisecond in place of the print statement?
EDIT: Adding a useful suggestion into this answer, provided by #Jskkc who commented below, use pygame.time.Clock() to limit the frame rate - this is a much better way than artificially limiting the update with the clumsy if block, which as you found out is affected by whatever code may or may not have been executed beforehand.

Related

How could I make a dash in a 2D pygame python program?

If the pygame program is just a basic entity you can move normally with arrow keys, how could i make it so that if space is pressed, based on the arrow key that was being held at the moment of pressing, a player dashes slightly to the specified direction? My idea is that when the space is pressed, program checks if other keys are being held down and based on that it rapidly increases the x and/or y coordinate and then stops at specific time, but I don't know how to make it stop as all of this is happening inside a main game loop. Any insight is highly appreciated.
You can use time.perf_counter.
So for using that function you will need to import module time
import time
And now you can store the value of time.perf_counter in a variable when space is pressed.
import time
jumping = False
start_time = 0
while True:
if ("space is pressed"): # Replace condition with what you want
jumping = True
start_time = time.perf_counter()
# Add your code here
if jumping:
# change of x, y and other jumping mechanism code here
if jumping and time.perf_counter() - start_time > 1: # Replace 1 with the amount you want this if condition to trigger, also add value in seconds
jumping = False
I'm not sure what your code is like, but this is how I'd go about it:
def dash(self):
keys = pygame.keys.get_pressed()
if keys[pygame.K_SPACE] and not self.dashing and self.jumping:
# ^^ Can be any key
if self.facing_right:
self.x += 20
if self.facing_left:
self.x -= 20
self.dashing = True
Make sure that when you hit the ground it does self.dashing = False else you will be able to dash forever.
Just put this function in the player class if you are doing it OOP.
Else if you are not using classes, put it anywhere in the file and take out all self..

Python Tkinter Tk root.after Delay

I'm trying to do a chess clock using tkinter, and to do so i'm using the root.after method from the class Tk of tkinter. When the program starts, it runs really well, but after a while the clock start to get slower and slower, but if i start shaking my mouse, the clock starts to run fast again. For a clock, time precision is crucial, so i can't afford to run the program in the way that is working now. How can i solve this problem?
def RunClock(self):
"""
Method that runs and change the clock info
"""
#t0 = time.time()
if self.playing:
#Time remaining in milliseconds
clock = self.clock
minutes = clock//60000
clock %= 60000
sec = clock//1000
clock %= 1000
mil = clock//10
#If the turn is of player 1
if self.turn == 1:
self.WriteClock(self.canvas1, self.play1, "%.2i:%.2i:%.2i"%(minutes, sec, mil))
else:
self.WriteClock(self.canvas2, self.play2, "%.2i:%.2i:%.2i"%(minutes, sec, mil))
#tf = time.time()
#deltat = (tf - t0)
#s = 1 - deltat
self.rel -= 1
#if s > 0:
# time.sleep(s/1000)
#else:
# self.rel += s*1000
self.root.after(1, self.RunClock)
Note: The time to run the function is very low (you can calculate it with the commented tf and t0 variables), so i didn't even consider it in the time interval
As Brian pointed out reducing the time interval will likely be the easiest solve to your question. Alternately though, you could try running your timer separately on it's own thread and having it run asynchronously and send it threading events as is discussed here:
Python threading.timer - repeat function every 'n' seconds

Stop time without freezing the program

In my pong game, whenever someone scores (the ball goes out to the left or to the right) the ball position is reset to the middle of screen and it waits one second before moving. In that one second I have a little animation going on.
My problem is this: if I pause the game in the middle of the animation, even though none of the objects are updated and only the pause text is drawn, time keeps rolling in. And if I wait time enough, the animation just stops right after I unpause the game. Here's what I mean. This is the ball update:
def update(self, dt):
now = pygame.time.get_ticks() / 1000
# if time elapsed since the ball got out >= BALL_WAIT_TIME
if now - self._spawn_time >= BALL_WAIT_TIME:
self.rect = self.calcnewpos(dt)
self.handle_collision()
# spawn animation
else:
step = 255 / (FPS * BALL_WAIT_TIME)
value = int(self._frame * step)
rgb = (value, value, value)
self._draw_ball(rgb)
self._frame += 1
From http://pygame.org/docs/ref/time.html#pygame.time.get_ticks:
pygame.time.get_ticks()
Return the number of millisconds since pygame.init() was called. Before pygame is initialized this will always be 0.
So even though nothing is drawn or updated while the game is paused, pygame.time.get_ticks() will still return the time elapsed since pygame.init. How can I solve this? Sorry if that is a little hard to understand, I'll post the rest of the code if needed.
Well it looks as though you're just subtracting the time that some event occurred from the current time. If that's your method for checking how much time has elapsed since the event, then it's not going to matter if the game has been paused. If the event happens and you then pause the game for 10 minutes, it's always going to have been 10 minutes since the event happened.
So with that in mind, you need some way to only count time when the game is active. Perhaps the ball could have an attribute that says how long since the ball got out, and you only increase it if the game isn't paused.
Edit: something like:
class Ball:
def spawn(self):
self.sinceSpawn = 0
def update(self, dt):
if not gamePaused:
self.sinceSpawn += dt
if self.sinceSpawn >= BALL_WAIT_TIME:
pass #Do something here

End a loop when current time is something specific

I have asked a question similar to this before, but this time it is a little different. To me, the following code should work.
import datetime
# run infinitly
while(True):
done = False
while(not done):
#
#main program
#
#stopping condition
if currenttime == '103000':
done = True
#continue with rest of program
However, it doesn't continue with the rest of the program when it hits 10:30:00am.
The following program I KNOW works (on a raspberry pi):
import datetime
done = False
while not done:
currenttime = datetime.datetime.now().strftime('%H%M%S')
if currenttime != '103000':
print currenttime
if currenttime == '103000':
done = True
print 'It is 10:30:00am, the program is done.'
It made logical sense to me what I did in that first example. Does anyone know why it won't exit that loop and continue with the rest?
If the main program takes a long time to run, currenttime could jump from 102958 to 103005. Therefore skipping 103000 entirely.
Maybe you need to set currenttime, before you check? Also, the if statement has to execute exactly at 103000 in order for done = True to execute.
while(True):
done = False
while(not done):
#
#main program
#
# need to set current time
currenttime = datetime.datetime.now().strftime('%H%M%S')
#stopping condition (use >= instead of just ==)
if currenttime >= '103000':
done = True
#continue with rest of program
Note that is not guaranteed that your loop has one iteration in each and every available second. The more load is on your system, the larger the likelihood that the loops skips a second, which potentially would be the termination criterion. There are also cases where seconds may be skipped, e.g. due to time synchronization or daylight saving issues.
Instead of a busy waiting loop, you could pre-compute the timedelta in seconds and then sleep for that many seconds.
Advantages:
You'll save computation power that other processes on your machine could use instead.
It probably increases the lifetime of your hardware.
This will also be more energy efficient.
Example:
import datetime
import time
def wait_until_datetime(target_datetime):
td = target_datetime - datetime.datetime.now()
seconds_to_sleep = td.total_seconds()
if seconds_to_sleep > 0:
time.sleep(seconds_to_sleep)
target_datetime = datetime.datetime(2025, 1, 1)
wait_until_datetime(target_datetime)
print "Happy New Year 2025!"
Note that this may still fail to produce the desired behavior due to arbitrary changes of the systems date and time settings. Probably it would be best to adopt a completely different strategy to execute a specific command at a specific time. Have you considered implementing the desired behavior using a cron job? (You could send a signal to the process and thereby issue it to cancel the loop...)
import datetime
done = False
while True:
currenttime = datetime.datetime.now().strftime('%H%M%S')
if currenttime >= '103000':
break
print currenttime
print 'It is 10:30:00am, the program is done.'
If you can't use break:
import datetime
done = False
while not done:
currenttime = datetime.datetime.now().strftime('%H%M%S')
if currenttime >= '103000':
done = True
else:
print currenttime
print 'It is 10:30:00am, the program is done.'

How to make an object have temporary invincibility?

I'm making a game in pygame and I need my ship to be immune for, about, 3 seconds after a collision with an asteroid. I tried every function I found, but it didn't work.
Here's the collision part of my code:
if collision == True:
ship_game = ship_destroyed
lifes -= 1;
And then i have this part:
if collision:
pi = True
collision = False
pygame.time.delay(1000)
This variable called pi I use in case of collision to put the ship in the middle of screen:
if pi == True:
ship_game = ship
pos_ship={'x': WScreen/2, 'y': HScreen/2}
pi = False
I think that's enough for you to understand my problem. Thank you :)
This could be accomplished by introducing a variable collision_immune that you set to True when a collision is detected. At the same time you record the time of the last collision in a variable collision_time. You can get the time from pygame.time.get_ticks(). Then in an appropriate part of your main loop, that is not shown above, you can check if the time since the last collision is more than, say, 3 seconds and reset collision_immune to False. This could look something like this
if collision == True:
ship_game = ship_destroyed
lifes -= 1;
collision_immune = True
collision_time = pygame.time.get_ticks()
Then somewhere, maybe at the beginning of the main loop, you put
if pygame.time.get_ticks() - collision_time > 3000: # The time is in ms.
collision_immune = False
Now you can use the variable collision_immune in your game logic to determine if the ship is immune from a recent collision.
Happy pygaming! :)
You need to keep track of the time when the last collision took place (last_collision_time = time.time()) and then when a collision happens you check if that value is less than 3 seconds ago (if time.time() - last_collision_time < 3.:) and handle it as you see fit. In this case clearly by not having the collision destroy the ship.
OOP style:
class Ship:
def immune(self):
return self.last_collide_time > current_time() - 3000
def collide(self):
if self.immune(): return
self.lifes -= 1
self.last_collide_time = current_time()
# ....

Categories