【问题标题】:Python Turtle - Why does this program not work?Python Turtle - 为什么这个程序不起作用?
【发布时间】:2022-01-17 23:01:05
【问题描述】:

所以,我在做一个蛇游戏。
我有以下代码:

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 窗口并保持空白。
如果我按s,背景颜色会变为白色,仅此而已。

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

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

提前致谢。

【问题讨论】:

  • 请参阅here,了解问题中的“紧急”相关主题。

标签: python turtle-graphics


【解决方案1】:

我不知道为什么这不起作用。

让我们列举一下可能性。您一贯使用分号 (;) 表明对 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();

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

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

【讨论】:

    猜你喜欢
    • 2015-05-21
    • 2013-01-26
    • 2016-04-20
    • 2015-08-16
    • 2015-07-28
    • 1970-01-01
    • 1970-01-01
    • 2019-12-31
    • 2022-01-28
    相关资源
    最近更新 更多