我想在我的程序中添加一个倒数计时器 [英] I want to add a countdown timer to my program
问题描述
所以我正在用 Python 制作一个游戏,虽然我被难住了,但我快完成了.我想要一些东西:
So I am making a game in Python, and I am almost done, though I am stumped. There are a few things I want:
- 我想制作一个 30 秒的计时器.玩家有 30 秒的时间收集尽可能多的颗粒.
- 其次,定时器完成后,乌龟不再可控.
我需要做什么来解决这些问题?
What do I need to do to fix these?
屏幕设置
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"))
在计时器结束后我的程序结束时,我希望乌龟停止移动;用户无法移动乌龟.
At the end of my program after the timer finishes, I want the turtle to stop moving; the user is unable to move the turtle.
推荐答案
我本来想给你一些乌龟 ontimer()
事件代码来处理你的倒计时,但发现你的代码没有正确组织处理它.例如.您的无限 while True:
循环可能会阻止某些事件触发,它肯定会阻止您的边界和碰撞代码运行.即使没有,您的边界和碰撞代码也只会设置为在每次飞船移动时需要运行一次.
I was going to give you some turtle ontimer()
event code to handle your countdown but realized your code is not organized correctly to handle it. E.g. your infinite while True:
loop potentially blocks some events from firing and it definately prevents your boundary and collision code from running. Even if it didn't, your boundary and collision code are only setup to run once when they need to run on every spaceship movement.
我已经围绕 ontimer()
事件重新组织了您的代码以运行宇宙飞船和另一个运行倒数计时器.我已经修复了边界和碰撞代码,基本上可以工作.我省略了一些功能(例如 spaceship.shapesize(add)
)来简化这个例子:
I've reorganized your code around an ontimer()
event to run the spaceship and another to run the countdown timer. I've fixed the boundary and collision code to basically work. I've left out some features (e.g spaceship.shapesize(add)
) to simplify this example:
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()
这篇关于我想在我的程序中添加一个倒数计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!