首页 > 解决方案 > 为什么 Python Turtle 不响应我的运动输入?

问题描述

我这学期要上一门编码课程,其中一个项目需要我们制作任何类型的游戏。我选择制作一个包含在一个盒子中的游戏,敌人在屏幕上完全移动,然后再次出现在不同的 x 或 y 坐标处以增加一些变化。我正在处理的问题是添加创建“敌人”所需的代码之后,我为玩家乌龟的移动设置的键不再起作用,另一个奇怪的事情是,玩家乌龟将在程序运行时正确旋转跑。我完全不知道如何让乌龟在为敌人编写代码之前正确地响应代码。我不相信敌人的代码是问题,因为当它被注释掉时,它的行为仍然相同。我没有' t 为简洁起见包括敌人代码。此代码是在 Python 3 中完成的。如果可以,请提供帮助!编辑:我删除了 window.onkeypress(mov_x,"x") 后面的括号,在为敌人添加代码后它不会响应,它在没有敌人代码的情况下工作,但这有点删除了游戏。谢谢你的帮助!

import turtle
import random
#screen
window = turtle.Screen()
window.title("Final Project Game")
window.bgcolor("gray")
window.setup(width=600,height=600)
#player
t= turtle.Turtle()
t.speed(5)
t.shape("triangle")
t.color("blue")
t.penup()
#player movement

def mov_rt():
    t.seth(0)
    t.fd(20)
def mov_lt():
    t.seth(180)
    t.fd(20)
def mov_up():
    t.seth(90)
    t.fd(20)
def mov_dw():
    t.seth(270)
    t.fd(20)


window.onkeypress(mov_rt,"d")
window.onkeypress(mov_lt,"a")
window.onkeypress(mov_up,"w")
window.onkeypress(mov_dw,"s")
window.listen()

#enemies
enemies = []
turt_num = turtle.numinput("Final","Number of Enemies", default=5, minval=1,maxval=10)
e_dir= [0,90,180,270]
if turt_num == 1:
    e1= turtle.Turtle("square",visible=False)
    e1.speed(5)
    e1.color("red")
    e1.penup()
    e1.setpos(random.randint(-290,290),random.randint(-290,290))
    e1.seth(random.choice(e_dir))
    enemies.append(e1)
    e1.st()
elif turt_num == 2:
    e1= turtle.Turtle("square",visible=False)
    e1.speed(5)
    e1.color("red")
    e1.penup()
    e1.setpos(random.randint(-290,290),random.randint(-290,290))
    e1.seth(random.choice(e_dir))
    enemies.append(e1)
    e2= turtle.Turtle("square",visible=False)
    e2.speed(5)
    e2.color("red")
    e2.penup()
    e2.setpos(random.randint(-290,290),random.randint(-290,290))
    e2.seth(random.choice(e_dir))
    enemies.append(e2)
    e1.st()
    e2.st()
elif turt_num ==3:
    e1= turtle.Turtle("square",visible=False)
    e1.speed(5)
    e1.color("red")
    e1.penup()
    e1.setpos(random.randint(-290,290),random.randint(-290,290))
    e1.seth(random.choice(e_dir))
    enemies.append(e1)
    e2= turtle.Turtle("square",visible=False)
    e2.speed(5)
    e2.color("red")
    e2.penup()
    e2.setpos(random.randint(-290,290),random.randint(-290,290))
    e2.seth(random.choice(e_dir))
    enemies.append(e2)
    e3= turtle.Turtle("square",visible=False)
    e3.speed(5)
    e3.color("red")
    e3.penup()
    e3.setpos(random.randint(-290,290),random.randint(-290,290))
    e3.seth(random.choice(e_dir))
    enemies.append(e3)
    e1.st()
    e2.st()
    e3.st()

#borders
def border(): #if you hold down the button it wont reappear bc youre still moving while the turtle is trying to move to the desired target
    tx, ty= t.pos()
    if t.xcor() >295:
        t.ht()
        t.setpos(-295,ty)
        t.st()
    if t.xcor() <-295:
        t.ht()
        t.setpos(295,ty)
        t.st()
    if t.ycor() >295:
        t.ht()
        t.setpos(tx,-295)
        t.st()
    if t.ycor() <-295:
        t.ht()
        t.setpos(tx,295)
        t.st()



#main game loop
while True:
    window.update()
    border()
turtle.mainloop()

标签: pythonpython-turtle

解决方案


尝试使用

window.onkey()

这是一种不同的聆听方式,它做的事情完全相同。另外,请记住在您需要的代码的末尾

window.listen()

推荐阅读