【发布时间】:2021-04-24 17:13:09
【问题描述】:
我正在尝试创建 Snake 游戏,但 tkinter 窗口在我看到任何内容之前关闭
我用过turtle.mainloop(),还是没用
import turtle as t
import random as rd
import time as ti
wn = t.Screen()
wn.bgcolor('yellow')
wn.title('Snake game')
wn.canvheight(200)
wn.canvwidth(300)
snake = t.Turtle()
scores = t.Turtle()
text = t.Turtle()
leaf = t.Turtle()
def start():
pass
def down_move():
if snake.heading() == 0 or snake.heading() == 180:
snake.setheading(270)
def up_move():
if snake.heading() == 0 or snake.heading() == 180:
snake.setheading(90)
def left_move():
if snake.heading() == 90 or snake.heading() == 270:
snake.setheading(180)
def right_move():
if snake.heading() == 90 or snake.heading() == 270:
snake.setheading(0)
text.write("Start the game", align = "center", font=("Arial", 50, "bold"))
wn.onkey(start, "space")
wn.onkey(down_move,"Down")
wn.onkey(up_move,"Up")
wn.onkey(left_move,"Left")
wn.onkey(right_move,"Right")
wn.listen()
wn.mainloop()
我使用 Visual Studio Code 作为编辑器 请让我知道如何阻止这种情况 看起来,它不是代码,而是其他东西
【问题讨论】:
-
您能否请教我们一些最小的工作代码以便我们测试它?例如我不知道
turtle是名为turtle的python模块还是名为turtle的<tkinter.Tk>窗口 -
请阅读如何创建minimal reproducible example
-
@Matiiss 我已经用最少的代码更新了问题,请看一下
-
@Ash
wn.canvheight是 pythonint。这不是设置海龟屏幕高度的正确方法。查看here 了解更多信息。 TL;DR 使用screen.setup(width, height)而不是wn.canvheight = 200和wn.canvwidth(300) -
@t 谢谢。
screen.setup(width, height)工作