Sprite Group不会blit

时间:2014-03-30 06:25:39

标签: python pygame

我再一次在一个学校项目上工作,我很难过,我觉得应该很容易......

我有代码工作;它没有崩溃,但它也没有blitting。我创造了第一人称射击游戏风格的游戏,使用Sprites作为" Zombie目标"我在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()

1 个答案:

答案 0 :(得分:0)

请参见pygame.sprite.Group.draw()

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

您根本不需要类self.x中的属性self.yslef.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()