Python Turtle窗口仅弹出一秒钟

时间:2018-07-17 04:59:07

标签: python turtle-graphics

我正在用python开发我的前几款游戏,我选择了Snake和Egg Catcher作为我的项目。我的问题是,当我执行代码时,图形窗口仅弹出一秒钟。我假设代码到达末尾时,它会自动关闭该窗口,但我希望该窗口保留在我的游戏中。

我可以添加些什么来确保图形窗口仍然存在,以便可以玩游戏?

非常感谢!

import random
import turtle as t


    #Creating the snake

caterpillar = t.Turtle()
caterpillar.shape('square')
caterpillar.color('red')
caterpillar.speed(0)
caterpillar.penup()
caterpillar.hideturtle()


# Creating the powerup

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)


# Settling our score

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)

# Game functions

def outside_window():
    pass
def game_over():
    pass
def display_score(current_score):
    pass
def place_leaf():
    pass
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()

# Game Drivers

while True:

    caterpillar.forward(caterpillar_speed)

    if caterpillar.distance(leaf) < 20:

        place_leaf()
        caterpillar_length = caterpillar_length + 1
        caterpillar_speed = caterpillar_speed + 1
        score = score + 10
        display_score(score)

    if outside_window():
        game_over()
        break

t.onkey(start_game, 'space')
t.listen()
t.mainloop()

1 个答案:

答案 0 :(得分:0)

由于语法错误,您发布的代码甚至无法打开图形窗口:

score_turtle - t.Turtle()
text_turtle.write('Press SPACE to start', align = 'center', font ('Arial',16,'bold'))

应该是:

score_turtle = t.Turtle()
text_turtle.write('Press SPACE to start', align='center', font=('Arial',16,'bold'))

此外:

  • 关于 global 变量,您有很多错误-我 建议您(重新)了解如何在Python中处理全局变量。

  • 您的while True:循环使您的游戏无法正确启动,并且 在事件驱动程序中没有任何位置。

  • 您的毛毛虫从叶子的顶端开始,因此您的游戏结束了 场景立即触发。

我已经对下面的代码进行了充分的修改,以使游戏清晰启动,但您仍有很多工作要做:

from turtle import Turtle, Screen

# Game functions

def outside_window():
    return False  # implement this for real

def game_over():
    pass  # implement this for real

def display_score(current_score):
    pass  # implement this for real

def place_leaf():
    leaf.hideturtle()
    leaf.goto(100, 100)  # implement this for real
    leaf.showturtle()

def start_game():
    global score
    global caterpillar_speed
    global caterpillar_length

    screen.onkey(None, 'space')

    score = 0
    text_turtle.clear()

    caterpillar_speed = 2
    caterpillar_length = 3
    caterpillar.shapesize(1, caterpillar_length, 1)
    caterpillar.showturtle()

    display_score(score)
    place_leaf()
    crawl()

# Game Drivers

def crawl():
    global score
    global caterpillar_speed
    global caterpillar_length

    caterpillar.forward(caterpillar_speed)

    if caterpillar.distance(leaf) < 20:

        place_leaf()

        caterpillar_speed = caterpillar_speed + 1
        caterpillar_length = caterpillar_length + 1
        caterpillar.shapesize(1, caterpillar_length, 1)

        score = score + 10
        display_score(score)

    if outside_window():
        game_over()
    else:
        screen.ontimer(crawl, 100)

screen = Screen()

# Creating the powerup

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()

# Creating the caterpillar

caterpillar = Turtle('square', visible=False)
caterpillar.color('red')
caterpillar.speed('fastest')
caterpillar.penup()

# Settling our score

text_turtle = Turtle(visible=False)
text_turtle.write('Press SPACE to start', align='center', font=('Arial', 16, 'bold'))

score_turtle = Turtle(visible=False)

screen.onkey(start_game, 'space')
screen.listen()
screen.mainloop()