r/pythonhelp • u/[deleted] • Mar 16 '22
python game project not working, any ideas to fix it? (code and stuff is all in description)
So I was following a tutorial on making a pong game using the turtle library in python in order to practice my python skills I recently learned. However, my code doesn't make the program work properly, I listed the issues and code below, please help if you can!
First, when the terminal opens, the window doesn't have an exit button or anything, which makes weird as someone playing the game I imagine.
The second issue is that the paddles move incorrectly, when I press the "q" and "v" keys, which are supposed to move the second paddle (the paddle on the right side, they move the paddle on the left side. even though my functions declared it to be the second paddle.
The third issue is also related to the paddles, both of the paddles in the pong game don't move properly when I press the proper up and down keys in the game.
I was following a tutorial on youtube from freecodecamp, so Im not sure why it isnt working
If you know how to solve this, I attached the code below, any help is appreciated.
Edit: some were saying to fix the code, so i made a copy on pastebin
pastebin link: https://pastebin.com/yd0t8vvE
import turtle
wn = turtle.Screen()
wn.title=("Pong game")
wn.bgcolor("black")
wn.setup(width=800, height=500)
wn.tracer(0)
#The first paddle (Paddle A)
paddle_a = turtle.Turtle()
paddle_a.speed(0)
paddle_a.shape("square")
paddle_a.shapesize(stretch_wid=5, stretch_len=1)
paddle_a.color("green")
paddle_a.penup()
paddle_a.goto(-350, 0)
#Paddle B (The second Paddle)
paddle_b = turtle.Turtle()
paddle_b.speed(0)
paddle_b.shape("square")
paddle_b.shapesize(stretch_wid=5, stretch_len=1)
paddle_b.color("green")
paddle_b.penup()
paddle_b.goto(-350, 0)
#BALL
ball = turtle.Turtle()
ball.speed(0)
ball.shape("square")
ball.shapesize(stretch_wid=5, stretch_len=1)
ball.color("green")
ball.penup()
ball.goto(0, 0)
#Function
def paddle_a_up():
y = paddle_a.ycor() y+= 20 paddle_a.sety(y) def paddle_a_down():
y = paddle_a.ycor() y -= 20 paddle_a.sety(y) def paddle_b_up():
y = paddle_b.ycor() y += 20 paddle_b.sety(y) def paddle_b_down():
y = paddle_b.ycor() y -= 20 paddle_b.sety(y) #Keyboard finding
wn.listen()
wn.onkeypress(paddle_a_up, "w")
wn.onkeypress(paddle_a_down, "s")
wn.onkeypress(paddle_b_up, "q")
wn.onkeypress(paddle_b_down, "v")
#MAIN LOOP
while True:
wn.update()1
u/Goobyalus Mar 17 '22 edited Mar 17 '22
Is this what your code looks like?
import turtle
wn = turtle.Screen()
wn.title=("Pong game")
wn.bgcolor("black")
wn.setup(width=800, height=500)
wn.tracer(0)
#The first paddle (Paddle A)
paddle_a = turtle.Turtle()
paddle_a.speed(0)
paddle_a.shape("square")
paddle_a.shapesize(stretch_wid=5, stretch_len=1)
paddle_a.color("green")
paddle_a.penup()
paddle_a.goto(-350, 0)
#Paddle B (The second Paddle)
paddle_b = turtle.Turtle()
paddle_b.speed(0)
paddle_b.shape("square")
paddle_b.shapesize(stretch_wid=5, stretch_len=1)
paddle_b.color("green")
paddle_b.penup()
paddle_b.goto(-350, 0)
#BALL
ball = turtle.Turtle()
ball.speed(0)
ball.shape("square")
ball.shapesize(stretch_wid=5, stretch_len=1)
ball.color("green")
ball.penup()
ball.goto(0, 0)
#Function
def paddle_a_up():
y = paddle_a.ycor()
y+= 20
paddle_a.sety(y)
def paddle_a_down():
y = paddle_a.ycor()
y -= 20
paddle_a.sety(y)
def paddle_b_up():
y = paddle_b.ycor()
y += 20
paddle_b.sety(y)
def paddle_b_down():
y = paddle_b.ycor()
y -= 20
paddle_b.sety(y)
#Keyboard finding
wn.listen()
wn.onkeypress(paddle_a_up, "w")
wn.onkeypress(paddle_a_down, "s")
wn.onkeypress(paddle_b_up, "q")
wn.onkeypress(paddle_b_down, "v")
#MAIN LOOP
while True:
wn.update()1
1
u/Goobyalus Mar 16 '22
Please edit to fix the formatting for Reddit.
You can get Reddit to format code by adding an additional 4 spaces before each line of code (indenting the code), and leaving blank lines before and after the code block.