Ejemplo n.º 1
0
    def __init__(self, scr, view_point, explosions, track):
        """Input parameters:
        scr - Surface for drawing;
        view_point - ViewPoint class instance;
        explosions - Explosions class instance for asteroid blowing;
        track - Track class instance (needed for spawning)."""
        self.scr = scr
        self.view_pt = view_point
        self.explosions = explosions
        self.track = track
        self.spawn_density = 0
        # Format: [(center_x, center_y),...]
        self.spawns = []
        self.images = []
        self.masks = []
        for filename in ASTEROID_FILES:
            image = pygame.image.load(f"img/{filename}")
            self.images.append(image)

            # Creating bitmasks for each frame of each asteroid image
            asteroid = AnimatedSprite(image, self.scr, self.view_pt,
                                      FRAME_COLS, FRAME_ROWS)
            asteroid_masks = []
            for frame in range(0, asteroid.get_max_frame() + 1):
                mask_image = pygame.Surface(asteroid.rect.size,
                                            pygame.SRCALPHA, image)
                mask_image.fill((0, 0, 0, 0))
                mask_image.blit(image, (0, 0), asteroid._get_frame_rect(frame))
                asteroid_masks.append(pygame.mask.from_surface(mask_image))
            self.masks.append(asteroid_masks)

        self.items = pygame.sprite.Group()
Ejemplo n.º 2
0
class PandaView:

    def __init__(self, image, width, height):
        self.__width = width
        self.__height = height
        self.__image = image
        self.__panda_sprite = AnimatedSprite(self.__slice_sprite(), 4)

    def get_image(self, time):
        self.__panda_sprite.update(time)
        return self.__panda_sprite.image

    def change_state(self, image):
        self.__image = image
        self.__panda_sprite.change_images(self.__slice_sprite())

    def __slice_sprite(self):
        images = []

        master_width, master_height = self.__image.get_size()
        for i in range(int(master_width/self.__width)):
            image = self.__image.subsurface((i*self.__width, 0,
                                             self.__width, self.__height))
            images.append(image)
        return images
Ejemplo n.º 3
0
 def add(self, center_x, center_y, explosion_ind=None):
     """Adds new explosion animation.
     Input parameters:
     center_x, center_y - absolute center coordinates of animation;
     explosion_ind - index of an animation in the list; if None then
     the animation is selected randomly (except small explosion)."""
     if explosion_ind == None:
         explosion_ind = randint(BLAST_EXPLOSION_IND, DOUBLE_EXPLOSION_IND)
     explosion = AnimatedSprite(self.images[explosion_ind], self.scr,
                                self.view_pt, FRAME_COLS, FRAME_ROWS)
     explosion.set_center(center_x, center_y)
     explosion.repeat = False
     self.items.add(explosion)
Ejemplo n.º 4
0
 def __init__(self, position, image_frames, lines, columns, fps=10):
     AnimatedSprite.__init__(self, position, image_frames, lines, columns, fps=10)
     
     self.jump_speed = 1
     self.jumping = False
     self.gravity = 0.7
     self.max_gravity_speed = 6
     
     self.xspeed = 1
     self.yspeed = 1
     self.xdir = 0
     self.ydir = 0
     self.last_xdir = 0
     self.last_ydir = 0
Ejemplo n.º 5
0
    def __init__(self, position, image_frames, lines, columns, fps=10):
        AnimatedSprite.__init__(self, position, image_frames, lines, columns, fps=10)

        self.jump_speed = 1
        self.jumping = False
        self.gravity = 0.7
        self.max_gravity_speed = 6

        self.xspeed = 1
        self.yspeed = 1
        self.xdir = 0
        self.ydir = 0
        self.last_xdir = 0
        self.last_ydir = 0
