敌人的精灵直到被击中才出现在pygame中

问题描述 投票:0回答:1

我正在像游戏一样入侵太空,一切正常,但是自从我使用列表添加了多个敌人以来,他们的图像/精灵消失了,只在被击中一秒钟后才再次出现,然后再消失一次。我检查了图像文件是否正常,据我所知,此后也未发生任何异常情况,每当有敌人被击中时,分数总和就很好。可能是什么问题?

import pygame
import random
import math

# Initializing pygame
pygame.init()

# Screen creation and config
screen = pygame.display.set_mode((1024, 600))

# Background
background = pygame.image.load('court_level_bg.jpg')
# Title and Icon
pygame.display.set_caption("Space Invaders")

# Player config
player_img = pygame.image.load('Mini_Bibi.png')
playerX = 430
playerY = 480
playerX_change = 0

# Enemy config
enemy_img = []
enemyX = []
enemyY = []
enemyX_change = []
enemyY_change = []
num_enemies = 5

for i in range(num_enemies):
    enemy_img.append(pygame.image.load('Enemy1.png'))
    enemyX.append(random.randint(0, 1024))
    enemyY.append(random.randint(5, 150))
    enemyX_change.append(1.5)
    enemyY_change.append(40)

# Bullet config
bullet_img = pygame.image.load('Weapon.png')
bulletX = 0
bulletY = 480
bulletX_change = 0
bulletY_change = 6
bullet_state = "ready"

score = 0


# Create player
def player(x, y):
    screen.blit(player_img, (x, y))


# Create enemy
def enemy(x, y, i):
    screen.blit(enemy_img[i], (x, y))


def fire_bullet(x, y):
    global bullet_state
    bullet_state = "fire"
    screen.blit(bullet_img, (x + 16, y + 10))


def isCollision(enemyX, enemyY, bulletX, bulletY):
    distance = math.sqrt((math.pow(enemyX - bulletX, 2)) + (math.pow(enemyY - bulletY, 2)))
    if distance < 27:
        return True
    else:
        return False


# Game Loop
scrn_stat = True
while scrn_stat:

    # Screen RGB color config
    screen.fill((0, 0, 0))
    # Background image
    screen.blit(background, (0, 0))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            scrn_stat = False

        # X axis Keystroke listening
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                playerX_change = -4
            if event.key == pygame.K_RIGHT:
                playerX_change = 4
            if event.key == pygame.K_SPACE:
                if bullet_state is "ready":
                    bulletX = playerX
                    fire_bullet(bulletX, bulletY)
            if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                playerX_change = 0

    playerX += playerX_change

    # Player boundaries
    if playerX <= 0:
        playerX_change = 0;
    elif playerX >= 945:
        playerX_change = 0;

    # Enemy movement
    for i in range(num_enemies):
        enemyX[i] += enemyX_change[i]
        if enemyX[i] <= 0:
            enemyX_change[i] = 3
            enemyY[i] += enemyY_change[i]
        elif enemyX[i] >= 945:
            enemyX_change[i] = -3
            enemyY[i] += enemyY_change[i]

        # Collision
        collision = isCollision(enemyX[i], enemyY[i], bulletX, bulletY)
        if collision:
            bulletY = 480
            bullet_state = "ready"
            score += 1
            print(score)
            enemyX[i] = random.randint(0, 945)
            enemyY[i] = random.randint(50, 150)
            enemy(enemyX[i], enemyY[i], i)

    # Bullet Movement
    if bulletY <= 0:
        bulletY = 480
        bullet_state = "ready"

    if bullet_state is "fire":
        fire_bullet(bulletX, bulletY)
        bulletY -= bulletY_change

    player(playerX, playerY)
    pygame.display.update()
python python-3.x pygame sprite
1个回答
1
投票

enemy()必须在每一帧中针对每个敌人而被调用,而不是仅在发生碰撞的情况下。这是一个Indentation问题:

© www.soinside.com 2019 - 2024. All rights reserved.