Home > Net >  Pong via Python Turtle issues
Pong via Python Turtle issues

Time:05-09

I've been looking at this for hours and can't figure out why it won't run properly. Any ideas? I've tried messing with different x and y coords to see if that was the problem but nothing works. The program just runs twitches and ends, when it should be playing through a game of pong until the user exits out.

code: you can ignore the comments

import turtle

# screen
screen = turtle.Screen()
screen.title('Pong')
screen.bgcolor('black')
screen.setup(width=1000, height=500)

# ball
ball = turtle.Turtle()
ball.shape('circle')
ball.color('white')
ball.speed(0)
ball.penup()                                        #solves line issue
ball.goto(0, 0)
ball.dx = 5
ball.dy = -5

# right paddle____add speed___
r_pad = turtle.Turtle()
r_pad.speed(0.5)
r_pad.shape('square')
r_pad.color('white')
r_pad.shapesize(stretch_wid=5, stretch_len=2)
r_pad.penup()
r_pad.goto(450, 0)

# left paddle
l_pad = turtle.Turtle()
l_pad.speed(0.5)
l_pad.shape('square')
l_pad.color('white')
l_pad.shapesize(stretch_wid=5, stretch_len=2)
l_pad.penup()
l_pad.goto(-450, 0)


# Score
score = turtle.Turtle()
score.speed(0)
score.color('white')
score.penup()
score.hideturtle()
score.goto(0, 200)
score.write("Left_player : 0 Right_player: 0",
             align="center", font=("arial", 24, "normal"))
#solving that score error
left_player = 0
right_player = 0

# movement
def paddler_up():
    y = r_pad.ycor()
    y  = 20
    r_pad.sety(y)


def paddler_down():
    y = r_pad.ycor()
    y -= 20
    r_pad.sety(y)


def paddlel_up():
    y = l_pad.ycor()
    y  = 20
    l_pad.sety(y)


def paddlel_down():
    y = l_pad.ycor()
    y -= 20
    l_pad.sety(y)



#main game
screen.listen()
screen.onkeypress(paddler_up, "w")
screen.onkeypress(paddler_down, "s")
screen.onkeypress(paddlel_up, "up")
screen.onkeypress(paddlel_down, "down")



while True:
    screen.update()

    ball.setx(ball.xcor()   ball.dx)
    ball.sety(ball.ycor()   ball.dy)

    # Checking borders
    if ball.ycor() > 230:
        ball.sety(230)
        ball.dy *= -1

    if ball.ycor() < -230:
        ball.sety(-230)
        ball.dy *= -1

    if ball.xcor() > 600:
        ball.goto(0, 0)
        ball.dy *= -1
        left_player  = 1
        score.clear()
        score.write("Left_player : {} Right_player: {}".format(
            left_player, right_player), align="center",
            font=("Courier", 24, "normal"))

    if ball.xcor() < -600:
        ball.goto(0, 0)
        ball.dy *= -1
        right_player  = 1
        score.clear()
        score.write("Left_player : {} Right_player: {}".format(
            left_player, right_player), align="center",
            font=("Courier", 24, "normal"))

    # Paddle ball collision
    if (ball.xcor() > 360 and ball.xcor() < 370) \
            and (ball.ycor() < r_pad.ycor()   40 and ball.ycor() > r_pad.ycor() - 40):
        ball.setx(360)
        ball.dx *= -1

    if (ball.xcor() < -360 and ball.xcor() > -370) \
            and (ball.ycor() < l_pad.ycor()   40 and ball.ycor() > l_pad.ycor() - 40):
        ball.setx(-360)
        ball.dx *= -1

CodePudding user response:

Your onkeypress calls for up and down keys should be capitalized (Up and Down instead of up and down) for it to work

screen.onkeypress(paddlel_up, "Up")
screen.onkeypress(paddlel_down, "Down")
  • Related