2

再一次,我在做一个学校项目,我很难做一些我觉得应该很容易的事情..

我有代码工作;它没有崩溃,但也没有 blitting。我创建了一个使用 Sprites 作为“僵尸目标”的第一人称射击风格游戏,我在 Zombie Class 中进行了更新以移动图像,但我知道我有多个问题。
与往常一样,我只是在寻求一些方向。

非常感谢!

import pygame, random, glob

pygame.init()
screen = pygame.display.set_mode((640, 480))

class Circle(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((50, 50))
        self.image.fill((255, 255, 255))
        self.image.set_colorkey(255)
        pygame.draw.circle(self.image, (255, 0, 0), (25, 25), 25, 1)
        self.image.set_alpha(75)
        pygame.draw.circle(self.image, (255,0, 0), (25,25), 3, 0)
        self.rect = self.image.get_rect()
            
    def update(self):
        self.rect.center = pygame.mouse.get_pos()
    
class Zombie(pygame.sprite.Sprite):

    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((50,50))
        self.image.fill((255,255,255))
        self.rect = self.image.get_rect()
        pos = pygame.mouse.get_pos()
        self.x = random.randrange(0,screen.get_width())
        self.y = random.randrange(0, screen.get_height())
        self.ani_speed_init = 8
        self.ani_speed = self.ani_speed_init
        self.ani = glob.glob("zombie_walk_r*.png")
        self.ani.sort()
        self.ani_pos = 0
        self.ani_max = len(self.ani) - 1
        self.img = pygame.image.load(self.ani[1])
    
    def update(self, pos):
        screen.blit(self.img, (self.x, self.y))               
    
    def shoot(killShotSpot):
        killshotbullet = pygame.mixer.Sound("bullet.wav")  
        killshotbullet.play()
        print "record point" + str(killShotSpot)                
    
def main():
    pygame.display.set_caption("Zombie Be GONE")

    background = pygame.Surface(screen.get_size())
    background.fill((255, 255, 255))
    screen.blit(background, (0, 0))
    circle = Circle()
    zombie = Zombie()
    allSprites = pygame.sprite.Group(circle, zombie)
    
    #hide mouse
    pygame.mouse.set_visible(False)
    clock = pygame.time.Clock()
    keepGoing = True

    while keepGoing:
        clock.tick(30)
        for event in pygame.event.get():
        if event.type == pygame.QUIT:
            keepGoing = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            killShotSpot = pygame.mouse.get_pos()
            shoot(killShotSpot)
            pygame.draw.circle(background,(255,0,0),killShotSpot,3,0)
                
    allSprites.clear(screen, background)
    allSprites.update()
    allSprites.draw(screen)
    pygame.display.flip()

    pygame.display.flip()
    
#return mouse
pygame.mouse.set_visible(True)

if __name__ == "__main__":
    main()
4

1 回答 1

0

pygame.sprite.Group.draw()

将包含的 Sprite 绘制到 Surface 参数。这将Sprite.image属性用于源表面和Sprite.rect位置。

您根本不需要类中的属性self.x, self.y, 。将位置存储在属性中,将图像存储在属性中:slef.imgZombie.rect.image

class Zombie(pygame.sprite.Sprite):

    def __init__(self):
        pygame.sprite.Sprite.__init__(self)

        self.ani_speed_init = 8
        self.ani_speed = self.ani_speed_init
        self.ani = glob.glob("zombie_walk_r*.png")
        self.ani.sort()
        self.ani_pos = 0
        self.ani_max = len(self.ani) - 1
        self.image = pygame.image.load(self.ani[1])
        
        x = random.randrange(0, screen.get_width())
        y = random.randrange(0, screen.get_height())
        self.rect = self.image.get_rect(center = (x, y))
    
    def update(self, pos):
        pass

此外,这是Indentation的问题。您需要在应用程序循环中而不是在应用程序循环之后绘制场景更新显示

def main():
    # [...]

    while keepGoing:
        clock.tick(30)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                keepGoing = False
            if event.type == pygame.MOUSEBUTTONDOWN:
                killShotSpot = pygame.mouse.get_pos()
                shoot(killShotSpot)
                pygame.draw.circle(background,(255,0,0),killShotSpot,3,0)

        #-->| INDENTATION

        allSprites.clear(screen, background)
        allSprites.update()
        allSprites.draw(screen)
        pygame.display.flip()
于 2020-12-18T10:16:15.330 回答