提问人:Christian Bernard 提问时间:11/17/2023 最后编辑:Christian Bernard 更新时间:11/17/2023 访问量:32
蟒蛇:如何制作可以自动射出子弹的物体?
python turtle : how to make a turtle object that can shoot bullets automatically?
问:
如何制作一个可以自动射出子弹并受到控制的物体?请帮帮我。我已经尝试了此代码,但游戏正在崩溃(敌机自首次出现以来一直连续发射子弹,没有停顿) 这是我的代码:
bg.addshape("F 22 Raptor.gif")
enemy2 = turtle.Turtle()
enemy2.shape("F 22 Raptor.gif")
enemy2.setheading(-90)
enemy2.turtlesize(3)
enemy2.penup()
enemy2.goto(0,360)
PlayerBullets = [] # Menyimpan peluru
EnemyBullets = [] # Menyimpan peluru musuh
def EnemyBullet():
Ebullet = turtle.Turtle()
Ebullet.shape("triangle")
Ebullet.color("yellow")
Ebullet.shapesize(stretch_wid=0.3, stretch_len=0.1)
Ebullet.penup()
Ebullet.goto(enemy2.xcor(), enemy2.ycor()) # Posisi awal peluru sama dengan posisi Spaceshuttle
Ebullet.speed(0.01)
return Ebullet
def EFireBullet():
Ebullet = EnemyBullet()
EnemyBullets.append(Ebullet)
bulletsSound.play()
def MoveEnemyBullets():
global EnemyBullets
newbullets = []
for Ebullet in EnemyBullets:
y = Ebullet.ycor()
y += -2 # Mengubah posisi Y agar peluru turun
Ebullet.sety(y)
if y > 300:# Hapus peluru yang mencapai batas atas layar
Ebullet.hideturtle()
else:
newbullets.append(Ebullet)
EnemyBullets = newbullets
def CheckEnemyCollision():
global score, HighScore
for Ebullet in EnemyBullets:
if Ebullet.distance(SpaceShuttle) < 20:
Ebullet.hideturtle() # menyembunyikan peluru jika mencapai batas atas layar
EnemyBullets.remove(Ebullet)
HitMarker.play()
if score > HighScore:
HighScore = score
with open("highscore.txt", "w") as file:
file.write(str(HighScore))
答: 暂无答案
评论
.forward()
.sety()