我想在我的程序中添加一个倒计时计时器



所以我在python做游戏,尽管我很困惑,但我几乎完成了。 我想要几件事:

  • 我想制作一个30秒的计时器。玩家有30秒的时间来收集尽可能多的颗粒。
  • 第二,在计时器完成后,乌龟不再可控制。

我需要做什么来解决这些?

屏幕设置

import turtle
import math
import random
#screen
wn=turtle.Screen()
wn.bgcolor("lightblue")
speed=1
wn.tracer(2)

#Score Variable
score=0
#Turtle Player
spaceship= turtle.Turtle()
spaceship.pensize(1)
spaceship.color("red")
spaceship.penup()
turtle.delay(3)
spaceship.shapesize(1,1)
add=1
#Create Goals
maxpoints = 6
points = []
for count in range(maxpoints):
    points.append(turtle.Turtle())
    points[count].color("green")
    points[count].shape("circle")
    points[count].penup()
    points[count].goto(random.randint(-300,300), random.randint(-200,200))
#Border
border = turtle.Turtle()
border.penup()
border.goto(-300,-200)
border.pendown()
border.pensize(5)
border.color("darkblue")
for side in range(2):
    border.forward(600)
    border.left(90)
    border.forward(400)
    border.left(90)

#Functions
def left():
    spaceship.left(30)
def right():
    spaceship.right(30)
def increasespeed():
    global speed
    speed += 1
def decreasespeed():
    global speed
    speed -= 1
def iscollision(turtle1,turtle2):
    collect = math.sqrt(math.pow(turtle1.xcor()-turtle2.xcor(),2)+ math.pow(turtle1.ycor()-turtle2.ycor(),2))
    if collect <20:
        return True
    else:
        return False

键盘绑定以移动乌龟

#Keyboard Bindings
turtle.listen()
turtle.onkey(left,"Left")
turtle.onkey(right,"Right")
turtle.onkey(increasespeed ,"Up")
turtle.onkey(decreasespeed ,"Down")
turtle.onkey(quit, "q")
pen=100
while True:
    spaceship.forward(speed)

#Boundary
if spaceship.xcor()>300 or spaceship.xcor()<-300:
    spaceship.left(180)
if spaceship.ycor()>200 or spaceship.ycor()<-200:
    spaceship.left(180)
#Point collection
for count in range(maxpoints):
    if iscollision(spaceship, points[count]):
        points[count].goto(random.randint(-300,300), random.randint(-200,200))
        score=score+1
        add+=1
        spaceship.shapesize(add)
        #Screen Score
        border.undo()
        border.penup()
        border.hideturtle()
        border.goto(-290,210)
        scorestring = "Score:%s" %score
        border.write(scorestring,False,align="left",font=("Arial",16,"normal"))

在计时器结束后我的程序结束时,我希望乌龟停止移动。用户无法移动乌龟。

我将为您提供一些乌龟ontimer()事件代码来处理您的倒数,但意识到您的代码无法正确组织以处理它。例如。您的无限while True:循环可能会阻止某些事件发射,并且它一定会阻止您的边界和碰撞代码运行。即使不是,您的边界和碰撞代码也仅在需要在每个太空船运动上运行时运行一次。

我已经围绕ontimer()事件重新组织了您的代码,以运行太空飞船,而另一个运行倒数计时器。我已经将边界和碰撞代码固定为基本工作。我遗漏了一些功能(例如spaceship.shapesize(add)(来简化此示例:

from turtle import Turtle, Screen
import random
MAXIMUM_POINTS = 6
FONT = ('Arial', 16, 'normal')
WIDTH, HEIGHT = 600, 400
wn = Screen()
wn.bgcolor('lightblue')
# Score Variable
score = 0
# Turtle Player
spaceship = Turtle()
spaceship.color('red')
spaceship.penup()
speed = 1
# Border
border = Turtle(visible=False)
border.pensize(5)
border.color('darkblue')
border.penup()
border.goto(-WIDTH/2, -HEIGHT/2)
border.pendown()
for _ in range(2):
    border.forward(WIDTH)
    border.left(90)
    border.forward(HEIGHT)
    border.left(90)
border.penup()
border.goto(-WIDTH/2 + 10, HEIGHT/2 + 10)
border.write('Score: {}'.format(score), align='left', font=FONT)
# Create Goals
points = []
for _ in range(MAXIMUM_POINTS):
    goal = Turtle('circle')
    goal.color('green')
    goal.penup()
    goal.goto(random.randint(20 - WIDTH/2, WIDTH/2 - 20), 
        random.randint(20 - HEIGHT/2, HEIGHT/2 - 20))
    points.append(goal)
# Functions
def left():
    spaceship.left(30)
def right():
    spaceship.right(30)
def increasespeed():
    global speed
    speed += 1
def decreasespeed():
    global speed
    speed -= 1
def iscollision(turtle1, turtle2):
    return turtle1.distance(turtle2) < 20
# Keyboard Bindings
wn.onkey(left, 'Left')
wn.onkey(right, 'Right')
wn.onkey(increasespeed, 'Up')
wn.onkey(decreasespeed, 'Down')
wn.onkey(wn.bye, 'q')
wn.listen()
timer = 30
def countdown():
    global timer
    timer -= 1
    if timer <= 0:  # time is up, disable user control
        wn.onkey(None, 'Left')
        wn.onkey(None, 'Right')
        wn.onkey(None, 'Up')
        wn.onkey(None, 'Down')
    else:
        wn.ontimer(countdown, 1000)  # one second from now
wn.ontimer(countdown, 1000)
def travel():
    global score
    spaceship.forward(speed)
    # Boundary
    if not -WIDTH/2 < spaceship.xcor() < WIDTH/2:
        spaceship.left(180)
    if not -HEIGHT/2 < spaceship.ycor() < HEIGHT/2:
        spaceship.left(180)
    # Point collection
    for count in range(MAXIMUM_POINTS):
        if iscollision(spaceship, points[count]):
            points[count].goto(random.randint(20 - WIDTH/2, WIDTH/2 - 20), 
                random.randint(20 - HEIGHT/2, HEIGHT/2 - 20))
            score += 1
            # Screen Score
            border.undo()
            border.write('Score: {}'.format(score), align='left', font=FONT)
    if timer:
        wn.ontimer(travel, 10)
travel()
wn.mainloop()

实现此目的的另一种简单方法是执行以下操作:

  1. 添加一个可容纳时间戳的变量,用于开始游戏时
  2. 包装您要尊重的代码,该代码在运行的一个时循环中,而当前时间和启动时间之间的差异小于您的时间限制。

例如:

import time
starting_time = time.time()
time_limit = 30
while (time.time() - starting_time) < time_limit:
     # YOUR GAME LOGIC HERE

对我有帮助的东西是使用time.time()。如果您在代码开头将变量(例如startTime(设置为time.time(),则可以在主游戏循环中添加IF。if将检查是否已经过去了。例如:

if time.time() > startTime + 30: #whatever you do to end the game

time.time()在秒内给出了当前时间,因此,如果您在代码开头为time.time()设置一个变量,则该变量将为您提供游戏启动的时间。在if中,您检查了当前时间是否达到了您的游戏开始的时间以及30秒。

我希望这有所帮助;如果确实一定要投票!:(

相关内容

最新更新