我正在尝试创建一个简单的python乌龟游戏,在该游戏中,乌龟需要使其绕圈,同时避免移动正方形。我希望代码能够减少生命,并将海龟移回起点,直到剩下0条生命为止。下面的代码允许一次播放,然后运动循环不再重复。
我尝试了递归函数(move(3)),但是onkey命令不起作用。...
import turtle, random, time
#background
canvas = turtle.Screen()
canvas.bgcolor('black')
#Pen
pen = turtle.Turtle()
pen.penup()
pen.color('white')
pen.hideturtle()
pen.penup()
#Lilypad
pad = turtle.Turtle()
pad.hideturtle()
pad.color('yellow')
pad.shape('circle')
pad.penup()
pad.setposition(0,290)
pad.showturtle()
#Turtle
player = turtle.Turtle()
player.hideturtle()
player.shape('turtle')
player.color('green')
player.penup()
player.left(90)
player.setposition(0, -290)
player.showturtle()
#truck
truck1 = turtle.Turtle()
truck1.hideturtle()
truck1.shape('square')
truck1.color('blue')
truck1.penup()
truck1.showturtle()
speed = random.randint(1,5)
def move():
#move player and truck
player.forward(2)
truck1.forward(speed)
if truck1.xcor() > 300 or truck1.xcor() < -300:
truck1.right(180)
#win if hit the lilypad
if player.distance(pad)<10:
pen.penup()
pen.setposition(0,-50)
pen.write('You win!', align='left', font=('Arial', 36, 'normal'))
done()
#lose a life if hit the truck
if player.distance(truck1) < 30:
player.setposition(0,-290)
life = life - 1
while life > 0:
pen.penup()
pen.setposition(0,-60)
pen.write('Try again', align='left', font=('Arial', 36, 'normal'))
time.sleep(1)
pen.clear()
move()
#game over if 0 lives left
pen.penup()
pen.setposition(0,-60)
pen.write('Game over!', align='left', font=('Arial', 36, 'normal'))
done()
canvas.ontimer(move,10)
canvas.onkey(lambda:player.setheading(90),'Up')
canvas.onkey(lambda:player.setheading(180),'Left')
canvas.onkey(lambda:player.setheading(0),'Right')
canvas.onkey(lambda:player.setheading(270),'Down')
canvas.listen()
life = 3
move()
canvas.mainloop()
答案 0 :(得分:0)
关键是将初始化代码 all 全部移入一个函数,该函数将在最后一步调用move()
函数。初始化代码要做的第一件事是调用canvas.clear()
,这几乎抹去了所有内容。然后,您的move()
函数最后选择是在下一个计时器迭代中调用自身,还是在下一个计时器迭代中调用初始化代码以重置所有内容并开始新游戏。
下面是对上述代码以及各种柚木代码的重新编写:
from turtle import Screen, Turtle
from random import randint
from time import sleep
FONT = ('Arial', 36, 'normal')
def initialize():
global pen, pad, player, truck, speed, life
canvas.clear() # assume that this resets *everything*
# background
canvas.bgcolor('black')
# Pen
pen = Turtle(visible=False)
pen.color('white')
pen.penup()
# Lilypad
pad = Turtle('circle', visible=False)
pad.color('yellow')
pad.penup()
pad.setposition(0, 290)
pad.showturtle()
# Turtle
player = Turtle('turtle', visible=False)
player.color('green')
player.penup()
player.setheading(90)
player.setposition(0, -290)
player.showturtle()
# truck
truck = Turtle('square', visible=False)
truck.color('blue')
truck.penup()
truck.showturtle()
speed = randint(1, 5)
canvas.onkey(lambda: player.setheading(90), 'Up')
canvas.onkey(lambda: player.setheading(180), 'Left')
canvas.onkey(lambda: player.setheading(0), 'Right')
canvas.onkey(lambda: player.setheading(270), 'Down')
canvas.listen()
life = 3
move()
def move():
global life
# move player and truck
player.forward(2)
truck.forward(speed)
if not -300 < truck.xcor() < 300:
truck.right(180)
# lose a life if hit the truck
if player.distance(truck) < 20:
player.setposition(0, -290)
life -= 1
if life > 0:
pen.setposition(0, -60)
pen.write('Try again', font=FONT)
sleep(1)
pen.clear()
# win if hit the lilypad
if player.distance(pad) < 20:
pen.setposition(0, -50)
pen.write('You win!', font=FONT)
canvas.ontimer(initialize, 1000)
elif life == 0: # game over if 0 lives left
pen.setposition(0, -60)
pen.write('Game over!', font=FONT)
canvas.ontimer(initialize, 1000)
else:
canvas.ontimer(move, 10)
canvas = Screen()
pen = None
pad = None
player = None
truck = None
speed = -1
life = -1
initialize()
canvas.mainloop()
无论您是获胜还是耗尽生命,这都应该让您再次玩下去。