3

I am trying to make the game Pong. I have everything (except moving the ball) set up but when I run it I can't move both paddles at the same time. Does anybody know what I need to do?

import time
import turtle

#window
Wn_width = 800
Wn_height = 600
Wn = turtle.Screen()
Wn.setup(Wn_width, Wn_height)
Wn.tracer(0)
Title = turtle.title("Pong")
Background_color = turtle.bgcolor("black")

#bats and ball
bat1 = turtle.Turtle("square")
bat1.color("white")
bat1.shapesize(3, 1)
bat1.penup()
bat1.setpos(360,0)

bat2 = turtle.Turtle("square")
bat2.color("white")
bat2.shapesize(3, 1)
bat2.penup()
bat2.setpos(-360, 0)

bal = turtle.Turtle("circle")
bal.color("white") 
bal.penup()

#up down
def up():
    bat1.sety(bat1.ycor() + 5)
    if bat1.ycor() > 260:
        bat1.sety(bat1.ycor() -5)

def down():
    bat1.sety(bat1.ycor() - 5)  
    if bat1.ycor() < -250:
        bat1.sety(bat1.ycor() + 5)  

def up2(): 
    bat2.sety(bat2.ycor() + 5)
    if bat2.ycor() > 260:
        bat2.sety(bat2.ycor() - 5)

def down2(): 
    bat2.sety(bat2.ycor() - 5)
    if bat2.ycor() < -250:
        bat2.sety(bat2.ycor() + 5)

Wn.onkeypress(up, "Up")
Wn.onkeypress(down, "Down")
Wn.onkeyrelease(up, "Up")
Wn.onkeyrelease(down, "Down")
Wn.onkeypress(up2, "w")
Wn.onkeypress(down2, "s")
Wn.onkeyrelease(up2, "w")
Wn.onkeyrelease(down2, "s")
Wn.listen()

#main loop
while True: 
    Wn.update()
    time.sleep(0.01)

Each paddle moves perfectly fine on its own, but when I try to move them simultaneously, for example while holding the "up" button, pressing the "w" button - the first paddle will just stop and the second will start moving. How can I be able to press both "up" (or "down") and "w" (or "s") at the same time and have both paddles move?

Tomerikoo
  • 15,737
  • 15
  • 35
  • 52
kroedie
  • 31
  • 3
  • I believe this contains your answer! It uses pygame. https://stackoverflow.com/a/64227080/12282360 – Noah Jan 21 '22 at 18:45
  • 1
    Well.... I found a question (an exact duplicate to be fair). Problem is: it doesn't have answers yet.... [How can I enable multiple key presses in Turtle?](https://stackoverflow.com/q/56384666/6045800) – Tomerikoo Jan 21 '22 at 18:59
  • that's a shame. it's really weird as it didn't occur on the youtube tutorial i was watching. i'll now just try to get the ball moving and after i will take a look at it again. thanks for trying to help :) – kroedie Jan 21 '22 at 19:04
  • No problem. Seems an interesting issue. If I find anything, I'll be sure to update here :) – Tomerikoo Jan 21 '22 at 19:07
  • Does this answer your question? [How to bind several key presses together in turtle graphics?](https://stackoverflow.com/questions/47879608/how-to-bind-several-key-presses-together-in-turtle-graphics) – ggorlen Feb 03 '22 at 22:48

0 Answers0