我的毛毛虫在一帧中显示,然后屏幕显示" Game Over"。请告诉我为什么会这样。屏幕应该说" Game Over"当毛毛虫离开屏幕时,它会在您启动后立即执行。我的代码:
import random
import turtle as t
t.bgcolor('yellow')
caterpillar = t.Turtle()
caterpillar.shape('square')
caterpillar.color('red')
caterpillar.speed(0)
caterpillar.penup()
caterpillar.hideturtle()
leaf = t.Turtle()
leaf_shape = ((0, 0), (14, 2), (18, 6), (20, 20), (6, 18), (2, 14))
t.register_shape('leaf', leaf_shape)
leaf.shape('leaf')
leaf.color('green')
leaf.penup()
leaf.hideturtle()
leaf.speed(0)
game_started = False
text_turtle = t.Turtle()
text_turtle.write('Press SPACE to start', align='center', font=('Arial', 16,
'bold'))
text_turtle.hideturtle
score_turtle = t.Turtle()
score_turtle.hideturtle()
score_turtle.speed(0)
def outside_window():
left_wall = -t.window_width() / 2
right_wall = t.window_width() / 2
top_wall = t.window_height() / 2
bottom_wall = -t.window_height() / 2
(x, y) = caterpillar.pos()
outside = \
x< left_wall or \
x> right_wall or \
y< bottom_wall or \
y> top_wall
return outside
def game_over():
caterpillar.color('yellow')
leaf.color('yellow')
t.penup()
t.hideturtle()
t.write('GAME OVER!', align='center', font=('Arial', 30, 'normal'))
def display_score(current_score):
score_turtle.clear()
score_turtle.penup()
x = (t.window_width() / 2) - 50
y = (t.window_height() / 2) - 50
score_turtle.setpos(x, y)
score_turtle.write(str(current_score), align='right', font=('Arial', 40,
'bold))
def place_leaf():
leaf.ht()
leaf.setx(random.randint(-200, 200))
leaf.sety(random.randint(-200, 200))
leaf.st()
def start_game():
global game_started
if game_started:
return
game_started = True
score = 0
text_turtle.clear()
caterpillar_speed = 2
caterpillar_length = 3
caterpillar.shapesize(1, caterpillar_length, 1)
caterpillar.showturtle()
display_score(score)
place_leaf()
while True:
caterpillar.forward(caterpillar_speed)
if caterpillar.distance(leaf) < 20:
place_leaf()
caterpillar_length = caterpillar_length + 1
caterpillar.shapesize(1, caterpillar_length, 1)
caterpillar_speed = caterpillar_speed + 1
score = score + 10
display_score()
if outside_window:
game_over()
break
def move_up():
if caterpillar.heading() == 0 or caterpillar.heading() == 180:
caterpillar.setheading(90)
def move_down():
if caterpillar.heading() == 0 or caterpillar.heading() == 180:
caterpillar.setheading(270)
def move_left():
if caterpillar.heading() == 90 or caterpillar.heading() == 270:
caterpillar.setheading(180)
def move_right():
if caterpillar.heading() == 90 or caterpillar.heading() == 270:
caterpillar.setheading(0)
t.onkey(start_game, 'space')
t.onkey(move_up, 'Up')
t.onkey(move_right, 'Right')
t.onkey(move_down, 'Down')
t.onkey(move_left, 'Left')
t.listen()
t.mainloop()
请帮我发现错误的事情。此代码来自Python&#34;中的编码项目。我无法找出它死亡的原因。我重新输入了代码,但后来卡特彼勒没动了!我发现了一些错误,但它确实是1帧和游戏结束!如果您发现一些逻辑错误,请告诉我。我是Python的菜鸟。
答案 0 :(得分:0)
我看到的是很多小的编辑错误:
text_turtle.hideturtle -> text_turtle.hideturtle()
font=('Arial', 40, 'bold) -> font=('Arial', 40, 'bold')
display_score() -> display_score(score)
特别是这个会导致程序快速退出:
if outside_window: -> if outside_window():
以下是我修复代码以修复上述问题以及代码的一些样式更改:
import random
from turtle import Turtle, Screen
screen = Screen()
screen.bgcolor('yellow')
caterpillar = Turtle('square', visible=False)
caterpillar.color('red')
caterpillar.speed('fastest')
caterpillar.penup()
leaf_shape = ((0, 0), (14, 2), (18, 6), (20, 20), (6, 18), (2, 14))
screen.register_shape('leaf', leaf_shape)
leaf = Turtle('leaf', visible=False)
leaf.color('green')
leaf.speed('fastest')
leaf.penup()
game_started = False
text_turtle = Turtle(visible=False)
text_turtle.write('Press SPACE to start', align='center', font=('Arial', 16, 'bold'))
score_turtle = Turtle(visible=False)
score_turtle.speed('fastest')
score_turtle.penup()
x = (screen.window_width() / 2) - 50
y = (screen.window_height() / 2) - 50
score_turtle.setpos(x, y)
score_turtle.write(0, align='right', font=('Arial', 40, 'bold'))
def outside_window():
left_wall = -screen.window_width() / 2
right_wall = screen.window_width() / 2
top_wall = screen.window_height() / 2
bottom_wall = -screen.window_height() / 2
x, y = caterpillar.pos()
outside = \
x < left_wall or \
x > right_wall or \
y < bottom_wall or \
y > top_wall
return outside
def game_over():
caterpillar.color('yellow')
leaf.color('yellow')
text_turtle.write('GAME OVER!', align='center', font=('Arial', 30, 'normal'))
def display_score(current_score):
score_turtle.undo() # erase previous score
score_turtle.write(current_score, align='right', font=('Arial', 40, 'bold'))
def place_leaf():
leaf.hideturtle()
leaf.setx(random.randint(-200, 200))
leaf.sety(random.randint(-200, 200))
leaf.showturtle()
def start_game():
global game_started
if game_started:
return
game_started = True
score = 0
text_turtle.clear()
caterpillar_speed = 2
caterpillar_length = 3
caterpillar.shapesize(1, caterpillar_length, 1)
caterpillar.showturtle()
display_score(score)
place_leaf()
while True:
caterpillar.forward(caterpillar_speed)
if caterpillar.distance(leaf) < 20:
place_leaf()
caterpillar_length += 1
caterpillar.shapesize(1, caterpillar_length, 1)
caterpillar_speed += 1
score += 10
display_score(score)
if outside_window():
game_over()
break
def move_up():
if caterpillar.heading() == 0 or caterpillar.heading() == 180:
caterpillar.setheading(90)
def move_down():
if caterpillar.heading() == 0 or caterpillar.heading() == 180:
caterpillar.setheading(270)
def move_left():
if caterpillar.heading() == 90 or caterpillar.heading() == 270:
caterpillar.setheading(180)
def move_right():
if caterpillar.heading() == 90 or caterpillar.heading() == 270:
caterpillar.setheading(0)
screen.onkey(start_game, 'space')
screen.onkey(move_up, 'Up')
screen.onkey(move_right, 'Right')
screen.onkey(move_down, 'Down')
screen.onkey(move_left, 'Left')
screen.listen()
screen.mainloop()
玩起来很有趣!