如何在 pygame 中生成具有指定条件的实体?

How to spawn entities with specified conditions in pygame?

借助 pygame 模块的一些基本 python 代码。每当我的前一个敌人在 Y 轴上达到 300 像素时,我希望能够产生另一个敌人。我试图在线搜索解决方案,但没有找到任何有用的信息。我是一个完全的初学者,所以如果对此有一个非常明显的答案,请原谅我..

import pygame
import random

pygame.init()


screen_width = 800
screen_height = int(screen_width * 0.8)

def canvas():

    canvas = pygame.display.set_mode((screen_width, screen_height))


    pygame.display.set_caption("Space Invaders")


    icon = pygame.image.load("/Users/yashas/Desktop/python_projects/SpaceInvaders/Images/icon.png")
    pygame.display.set_icon(icon)

# player and enemy coordinates while spawning
playerX = (screen_width / 2) - 35
playerY = screen_height - 100
enemyX = random.randint(0, 736)
enemyY = screen_height - (screen_height + 70)
playerSpeedX = 0
enemySpeed = 0.4

def player():
    playerImg = pygame.image.load("/Users/yashas/Desktop/python_projects/SpaceInvaders/Images/player.png")
    canvas.blit(playerImg, (playerX, playerY))

def enemy():
    enemyImg = pygame.image.load("/Users/yashas/Desktop/python_projects/SpaceInvaders/Images/enemy.png")

    # used rotozoom because the img was facing the wrong direction
    enemyImg = pygame.transform.rotozoom(enemyImg, 180, 1)
    canvas.blit(enemyImg, (enemyX, enemyY))

# supposed to run first
if __name__ == "__main__":
    while True:

        canvas.fill((0, 0, 40))


        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()


            if event.type == pygame.KEYDOWN and event.key == pygame.K_LEFT:
                playerSpeedX = -0.7

            if event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT:
                playerSpeedX = 0.7



        if playerX <= 0:
            playerX = 0

        elif playerX >= 736:
            playerX = 736



        playerX += playerSpeedX
        enemyY += enemySpeed
        
        player()
        enemy()

   
        pygame.display.update()

不要在 playerenemy 中连续加载图像。加载图像是一项非常耗时的操作。在初始化时加载一次图像。

playerImg = pygame.image.load("/Users/yashas/Desktop/python_projects/SpaceInvaders/Images/player.png")
enemyImg = pygame.image.load("/Users/yashas/Desktop/python_projects/SpaceInvaders/Images/enemy.png")
# used rotozoom because the img was facing the wrong direction
enemyImg = pygame.transform.rotozoom(enemyImg, 180, 1)

def player():
    canvas.blit(playerImg, (playerX, playerY))

def enemy():
    canvas.blit(enemyImg, (enemyX, enemyY))

您需要一份敌人列表。在一个列表中你可以管理多个敌人。

enemy_list = []
enemy_list.append([random.randint(0, 736), -70])

循环移动并绘制敌人。当列表中最后一个敌人的 y 坐标大于 300 时创建一个新敌人。

while True:
    # [...]

    playerX += playerSpeedX
    
    for enemy_pos in enemy_list[:]:
        enemy_pos[1] += enemySpeed
        if enemy_pos[1] > screen_height:
            enemy_list.remove(enemy_pos)

    if len(enemy_list) == 0 or enemy_list[-1][1] > 300:
        enemy_list.append([random.randint(0, 736), -70])
        
    player()
    for enemy_pos in enemy_list: 
        canvas.blit(enemyImg, enemy_pos)

    pygame.display.update()

最小示例:

import pygame
import random

pygame.init()
screen_width = 800
screen_height = int(screen_width * 0.8)
canvas = pygame.display.set_mode((screen_width, screen_height))
clock = pygame.time.Clock()
pygame.display.set_caption("Space Invaders")
icon = pygame.image.load("/Users/yashas/Desktop/python_projects/SpaceInvaders/Images/icon.png")
pygame.display.set_icon(icon)

# player and enemy coordinates while spawning
playerX = (screen_width / 2) - 35
playerY = screen_height - 100
enemyX = random.randint(0, 736)
enemyY = screen_height - (screen_height + 70)
playerSpeedX = 3
enemySpeed = 3

playerImg = pygame.image.load("/Users/yashas/Desktop/python_projects/SpaceInvaders/Images/player.png")
enemyImg = pygame.image.load("/Users/yashas/Desktop/python_projects/SpaceInvaders/Images/enemy.png")
enemyImg = pygame.transform.rotozoom(enemyImg, 180, 1)

def player():
    canvas.blit(playerImg, (playerX, playerY))
    
enemy_list = []

# supposed to run first
if __name__ == "__main__":
    
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
            if event.type == pygame.KEYDOWN and event.key == pygame.K_LEFT:
                playerSpeedX = -3
            if event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT:
                playerSpeedX = 3

        playerX = max(0, min(736, playerSpeedX + playerX))
    
        for enemy_pos in enemy_list[:]:
            enemy_pos[1] += enemySpeed
            if enemy_pos[1] > screen_height:
                enemy_list.remove(enemy_pos)

        if len(enemy_list) == 0 or enemy_list[-1][1] > 300:
            enemy_list.append([random.randint(0, 736), -70])

        canvas.fill((0, 0, 40))     
        player()
        for enemy_pos in enemy_list:
            canvas.blit(enemyImg, enemy_pos)
        pygame.display.update()

        clock.tick(100)