I am making a point game, and I want to make both paddles move at the same time using key press since it is a 2 player game. But the problem is that I cant do that. My left player moves using WS and the right one using the up and down keys. My code is pasted below, I did not use pygame:
This is the main file:
from turtle import Turtle, Screen #need a turtle for the dotted line
from paddles import Paddles
from ball import Ball
#Set the screen up:
screen = Screen()
screen.setup(800,600)
screen.bgcolor("black")
#screen.tracer(0)
xcor1, xcor2, = -390, 380
ycor1, ycor2 = 0, 0
#Create the objects:
turtle = Turtle() #make a turtle for the dotted line
turtle.pencolor("white") #for the dotted line
paddle1 = Paddles(xcor1, ycor1) #LEFT paddle
paddle2 = Paddles(xcor2, ycor2) #RIGHT paddle
ball = Ball()
screen.listen()
screen.onkey(paddle1.up, "w")
screen.onkey(paddle1.down, "s")
screen.onkey(paddle2.up, "Up")
screen.onkey(paddle2.down, "Down")
def dottedLine(): #making the dotted line at the centre of the screen
turtle.penup()
turtle.goto(0,295)
turtle.setheading(270)
for i in range(1,60):
turtle.hideturtle()
turtle.pendown()
turtle.forward(5)
turtle.penup()
turtle.forward(5)
dottedLine()
while True:
ball.penup()
ball.setx(ball.xcor()+ball.velocity_x) #set the xcor to the orignal x cor and then add the x values from the velocity
ball.sety(ball.ycor()+ball.velocity_y)
if ball.xcor() > 380 :
ball.goto(0,0)
elif ball.ycor() > 280:
ball.goto(0,0)
screen.exitonclick()
This is the file for the paddles:
from turtle import Turtle
class Paddles(Turtle):
def __init__(self, xcor, ycor):
super().__init__()
self.shape("square") #rectangles are not a python turtle shape
self.shapesize(5,1)
self.color("white")
self.penup()
self.goto(xcor, ycor)
#DATE: 03.10.2023: amd 06.10.2023
#FOR THE RIGHT PADDLE = PADDLE 2
def up(self):
upY = self.ycor() + 10 #keep the brackets for the y cor
self.goto(self.xcor(), upY)
def down(self):
downY = self.ycor() - 10
self.goto(self.xcor(), downY)