我在使用Turtle创建游戏时遇到了麻烦。每当我尝试运行程序时,我都会收到一个回溯错误:ParseError: bad input on line 69 in main.py
。但是早些时候(当错误没有发生时)我的坐标没有正常工作。我想我键入了正确的,但它不起作用。在游戏中你需要打水果。因此,游戏主要是这样的:如果你错过了果实,它应该从你的分数和你生命中的一颗心中拿出一分。首先,它会检查你是否得分为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()
注意:我在Trinket.io
创建游戏。要在Trinket
中查看该计划,请点击here。
答案 0 :(得分:0)
问题是你说if bullet.xcor() = enemy.xcor() - 10:
。 =
用于分配,但您无法在if
块中进行分配。您应该将其更改为if bullet.xcor() == enemy.xcor() - 10: