【问题标题】:in python turtle my pong player1 is not going down在 python turtle 中,我的 pong player1 不会关闭
【发布时间】:2020-06-17 11:52:36
【问题描述】:

我是 python 游戏开发的新手。我正在使用 python 3.8。我使用 python turtle 模块编写代码,但是在运行时一切正常,但是我的左栏(即玩家 1)在按下后没有向下移动s'键,但按'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()

【问题讨论】:

  • 你能和我们分享你的代码吗?

标签: python python-3.x pong python-turtle


【解决方案1】:

我的左栏是玩家 1 在按下 's' 键后不会向下移动

我猜这是由于以下函数中的拼写错误:

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

lbar.directon -> lbar.direction

这应该在您的控制台中显示为错误消息。

我对您的代码进行了修改以解决上述问题以及其他几个问题:

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

【讨论】:

    猜你喜欢
    • 2019-09-12
    • 1970-01-01
    • 2022-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-01
    • 1970-01-01
    • 2018-11-12
    相关资源
    最近更新 更多