Ask coding questions

← Back to all posts
Help with my 'Pong' game
vitorcurado (5)

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 :)

Answered by glench (39) [earned 5 cycles]
View Answer
Commentshotnewtop
glench (39)

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!

glench (39)

You can also read more about the different commands in Python Play (including adding images) here: https://github.com/replit/play

kaldisberzins (304)

The link you posted doesn't work. I found your project, however and found out that you are using pygame. You may want to look at the documentation for it: https://www.pygame.org/docs/
or maybe some tutorials: https://www.pygame.org/wiki/tutorials
Hope this helps!

vitorcurado (5)

@kaldisberzins thank you! I'll try doing it :)

Geocube101 (566)

Question 1: Which language are you using?
Question 2: Can you paste a repl link?

vitorcurado (5)

@Geocube101 oops sorry I'm using python and I think this repl link should work
@vitorcurado/ping pong ping pong ping

PYer (3007)

@vitorcurado copy and paste from the url bar when you have the repl open

PYer (3007)

@vitorcurado i dont know much about pygame, you may want to check the docs or search like @kaldisberzins said.

FootSpecialist (0)

@PYer How can you not know much about pygame with a name like that? You are a disgrace to Pie related coding.

PYer (3007)

@FootSpecialist :( I only began learning pygame when repl.it supported it.

glench (39)

@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 :)

FootSpecialist (0)

@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

FootSpecialist (0)

@glench That came across a little passive aggressive so I would like to assure you that I am, indeed, still joking.

glench (39)

@FootSpecialist ok. I’ll just say that being nice and affirming as opposed to sarcastic and unaffirming is a vastly underrated way to be. And check the response to your comment from the person it was aimed at: wouldn’t it be better if your comments didn’t cause people to frown?

FootSpecialist (0)

@glench lmao he is a pie man, he was joking

being nice and affirming is also a great way to not have any fun.