我在我在龜中創建的遊戲遇到問題。每當我嘗試運行該程序時,都會收到回溯錯誤:ParseError: bad input on line 69 in main.py
。但更早的時候(當錯誤沒有發生時)我的座標不能正常工作。我想我輸入了正確的,但它不起作用。在遊戲中你需要擊中果實。所以遊戲主要是這樣的:如果你錯過了水果,它應該從你的分數中取一分,在你的生命中取一分。起初它會檢查你是否沒有0分。如果你打到水果,你應該得到+1分。但令人遺憾的是,如果我在窗口左側的任何地方拍攝,我都會獲得+1分。不管距離如何。ParseError和不正確的座標
中,我懷疑這個問題的代碼是:
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。
_尋求調試幫助的問題(「爲什麼這個代碼不工作?」)必須包含所需的行爲,特定問題或錯誤以及在問題本身中重現問題所需的**最短代碼**。沒有明確問題陳述的問題對其他讀者無益。請參閱:如何創建一個最小,完整和可驗證的示例._我非常確定這個代碼塊如果遠遠不是最簡單的示例。 –