Ejemplo n.º 6
0
class Game:
    def __init__(self, size):
        pygame.init()
        pygame.key.set_repeat(200, 5)
        
        self.screen = pygame.display.set_mode(size)
        
        guy_image = pygame.image.load("data/images/guy.png")
        guy_image = guy_image.convert()
        self.guy = AnimatedSprite([0, 0], guy_image, 3, 4, 10)
        self.guy_idle = self.guy.create_animation([0, 1, 2, 3, 3, 2, 1, 0])
        self.guy_waving = self.guy.create_animation([7, 8, 9, 10, 11, 11, 10, 9, 8, 7])
        
        self.guy.set_animation(self.guy_idle)
        
        world_image = pygame.image.load("data/images/world.png")
        world_image = world_image.convert()
        self.world = World([200, 200], world_image)
        
        self.running = True
        
    def main_loop(self):
        while self.running:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    self.running = False
                elif event.type == pygame.KEYDOWN:
                    self.key_handler(event.key)
            
            if distance(self.guy, self.world) < 140:
                if self.guy.animation != self.guy_waving:
                    self.guy.set_animation(self.guy_waving)
            else:
                if self.guy.animation != self.guy_idle:
                    self.guy.set_animation(self.guy_idle)
            
            self.guy.update()
            self.screen.blit(self.world.image, self.world.rect)
            self.screen.blit(self.guy.image, self.guy.rect)
            pygame.display.flip()

    def key_handler(self, key):
        if (key == pygame.K_DOWN
        or key == pygame.K_UP
        or key == pygame.K_LEFT
        or key == pygame.K_RIGHT):
            self.world.move(key)
            self.screen.fill((0, 0, 0))
Ejemplo n.º 7
0
    def on_animated_sprite_menu_item_activate(self, menuitem, data=None):
        filename = self.get_open_filename()

        if filename and self.drawing_area:
            x, y, lines, columns = self.get_sprite_data()

            if x:
                #frame_pixbuf = tools.get_frame_pixbuf(image_frames, (0,0,101,101))
                #pixmap, mask = frame_pixbuf.render_pixmap_and_mask()

                animated_sprite = AnimatedSprite(filename, x, y, lines,
                                                 columns)
                name = self.sprite_name.next()
                self.animated_sprites_draw[name] = animated_sprite

                self.draw_background()
                self.draw_sprites()

                # Adding the animated image to the dictionary that
                # contains all animated sprites to be drawn
                animated_image = {
                    "x": x,
                    "y": y,
                    "lines": lines,
                    "columns": columns,
                    "filename": filename,
                    "animations": {},
                }

                self.animated_sprites[name] = animated_image
Ejemplo n.º 8
0
class Enemy(pygame.sprite.Sprite):
    def __init__(self, x, y, speed):
        pygame.sprite.Sprite.__init__(self)
        # (x, y) represents bottom center
        self.x = x
        self.y = y
        self.time = 0
        self.speed = speed
        self.interval = z_walk.animSpeed / abs(self.speed)
        self.dead = False
        self.deadShot = False
        self.size = 25
        self.animSprite = AnimatedSprite(ZOMBIE_SPRITE_SHEET,
                                         [z_walk, z_hit, z_die], 'walk')
        self.animSprite.loadImages()
        self.rect = pygame.rect.Rect(
            (self.x - self.size / 2, self.y - self.size, self.size, self.size))
        self.image = pygame.Surface((self.size, self.size))
        self.hp = 100

    def updateHit(self):
        iw = self.image.get_width()
        ih = self.image.get_height()
        self.rect = pygame.rect.Rect((self.x - iw / 2, self.y - ih, iw, ih))

    def update(self):
        self.updateHit()
        self.image = self.animSprite.getImage(
            True if self.speed < 0 else False)
        if not self.dead and self.time % self.interval == 0:
            self.x += self.speed * self.interval
        self.time += 1 * abs(self.speed)

    def hItYoUrSeLf(self):
        if not self.dead:
            self.animSprite.clearStack()
            self.animSprite.addAnimation('hit')
            self.hp -= 25
            if self.hp <= 0:
                self.die()

    def die(self):
        self.animSprite.addAnimation('die')
        self.dead = True

    def draw(self, surf):
        surf.blit(self.image, (self.x - self.image.get_width() / 2,
                               self.y - self.image.get_height()))

    def collidePoint(self, x, y):
        return self.rect.collidepoint(x, y)
Ejemplo n.º 9
0
 def __init__(self, x, y, speed):
     pygame.sprite.Sprite.__init__(self)
     # (x, y) represents bottom center
     self.x = x
     self.y = y
     self.time = 0
     self.speed = speed
     self.interval = z_walk.animSpeed / abs(self.speed)
     self.dead = False
     self.deadShot = False
     self.size = 25
     self.animSprite = AnimatedSprite(ZOMBIE_SPRITE_SHEET,
                                      [z_walk, z_hit, z_die], 'walk')
     self.animSprite.loadImages()
     self.rect = pygame.rect.Rect(
         (self.x - self.size / 2, self.y - self.size, self.size, self.size))
     self.image = pygame.Surface((self.size, self.size))
     self.hp = 100
