简体   繁体   English

在 python 龟中,我的乒乓球播放器 1 没有下降

[英]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!我是 python 游戏开发的新手。我正在使用 python 3.8。我使用 python 乌龟模块编写代码,但是在运行时一切正常,但在按下播放器键 1 后我的左栏没有移动但按'w'键向上移动。它甚至没有给出错误。它只是一个结构。请帮助我!

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()

my left bar which is player 1 doesn't move down after pressing 's' key按下“s”键后,我的左栏是玩家 1 并没有向下移动

My guess it's due to the typo in the following function:我猜这是由于以下 function 中的错字:

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

lbar.directon -> lbar.direction 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM