Guys, I have to add countdowns and images, but I don't know how to code my way out. For example, I would like to put a 'you died' sentence, just like dark souls, when the player loses and put a 3 seconds countdown before starting the game, could someone tell me briefly how to do these kinds of stuff? And also, where can I learn these topics in a book? Does someone know a book that I can download? Thank you in advance :)
Hey @vitorcurado, looks like you're using Play and the pong tutorial I wrote! Cool! I'd love to offer some help here. In programming, it's helpful to break down what you want.
First, we need the text you want to display, something like "You lost :(". To do that, you can add this line of code near the beginning of your program:
losing_text = play.new_text('You lost :(', font_size=120)
Try it out and see if the text shows up. If the text is showing up, it will stay on the screen even when you start the game, which isn't what we want. So for now we should hide the text with the hide command:
losing_text = play.new_text('You lost :(', font_size=120) losing_text.hide()
We'll bring it back when we detect the player loses.
Second, you want to know when the player loses. How do you detect when the player loses? In most pong games, the player loses when the ball goes past the player's paddle or past the edge of the screen. How do we translate this into code?
To check for a condition, Python's
if statement is what you want. You can do something like this:
# check to see if the ball has gone past the right of the screen @play.repeat_forever async def check_losing(): if ball.x > play.screen.right: losing_text.show()
This will repeatedly check if the ball's
x position (its horizontal position) is bigger than (
>) the position of right of the screen. If it is, we'll show the losing text we made earlier. Try it out, does it work?
Once it's working, the third thing you said you want to do is wait three seconds. In Python Play, there's a special command to wait called the timer. You use it like this:
# check to see if the ball has gone past the right of the screen @play.repeat_forever async def check_losing(): if ball.x > play.screen.right: losing_text.show() await play.timer(seconds=3)
But what do we want to have happen after three seconds? We want to hide the losing text and put the ball back in the middle of the screen. You can do that with two more commands, like this:
# check if the ball has passed the right of the screen @play.repeat_forever async def check_losing(): if ball.x > play.screen.right: losing_text.show() await play.timer(seconds=3) losing_text.hide() ball.go_to(x=0, y=0) # go back to the center of the screen
And that's it! I think this should do what you want. Check this repl I made for you with the new code. I hope this helps!
@FootSpecialist hey! I think you might have been joking around with @PYer which is cool, but in text it's a little hard to tell if you're joking or being mean. It's okay if someone that likes Python doesn't know much about pygame. I've been coding for over 10 years and I didn't know anything about pygame until a few months ago! So if you were joking please let us know and if you weren't, please try to be nicer since we're all learning here :)
@glench Damn bro, writing that you are joking ruins jokes. I asked the pie community and it seems like we are all in agreement that my comment was clear enough. So if you believe that the joke came across muddy, I might be able to write a script for a Raspberry Pi that detects sarcasm for you.
Wishing you the best in both crust and filling,
Your favorite foot fetishist