r/pythonhelp • • 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()
2 Upvotes

16 comments sorted by

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.

1

u/[deleted] Mar 16 '22

Ok

1

u/Goobyalus Mar 16 '22

you can also use a site like pastebin and post the link

1

u/[deleted] Mar 16 '22

sure let me do that

1

u/[deleted] Mar 16 '22

1

u/Goobyalus Mar 17 '22

Is how it looks at this link how it looks in your editor? This also looks like something didn't copy and paste right. There's no indentation, and there are blank lines between everything.

1

u/[deleted] Mar 17 '22

Hmm weird, I'll copy and paste again tomorrow, also thanks for helping and other than the indentation did you see any errors?

1

u/Goobyalus Mar 17 '22

Is the ball supposed to look like a paddle on the right side, and are the 2 paddles supposed to be on the left?

1

u/[deleted] Mar 17 '22

there is supposed to be a paddle on each side and a ball in the middle like the usual pong game

1

u/Goobyalus Mar 17 '22

k well the "paddle" on the right side is actually your ball which is shaped like a paddle, and your 2 paddles start in the same place on the left overlapping which is why it looks weird when you move them. If you move one up and one down you can separate them and see.

1

u/[deleted] Mar 17 '22

So basically both my paddles are on the left side?

→ More replies (0)

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

u/[deleted] Mar 17 '22

exactly like that