0
# Game creation

import turtle

wn = turtle.Screen()
wn.title("Pong")
wn.bgcolor("Black")
wn.setup(width=800, height=800)
wn.tracer(0)

# paddle a
paddle_a = turtle.Turtle()
paddle_a.speed(0)
paddle_a.shape("square")
paddle_a.color("white")
paddle_a.penup()
paddle_a.goto(0, 0)



# Functions

def paddle_a_right():
    turtle.forward(100)

wn.onkeypress(paddle_a_right, 'd')

while True:
    wn.update()

Want the square to move to the right or left using 'a' or 'd' I don't know very much about turtle, I just want to program a simple game.

  • 1
    `turtle.forward()` moves the turtle forward _in the direction it's currently facing_. If you want it to move to the right, you have to make sure it's facing the right way before calling `.forward()`. – John Gordon Dec 02 '22 at 00:21
  • I suggest using an event loop like [this](https://stackoverflow.com/a/70979967/6243352) rather than a `while True` which just slams the CPU and will result in totally different framerates on different machines depending on how fast they can run. – ggorlen Dec 02 '22 at 02:00

1 Answers1

0

There are three major issues with your code. First, you need to call wn.listen() to allow the window to receive keyboard input. Second, you do turtle.forward(100) when you mean paddle_a.forward(100). Finally, since you did tracer(0), you now need to call wn.update() anytime a change is made that you want your user to see.

Here's a simplified example:

from turtle import Screen, Turtle

def paddle_right():
    paddle.forward(10)
    screen.update()

screen = Screen()
screen.title("Pong")
screen.bgcolor("Black")
screen.setup(width=800, height=800)
screen.tracer(0)

paddle = Turtle()
paddle.shape("square")
paddle.color("white")
paddle.penup()

screen.onkeypress(paddle_right, 'd')
screen.listen()
screen.update()
screen.mainloop()
cdlane
  • 40,441
  • 5
  • 32
  • 81