destro
destro

Reputation: 11

in python turtle my pong player1 is not going down

I am new in python game developing.I am using python 3.8.I write a code using python turtle module, but when in run it everything works fine but my left bar which is player 1 doesn't move down after pressing 's' key but moves upward on pressing 'w' key.It is not even giving a error.It is just a structure.Please help me!

import turtle
import time
import random

#variabals
delay=0.05

#screen
wn=turtle.Screen()
wn.bgcolor('black')
wn.title('pong')
wn.setup(height=700,width=600)
wn.tracer(0)


#left player
lbar=turtle.Turtle()
lbar.speed(0)
lbar.direction='stop'
lbar.color('white')
lbar.shape('square')
lbar.shapesize(6,1)
lbar.up()
lbar.goto(-270,100)


#right player
rbar=turtle.Turtle()
rbar.speed(0)
rbar.direction='stop'
rbar.color('white')
rbar.shape('square')
rbar.shapesize(6,1)
rbar.up()
rbar.goto(270,-270)

#pong ball
ball=turtle.Turtle()
ball.speed(0)
ball.shape('circle')
ball.color('red')
ball.shapesize(0.5)
ball.up()
ball.goto(0,0)

#functions
def move():
    if rbar.direction=='up':
        y=rbar.ycor()
        rbar.sety(y+20)

    if rbar.direction=='down':
        y=rbar.ycor()
        rbar.sety(y-20)

    if lbar.direction=='up':
        lbar.sety(lbar.ycor()+20)

    if lbar.direction=='down':
        y=lbar.ycor()
        lbar.sety(y-20)

def rup():
    rbar.direction='up'

def rdown():
    rbar.direction='down'

def lup():
    lbar.direction='up'

def ldown():
    lbar.directon='down'

#inputs
wn.listen()
wn.onkey(lup,'w')
wn.onkey(ldown,'s')
wn.onkey(rup,'Up')
wn.onkey(rdown,'Down')

#gameloop
while True:
    wn.update()
    move()
    time.sleep(delay)
wn.mainloop()

Upvotes: 1

Views: 59

Answers (1)

cdlane
cdlane

Reputation: 41872

my left bar which is player 1 doesn't move down after pressing 's' key

My guess it's due to the typo in the following function:

def ldown():
    lbar.directon='down'

lbar.directon -> lbar.direction

This should have shown up as an error message in your console.

My rework of your code to address the above, and several other issues:

from turtle import Screen, Turtle

# constants
DELAY = 50  # milliseconds

# functions
def move():
    if rbar.direction == 'up':
        rbar.sety(rbar.ycor() + 20)
    elif rbar.direction == 'down':
        rbar.sety(rbar.ycor() - 20)

    if lbar.direction == 'up':
        lbar.sety(lbar.ycor() + 20)
    elif lbar.direction == 'down':
        lbar.sety(lbar.ycor() - 20)

def rup():
    rbar.direction = 'up'

def rdown():
    rbar.direction = 'down'

def lup():
    lbar.direction = 'up'

def ldown():
    lbar.direction = 'down'

def gameloop():
    screen.update()
    move()
    screen.ontimer(gameloop, DELAY)

# screen
screen = Screen()
screen.bgcolor('black')
screen.title('pong')
screen.setup(height=700, width=600)
screen.tracer(False)

# left player
lbar = Turtle()
lbar.color('white')
lbar.shape('square')
lbar.shapesize(6, 1)
lbar.penup()
lbar.goto(-270, 100)

lbar.direction = 'stop'

# right player
rbar = lbar.clone()
rbar.goto(270, -270)

rbar.direction = 'stop'

# pong ball
ball = Turtle()
ball.shape('circle')
ball.color('red')
ball.shapesize(0.5)
ball.penup()

# inputs
screen.onkey(lup, 'w')
screen.onkey(ldown, 's')
screen.onkey(rup, 'Up')
screen.onkey(rdown, 'Down')
screen.listen()

gameloop()

screen.mainloop()

Upvotes: 1

Related Questions