太空入侵者游戏bug

时间:2013-11-30 23:13:57

标签: python pygame

之前我已经发布过一部分太空入侵者游戏,现在我已经进一步了解它。我是编程的新手,所以这就是为什么代码可能看起来很草率,我知道它的效率非常低,但我认为我现在做得还不够好。

我有这个奇怪的问题。它发生在三个周期之前,敌人向下移动我无法击中任何一个。然后他们向下移动,我只能击中第二排,然后他们再次向下移动,我只能击中第一排并且不断重复。这是我的所有代码:

#-----!!!!SPACE INVADERS!!!!-----
import pygame, sys
from pygame.locals import *
#-----MAIN FUNCTIONS-----
def movement(move_x):
    if event.type == KEYDOWN:
        if event.key == K_LEFT and x > 4:
            move_x = -5
        elif event.key == K_LEFT and x < 4:
            move_x = 0
        if event.key == K_RIGHT and x < (w-4):
            move_x = 5
        elif event.key == K_RIGHT and x > (w-4):
            move_x = 0
    if event.type == KEYUP:
        if event.key == K_LEFT:
            move_x = 0
        if event.key == K_RIGHT:
            move_x = 0
    return (move_x)

def SHOTS(shots,max_shot,x,shot_y,list_of_shots):
    if event.type == KEYDOWN:
        if event.key == K_SPACE:
                if len(shots) < max_shot:
                    list_of_shots = [x-shot.get_width()/2,shot_y]
                    shots.append(list_of_shots)
    return (shots,list_of_shots)

def bullets(list_of_shots,shots):
    for list_of_shots in shots:
        if list_of_shots[1] <= 0:
            del shots[0]
    if len(shots) == 1:
        screen.blit(shot, shots[0])
    if len(shots) == 2:
        screen.blit(shot, shots[1])
        screen.blit(shot, shots[0])
    for i in shots:
        i[1] -= 15

def move_enemy(enemies,enemy_move,test_y,w,enemy_len):
    for i in enemies:
        enemy_len += 1
    for i in enemies[enemy_len-1]:
        if i == w-50:
            enemy_move = -.5
            test_y += .25
        elif i == (w-300):
            enemy_move = .5
            test_y += .25
    return (test_y,enemy_move)

def new_enemy(enemies,enemy_move,test_y,rep_test_y):
    for i in enemies:
        i[0] += enemy_move
        if test_y > rep_test_y:
            i[1] += 50
    return enemies

#-----FFRAME RAEE / SCREEN SIZE-----
clock = pygame.time.Clock()
w,h = 1200,800
screen = pygame.display.set_mode((w,h))

#-----SETTING IMAGES-----
pygame.mouse.set_visible(0)

ship = pygame.image.load("spaceship.png")
ship = pygame.transform.scale(ship,(100,50))
ship_top = screen.get_height() - ship.get_height()
ship_left = screen.get_width()/2 - ship.get_width()/2

screen.blit(ship, (ship_left,ship_top))

shot = pygame.image.load("SingleBullet.png")
shot = pygame.transform.scale(shot,(25,25))
shot_y = h-75

enemy = pygame.image.load("enemy.png")
enemy = pygame.transform.scale(enemy, (50,50))
#-----GLOBAL VARIABLES-----
x = 0
delete_1 = 0
max_shot = 2
shots = []
list_of_shots = []
move_x = 0
enemy_y = 50
enemy_x = 0
enemies = []
enemy_move = 0
test_y = 0
rep_test_y = 0
number = 0
enemy_len = 0

#-----MAIN GAME LOOP-----
for i in range(20):
    enemies.append([enemy_x, enemy_y])
    if enemy_x >= w-300:
        enemy_x = -100
        enemy_y += 100
    enemy_x += 100

enemies.append([900,-h,])

while True:
    #----game settings----
    clock.tick(60)
    screen.fill((0,0,0))
    #----setting images
    screen.blit(ship, (x-ship.get_width()/2,ship_top))                      #put ship in new postition
    for i in range(len(enemies)):
        screen.blit(enemy,enemies[i])                                       #put enemies in new position

    for event in pygame.event.get():                                        #system exit
        if event.type == pygame.QUIT:
            sys.exit()
        shots,list_of_shots = SHOTS(shots,max_shot,x,shot_y,list_of_shots)  #checks if shots have been fired and appends them to list of shots
    #----enemies-----
    rep_test_y = test_y
    test_y,enemy_move = move_enemy(enemies,enemy_move,test_y,w,enemy_len)             #makes the enemies move
    enemies = new_enemy(enemies,enemy_move,test_y,rep_test_y)
    #----movement / bullets----
    move_x = movement(move_x)                                               #move_x defines how much the ship moves and movement checks for and pres of arrow keys and move 5 pixles per check
    bullets(list_of_shots,shots)                                            #blits the bullets to the screen and checks if they are on the screen if they arnt then it removes from the list
    x+=move_x
    #----delets bullet----
    for i in enemies:
        number += 1
        for list_of_shots in shots:
            if list_of_shots[1] == (i[1] + 50):
                if list_of_shots[0] >= i[0] and list_of_shots[0] <= (i[0] +50):
                    del shots[0]
                    del enemies[number-1]
    number = 0

    #----update----
    pygame.display.update()                                                 #Updates screen

1 个答案:

答案 0 :(得分:2)

问题在于

if list_of_shots[1] == (i[1] + 50):

有时只对第一排的敌人有效,有时对第二排的敌人有效,有时对所有敌人都是假的。

使用

if abs( list_of_shots[1] - i[1] ) <= 50:

顺便说一句:

取代

for i in enemies:
    enemy_len += 1
for i in enemies[enemy_len-1]:

你可以使用

for i in enemies[-1]:

您可以使用pygame.Rect()作为精灵位置 - 而不是使用i.x代替i[0]。或事件i.centerxi.bottomi.top等(pygame.Rect()


我认为你得到了太多的downvote,因为你的代码非常混乱。将classpygame.Rect()一起使用,然后使用pygame碰撞检测等。

使用Ican'tdon'tI'mI've(等)代替i,{{1 }},cantdontIm