Python Turtle - 为什么这个程序不起作用?

Python Turtle - Why does this program not work?

所以,我在制作蛇游戏。
我有以下代码:

import turtle as tur;
import random, sys, time;

wn = tur.Screen(); #Screen
wn.setup(width=640, height=320);
wn.bgcolor("#000000")

"""

Instructions

"""
boundary = tur.Turtle();
boundary.hideturtle();
writingInstructions = ("Times New Roman", 30, "bold");
writing = ("Times New Roman", 20, "bold");
time.sleep(2);
boundary.write("Instructions:", font=writingInstructions, align="center");
time.sleep(2);
boundary.write("1. You need to eat the apples.", font=writing, align="center");
time.sleep(2);
boundary.write("2. You must not touch the boundary.", font=writing, align="center");
time.sleep(2);
boundary.write("Press 's' to start...");
def no():
    wn.clear();
wn.onkey(no, 's');
wn.listen();

"""

Code for game

"""
shapeSnake = ((-10, 0), (0, 10), (10, 0), (-10, -10)); 
#Initial shape of the snake declared
playArea = ((-200, 200), (200, 200), (200, -200), (-200, -200)); #Play area declared
tur.register_shape('snake', shapeSnake); #Reigster shape for snake
tur.register_shape('play', playArea); #Register shape for play area
t = tur.Turtle(shape='snake'); #Create snake shape
t.up();
a = tur.Turtle(shape='circle'); #Create apple
a.up();
a.goto(randint(-199, 199), randint(-199, 199));
screen = tur.Turtle(shape='play'); #Create play area
score = 0; #score initialisation

def travel(snakeSpeed): #For snake to move
    t.fd(snakeSpeed);

snakeSpeed = 2;

travel(snakeSpeed)
wn.onkey(t.seth(0), 'Right'); #For "right" button
wn.onkey(t.seth(180), 'Left'); #For "left" button
wn.onkey(t.seth(270), 'Down'); #For "down" button
wn.onkey(t.seth(90), 'Up'); #For "up" button

tx, ty = t.pos();
ax, ay = a.pos();
if (tx == ax) and (tx == ax): #Check if apple is eaten
    a.goto(randint(-199, 199), randint(-199, 199));
    score += 1;
    snakeSpeed += 2;
else:
    if (tx >= 200) or (tx <= -200): #Check if outside boundary in X-axis
        sys.exit();
    elif (ty >= 200) or (ty <= -200): #Check if outside boundary in Y-axis
        sys.exit();

wn.listen();
travel(snakeSpeed);
wn.mainloop();

它的作用是只弹出一个黑色背景的 Turtle window 并保持空白。
如果我按下 s,背景颜色会变为白色,仅此而已。

我不知道为什么这不起作用。
非常感谢任何帮助。

请问:我明天要送这个生日礼物。如果有人能立即给予答复,我将不胜感激

提前致谢。

I have no idea why this doesn't work.

让我们列举一下可能性。您对分号 (;) 的一贯使用表明您对 Python 语法缺乏理解。这个序列:

boundary.write("Press 's' to start...");
def no():
    wn.clear();
wn.onkey(no, 's');
wn.listen();

"""
Code for game
"""
shapeSnake = ((-10, 0), (0, 10), (10, 0), (-10, -10)); 

表示您希望 onkey() 在程序继续之前等待按下 's' 键。这不,马上就继续了。在这里,您将调用 t.seth() 的结果传递给 None,而不是稍后调用的函数:

wn.onkey(t.seth(0), 'Right'); #For "right" button
wn.onkey(t.seth(180), 'Left'); #For "left" button
wn.onkey(t.seth(270), 'Down'); #For "down" button
wn.onkey(t.seth(90), 'Up'); #For "up" button

这是对如何设置事件处理程序的误解。我们期望的更像是:

wn.onkey(lambda: t.seth(90), 'Up')  # For "up" button

此评分和边界检查代码出现在文件的顶层并且只执行一次:

tx, ty = t.pos();
ax, ay = a.pos();
if (tx == ax) and (tx == ax): #Check if apple is eaten
    a.goto(randint(-199, 199), randint(-199, 199));
    score += 1;
    snakeSpeed += 2;
else:
    if (tx >= 200) or (tx <= -200): #Check if outside boundary in X-axis
        sys.exit();
    elif (ty >= 200) or (ty <= -200): #Check if outside boundary in Y-axis
        sys.exit();

我们希望这段代码在每次移动后执行,而不是在蛇真正移动之前执行一次。

没有让蛇保持向前移动的代码。它在程序开始时分两次向前移动四个像素,然后再也不会。