Pygame New Object Every Second - python

In pygame I need to make a flappy bird clone. I need to make recurring pipes so I want to do this by drawing new ones at the end of the screen every couple of seconds. I know there is a pygame.set_timer function but how do I implement this so that it counts in seconds and draws every couple of seconds

If you posted your entire code I might be able to help you. Since you didn't post your code you should try something like this.
import time
while True:
if (bird went past tube):
return True
...create(...)
else:
return False

Related

How to do a thing if user does something with a certain time limit?

I'm trying to make a 3D game but right now just jumping, I have got the jumping working, but I want to do a thing that (for example if speed=10) but if space is pressed 2x within (for example 0.5 seconds) the speed will go up by (for example 2) and if the player doesn't jump 2x times within the 0.5 seconds the speed stays/resets to normal.
So to simplify character speed becomes faster if you press space in certain time and if you don't for it stays 10, for example if I press space/jump and after 1 second do the same, the speed won't go up, but if I press space/jump within seconds <= 0.5 seconds the speed will go up, until I don't press it within the time, so if I stop pressing it within seconds <= 0.5 seconds the jump resets to 10 and if I jump --> 0.6 seconds =< seconds the speed stays the same.
And to people who don't know ursina, its a python based game engine and you modify the player using this type
----> player=FirstPersonController(gravity = (1), speed=10)
(Not sure but I think the engine is fully based on Python. Just so you know.
The engine I'm learning in is "Ursina Engine"
Sounds like you need to save the timestamp whenever the space is pressed. Then, every time it is pressed, before updating the timestamp, compare now against the previous timestamp... if it is less than 500ms, then speed up!
Reacting to the user doing nothing is harder. I have not used ursula, and a quick look at the documentation, I do not see much in the Ursina Engine to help you.
But I have an idea you could try? When the character is sped up, create an invisible wall to hit. Set the wall up exactly where the character will be in 500ms time, if the user does nothing, they will "collide" with the wall and it will trigger your routine to reduce the speed. If the user does do something, remove the wall. Just an idea. Not sure if that will work. Enjoy!

play sound if object is detected

I'm working on an object detection model using raspberry Pi. I've used Google's Object Detection API to detect models, My question is how to play sound when an object of a specific class(say human (i.e 'id' : 22))is detected.
I've tried a little and the code I came to is this,
if 22 in classes:
threading.Thread(play_sound()).start()
def play_sound():
pygame.init()
pygame.mixer.music.load("")
pygame.mixer.music.play(1,0.0)
pygame.time.wait(5000)
pygame.mixer.stop()
In this code, the problem I'm getting is
Sound starts playing even before the object is detected, I tried debugging but don't know why.
I'm starting the same thread again
If I use different threads, the pi runs out of resources and the whole execution stops
Is there any way to get this to work?
Thanks in advance
Don't use threads (you don't need them), don't use pygame.time.wait, and don't use pygame.mixer.music if you don't want to use it for background music.
Use a Sound object (and maybe provide a maxtime if you want to it's play function).
So your code should look more like this:
pygame.init()
detected_sound = pygame.mixer.Sound('filename')
...
if 22 in classes:
# use loops=-1 if the sound's length is less than 5 seconds
# so it's repeated until we hit the maxtime of 5000ms
detected_sound.play(loops=-1, maxtime=5000)
...

How to get rectangle to appear on screen (pygame) for a set amount of time?

How do you get a rectangle (rect) to appear on the output screen for a set amount of time, for example 2 seconds, before disappearing? I am making a version of wormy a nibbles clone. I would also like to know how to make an apple disappear when the worm "eats" it.
You don't delete objects, you draw over them in Pygame. Concerning your disappearing rectangles, a simple Timer will suffice. Now, we can't write code for you here without you providing any info so I've just stated some basic information, so the next time you post a question, provide some info and read the How to ask page, and welcome to SO.

Delay one thing in python without delaying anything else

I want this animation to play with a slight delay inbetween each dripx() command, but I can't delay anything else because the players character is also effected by any sleep/wait commands. (Kinda new to python/pygame so I don't know everything)
def wateranimation():
drip1()
drip2()
drip3()
drip4()
drip5()
drip6()
There are 2 ways to do this:
1) Store the current state, using a class. Every frame you call a specific function with the number of milliseconds since the last frame, and have it figure out if it needs to do anything this frame.
2) Threaded programming. You create a start a thread that just has the job of running that animation, and spends most of its time asleep.

Clutter does not update screen outside of breakpoints

I have some code:
l1 = clutter.Label()
l1.set_position(100,100)
for i in range(0,10):
l1.set_text(str(i))
time.sleep(1)
That is designed to show a count from 1 to 10 seconds on the screen in clutter, but I'm getting a strange error. When I run the script normally the screen runs as it should do, but there is no text displayed until 10 seconds are up. However, When I run with breakpoints in pdb the text shows up just fine.
I'm also getting a strange error at the start of the program:
do_wait: drmWaitVBlank returned -1, IRQs don't seem to be working correctly.
Try adjusting the vlank_mode configuration parameter.
But I don't see why that would affect the code out of break points but not in breakpoints.
Any help would be greatly appreciated.
Not sure if you've already figured out the answer to this but:
The reason you are having this problem is because you are blocking the main thread (where all the drawing occurs) with your time.sleep() calls, preventing the library from redrawing the screen.
E.g. your code is currently doing this:
Clutter redraws the screen.
You loop over ten seconds and change the text ten times.
Clutter redraws the screen.
If you want to queue something on a timer, you should look into gobject.timeout_add.
Have you tried posting (or searching) on the Clutter mailing list? Here's someone who got the same message about drmWaitVBlank for example.
My guess is most people on SO wouldn't be familiar with solving Clutter problems. I know I'm not :)

Categories

Resources