【问题标题】:Python Turtle window only pops up for a secondPython Turtle 窗口只弹出一秒钟
【发布时间】:2018-07-17 04:59:06
【问题描述】:

我正在用 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()

【问题讨论】:

  • 是turtle还是pygame?
  • 这是一只乌龟 - 如有错误请见谅!

标签: python turtle-graphics


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-02
    • 1970-01-01
    • 1970-01-01
    • 2018-01-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多