-1
enemy.x沒有工作,因爲在被選中的敵人遇難後從列表中消失。我嘗試了其他方法並設法讓他們停止,但他們不會回來。這是我的第一場比賽,所以我不熟悉這些可能的錯誤。請幫助:)我有一個敵人的名單,但我不知道如何讓他們在屏幕上。 (太空入侵者)
def level1():
shipX = 462
shipY = 668
bg = pygame.image.load('spacebackground.png')
ship1 = pygame.image.load('ship1.png')
lives = pygame.image.load('ship1.png')
lasers = []
enemy_x = 50
enemy_x2 = 50
enemy_x3 = 50
enemy1 = pygame.image.load('enemy1.png')
enemy1 = pygame.transform.scale(enemy1, (50, 50))
enemies = []
for i in range(10):
enemies.append(pygame.Rect(enemy_x, 100, 50, 50))
enemy_x += 65
for i in range(10):
enemies.append(pygame.Rect(enemy_x2, 150, 50, 50))
enemy_x2 += 65
for i in range(10):
enemies.append(pygame.Rect(enemy_x3, 200, 50, 50))
enemy_x3 += 65
#Event handling
while True:
clock.tick(60)
laserX = shipX + 23
laserY = 658
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if (pygame.key.get_pressed()[pygame.K_SPACE]) != 0:
lasers.append(pygame.Rect(laserX, laserY, 5, 20))
#setting background
bg = pygame.transform.scale(bg, (1024, 768))
shipRect = pygame.Rect(shipX, shipY, 50, 50)
ship1 = pygame.transform.scale(ship1, (50, 50))
lives = pygame.transform.scale(lives, (25, 25))
if (pygame.key.get_pressed()[pygame.K_RIGHT]) != 0:
shipX += 20
if (pygame.key.get_pressed()[pygame.K_LEFT]) != 0:
shipX += -20
for enemy in enemies:
for laser in lasers:
if laser.colliderect(enemy):
enemies.remove(enemy)
lasers.remove(laser)
break
for laser in lasers:
laser.move_ip(0, -10)
for laser in lasers:
if laser.y <= -20:
lasers.remove(laser)
if shipX <= 0:
shipX = 0
if shipX >= 974:
shipX = 974
screen.blit(bg, (0,0))
for laser in lasers:
pygame.draw.rect(screen, (red), laser)
for enemy in enemies:
screen.blit(enemy1, enemy.topleft)
enemy.x += 1
screen.blit(ship1, (shipRect))
screen.blit(lives, (5,733))`enter code here`
screen.blit(lives, (33,733))
#update Display
pygame.display.update()
什麼_exactly_是問題所在?你只是想在列表中保留死敵?你希望敵人不會消失(留下沉船或其他東西),或者稍後消失(在爆炸動畫之後)? 「嘗試其他方式」是什麼意思 - 還有什麼其他方式? 「設法讓他們停下來」是什麼意思---停止什麼?你想做什麼,發生了什麼,爲什麼這是錯的? –