当我在Tkinter游戏中按一个键时,敌人停止

时间:2019-05-28 17:08:03

标签: python tkinter turtle-graphics game-loop

为了进行实践编程,我用Python创建了带有乌龟图形和tkinter的游戏。我有一个问题:如果我按一个键,游戏循环就会停止并且敌人不会移动。这个问题有什么解决办法吗?

import random
import time
import turtle
from tkinter import *

running = True

#window

wn = Tk()
wn.geometry("900x700")

canvas = Canvas(wn, width=750, height=600)
canvas.pack()
canvas.place(relx=0.5, rely=0.5, anchor=CENTER)


#player

player = turtle.RawTurtle(canvas)

player_speed = 3


#enemies

enemies = []

for enemy in range(5):
    enemies.append((turtle.RawTurtle(canvas), 10))


for enemy in enemies:
    enemy[0].penup()
    enemy[0].speed(0)
    enemy[0].setheading(90)
    enemy[0].goto(enemy_position_x(), enemy_position_y())#random position


#move the player
def move_left(event):
    player.setheading(180)
    player.setx(player.xcor() - player_speed)


def move_right(event):
    player.setheading(360)
    player.setx(player.xcor() + player_speed)


def move_up(event):
    player.setheading(90)
    player.sety(player.ycor() + player_speed)


def move_down(event):
    player.setheading(270)
    player.sety(player.ycor() - player_speed)


def shoot(event):
    #I don't write the shoot function, because it's long
    print("shoot")


wn.bind('<Left>', move_left)
wn.bind('<Right>', move_right)
wn.bind('<Up>', move_up)
wn.bind('<Down>', move_down)
wn.bind('<space>', shoot)


#game loop
while running:

    #move the enemy
    for this_enemy in enemies:
        if this_enemy[0].xcor() < player.xcor():
            this_enemy[0].setx(this_enemy[0].xcor() + 3)
            time.sleep(0.1)

        if this_enemy[0].xcor() > player.xcor():
            this_enemy[0].setx(this_enemy[0].xcor() - 3)
            time.sleep(0.1)

        if this_enemy[0].ycor() < player.ycor():
            this_enemy[0].sety(this_enemy[0].ycor() + 3)
            time.sleep(0.1)

        if this_enemy[0].ycor() > player.ycor():
            this_enemy[0].sety(this_enemy[0].ycor() - 3)
            time.sleep(0.1)

wn.mainloop()

1 个答案:

答案 0 :(得分:0)

您的程序的基本设计不正确。 Turtle(和tkinter)是事件驱动的,但是将while True:time.sleep()循环一起使用可以解决此问题。我在下面重写了您的代码,以使用计时器事件以及其他更改。用键盘控制播放器时,敌人应保持移动。我扔了tkinter包装器代码,因为它没有做乌龟本身无法做的任何事情:

from turtle import Screen, Turtle
from random import randint

def enemy_position_x():
    return randint(-325, 325)

def enemy_position_y():
    return randint(-300, 300)

# move the player
def move_left():
    player.setheading(180)
    player.setx(player.xcor() - player_speed)

def move_right():
    player.setheading(360)
    player.setx(player.xcor() + player_speed)

def move_up():
    player.setheading(90)
    player.sety(player.ycor() + player_speed)

def move_down():
    player.setheading(270)
    player.sety(player.ycor() - player_speed)

def move():

    # move the enemy
    for enemy in enemies:
        enemy.setheading(enemy.towards(player))

        if enemy.distance(player) > 10:
            enemy.forward(enemy_speed)

    screen.ontimer(move, 100)

# window

screen = Screen()
screen.setup(900, 700)

# player

player = Turtle()
player.speed('fastest')
player.color('green')
player.penup()

player_speed = 3

# enemies

enemies = []

for _ in range(5):
    enemy = Turtle()
    enemy.penup()
    enemy.speed('fastest')
    enemy.color('red')
    enemy.goto(enemy_position_x(), enemy_position_y())

    enemies.append(enemy)

enemy_speed = 3

screen.onkey(move_left, 'Left')
screen.onkey(move_right, 'Right')
screen.onkey(move_up, 'Up')
screen.onkey(move_down, 'Down')
screen.listen()

move()

screen.mainloop()