Python 解析错误和不正确的坐标
我在《海龟》中创作的游戏遇到了麻烦。每当我尝试运行该程序时,都会出现回溯错误:Python 解析错误和不正确的坐标,python,turtle-graphics,Python,Turtle Graphics,我在《海龟》中创作的游戏遇到了麻烦。每当我尝试运行该程序时,都会出现回溯错误:ParseError:main.py的第69行输入错误。但是早些时候(当错误没有发生时),我的坐标没有正常工作。我想我输入的是正确的,但不起作用。在游戏中,你需要击中水果。因此,游戏主要是这样的:如果你错过了水果,你的分数将减少一分,你的生活将减少一颗心。首先,它会检查您的分数是否为0。如果你吃了水果,你应该得到+1分。但不幸的是,如果我在窗口左侧的任何地方投篮,我会得到+1分。不管距离有多远 我怀疑问题所在的代码是:
ParseError:main.py的第69行输入错误。但是早些时候(当错误没有发生时),我的坐标没有正常工作。我想我输入的是正确的,但不起作用。在游戏中,你需要击中水果。因此,游戏主要是这样的:如果你错过了水果,你的分数将减少一分,你的生活将减少一颗心。首先,它会检查您的分数是否为0。如果你吃了水果,你应该得到+1分。但不幸的是,如果我在窗口左侧的任何地方投篮,我会得到+1分。不管距离有多远
我怀疑问题所在的代码是:
def cannon_shooting():
bullet1 = random.randint(1, 4)
global live_score
if live_score >= 100:
bullet1 = 5
if bullet1 == 1:
bullet.color('black')
if bullet1 == 2:
bullet.color('green')
if bullet1 == 3:
bullet.color('red')
if bullet1 == 4:
bullet.color('grey')
if bullet1 == 5:
bullet.color('white')
bullet.goto(cannon_x, -30)
bullet.showturtle()
bullet.goto(cannon_x, 130)
if bullet.ycor() == enemy.ycor() - 10:
if not bullet.xcor() == enemy.xcor() - 10:
if live_score == 0:
if live_score == 100:
score.color('white')
live_score = 0
score.clear()
if live_score >= 0:
score.write("Your score: %s" % live_score, font=(None, 11, "bold"))
else:
if live_score == 100:
score.color('white')
live_score = live_score - 1
score.clear()
if live_score >= 0:
score.write("Your score: %s" % live_score, font=(None, 11, "bold"))
move_speed = random.randint(1, 10)
lives = lives - 1
if bullet.xcor() = enemy.xcor() - 10:
if live_score == 100:
score.color('white')
live_score = live_score + 1
score.clear()
if live_score >= 0:
score.write("Your score: %s" % live_score, font=(None, 11, "bold"))
enemy1 = random.randint(1, 3)
move_speed = random.randint(1, 10)
if live_score == 100:
score.clear()
cannon.goto(0, -100)
cannon.hideturtle()
enemy.hideturtle()
bullet.hideturtle()
bullet.goto(0, -30)
win.color('green')
win.write("Congratulations!", font=(None, 20, "bold"))
win.goto(-140, -40)
win.write("You scored 100 points!", font=(None, 20, "bold"))
if lives == 0:
score.clear()
cannon.goto(0, -100)
cannon.hideturtle()
enemy.hideturtle()
bullet.hideturtle()
bullet.goto(0, -30)
game_over.color('red')
game_over.write("Game Over", font=(None, 20, "bold"))
game_over.goto(-140, -40)
game_over.write("You scored %d points!" % live_score, font=(None, 20, "bold"))
bullet.hideturtle()
全部代码:
import random
import turtle
#images
image_coconut = "Coconut.png"
image_banana = "Banana.png"
image_pineapple = "Pineapple.png"
image_cannon = "Cannon.png"
#definitions
live_score = 0
screen = turtle.Screen()
wn = turtle.Screen()
cannon = turtle.Turtle()
enemy = turtle.Turtle()
score = turtle.Turtle()
score1 = turtle.Turtle()
background = turtle.Turtle()
reset = turtle.Turtle()
bullet = turtle.Turtle()
overlay = turtle.Turtle()
enemy_x = enemy.xcor()
enemy_y = enemy.ycor()
win = turtle.Turtle()
game_over = turtle.Turtle()
lives = 3
cannon_x = 0
cannon_x
move_speed = 2
enemy1 = 0
score_goto = 0
def cannon_shooting():
bullet1 = random.randint(1, 4)
global live_score
if live_score >= 100:
bullet1 = 5
if bullet1 == 1:
bullet.color('black')
if bullet1 == 2:
bullet.color('green')
if bullet1 == 3:
bullet.color('red')
if bullet1 == 4:
bullet.color('grey')
if bullet1 == 5:
bullet.color('white')
bullet.goto(cannon_x, -30)
bullet.showturtle()
bullet.goto(cannon_x, 130)
if bullet.ycor() == enemy.ycor() - 10:
if not bullet.xcor() == enemy.xcor() - 10:
if live_score == 0:
if live_score == 100:
score.color('white')
live_score = 0
score.clear()
if live_score >= 0:
score.write("Your score: %s" % live_score, font=(None, 11, "bold"))
else:
if live_score == 100:
score.color('white')
live_score = live_score - 1
score.clear()
if live_score >= 0:
score.write("Your score: %s" % live_score, font=(None, 11, "bold"))
move_speed = random.randint(1, 10)
lives = lives - 1
if bullet.xcor() = enemy.xcor() - 10:
if live_score == 100:
score.color('white')
live_score = live_score + 1
score.clear()
if live_score >= 0:
score.write("Your score: %s" % live_score, font=(None, 11, "bold"))
enemy1 = random.randint(1, 3)
move_speed = random.randint(1, 10)
if live_score == 100:
score.clear()
cannon.goto(0, -100)
cannon.hideturtle()
enemy.hideturtle()
bullet.hideturtle()
bullet.goto(0, -30)
win.color('green')
win.write("Congratulations!", font=(None, 20, "bold"))
win.goto(-140, -40)
win.write("You scored 100 points!", font=(None, 20, "bold"))
if lives == 0:
score.clear()
cannon.goto(0, -100)
cannon.hideturtle()
enemy.hideturtle()
bullet.hideturtle()
bullet.goto(0, -30)
game_over.color('red')
game_over.write("Game Over", font=(None, 20, "bold"))
game_over.goto(-140, -40)
game_over.write("You scored %d points!" % live_score, font=(None, 20, "bold"))
bullet.hideturtle()
#image adding
screen.addshape(image_coconut)
screen.addshape(image_banana)
screen.addshape(image_pineapple)
screen.addshape(image_cannon)
def cannon_left():
global cannon_x
if cannon_x == -140:
cannon_x = 140
cannon.goto(cannon_x, -100)
cannon_x = cannon_x - 10
cannon.goto(cannon_x, -100)
bullet.goto(cannon_x, -30)
def cannon_right():
global cannon_x
if cannon_x == 140:
cannon_x = -140
cannon.goto(cannon_x, -100)
cannon_x = cannon_x + 10
cannon.goto(cannon_x, -100)
bullet.goto(cannon_x, -30)
#setup
screen.bgcolor('white')
game_over.hideturtle()
game_over.speed(0)
game_over.penup()
game_over.goto(-100, 0)
win.hideturtle()
win.speed(0)
win.penup()
win.goto(-100, 0)
bullet.hideturtle()
bullet.speed(5)
bullet.penup()
bullet.shape('circle')
bullet.goto(0, -140)
bullet.left(90)
enemy.speed(0)
enemy.penup()
enemy.hideturtle()
enemy.goto(0, 140)
screen.addshape(image_coconut)
enemy.shape(image_coconut)
enemy.showturtle()
cannon.speed(0)
cannon.penup()
cannon.hideturtle()
cannon.goto(0, -100)
screen.addshape(image_cannon)
cannon.shape(image_cannon)
cannon.showturtle()
cannon.left(90)
score.speed(0)
score.penup()
score.hideturtle()
score.goto(-45, -190)
score.color('black')
score.write("Your score: %s" % live_score, font=(None, 11, "bold"))
#bindings
wn.onkey(cannon_right, "D")
wn.onkey(cannon_left, "A")
wn.onkey(cannon_right, "Right")
wn.onkey(cannon_left, "Left")
wn.onkey(cannon_shooting, "SPACE")
#movement
enemy.forward(move_speed)
if enemy.xcor() == 140:
enemy.left(180)
enemy.forward(move_speed)
if enemy.xcor() == -140:
enemy.right(180)
enemy.forward(move_speed)
if enemy1 == 1:
screen.addshape(image_banana)
enemy.shape(image_banana)
if enemy1 == 2:
screen.addshape(image_pineapple)
enemy.shape(image_pineapple)
if enemy1 == 3:
enemy.shape(image_coconut)
#others
wn.listen()
wn.mainloop()
注意:我正在welkit.io
中创建游戏。要在韦小宝中查看该程序,请单击。问题是如果bullet.xcor()=bullet.xcor()-10:
,您会说=
用于赋值,但是如果块中有,则不能在中进行赋值。如果bullet.xcor()==bullet.xcor()-10:
寻求调试帮助的问题(“为什么此代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现它所需的最短代码,则应将其更改为。没有明确问题陈述的问题对其他读者没有用处。请参阅:如何创建一个最小、完整且可验证的示例。我很确定这段代码远不是一个简单的例子,这对我没有帮助。是的,它修复了错误,但坐标仍然混乱。