ทำไมศัตรูถึงไม่เคลื่อนไหว?

ฉันมีปัญหาทุกอย่างทำงานได้ ยกเว้นเมื่อศัตรูควรจะเคลื่อนที่ไปทางขวา มันบอกว่ามีข้อผิดพลาดในบรรทัด ซึ่งเป็นบรรทัดสุดท้าย มันเริ่มต้นเมื่อฉันสร้างกระสุน ก่อนหน้านั้นทุกอย่างทำงานได้ดี ฉันไม่รู้ว่าทำไมมันถึงทำแบบนั้น แต่ฉันคิดไม่ออก มันเป็นเรื่องแปลก. โปรดช่วยด้วย ขอบคุณมาก และโปรดอย่าตัดสินความสามารถในการอ่านของฉันเพราะฉันเพิ่งเริ่มเขียนโค้ด นี่คือข้อความไฟล์ "E:\Code\Space Invaders.py" บรรทัด 106 ใน Enemy.setx(x)

 import turtle
 import os

 wn = turtle.Screen()
 wn.bgcolor("black")
 wn.title('Space Invasders')

 border_pen = turtle.Turtle()
 border_pen.speed(0)
 border_pen.color('white')
 border_pen.penup()
 border_pen.setposition(-300,-300)
 border_pen.pendown()
 border_pen.pensize(3)
 for side in range(4):
    border_pen.fd(600)
    border_pen.lt(90)
 border_pen.hideturtle()

 player = turtle.Turtle()
 player.color('blue')
 player.shape('triangle')
 player.penup()
 player.speed(0)
 player.setposition(0, -250)
 player.setheading(90)
 playerspeed = 15

 enemy = turtle.Turtle()
 enemy.color('red')
 enemy.shape('circle')
 enemy.penup()
 enemy.speed(0)
 enemy.setposition(-200, 250)

 enemyspeed = 2

 bullet = turtle.Turtle()
 bullet.color('yellow')
 bullet.shape('triangle')
 bullet.penup()
 bullet.speed(0)
 bullet.setheading(90)
 bullet.shapesize(0.5, 0.5)
 bullet.setposition(player.xcor(), player.ycor() + 10)
 bullet.ht()
 bulletspeed = 20

 player_alive = True

 if enemy.xcor() == player.xcor() and enemy.ycor() == player.ycor():
    player_alive = False


 def fire():

        x = player.xcor()
        y = player.ycor() + 10
        bullet.st()
        bullet.setposition(x, y)

 def right():
    x = player.xcor()
    x += playerspeed
    if x > 280:
        x = 280
    player.setx(x)


 def left():
    x = player.xcor()
    x -= playerspeed
    if x < -280:
        x = - 280
    player.setx(x)


 wn.onkey(left, "Left")
 wn.onkey(right, "Right")
 wn.onkey(fire, "space")
 wn.listen()
 wn.mainloop()

 while player_alive == True:
    x = enemy.xcor()
    x += enemyspeed
    enemy.setx(x) #this line is messed up

person Jonah.Jackson    schedule 20.01.2020    source แหล่งที่มา
comment
จะเกิดอะไรขึ้นเมื่อคุณเปลี่ยน x = - 280 เป็น x = -280 ไม่แน่ใจว่าทำไมคุณถึงมีช่องว่างตรงนั้น   -  person KILLtheWEEZEL    schedule 20.01.2020
comment
แสดงความคิดเห็นในบรรทัดที่ร้องเรียนและให้ข้อความแสดงข้อผิดพลาดที่แน่นอนแก่เรา   -  person hkBst    schedule 20.01.2020
comment
ฉันไม่สามารถจำลองพฤติกรรมที่อธิบายไว้ได้ คุณสามารถโพสต์ข้อความแสดงข้อผิดพลาดแบบเต็มได้หรือไม่   -  person YFrog    schedule 20.01.2020
comment
@KILLtheWEEZEL แม้ว่า x = - 5 ไม่ควรจะมีช่องว่างระหว่าง - และ 5 ตามรูปแบบการเข้ารหัสมาตรฐาน ฉันเชื่อว่าในทางเทคนิคแล้วก็โอเคที่จะทำเช่นนั้น การไปที่ python REPL ออนไลน์แสดงให้เห็นว่ามันไม่ใช่ข้อผิดพลาดที่รับประกัน - อย่างน้อยคอมไพเลอร์บางคนก็ยอมรับมัน และฉันคาดหวังว่าส่วนใหญ่จะเป็นเช่นนั้น OP ไม่จำเป็นต้องรวมข้อความแสดงข้อผิดพลาดที่แน่นอน และควรเพิ่มประโยคเพิ่มเติมที่ระบุบรรทัดของโค้ดที่ถูกอ้างอิงทุกประการ โดยบรรทัดนั้นซ้ำกันเป็น บล็อกโค้ดบรรทัดเดียว   -  person SherylHohman    schedule 21.01.2020
comment
ฉันนับบรรทัดและพบว่าบรรทัด 75 เป็น player.setx(x) ในบล็อกโค้ด def left(): สิ่งนี้ถูกต้องหรือไม่?   -  person SherylHohman    schedule 21.01.2020
comment
BTW Space Invaders สะกดผิด ;)   -  person SherylHohman    schedule 21.01.2020
comment
ฉันพูดผิดประโยค เชอริลโฮห์มาน   -  person Jonah.Jackson    schedule 21.01.2020


คำตอบ (1)


นี่คือปัญหา:

wn.mainloop()

while player_alive == True:
    x = enemy.xcor()
    x += enemyspeed
    enemy.setx(x) #this line is messed up

เมธอด mainloop() ควรเป็นการเรียกครั้งสุดท้ายในโปรแกรมของคุณ โดยเรียกให้เปลี่ยนการจัดการเหตุการณ์เป็นลูปเหตุการณ์ของ tkinter โค้ดต่อไปนี้จะไม่ทำงานจนกว่าโลกเต่าจะปิดตัวลง

เพื่อการแก้ไขโดยทันที ฉันขอแนะนำให้คุณย้ายการโทร mainloop() ของคุณไปจนกระทั่งหลัง while วนซ้ำ:

while player_alive:
    x = enemy.xcor()
    x += enemyspeed
    enemy.setx(x)

wn.mainloop()

เพื่อการแก้ไขในระยะยาว ฉันขอแนะนำไม่ให้คุณใช้การวนซ้ำ while แต่ควรใช้เหตุการณ์ ontimer()

person cdlane    schedule 21.01.2020