我正在制作一种躲避游戏,其中乌龟(形状像乌龟)必须避开其他海龟(形状像鳄鱼)。我让游戏正常运行,但我试图在游戏开始之前添加一个倒计时3,2,1,GO!
我的想法是,因为countdown_number变量我已经变为3,2,1,然后是GO !,我可以通过让其他所有依赖于countdown_number的值来让它在游戏的其余部分之前出现“ GO”。
然而,现在,当我启动程序时,它会卡在3上,并且在更新间隔再次开始之后,而不是继续通过2,1,GO!所以游戏无法启动。
我已经看到很多关于Stack Overflow的问题,这些问题与游戏中的运行计时器和事物有关,但我需要在游戏开始之前进行此操作。
import turtle
import random
import pygame
window_height = 600
window_width = 600
update_interval = 25
countdown_number = 3
river_width = 400
minimum_river_width = 200
border_height = 600
river_width_update = 0.5
safe_distance_from_border = border_height / 2 + 3
number_of_enemies = 10
enemies = []
enemy_speeds = []
enemy_width = 100
enemy_height = 40
enemy_speed_min, enemy_speed_max = 10, 20
safe_distance_from_enemy = 15
def startgame():
global countdown_number
if countdown_number >= 1:
turtle.home()
turtle.left(270)
turtle.clear()
turtle.speed(0)
for _ in range(0,100,5):
turtle.forward(1)
turtle.color("grey"+str(_))
turtle.write(countdown_number, align="center", font=("Arial", 200, "normal"))
if countdown_number == 0:
countdown_number = "GO!"
turtle.home()
turtle.left(270)
turtle.clear()
turtle.speed(0)
for _ in range(0,100,5):
turtle.forward(1)
turtle.color("grey"+str(_))
turtle.write(countdown_number, align="center", font=("Arial", 200, "normal"))
turtle.clear()
#print(number)
def gameover(msg):
print(msg) # Probably comment this out later
turtle.home()
turtle.write(msg, align="center", font=("Arial", 24, "normal"))
def moveplayerturtle(x, y):
if x > -window_width / 2 and x < window_width / 2:
turtle.goto(x, y)
def updatescreen():
global river_width
global countdown_number
if countdown_number != "GO!":
startgame()
if countdown_number == "GO!":
if upper_river_border.ycor() >= window_height-200:
upper_river_border.sety(upper_river_border.ycor() - river_width_update)
if lower_river_border.ycor() <= -window_height+200:
lower_river_border.sety(lower_river_border.ycor() + river_width_update)
if upper_river_border.ycor() - turtle.ycor() < safe_distance_from_border:
gameover("Game over!")
return
if turtle.ycor() - lower_river_border.ycor() < safe_distance_from_border:
gameover("Game over!")
return
for i in range(number_of_enemies):
enemies[i].forward(enemy_speeds[i])
if enemies[i].xcor() > (window_width+enemy_width)/2:
x = -(window_width+enemy_width)/2
y = int(turtle.ycor())
enemies[i].goto(x, y)
s=1
enemy_speeds[i]=int(random.randrange(10+s, 20+s))
if turtle.distance(enemies[i]) < safe_distance_from_enemy:
gameover("You lose!")
return
turtle.ontimer(updatescreen, update_interval)
turtle.update()
turtle.setup(window_width, window_height) # Set the window size
turtle.bgcolor("DarkBlue")
turtle.tracer(False)
upper_river_border = turtle.Turtle()
upper_river_border.up()
lower_river_border = turtle.Turtle()
lower_river_border.up()
upper_river_border.shape("square")
lower_river_border.shape("square")
upper_river_border.color("DarkOrange4")
lower_river_border.color("DarkOrange4")
upper_river_border.shapesize(30, 40)
lower_river_border.shapesize(30, 40)
upper_river_border.sety((border_height + river_width) / 2)
lower_river_border.sety(-(border_height + river_width) / 2)
turtle.addshape("crocodile.gif")
for _ in range(number_of_enemies):
enemy = turtle.Turtle()
turtle.addshape("crocodile.gif")
enemy.shape("crocodile.gif")
enemy.up()
x = -(window_width + enemy_width) / 2
y = random.randint(-(river_width-enemy_height)/2, (river_width-enemy_height)/2)
enemy.goto(x, y)
enemies.append(enemy)
enemy_speeds.append(random.randint(enemy_speed_min, enemy_speed_max))
turtle.shape("turtle")
turtle.left(180)
turtle.color("GreenYellow")
turtle.up()
turtle.ondrag(moveplayerturtle)
turtle.ontimer(updatescreen, update_interval)
turtle.done()
当我把它写成自己的程序时,我已经把倒计时器自行完美地工作了所以我认为它必须与从倒计时序列转换到导致问题的游戏循环本身有关
任何帮助都将不胜感激。
答案 0 :(得分:0)
你的电线越过闪屏倒计时 - 在错误的时间调用错误的东西。以下是关于最小修复的建议。首先,重新定义startgame()
如下:
def startgame():
global countdown_number
turtle.home()
turtle.clear()
turtle.left(270)
if countdown_number > 0:
for grey in range(0, 100, 5):
turtle.forward(1)
turtle.color("grey" + str(grey))
turtle.write(countdown_number, align="center", font=("Arial", 200, "normal"))
countdown_number -= 1
turtle.ontimer(startgame, countdown_interval)
else:
for grey in range(0, 100, 5):
turtle.forward(1)
turtle.color("grey" + str(grey))
turtle.write("GO!", align="center", font=("Arial", 200, "normal"))
turtle.clear()
turtle.ontimer(updatescreen, countdown_interval)
接下来一些小改动以适应这种重写。在代码的顶部,添加定义:
countdown_interval = 1000
接近update_interval
的定义。
在代码的底部,而不是:
turtle.ontimer(updatescreen, update_interval)
turtle.done()
这样做:
turtle.ontimer(startgame, update_interval)
turtle.done()
最后,在updatescreen()
中删除所有处理countdown_number
的代码:
global countdown_number
if countdown_number != "GO!":
startgame()
if countdown_number == "GO!":
并根据需要重新使用此功能的主体。
应该这样做。快乐的躲闪!