将新项目添加到列表时出现的问题

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

我正在尝试为我的游戏制作一个随机的敌人生成器。为此,我已经在Stack Overflow上看到了这种方法,但它似乎对我不起作用,我不确定为什么。将代码附加到我的列表时,由于某种原因,代码无法正常通过。以下是我的相关代码,以进一步阐明我的意思。标签之间的代码是要突出显示的代码

moveFunctions:

def moveFunctions(bullets, E_bullets, score, font, window,
                  protagR, protagL, protagU, protagD, enemyR, enemyL, enemyU, enemyD, bg):
    clock = pygame.time.Clock()

    bulletSFX = pygame.mixer.Sound("ASG2_Music/shoot_Bullet_SFX.wav")
    getHitSFX = pygame.mixer.Sound("ASG2_Music/Hit_SFX.wav")
    enemy_getHitSFX = pygame.mixer.Sound("ASG2_Music/Monster_Hit_SFX.wav")
    music = pygame.mixer.music.load("ASG2_Music/stand_Proud.mp3")
    pygame.mixer.music.play(-1)

    bulletDelay = 0
    game_Over = True
    run = True
    ##########################################################################################
    DiArrow = enemy(random.randint(1, 500), random.randint(1, 500), 48, 48, 410)
    board = player(225, 225, 64, 64, 0)

    while run:
        enemies = []
        maxEnemies = 5
        for i in range(maxEnemies):
            enemies.append(enemy(random.randint(1, 500), random.randint(1, 500), 48,48, 410))
            print(enemies)
    ##########################################################################################
        if game_Over:
            # show_game_Over_Screen()
            game_Over = False
        clock.tick(15) # Frame rate per second (FPS)

        if bulletDelay > 0:
            bulletDelay += 1
        if bulletDelay > 3:
                bulletDelay = 0

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
                stopGame()

        fireBullet(board, bullets, DiArrow, score, enemy_getHitSFX)
        E_fireBullet(board, E_bullets, DiArrow, getHitSFX)

        arrowKeys = pygame.key.get_pressed()

        score = fireBullet(board, bullets, DiArrow, score, enemy_getHitSFX)

        bulletDirection(arrowKeys, board, bullets, bulletDelay, bulletSFX)
        bulletDelay = bulletDirection(arrowKeys, board, bullets, bulletDelay, bulletSFX)
        E_bulletDirection(DiArrow, E_bullets, board)
        playerControls(arrowKeys, board)
        enemyControls(DiArrow, board)
        redrawGameWindow(board, DiArrow, bullets, E_bullets, font, score, window, protagR, protagL, protagU, protagD,
                         enemyR, enemyL, enemyU, enemyD, enemies, bg)

redrawGameWindow:

def redrawGameWindow(board, DiArrow, bullets, E_bullets, font, score, window, protagR, protagL, protagU, protagD, enemyR,
                     enemyL, enemyU, enemyD, enemies, bg):
    window.blit(bg, (0,0))
    text = font.render("Score: " + str(score), 1, (255, 255, 255))
    window.blit(text, (340, 513))
    board.drawCharacter(window,  protagR, protagL, protagU, protagD)
    ##########################################################################################
    for i in enemies:
        DiArrow.drawEnemy(window, enemyR, enemyL, enemyU, enemyD)
    ##########################################################################################
    for bullet in bullets:
        bullet.draw(window)
    for bullet in E_bullets:
        bullet.draw(window)
    pygame.display.update()

类敌人():

class enemy():
    def __init__(self, x, y, width, height, walkCount):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.vel = 3
        self.lastDirection = "right" or "left" or "up" or "down"
        self.walkCount = walkCount
        self.hitbox = (self.x, self.y, 48, 50)
        self.health = 4
        self.visible = True

    def drawEnemy(self, window, enemyR, enemyL, enemyU, enemyD):
        dest = (self.x, self.y)

        if self.visible:
            if self.walkCount + 1 >= 15:
                self.walkCount = 0

            if self.lastDirection == "left":
                window.blit(enemyL[self.walkCount//3], dest)
                self.walkCount += 1
            elif self.lastDirection == "right":
                window.blit(enemyR[self.walkCount//3], dest)
                self.walkCount += 1
            elif self.lastDirection == "up":
                window.blit(enemyU[self.walkCount//3], dest)
                self.walkCount += 1
            elif self.lastDirection == "down":
                window.blit(enemyD[self.walkCount//3], dest)
                self.walkCount += 1
            self.hitbox = (self.x, self.y, 48, 50)

            pygame.draw.rect(window, (255, 0, 0), (self.hitbox[0], self.hitbox[1] - 15, 50, 10))
            pygame.draw.rect(window, (177, 0, 175), (self.hitbox[0], self.hitbox[1] - 15, 50 - ((50/5)*(4 - self.health)), 10))

    def getHit(self):
        if self.health > 0:
            print("Enemy Got Hit!")
            self.health -= 1
        else:
            print("Enemy Killed!")
            self.visible = False

    def gameOver(self):
        if self.visible == False:
            pass

[我发现,当尝试检查错误时,在moveFunctions中打印敌人实际上不能正确打印列表,而是给出了此信息。

[<__main__.enemy object at 0x00000251C53A3828>]
[<__main__.enemy object at 0x00000251C53A3828>, <__main__.enemy object at 0x00000251C53A3470>]
[<__main__.enemy object at 0x00000251C53A3828>, <__main__.enemy object at 0x00000251C53A3470>, <__main__.enemy object at 0x00000251C53A3EB8>]
[<__main__.enemy object at 0x00000251C53A3828>, <__main__.enemy object at 0x00000251C53A3470>, <__main__.enemy object at 0x00000251C53A3EB8>, <__main__.enemy object at 0x00000251C53A39E8>]
[<__main__.enemy object at 0x00000251C53A3828>, <__main__.enemy object at 0x00000251C53A3470>, <__main__.enemy object at 0x00000251C53A3EB8>, <__main__.enemy object at 0x00000251C53A39E8>, <__main__.enemy object at 0x00000251C53A3048>]

您可以看到,列表正在更新,但是我确实理解了这个奇怪的字符串。

为全面披露,我在Python中使用pygame模块,但我怀疑这是在这里给我带来这个问题的原因。

我在这里假设我的不产生maxEnemies的问题与此直接相关。任何帮助将不胜感激,谢谢

python list class pygame append
1个回答
0
投票

您实际上在每个帧中创建了一个新的敌人列表(enemies = []),并将5个新的enemy实例添加到列表中。此外,列表将打印在循环中。

在主应用程序循环之前创建敌人列表,并打印每个敌人的属性而不是敌人列表:

def moveFunctions(.....):
    # [...]

    enemies = []
    maxEnemies = 5
    for i in range(maxEnemies):
        newEnemy = enemy(random.randint(1, 500), random.randint(1, 500), 48,48, 410)
        enemies.append(newEnemy)
        print(newEnemy.x, newEnemy.y)

    while run:
        # [...]

注意,Class Names通常应使用CapWords约定。将类命名为Enemy而不是enemy

class enemy():

class Enemy():
© www.soinside.com 2019 - 2024. All rights reserved.