本文介绍了在 pygame 中管理组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个游戏,当你杀死一个生物时,会出现另外两个.我有它,所以当你杀死一个暴徒时,另外两个会出现,但只有一个保持可见并且表现得像它应该的那样.另一个只是出现然后消失.我怎样才能得到它,让他们两个都按照他们应该的方式行事.这是我目前所拥有的:

I am creating a game where when you kill one mob, two more appears. I have it so when you kill one mob, the two others appear, but only one stays visible and behaves like its supposed to. The other just appears then disappears. How can I get it so both of them behave the way they are supposed to. Here is what I have so far:

[MOB 类]

class MOB(pygame.sprite.Sprite):
    def __init__(self, location):
        self.pos = [0,0]
        self.image = ENEMY
        pygame.sprite.Sprite.__init__(self)
        self.rect = self.image.get_rect()
        self.rect.left, self.rect.top = location
        self.rect.right, self.rect.bottom = location
        self.rect.center = location
        self.speed = random
        self.hp = 4
    def update(self):
         if self.hp == 0:
            mobs.add(self)
            self.image = ENEMY
            self.pos = [0,0]
            self.hp = 4
    def moveH(self):
        if self.rect.centerx >= Player.rect.centerx:
            self.rect.left = self.rect.left - 4
        elif self.rect.centerx <= Player.rect.centerx:
            self.rect.left = self.rect.left + 4

        def moveV(self):
        if self.rect.centery <= Player.rect.centery:
            self.rect.top = self.rect.top + 4
        if self.rect.centery >= Player.rect.centery:
            self.rect.top = self.rect.top - 4

[主循环中的添加和删除]

[Adding and Removing in main loop]

for Mob in mobs:
    if Mob.hp == 0:
        score = score + 1
        Mob.kill()
        new_mobs = MOB([50, 50]), MOB([60, 300])
        mobs.add(*new_mobs)

[在主循环中重绘和移动]

[Redrawing and moving in main loop]

for Mob in mobs:
    Mob.moveV()
    Mob.moveH()
    screen.blit(Mob.image, Mob.rect)

推荐答案

  1. 您的代码正在擦除 Rect 的宽度和高度.如果要使用中心坐标,请使用:

  1. Your code is erasing the Rect's width and height. If you want to use coordinates of the center, use:

self.rect = self.image.get_rect()
self.rect.center = location

  • update() 中的 mobs.add() 是什么?因为更新通常每帧调用一次,我猜 mobs 是一个精灵组.

  • What is mobs.add() in update()? Because update is normally called every frame, and I'm guessing mobs is a sprite group.

    您的更新:

    对于暴徒中的暴民:Mob.moveV()移动.moveH()screen.blit(Mob.image, Mob.rect)

    for Mob in mobs: Mob.moveV() Mob.moveH() screen.blit(Mob.image, Mob.rect)

    应该是

    mobs.update()
    mobs.draw(screen)
    

    看看这个家伙的例子https://stackoverflow.com/a/10418347/341744

    这篇关于在 pygame 中管理组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

  • 10-28 17:13