Ejemplo n.º 10
0
def generate_avatar(unit_type: str, stack_count, cell: Cell = None):
    with open('pdata/wcAnimations.json', 'r') as file:
        sheet_data = json.load(file)
        sprite = AnimatedSprite(
            os.path.join('assets', 'unit', unit_type) + '.png',
            **sheet_data[unit_type])

    with open('pdata/wcUnits.json', 'r') as file:
        unit_data = json.load(file)
        avatar = Avatar(sprite, stack_count, cell, **unit_data[unit_type])

    return avatar
Ejemplo n.º 11
0
 def __init__(self, scr, view_point, explosions):
     """Input parameters:
     scr - Surface for drawing;
     view_point - ViewPoint class instance;
     explosions - Explosions class instance for ship destroying."""
     self.scr = scr
     self.view_pt = view_point
     self.explosions = explosions
     self.image = pygame.image.load(f'img/{SHIP_FILE}')
     self.mask = pygame.mask.from_surface(self.image)
     self.rect = self.image.get_rect()
     self.x = -(self.rect.width / 2)
     self.y = self.rect.height
     self._update_rect()
     self.laser = Laser(self.scr, self.view_pt)
     self._update_laser_pos()
     self.jet_image = pygame.image.load(f'img/{JET_FILE}')
     self.jets = {
         'left':
         AnimatedSprite(self.jet_image,
                        self.scr,
                        self.view_pt,
                        JET_FRAME_COLS,
                        JET_FRAME_ROWS,
                        reverse=True),
         'right':
         AnimatedSprite(self.jet_image,
                        self.scr,
                        self.view_pt,
                        JET_FRAME_COLS,
                        JET_FRAME_ROWS,
                        reverse=True),
     }
     self._update_jets_pos()
     self.speed = 0
     self._reset_user_control()
     self.status = STATUS_NORMAL
     self._reset_progress()
Ejemplo n.º 12
0
 def __init__(self, size):
     pygame.init()
     pygame.key.set_repeat(200, 5)
     
     self.screen = pygame.display.set_mode(size)
     
     guy_image = pygame.image.load("data/images/guy.png")
     guy_image = guy_image.convert()
     self.guy = AnimatedSprite([0, 0], guy_image, 3, 4, 10)
     self.guy_idle = self.guy.create_animation([0, 1, 2, 3, 3, 2, 1, 0])
     self.guy_waving = self.guy.create_animation([7, 8, 9, 10, 11, 11, 10, 9, 8, 7])
     
     self.guy.set_animation(self.guy_idle)
     
     world_image = pygame.image.load("data/images/world.png")
     world_image = world_image.convert()
     self.world = World([200, 200], world_image)
     
     self.running = True
Ejemplo n.º 13
0
GAMEOVER = False
keys = [False for i in range(1000)]

z_walk = SpriteConfig(
    pygame.Rect(6, 90, K_WIDTH - 1, 68),  # capture rect of all
    12,  # number
    'walk',  # name
    40,  # max width per tile
    3,  # border
    0.0,  # look buffer
    8,  # frames / animation_frame
    0,  # skipping buffer - how many pixels to skip after green line
)
z_die = SpriteConfig(pygame.Rect(9, 395, 420, 70), 8, 'die', 48, 3, 0.0, 4, 0)
z_hit = SpriteConfig(pygame.Rect(9, 395, 420, 70), 2, 'hit', 48, 3, 0.0, 10, 0)
ex_zomb = AnimatedSprite(ZOMBIE_SPRITE_SHEET, [z_walk, z_hit, z_die], 'walk')
ex_zomb.loadImages()
kirb = AnimatedSprite(
    KIRBY_SPRITE_SHEET,
    [idle, walk, attack, flip, spin, land, ascend, spin_attack, shoot], "idle")
SCREEN.blit(KIRBY_SPRITE_SHEET, (0, 0))
kirb.loadImages(SCREEN)
enemies = []
bullets = []


def generateZombie():
    x = 0
    y = random.randint(0, HEIGHT - UI_SIZE * 2) + UI_SIZE
    dir = 1 + DIFFICULTY
    if bool(random.randint(0, 1)):
Ejemplo n.º 14
0
 def __init__(self, image, width, height):
     self.__width = width
     self.__height = height
     self.__image = image
     self.__panda_sprite = AnimatedSprite(self.__slice_sprite(), 4)