简体   繁体   English

算下蟒蛇的生活

[英]Counting down lives in python turtle

I am trying to create a simple python turtle game where the turtle needs to make it to the circle, while avoiding the moving square. 我正在尝试创建一个简单的蟒蛇龟游戏,其中乌龟需要将它带到圆圈,同时避免移动的方块。 I would like the code to count down lives and move the turtle back to the beginning until there are 0 lives left. 我希望代码可以倒数,然后将乌龟移回到开头,直到剩下0个生命。 The code below allows for one play and then the motion loop does not repeat. 下面的代码允许一次播放,然后运动循环不重复。

I have tried a recursive function (move(3)), but then the onkey commands don't work.... 我尝试了一个递归函数(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()

The key to this is to move all your initialization code into a function, which invokes your move() function as it's last step. 关键是将所有初始化代码移动到一个函数中,该函数调用你的move()函数作为它的最后一步。 The first thing that the initialization code does is call canvas.clear() which pretty much wipes out everything. 初始化代码所做的第一件事是调用canvas.clear() ,它几乎消除了所有内容。 Then your move() function makes a choice at the end whether to call itself on the next timer iteration, or call the initialize code on the next timer iteration to reset everything and start a new game. 然后你的move()函数在最后选择是否在下一次定时器迭代时调用自身,或者在下一次定时器迭代时调用初始化代码来重置所有内容并开始新游戏。

Below is your code reworked along the above lines as well as various teaks: 下面是您在上面的代码和各种柚木上重新编写的代码:

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

This should let you play over again whether you win or run out of lives. 无论你是赢还是没有生命,这都应该让你重新开始。

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

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