【问题标题】:python turtle if error蟒蛇龟如果错误
【发布时间】:2018-08-24 13:28:47
【问题描述】:

我想用海龟制作蟒蛇游戏并制作关卡。所以,我检查了分数来决定用户的级别但是,当程序运行[if statements]时,我已经制作的海龟的移动会很慢。我不知道这个错误的原因以及如何弄清楚。我需要你的帮助;(

def level():
global speed
if 0 <= score < 5:
    mypen.penup()
    mypen.hideturtle()
    mypen.setposition(-290, 310)
    levelstring = "level: 1"
    mypen.write(levelstring, False, align="left", font=('Consolas', 15, 'bold'))
    speed += 1
if 5 <= score < 10:
    mypen.penup()
    mypen.hideturtle()
    mypen.setposition(-290, 310)
    levelstring = "level: 2"
    mypen.write(levelstring, False, align="left", font=('Consolas', 15, 'bold'))
    speed += 2
elif 10 <= score:
    exit()

while True:
alex.forward(speed)

#boundary checking
if alex.xcor() > 300 or alex.xcor() < -300:
    alex.right(180)

if alex.ycor() > 300 or alex.ycor() < -300:
    alex.right(180)

# boundary checking
if ts.xcor() > 300 or ts.xcor() < -300:
    ts.right(180)

if ts.ycor() > 300 or ts.ycor() < -300:
    ts.right(180)

#collision.checking
d = math.sqrt(math.pow(alex.xcor()-ts.xcor(),2) + math.pow(alex.ycor()-ts.ycor(),2))
if d < 20:
    ts.setposition(random.randint(-300, 300), random.randint(-300, 300))
    score += 1
    level()

#Draw the score
mypen.penup()
mypen.hideturtle()
mypen.setposition(250,310)
scorestring = "Score: %s" %score
mypen.write(scorestring, False, align= "left", font=('Consolas', 15, 'bold'))

【问题讨论】:

  • 寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及重现它所需的最短代码在问题本身。没有清晰的问题陈述的问题对其他读者没有用处。请参阅:How to create a Minimal, Complete, and Verifiable example。
  • 您发布的代码不可运行,因此也不可调试。
  • 发布错误。
  • 如果你惹恼了乌龟......你会得到 Trackeback 毒液告诉你某些东西有毒。修复代码!并开始使用编辑器(例如 IDLE、IPyton 或 komodo 编辑)!
  • 对不起,这是我第一次发布我的代码,整个代码太长了,无法发布。所以,我不知道如何显示我的错误代码

标签: python if-statement turtle-graphics


【解决方案1】:

以下是我根据您提供的代码扩展的工作示例。它会随着每个分数加速自动机,并且随着每个“级别”越来越快。看看它的行为是否符合您的要求:

from turtle import Turtle, Screen
from random import randint

FONT = ('Consolas', 15, 'bold')

def check_level():
    global speed, level

    score_pen.undo()
    score_pen.write("Score: %s" % score, False, align='right', font=FONT)

    if 0 <= score < 5:
        speed += 1
        level = 1
    elif 5 <= score < 10:
        speed += 3
        level = 2
        alex.speed('fastest')
    elif score >= 10:
        screen.bye()

    level_pen.undo()
    level_pen.write("Level: %s" % level, False, align='left', font=FONT)

def move_alex():
    global score

    alex.forward(speed)

    #boundary checking
    if alex.xcor() > 300 or alex.xcor() < -300:
        alex.right(180)

    if alex.ycor() > 300 or alex.ycor() < -300:
        alex.right(180)

    # boundary checking
    if ts.xcor() > 300 or ts.xcor() < -300:
        ts.right(180)

    if ts.ycor() > 300 or ts.ycor() < -300:
        ts.right(180)

    # collision.checking
    if ts.distance(alex) < 20:
        score += 1
        check_level()

        alex.hideturtle()
        alex.setposition(randint(-300, 300), randint(-300, 300))
        alex.showturtle()

    screen.ontimer(move_alex, 100)

def move_right():
    screen.onkey(None, 'Right')
    ts.right(45)
    screen.onkey(move_right, 'Right')

def move_left():
    screen.onkey(None, 'Left')
    ts.left(45)
    screen.onkey(move_left, 'Left')

def move_forward():
    screen.onkey(None, 'Up')
    ts.forward(speed * 1.25)
    screen.onkey(move_forward, 'Up')

screen = Screen()
screen.setup(660, 660)

score = 0
level = 1
speed = 5

# Draw the score
score_pen = Turtle(visible=False)
score_pen.penup()
score_pen.setposition(290, 310)
score_pen.write("Score: %s" % score, False, align='right', font=FONT)

level_pen = Turtle(visible=False)
level_pen.penup()
level_pen.setposition(-290, 310)
level_pen.write("Level: %s" % level, False, align='left', font=FONT)

alex = Turtle('square', visible=False)
alex.speed('normal')
alex.penup()
alex.setheading(45)
alex.setposition(randint(-300, 300), randint(-300, 300))
alex.showturtle()

ts = Turtle('turtle')
ts.speed('fastest')
ts.penup()

screen.onkey(move_right, 'Right')
screen.onkey(move_left, 'Left')
screen.onkey(move_forward, 'Up')

move_alex()
screen.listen()
screen.mainloop()

这只是一个勉强可玩的游戏的例子——它显然需要工作才能完成它并使它像蛇一样。

【讨论】:

    猜你喜欢
    • 2021-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-01
    相关资源
    最近更新 更多