Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pong paddles wont move
#2
You need to listen() for key press events. You also had an indenting problem on wn.onkeypress()

You should consider making some classes to organize your code. This is your program using classes to make two movable paddles and a ball with 14 fewer lines of code.
import turtle

class Paddle(turtle.Turtle):
    def __init__(self, x, keys=None, color='black'):
        super().__init__()
        self.speed(3)
        self.shape('square')
        self.shapesize(stretch_wid=5, stretch_len=1)
        self.color(color)
        self.penup()
        self.goto(x, 0)
        if keys:
            # Have key press events move the paddles
            wn.onkeypress(lambda: self.move_y(20), keys[0])
            wn.onkeypress(lambda: self.move_y(-20), keys[1])

    def move_y(self, step):
        self.sety(self.ycor()+step)

class Ball(turtle.Turtle):
    def __init__(self, color='black', keys=None):
        super().__init__()
        self.speed(3)
        self.shape('circle')
        self.color(color)
        self.penup()
        self.goto(0, 0)

wn = turtle.Screen()
wn.setup(width=800, height=600)
paddles = (Paddle(-350, "ad"), Paddle(350, "jl"))
ball = Ball()

wn.listen()  # Need to call this to have turtle listen for key press events.
while True:
    wn.update()
Reply


Messages In This Thread
pong paddles wont move - by skullkat232 - Dec-29-2022, 07:42 PM
RE: pong paddles wont move - by deanhystad - Dec-29-2022, 11:20 PM
RE: pong paddles wont move - by skullkat232 - Dec-30-2022, 05:52 PM
RE: pong paddles wont move - by deanhystad - Dec-30-2022, 07:02 PM
RE: pong paddles wont move - by skullkat232 - Dec-30-2022, 08:02 PM
RE: pong paddles wont move - by Vadanane - Feb-13-2023, 03:53 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question [PyGame] my pygame car wont move Erio85 1 1,214 Apr-24-2023, 04:52 PM
Last Post: Erio85
  Problem with my pong code Than999 3 3,632 Oct-22-2021, 08:50 AM
Last Post: Qanima
  Game “Pong” I have problems with the code BenBach18 2 3,640 Jan-10-2021, 05:16 PM
Last Post: michael1789
  [PyGame] Creating Pong in Pygame Russ_CW 2 2,926 Oct-11-2020, 11:56 AM
Last Post: Russ_CW
  Trying to make a simple pong game. kevindadmun 1 4,046 Aug-05-2019, 06:39 AM
Last Post: kevindadmun
  Smiley Pong Help Jasmineleroy 6 4,935 May-22-2019, 11:36 AM
Last Post: metulburr
  [PyGame] How do I add more balls to basic Pong game? JUtah 2 4,605 Apr-18-2019, 08:40 PM
Last Post: JUtah
  [PyGame] Pong game key.event problem erickDarko 2 4,318 Dec-12-2018, 03:17 PM
Last Post: erickDarko
  Text wont draw fierygaming 1 3,073 Jul-28-2018, 10:39 PM
Last Post: Windspar

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020