如何在游戏中添加计时器,以便在计时器达到 0 时重置游戏?
How can I add a timer to a game to make the game reset when the timer reaches 0?
我正在制作一个屏幕上有追逐者和奔跑者(Bob 和 Phil)的游戏。这很像标签。截至目前,如果追赶者与奔跑者相撞,游戏将重置,玩家将移回原来的位置。我想添加一个计时器,以便当计时器达到 0 时,游戏将重置。
我想过有一个名为 'time' 的变量,并让它等于 60。然后我会让变量每秒减 1,时间 -= 1 和 time.sleep 函数。问题是 time.sleep 函数将停止整个程序。有没有什么办法可以在我的游戏中实现一个计时器而不用 运行 解决前面提到的问题?
import turtle
import math
# Distance formula for collision checking
def isCollision(t1, t2):
distance = math.sqrt(math.pow(t1.xcor()-t2.xcor(),2)+math.pow(t1.ycor()-t2.ycor(),2))
if distance < 30:
return True
else:
return False
# Makes the wn that the game is played on
wn = turtle.Screen()
wn.bgcolor("black")
wn.setup(width = 900, height = 900)
wn.tracer()
# Runs the actual game when space is pressed, see around end of code to understand
def game():
start.clear()
runner = "omright.gif"
chaser = "pilav angry.gif"
wn.addshape(chaser)
wn.addshape(runner)
#Designs the 4 sides of the game
border_pen = turtle.Turtle()
border_pen.speed(0)
border_pen.color("white")
border_pen.penup()
border_pen.setposition(-300,-300)
border_pen.pendown()
border_pen.pensize(3)
for side in range(4):
border_pen.fd(600)
border_pen.lt(90)
border_pen.hideturtle()
title = turtle.Turtle()
title.speed(0)
title.color("white")
title.penup()
title.setpos(0, 325)
title.write("THE TAG GAME BY OM", False, align = 'center', font=('Impact', 55, 'normal'))
title.setpos(370, 315)
title.write("Patent Pending", False, align = 'center', font=('Calibri', 11, 'normal'))
title.setpos(370, -335)
title.write("Not Really Pending", False, align = 'center', font=('Calibri', 11, 'normal'))
title.hideturtle()
# First turtle (basically an object that can be manipulated)
bob = turtle.Turtle()
bob.speed(0)
bob.color("blue")
bob.penup()
bob.setpos(-50, 0)
bob.seth(0)
bob.shape(chaser)
# Function to move first turtle (bob)
def fd():
bob.seth(90)
bob.fd(20)
# Border Checking
if bob.xcor() > 280:
bob.setx(280)
if bob.ycor() > 280:
bob.sety(280)
if bob.xcor() < -280:
bob.setx(-280)
if bob.ycor() < -280:
bob.sety(-280)
# Checks for collision with enemy player
if isCollision(bob, phil):
# If there is collision, resets players'
bob.setposition(-50, 0)
phil.setposition(50, 0)
bob.seth(0)
phil.seth(180)
# Moves right (90 degrees)
def right():
bob.seth(0)
bob.fd(20)
# Border Checking
if bob.xcor() > 280:
bob.setx(280)
if bob.ycor() > 280:
bob.sety(280)
if bob.xcor() < -280:
bob.setx(-280)
if bob.ycor() < -280:
bob.sety(-280)
# Checks for collision with enemy player
if isCollision(bob, phil):
# If there is collision, resets players'
bob.setposition(-50, 0)
phil.setposition(50, 0)
bob.seth(0)
phil.seth(180)
# Moves left (90 degrees)
def left():
bob.seth(180)
bob.fd(20)
# Border Checking
if bob.xcor() > 280:
bob.setx(280)
if bob.ycor() > 280:
bob.sety(280)
if bob.xcor() < -280:
bob.setx(-280)
if bob.ycor() < -280:
bob.sety(-280)
# Checks for collision with enemy player
if isCollision(bob, phil):
# If there is collision, resets players'
bob.setposition(-50, 0)
phil.setposition(50, 0)
bob.seth(0)
phil.seth(180)
def down():
bob.seth(270)
bob.fd(20)
# Border Checking
if bob.xcor() > 280:
bob.setx(280)
if bob.ycor() > 280:
bob.sety(280)
phil.seth(180)
if bob.xcor() < -280:
bob.setx(-280)
if bob.ycor() <- 280:
bob.sety(-280)
# Checks for collision with enemy player
if isCollision(bob, phil):
# If there is collision, resets players'
bob.setposition(-50, 0)
phil.setposition(50, 0)
bob.seth(0)
phil.seth(180)
# Logs keys and outputs function
wn.listen()
wn.onkey(left, "Left")
wn.onkey(fd, "Up")
wn.onkey(right, "Right")
wn.onkey(down, "Down")
# Second turtle
phil = turtle.Turtle()
phil.speed(0)
phil.penup()
phil.shape("triangle")
phil.color("red")
phil.setpos(50, 0)
phil.seth(180)
phil.shape(runner)
#Refer back to first turtle
def fdp():
phil.seth(90)
phil.fd(20)
if phil.xcor() > 280:
phil.setx(280)
if phil.ycor() > 280:
phil.sety(280)
if phil.xcor() < -280:
phil.setx(-280)
if phil.ycor() < -280:
phil.sety(-280)
if isCollision(bob, phil):
bob.setposition(-50, 0)
phil.setposition(50, 0)
bob.seth(0)
phil.seth(180)
def rightp():
phil.seth(0)
phil.fd(20)
# Border Checking
if phil.xcor() > 280:
phil.setx(280)
if phil.ycor() > 280:
phil.sety(280)
if phil.xcor() < -280:
phil.setx(-280)
if phil.ycor() < -280:
phil.sety(-280)
# Checks for collision with enemy player
if isCollision(phil, bob):
# If there is collision, resets players'
bob.setposition(-50, 0)
phil.setposition(50, 0)
bob.seth(0)
phil.seth(180)
def leftp():
phil.seth(180)
phil.fd(20)
# Border Checking
if phil.xcor() > 280:
phil.setx(280)
if phil.ycor() > 280:
phil.sety(280)
if phil.xcor() < -280:
phil.setx(-280)
if phil.ycor() < -280:
phil.sety(-280)
# Checks for collision with enemy player
if isCollision(phil, bob):
# If there is collision, resets players'
bob.setposition(-50, 0)
phil.setposition(50, 0)
bob.seth(0)
phil.seth(180)
def downp():
phil.seth(270)
phil.fd(20)
# Border Checking
if phil.xcor() > 280:
phil.setx(280)
if phil.ycor() > 280:
phil.sety(280)
if phil.xcor() < -280:
phil.setx(-280)
if phil.ycor() < -280:
phil.sety(-280)
# Checks for collision with enemy player
if isCollision(phil, bob):
# If there is collision, resets players'
bob.setposition(-50, 0)
phil.setposition(50, 0)
bob.seth(0)
phil.seth(180)
wn.listen()
wn.onkey(leftp, "a")
wn.onkey(fdp, "w")
wn.onkey(rightp, "d")
wn.onkey(downp, "s")
# Just a start screen
start = turtle.Turtle()
start.speed(0)
start.color("white")
start.penup()
start.goto(0, 0)
start.write("PRESS SPACE TO START!", False, align='center', font=('Arial Bold', 48, 'normal'))
start.hideturtle()
wn.listen()
wn.onkey(game, "space")
# Stops the program from closing automatically when it is run
turtle.done()
除了实际的计时器,我还想在屏幕顶部以文本形式实际显示计时器,但我相当有信心在我可以实际添加计时器时将其关闭.
我不熟悉你用来创建游戏的包,但我发现这个 similar question 在答案中说明 turtle 中有一个 ontimer()
函数。
这部分特别有趣:
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')
# Whatever else you need to do to tell the user its game over
else:
wn.ontimer(countdown, 1000) # one second from now
wn.ontimer(countdown, 1000) # start the timer
因此该函数仅在设定的毫秒数(1000,在本例中为 1 秒)后调用自身,如果它为零则禁用控件(根据您的游戏需要自定义)。
您可以将计时器设置为 100 毫秒,并将 timer -= 1
更改为 -= 0.1
以获得用于显示目的的更精细的时钟。
只记得在全局的某个地方创建一个计时器。
我正在制作一个屏幕上有追逐者和奔跑者(Bob 和 Phil)的游戏。这很像标签。截至目前,如果追赶者与奔跑者相撞,游戏将重置,玩家将移回原来的位置。我想添加一个计时器,以便当计时器达到 0 时,游戏将重置。
我想过有一个名为 'time' 的变量,并让它等于 60。然后我会让变量每秒减 1,时间 -= 1 和 time.sleep 函数。问题是 time.sleep 函数将停止整个程序。有没有什么办法可以在我的游戏中实现一个计时器而不用 运行 解决前面提到的问题?
import turtle
import math
# Distance formula for collision checking
def isCollision(t1, t2):
distance = math.sqrt(math.pow(t1.xcor()-t2.xcor(),2)+math.pow(t1.ycor()-t2.ycor(),2))
if distance < 30:
return True
else:
return False
# Makes the wn that the game is played on
wn = turtle.Screen()
wn.bgcolor("black")
wn.setup(width = 900, height = 900)
wn.tracer()
# Runs the actual game when space is pressed, see around end of code to understand
def game():
start.clear()
runner = "omright.gif"
chaser = "pilav angry.gif"
wn.addshape(chaser)
wn.addshape(runner)
#Designs the 4 sides of the game
border_pen = turtle.Turtle()
border_pen.speed(0)
border_pen.color("white")
border_pen.penup()
border_pen.setposition(-300,-300)
border_pen.pendown()
border_pen.pensize(3)
for side in range(4):
border_pen.fd(600)
border_pen.lt(90)
border_pen.hideturtle()
title = turtle.Turtle()
title.speed(0)
title.color("white")
title.penup()
title.setpos(0, 325)
title.write("THE TAG GAME BY OM", False, align = 'center', font=('Impact', 55, 'normal'))
title.setpos(370, 315)
title.write("Patent Pending", False, align = 'center', font=('Calibri', 11, 'normal'))
title.setpos(370, -335)
title.write("Not Really Pending", False, align = 'center', font=('Calibri', 11, 'normal'))
title.hideturtle()
# First turtle (basically an object that can be manipulated)
bob = turtle.Turtle()
bob.speed(0)
bob.color("blue")
bob.penup()
bob.setpos(-50, 0)
bob.seth(0)
bob.shape(chaser)
# Function to move first turtle (bob)
def fd():
bob.seth(90)
bob.fd(20)
# Border Checking
if bob.xcor() > 280:
bob.setx(280)
if bob.ycor() > 280:
bob.sety(280)
if bob.xcor() < -280:
bob.setx(-280)
if bob.ycor() < -280:
bob.sety(-280)
# Checks for collision with enemy player
if isCollision(bob, phil):
# If there is collision, resets players'
bob.setposition(-50, 0)
phil.setposition(50, 0)
bob.seth(0)
phil.seth(180)
# Moves right (90 degrees)
def right():
bob.seth(0)
bob.fd(20)
# Border Checking
if bob.xcor() > 280:
bob.setx(280)
if bob.ycor() > 280:
bob.sety(280)
if bob.xcor() < -280:
bob.setx(-280)
if bob.ycor() < -280:
bob.sety(-280)
# Checks for collision with enemy player
if isCollision(bob, phil):
# If there is collision, resets players'
bob.setposition(-50, 0)
phil.setposition(50, 0)
bob.seth(0)
phil.seth(180)
# Moves left (90 degrees)
def left():
bob.seth(180)
bob.fd(20)
# Border Checking
if bob.xcor() > 280:
bob.setx(280)
if bob.ycor() > 280:
bob.sety(280)
if bob.xcor() < -280:
bob.setx(-280)
if bob.ycor() < -280:
bob.sety(-280)
# Checks for collision with enemy player
if isCollision(bob, phil):
# If there is collision, resets players'
bob.setposition(-50, 0)
phil.setposition(50, 0)
bob.seth(0)
phil.seth(180)
def down():
bob.seth(270)
bob.fd(20)
# Border Checking
if bob.xcor() > 280:
bob.setx(280)
if bob.ycor() > 280:
bob.sety(280)
phil.seth(180)
if bob.xcor() < -280:
bob.setx(-280)
if bob.ycor() <- 280:
bob.sety(-280)
# Checks for collision with enemy player
if isCollision(bob, phil):
# If there is collision, resets players'
bob.setposition(-50, 0)
phil.setposition(50, 0)
bob.seth(0)
phil.seth(180)
# Logs keys and outputs function
wn.listen()
wn.onkey(left, "Left")
wn.onkey(fd, "Up")
wn.onkey(right, "Right")
wn.onkey(down, "Down")
# Second turtle
phil = turtle.Turtle()
phil.speed(0)
phil.penup()
phil.shape("triangle")
phil.color("red")
phil.setpos(50, 0)
phil.seth(180)
phil.shape(runner)
#Refer back to first turtle
def fdp():
phil.seth(90)
phil.fd(20)
if phil.xcor() > 280:
phil.setx(280)
if phil.ycor() > 280:
phil.sety(280)
if phil.xcor() < -280:
phil.setx(-280)
if phil.ycor() < -280:
phil.sety(-280)
if isCollision(bob, phil):
bob.setposition(-50, 0)
phil.setposition(50, 0)
bob.seth(0)
phil.seth(180)
def rightp():
phil.seth(0)
phil.fd(20)
# Border Checking
if phil.xcor() > 280:
phil.setx(280)
if phil.ycor() > 280:
phil.sety(280)
if phil.xcor() < -280:
phil.setx(-280)
if phil.ycor() < -280:
phil.sety(-280)
# Checks for collision with enemy player
if isCollision(phil, bob):
# If there is collision, resets players'
bob.setposition(-50, 0)
phil.setposition(50, 0)
bob.seth(0)
phil.seth(180)
def leftp():
phil.seth(180)
phil.fd(20)
# Border Checking
if phil.xcor() > 280:
phil.setx(280)
if phil.ycor() > 280:
phil.sety(280)
if phil.xcor() < -280:
phil.setx(-280)
if phil.ycor() < -280:
phil.sety(-280)
# Checks for collision with enemy player
if isCollision(phil, bob):
# If there is collision, resets players'
bob.setposition(-50, 0)
phil.setposition(50, 0)
bob.seth(0)
phil.seth(180)
def downp():
phil.seth(270)
phil.fd(20)
# Border Checking
if phil.xcor() > 280:
phil.setx(280)
if phil.ycor() > 280:
phil.sety(280)
if phil.xcor() < -280:
phil.setx(-280)
if phil.ycor() < -280:
phil.sety(-280)
# Checks for collision with enemy player
if isCollision(phil, bob):
# If there is collision, resets players'
bob.setposition(-50, 0)
phil.setposition(50, 0)
bob.seth(0)
phil.seth(180)
wn.listen()
wn.onkey(leftp, "a")
wn.onkey(fdp, "w")
wn.onkey(rightp, "d")
wn.onkey(downp, "s")
# Just a start screen
start = turtle.Turtle()
start.speed(0)
start.color("white")
start.penup()
start.goto(0, 0)
start.write("PRESS SPACE TO START!", False, align='center', font=('Arial Bold', 48, 'normal'))
start.hideturtle()
wn.listen()
wn.onkey(game, "space")
# Stops the program from closing automatically when it is run
turtle.done()
除了实际的计时器,我还想在屏幕顶部以文本形式实际显示计时器,但我相当有信心在我可以实际添加计时器时将其关闭.
我不熟悉你用来创建游戏的包,但我发现这个 similar question 在答案中说明 turtle 中有一个 ontimer()
函数。
这部分特别有趣:
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')
# Whatever else you need to do to tell the user its game over
else:
wn.ontimer(countdown, 1000) # one second from now
wn.ontimer(countdown, 1000) # start the timer
因此该函数仅在设定的毫秒数(1000,在本例中为 1 秒)后调用自身,如果它为零则禁用控件(根据您的游戏需要自定义)。
您可以将计时器设置为 100 毫秒,并将 timer -= 1
更改为 -= 0.1
以获得用于显示目的的更精细的时钟。
只记得在全局的某个地方创建一个计时器。