Ejemplo n.º 1
0
    def __init__(self, game, x, y):
        Enemy.__init__(self, game, x, y)
        self.spritesheet = Spritesheet(path.join(self.game.img_dir, 'enemies.png'))

        self.load_images()
        self.image = self.walking_frames[1]
        self.image = pg.transform.scale(self.image, (TILESIZE, TILESIZE))
Ejemplo n.º 2
0
def doBossLevel():
    # on vide le niveau
    RazLevel()
    Ced.boss_sps = None
    Ced.boss_brL = None
    Ced.boss_brR = None
    # on place l'image du boss au centre
    # taille en briques: 3x8
    # la marge
    marge = (Config.bords.rectT.w - (Config.maxbr * Config.brw)) // 2
    row   = (Config.maxli - 8) // 2
    col   = (Config.maxbr - 3) // 2
    x     = (Config.bords.rectL.w + marge) + (col * Config.brw)
    y     = (Config.bords.rectT.h + 5) + (row * Config.brh)
    if Config.level > 34:
        # boss doh
        Ced.boss_sps = Spritesheet.SpriteSheet('boss-doh.png', 3, 1, (x, y), True)
        # on place les bras
        x           -= (Ced.boss_sps.rect.width - 44)
        y           += 27
        Ced.boss_brL = Spritesheet.SpriteSheet('boss-doh-bras-gauche.png', 6, 1, (x, y), True)
        x           += (Ced.boss_sps.rect.width + 56)
        Ced.boss_brR = Spritesheet.SpriteSheet('boss-doh-bras-droit.png', 6, 1, (x, y), True)
    else:
        # boss classique
        Ced.boss_sps = Spritesheet.SpriteSheet('boss.png', 4, 1, (x, y), True)
    Ced.boss_sps.draw()
    if Ced.boss_brL is not None:
        Ced.boss_brL.draw()
    if Ced.boss_brR is not None:
        Ced.boss_brR.draw()
Ejemplo n.º 3
0
    def __init__(self):
        pygame.init()
        pygame.mixer.init()

        self.END_MUSIC = pygame.mixer.Sound('end.ogg')
        self.KILL_SOUND = pygame.mixer.Sound('lose.wav')
        self.ENEMY_DEAD_SOUND = pygame.mixer.Sound('scream.wav')
        self.ENEMY_DEAD_SOUND.set_volume(.4)
        self.WIN_SOUND = pygame.mixer.Sound("win.wav")

        pygame.display.set_caption(title)
        pygame.font.init()
        self.font = pygame.font.Font(FONTNAME, 20)
        self.fileName = "HighScore.json"
        # if there is no file then make one
        if not os.path.isfile(self.fileName):
            empty_score_file = open(self.fileName, "w")
            empty_score_file.write("[]")
            empty_score_file.close()
        self.running = True
        self.playerDead = False
        self.win = False
        self.highscore = HighScores()
        self.platformSpriteSheet = Spritesheet(spritesheetPlatformFile)
        self.enemiesSpriteSheet = Spritesheet(enemyspritesheetFile)
        self.coinSpriteSheet = Spritesheet(spritesheetPlatformFile)
        self.cloud_images = []
        for cloud in range(1, 4):
            self.cloud_images.append(
                pygame.image.load("cloud{}.png".format(cloud)).convert())
Ejemplo n.º 4
0
    def __init__(self, game, x, y):
        self._layer = PLAYER_LAYER
        self.groups = game.sprites
        pg.sprite.Sprite.__init__(self, self.groups)

        self.game = game
        self.won = False

        self.spritesheet = Spritesheet(
            path.join(self.game.img_dir, 'player.png'))

        self.walking = False
        self.last_update = 0
        self.current_frame = 0
        self.going_right = True

        self.load_images()
        self.image = self.standing_frames_r
        self.image = pg.transform.scale(self.image, (TILESIZE, TILESIZE))

        self.rect = self.image.get_rect()

        self.move_left = self.move_right = False

        self.pos = pg.math.Vector2(x * TILESIZE, y * TILESIZE)
        self.acc = pg.math.Vector2(0, GRAVITY)
        self.vel = pg.math.Vector2(0, 0)
Ejemplo n.º 5
0
 def __init__(self):
     self.spritesheet = Spritesheet(S_CHARACTER, CHARACTER_SPRITESHEET_SIZE)
     (spr_unit_x, spr_unit_y) = CHARACTER_SPRITESHEET_UNIT
     self.standing = self.spritesheet.imagesAt([
         (0, 0, spr_unit_x, spr_unit_y), (24, 30, spr_unit_x, spr_unit_y)
     ])
     self.index = 0
     self.image = self.standing[self.index]
     self.pos = self.image.get_rect()
Ejemplo n.º 6
0
 def __init__(self, screen, player, screen_size, generator):
     self.screen = screen
     self.player = player
     self.screen_size = screen_size
     self.generator = generator
     self.font = pygame.font.Font(
         self.resource_path('Game/font/kongtext.ttf'), 8)
     self.score = 0
     self.ss = Spritesheet(self.resource_path('Game/sprites/coins.png'))
     self.coin = self.ss.image_at(pygame.Rect(79, 5, 5, 5))
Ejemplo n.º 7
0
    def __init__(self, enemy_xpos, enemy_ypos, enemy_ID, object_ID, screen):

        self.ss = Spritesheet(
            self.resource_path('Game/sprites/SpaceShipAsset.png'))
        self.sound = Soundplayer()
        self.ss2 = Spritesheet(
            self.resource_path('Game/sprites/bullets/allTheBullets.png'))
        self.img = self.ss.image_at(pygame.Rect(2, 41, 12, 12))
        self.img_bullet = self.ss2.image_at(pygame.Rect(1, 1, 1, 1))
        # Gul enemy
        if enemy_ID == 0:
            self.img = self.ss.image_at(pygame.Rect(2, 41, 12, 12))
            self.img_bullet = self.ss2.image_at(pygame.Rect(166, 8, 16, 16))
            self.img_bullet = pygame.transform.scale(self.img_bullet, (8, 8))

            self.bullet_speed = 1
            self.bullet_interval = 60
        # Grøn enemy
        elif enemy_ID == 1:
            self.img = self.ss.image_at(pygame.Rect(19, 44, 14, 9))
            self.img_bullet = self.ss2.image_at(pygame.Rect(225, 8, 16, 16))
            self.img_bullet = pygame.transform.scale(self.img_bullet, (8, 8))

            self.bullet_speed = 2
            self.bullet_interval = 75
        #Blå enemy
        elif enemy_ID == 2:
            self.img = self.ss.image_at(pygame.Rect(39, 44, 9, 10))
            self.img_bullet = self.ss2.image_at(pygame.Rect(187, 8, 16, 16))
            self.img_bullet = pygame.transform.scale(self.img_bullet, (8, 8))

            self.bullet_speed = 2.5
            self.bullet_interval = 90

        self.enemy_bullet_cooldown = random.randint(0, 60)
        self.enemy_timer = 0

        self.x_direction = [1, -1]
        self.enemy_x = enemy_xpos
        self.enemy_y = enemy_ypos
        self.enemy_speed_x = 0.6 * self.x_direction[random.randint(0, 1)]
        self.enemy_speed_y = 0.1
        self.enemy_health = 20

        self.enemy_damage_cooldown = 6

        self.enemy_rect = self.img.get_rect()
        self.object_ID = object_ID
        self.screen = screen

        self.alive = True

        self.temp_image = self.img.copy()
        self.temp_image.set_alpha(120)
        self.temp_image.fill((254, 254, 254), None, BLEND_RGB_MAX)
Ejemplo n.º 8
0
 def __init__(self, sname, sname_hit, sname_death, bras_G, bras_D):
     pygame.sprite.Sprite.__init__(self)
     self.setPos()
     self.sps_n = Spritesheet.SpriteSheet(sname[0], sname[1], sname[2],
                                          (self.x, self.y), True)
     self.sps_h = Spritesheet.SpriteSheet(sname_hit[0], sname_hit[1],
                                          sname_hit[2], (self.x, self.y),
                                          True)
     self.sps_d = Spritesheet.SpriteSheet(sname_death[0], sname_death[1],
                                          sname_death[2], (self.x, self.y),
                                          True)
     self.sps_brL = None
     self.sps_brR = None
     self.brticks = pygame.time.get_ticks()
     # flag pour les bras, False: vers le bas, True: vers le haut
     self.brLUp = True
     self.brRUp = True
     if bras_G is not None:
         # on place les bras
         x = self.x - (self.sps_n.rect.width - 44)
         y = self.y + 27
         self.sps_brL = Spritesheet.SpriteSheet(bras_G[0], bras_G[1],
                                                bras_G[2], (x, y), True)
     if bras_D is not None:
         x = self.x - (self.sps_n.rect.width -
                       44) + (self.sps_n.rect.width + 56)
         y = self.y + 27
         self.sps_brR = Spritesheet.SpriteSheet(bras_D[0], bras_D[1],
                                                bras_D[2], (x, y), True)
     self.maxhits = 20
     self.hits = self.maxhits
     self.npts = 500
     # ticks max par image (en ms) pour l'animation, fonction du mode
     self.ticks_m = 250
     # timer du mode 'hit'
     self.time_h = pygame.time.get_ticks()
     # les bombes larguées
     self.bombs = []
     # les lasers (missiles)
     self.laser = []
     # boss mort?
     self.isDead = False
     # la gauge de vie
     self.gauge = pygame.Surface([
         (Config.screenWidth - Config.bords.width) - 20, 20
     ])
     self.gauge.fill(THECOLORS["black"])
     self.gauge_r = self.gauge.get_rect()
     # centrée dans la zone d'info
     self.gauge_r.x = Config.bords.width + (
         (Config.screenWidth - Config.bords.width) // 2)
     self.gauge_r.x -= self.gauge_r.width // 2
     self.gauge_r.y = 550
     # mode normal par défaut
     self.setNormalMode()
Ejemplo n.º 9
0
def init():
    # on initialise les générateurs
    # on place le premier à gauche
    genL = Spritesheet.SpriteSheet('generator.png', 1, 6, (60, 0), False)
    # pour l'animation
    genL.state = "closed"
    genL.ticks = pygame.time.get_ticks()
    Config.generator.append(genL)
    # on place le second à droite
    genR = Spritesheet.SpriteSheet('generator.png', 1, 6,
                                   ((Config.bords.width - 60 - 88), 0), False)
    # pour l'animation
    genR.state = "closed"
    genR.ticks = pygame.time.get_ticks()
    Config.generator.append(genR)
Ejemplo n.º 10
0
class Character:
    def __init__(self):
        self.spritesheet = Spritesheet(S_CHARACTER, CHARACTER_SPRITESHEET_SIZE)
        (spr_unit_x, spr_unit_y) = CHARACTER_SPRITESHEET_UNIT
        self.standing = self.spritesheet.imagesAt([
            (0, 0, spr_unit_x, spr_unit_y), (24, 30, spr_unit_x, spr_unit_y)
        ])
        self.index = 0
        self.image = self.standing[self.index]
        self.pos = self.image.get_rect()

    def move(self, key):
        if key[pygame.K_LEFT]:
            self.pos.x -= 1
        elif key[pygame.K_RIGHT]:
            self.pos.x += 1

    def setPos(self, x=0, y=0):
        self.pos.x = x
        self.pos.y = y

    def update(self):
        self.index += 0.1  #pygame.time.Clock.get_time()
        if self.index >= len(self.standing):
            self.index = 0
        self.image = self.standing[int(self.index)]

    def render(self, screen):
        screen.blit(self.image, self.pos, self.image.get_rect())
Ejemplo n.º 11
0
class Goomba(Enemy):
    def __init__(self, game, x, y):
        Enemy.__init__(self, game, x, y)
        self.spritesheet = Spritesheet(path.join(self.game.img_dir, 'enemies.png'))

        self.load_images()
        self.image = self.walking_frames[1]
        self.image = pg.transform.scale(self.image, (TILESIZE, TILESIZE))
    
    def load_images(self):
        self.walking_frames =   [ 
                                    self.spritesheet.get_image(0, 0, 16, 16),
                                    self.spritesheet.get_image(16, 0, 16, 16)
                                ]
        
        for frame in self.walking_frames:
            frame.set_colorkey(WHITE)
Ejemplo n.º 12
0
    def __init__(self, x_pos, y_pos, screen, object_ID):
        self.screen = screen
        self.x_pos = x_pos
        self.y_pos = y_pos

        self.x_speed = random.uniform(-0.5, 0.5)

        self.y_speed = -2

        self.object_ID = object_ID
        self.ss = Spritesheet(self.resource_path('Game/sprites/coins.png'))
        self.img = self.ss.image_at(pygame.Rect(1, 2, 1, 2))

        self.rect = pygame.Rect(x_pos, y_pos, 2, 2)

        if self.object_ID == 'coin':
            self.img = self.ss.image_at(pygame.Rect(3, 2, 10, 11))
        self.alive = True
Ejemplo n.º 13
0
class Collectables(GameObject):
    def __init__(self, x_pos, y_pos, screen, object_ID):
        self.screen = screen
        self.x_pos = x_pos
        self.y_pos = y_pos

        self.x_speed = random.uniform(-0.5, 0.5)

        self.y_speed = -2

        self.object_ID = object_ID
        self.ss = Spritesheet(self.resource_path('Game/sprites/coins.png'))
        self.img = self.ss.image_at(pygame.Rect(1, 2, 1, 2))

        self.rect = pygame.Rect(x_pos, y_pos, 2, 2)

        if self.object_ID == 'coin':
            self.img = self.ss.image_at(pygame.Rect(3, 2, 10, 11))
        self.alive = True

    def get_rect(self):
        return self.rect

    def loop(self):
        self.x_pos += self.x_speed
        self.y_pos += self.y_speed

        self.y_speed += 0.1

        if self.y_speed > 3:
            self.y_speed = 3

        self.rect = self.img.get_rect(x=self.x_pos, y=self.y_pos)

        if self.collision('player', self.rect):
            JsonLoader.updateJsonFile(JsonLoader, 'coin')
            Mediator.to_be_removed.append(self)

        if self.y_pos > self.screen.get_height() + self.img.get_height():
            Mediator.to_be_removed.append(self)

    def draw(self):
        self.screen.blit(self.img, (self.x_pos, self.y_pos))
Ejemplo n.º 14
0
    def __init__(self, xpos, ypos, ID, object_ID, screen, level=1):

        self.ss = Spritesheet(
            self.resource_path('Game/sprites/SpaceShipAsset.png'))
        self.ss2 = Spritesheet(
            self.resource_path('Game/sprites/bullets/allTheBullets.png'))
        self.soundplayer = Soundplayer()

        self.img = self.ss.image_at(pygame.Rect(4, 56, 45, 22))
        self.img = pygame.transform.scale(self.img, (90, 44))
        self.img_bullet_blue_rect = self.ss2.image_at(
            pygame.Rect(111, 108, 11, 36))
        self.img_bullet_red = self.ss2.image_at(pygame.Rect(27, 106, 17, 16))
        self.img_bullet_blue = self.ss2.image_at(pygame.Rect(8, 129, 17, 16))
        self.img_bullet_green = self.ss2.image_at(pygame.Rect(27, 129, 17, 16))

        self.boss_xpos = xpos
        self.boss_ypos = ypos
        self.boss_x_speed = 0
        self.boss_y_speed = 0.5
        self.boss_ready = False

        self.boss_health = 2000
        self.boss_health_max = self.boss_health

        self.boss_rect = pygame.Rect(0, 0, 0, 0)

        self.boss_damage_cooldown = 0
        self.bullet_counter = 60
        self.timer = 0

        ##boss bullet pattern 4, random movement
        self.tp_timer = 0

        self.boss_id = ID
        self.object_ID = object_ID
        self.screen = screen

        self.level = level
        self.update_boss()
        self.alive = True
Ejemplo n.º 15
0
def randomBonus(pos):
    if random.randrange(1, 11) in [3, 6, 9]:
        sname = random.choice([
            'pastille-D.png', 'pastille-B.png', 'pastille-P.png',
            'pastille-S.png', 'pastille-C.png', 'pastille-E.png',
            'pastille-L.png', 'pastille-V.png', 'pastille-T.png',
            'pastille-M.png', 'pastille-X.png', 'pastille-R.png',
            'pastille-F.png'
        ])
        # la lettre de la pastille
        ptype = re.findall(r'-([A-Z]{1})', sname)[0]
        pastille = Spritesheet.SpriteSheet(sname, 8, 1, pos, True)
        pastille.ticks = pygame.time.get_ticks()
        # vitesse entre 3 et 5
        pastille.speed = random.choice([3, 4, 5])
        bonus = [ptype, pastille]
        Config.bonus.append(bonus)
Ejemplo n.º 16
0
    def __init__(self, name, x, y, standalone = None, standaloneDesc = None):

        if standalone != None and standaloneDesc != None:
            self.image = standalone
            self.desc = standaloneDesc
        else:
            parser = JsonParser.Parser()
            parser.parse("GameConfig/ItemDatabase.json")
            ss = Spritesheet.spritesheet("Sprites/Spritesheets/pixelArt.png")

            self.image = ss.image_at(tuple(parser.settings[name]["coords"]), (0, 0, 0, 0))
            self.desc = parser.settings[name]["desc"]

        self.consumable = True
        self.name = name
        self.pickedUp = False

        self.spriteImage = sprite(self.image, x, y, self.name)
        self.spriteImage.description = self.desc

        self.invSlot = ""
Ejemplo n.º 17
0
class Player(pg.sprite.Sprite):
    def __init__(self, game, x, y):
        self._layer = PLAYER_LAYER
        self.groups = game.sprites
        pg.sprite.Sprite.__init__(self, self.groups)

        self.game = game
        self.won = False

        self.spritesheet = Spritesheet(
            path.join(self.game.img_dir, 'player.png'))

        self.walking = False
        self.last_update = 0
        self.current_frame = 0
        self.going_right = True

        self.load_images()
        self.image = self.standing_frames_r
        self.image = pg.transform.scale(self.image, (TILESIZE, TILESIZE))

        self.rect = self.image.get_rect()

        self.move_left = self.move_right = False

        self.pos = pg.math.Vector2(x * TILESIZE, y * TILESIZE)
        self.acc = pg.math.Vector2(0, GRAVITY)
        self.vel = pg.math.Vector2(0, 0)

    def load_images(self):
        self.standing_frames_r = self.spritesheet.get_image(1, 1, 16, 16)
        self.standing_frames_r.set_colorkey(WHITE)

        self.standing_frames_l = pg.transform.flip(self.standing_frames_r,
                                                   True, False)
        self.standing_frames_l.set_colorkey(WHITE)

        self.walking_frames_r = [
            self.spritesheet.get_image(18, 1, 16, 16),
            self.spritesheet.get_image(35, 1, 16, 16),
            self.spritesheet.get_image(52, 1, 16, 16)
        ]
        for frame in self.walking_frames_r:
            frame.set_colorkey(WHITE)

        self.walking_frames_l = []
        for frame in self.walking_frames_r:
            self.walking_frames_l.append(pg.transform.flip(frame, True, False))
            frame.set_colorkey(WHITE)

    def jump(self):
        # if we stand on platforms, we can jump
        self.rect.y += 1
        hits = pg.sprite.spritecollide(self, self.game.obstacles, False)
        self.rect.y -= 1

        if hits:
            self.game.jump_sound.play()
            self.vel.y -= PLAYER_JUMP

    def collide_with_obstacles(self, dir):
        if dir == 'x':
            hits = pg.sprite.spritecollide(self, self.game.obstacles, False)
            if hits:
                if self.vel.x > 0:
                    self.pos.x = hits[0].rect.left - self.rect.width
                    if isinstance(hits[0], Flag):
                        self.won = True
                if self.vel.x < 0:
                    self.pos.x = hits[0].rect.right
                self.vel.x = 0
                self.rect.x = self.pos.x
        if dir == 'y':
            hits = pg.sprite.spritecollide(self, self.game.obstacles, False)
            if hits:
                if self.vel.y > 0:
                    self.pos.y = hits[0].rect.top - self.rect.height
                if self.vel.y < 0:
                    self.pos.y = hits[0].rect.bottom
                    if isinstance(hits[0], Wall):
                        hits[0].kill()
                        self.game.score += 50
                    if isinstance(hits[0],
                                  QuestionBlock) and hits[0].hitted == False:
                        self.game.coin_sound.play()
                        hits[0].hitted = True
                        self.game.score += 500
                self.vel.y = 0
                self.rect.y = self.pos.y

    def update(self):
        self.animate()
        self.vel.x = 0

        if self.move_left:
            self.vel.x = -PLAYER_SPEED
        if self.move_right:
            self.vel.x = PLAYER_SPEED

        self.image = pg.transform.scale(self.image, (TILESIZE, TILESIZE))

        self.vel += self.acc

        self.pos.x += self.vel.x
        self.pos.y += self.vel.y

        # updating position for the two axis checking if they're colliding
        self.rect.x = self.pos.x
        self.collide_with_obstacles('x')
        self.rect.y = self.pos.y
        self.collide_with_obstacles('y')

        # collision with enemy
        hits = pg.sprite.spritecollide(self, self.game.enemies, False)

        if hits:
            # check if player jumped on the enemy and if so delete the enemy
            if self.vel.y > 0:
                self.vel.y = -20
                self.game.bump_sound.play()
                hits[0].kill()
                self.game.score += 100

            # if player did not jump on the enemy, game over
            else:
                self.game.tries += 1
                self.game.show_gameover_screen()
                self.game.playing = False

        # gameover if player falls down
        if self.pos.y > HEIGHT:
            self.game.tries += 1
            self.game.show_gameover_screen()
            self.game.playing = False

        # exit if player touches flag
        if self.won:
            exit()

    def animate(self):
        now = pg.time.get_ticks()
        if self.vel.x != 0:
            self.walking = True
        else:
            self.walking = False

        if self.walking:
            if now - self.last_update > 100:
                self.last_update = now
                self.current_frame = (self.current_frame + 1) % len(
                    self.walking_frames_r)
                if self.vel.x > 0:
                    self.going_right = True
                    self.image = self.walking_frames_r[self.current_frame]
                elif self.vel.x < 0:
                    self.going_right = False
                    self.image = self.walking_frames_l[self.current_frame]

        else:
            if self.going_right:
                self.image = self.standing_frames_r
            else:
                self.image = self.standing_frames_l
Ejemplo n.º 18
0
class Boss(GameObject):

    ## Choose enemy sprite ##
    def __init__(self, xpos, ypos, ID, object_ID, screen, level=1):

        self.ss = Spritesheet(
            self.resource_path('Game/sprites/SpaceShipAsset.png'))
        self.ss2 = Spritesheet(
            self.resource_path('Game/sprites/bullets/allTheBullets.png'))
        self.soundplayer = Soundplayer()

        self.img = self.ss.image_at(pygame.Rect(4, 56, 45, 22))
        self.img = pygame.transform.scale(self.img, (90, 44))
        self.img_bullet_blue_rect = self.ss2.image_at(
            pygame.Rect(111, 108, 11, 36))
        self.img_bullet_red = self.ss2.image_at(pygame.Rect(27, 106, 17, 16))
        self.img_bullet_blue = self.ss2.image_at(pygame.Rect(8, 129, 17, 16))
        self.img_bullet_green = self.ss2.image_at(pygame.Rect(27, 129, 17, 16))

        self.boss_xpos = xpos
        self.boss_ypos = ypos
        self.boss_x_speed = 0
        self.boss_y_speed = 0.5
        self.boss_ready = False

        self.boss_health = 2000
        self.boss_health_max = self.boss_health

        self.boss_rect = pygame.Rect(0, 0, 0, 0)

        self.boss_damage_cooldown = 0
        self.bullet_counter = 60
        self.timer = 0

        ##boss bullet pattern 4, random movement
        self.tp_timer = 0

        self.boss_id = ID
        self.object_ID = object_ID
        self.screen = screen

        self.level = level
        self.update_boss()
        self.alive = True

    def get_rect(self):
        return self.boss_rect

    ## Damage the enemy

    def get_enemy_health(self):
        return self.enemy_health

    def boss_draw(self):
        self.screen.blit(self.img, (self.boss_xpos, self.boss_ypos))

    ##Make sure the enemy stays in the screen
    def boss_move(self):
        self.timer += 1
        self.tp_timer += 1
        self.boss_damage_cooldown += 1

        self.boss_xpos += self.boss_x_speed
        self.boss_ypos += self.boss_y_speed

        if self.boss_ypos > 50 and not self.boss_ready:
            self.boss_ready = True
            self.boss_y_speed = 0
            self.boss_x_speed = 0.2
            if self.level == 2:
                self.boss_x_speed = 0.4

        if self.boss_xpos + self.img.get_width() + 2 > self.screen.get_width():
            self.boss_x_speed *= -1
        if self.boss_xpos < 2:
            self.boss_x_speed *= -1

        self.boss_rect = pygame.Rect(self.boss_xpos, self.boss_ypos,
                                     self.img.get_width(),
                                     self.img.get_height())

        if self.boss_ready and self.level == 1:
            if self.boss_health > 1400:
                if self.timer > 10:
                    self.boss_x_speed = 0.4
                    self.timer = 0
                    self.boss_bullet_pattern_1()

            elif self.boss_health > 900:
                if self.timer > 40:
                    self.boss_x_speed = 0.2
                    self.timer = 0
                    self.boss_bullet_pattern_2()
            else:
                if self.timer > 20:
                    self.timer = 0
                    self.boss_bullet_pattern_3()

        elif self.boss_ready and self.level == 2:
            if self.boss_health > 3500:
                if self.timer > 10:
                    self.timer = 0
                    self.boss_bullet_pattern_1()

            elif self.boss_health > 2000:
                if self.timer > 40:
                    self.timer = 0
                    self.boss_bullet_pattern_2()
            else:
                self.boss_x_speed = 0
                if self.timer > 30 and self.tp_timer > 20:
                    self.timer = 0
                    self.boss_bullet_pattern_3()
                if self.tp_timer > 180:
                    self.tp_timer = 0
                    self.tp_boss()

    def loop(self):

        self.boss_move()

        if self.boss_ready:
            hit_count = self.collision('f_bullet', self.boss_rect)
            if hit_count >= 1:
                Soundplayer.enemy_hit_sound(Soundplayer())
                self.boss_health -= float(
                    Upgrades.get_level_bullet_damage(
                        Upgrades,
                        JsonLoader.get_bullet_damage(JsonLoader))) * hit_count

        if self.boss_health < 0:

            Mediator.to_be_removed.append(self)

    def draw(self):
        self.draw_boss_rect()
        self.boss_draw()

    ## Shower of bullets ##
    def boss_bullet_pattern_1(self):

        Mediator.all_game_objects.append(
            EnemyBullet(self.boss_xpos + 6, self.boss_ypos + 26, 0, 6, False,
                        self.img_bullet_blue_rect, 'e_bullet', self.screen))
        Mediator.all_game_objects.append(
            EnemyBullet(self.boss_xpos + 70, self.boss_ypos + 26, 0, 6, False,
                        self.img_bullet_blue_rect, 'e_bullet', self.screen))

    ## Burst of bullets with space in ##
    def boss_bullet_pattern_2(self):
        bullet_list = [i for i in range(-8, 8)]
        random_number = random.randint(5, len(bullet_list) - 7)
        bullet_list[random_number] = -200
        bullet_list[random_number - 1] = -200
        bullet_list[random_number + 1] = -200

        for i in bullet_list:
            if i == -200:
                pass
            else:
                Mediator.all_game_objects.append(
                    EnemyBullet(self.boss_xpos + 45, self.boss_ypos + 26,
                                (i * 0.4), 3.5, True, self.img_bullet_red,
                                'e_bullet', self.screen))

    ## Blue burst of bullets constant ##
    def boss_bullet_pattern_3(self):

        for i in range(-6, 9):
            Mediator.all_game_objects.append(
                EnemyBullet(self.boss_xpos + 45, self.boss_ypos + 26,
                            (i * 0.5), 3, True, self.img_bullet_blue,
                            'e_bullet', self.screen))

    ##
    def boss_bullet_pattern_4(self):
        for i in range(-6, 9):
            Mediator.all_game_objects.append(
                EnemyBullet(self.boss_xpos + 45, self.boss_ypos + 26,
                            (i * 0.5), 3, True, self.img_bullet_green,
                            'e_bullet', self.screen))

    def tp_boss(self):
        if self.boss_xpos >= self.screen.get_width() / 2:
            self.boss_xpos = random.randint(self.img.get_width() + 6,
                                            (self.screen.get_width() / 2))
        else:
            self.boss_xpos = random.randint(
                self.screen.get_width() / 2,
                self.screen.get_width() - self.img.get_width())

    def draw_boss_rect(self):
        if self.boss_ready:
            pygame.draw.rect(self.screen, (112, 128, 144),
                             (pygame.Rect(40, 30, 220, 8)))
            pygame.draw.rect(self.screen, (220, 20, 60), (pygame.Rect(
                40, 30, 220 * (self.boss_health / self.boss_health_max), 8)))

    def update_boss(self):
        if self.level == 2:
            self.boss_health = 5000
            self.boss_health_max = 5000
Ejemplo n.º 19
0
class HUD:
    def __init__(self, screen, player, screen_size, generator):
        self.screen = screen
        self.player = player
        self.screen_size = screen_size
        self.generator = generator
        self.font = pygame.font.Font(
            self.resource_path('Game/font/kongtext.ttf'), 8)
        self.score = 0
        self.ss = Spritesheet(self.resource_path('Game/sprites/coins.png'))
        self.coin = self.ss.image_at(pygame.Rect(79, 5, 5, 5))

    def draw_overlay_HUD(self):
        #overlay bottom background
        pygame.draw.rect(
            self.screen, (93, 100, 117),
            pygame.Rect(0, self.screen_size[1] * 0.9, self.screen_size[0],
                        self.screen_size[1] * 0.1))
        #overlay bottom background border
        pygame.draw.rect(self.screen, (52, 61, 82),
                         pygame.Rect(0, self.screen_size[1] * 0.9,
                                     self.screen_size[0] - 1,
                                     (self.screen_size[1] - 1) * 0.1),
                         width=2)
        pygame.draw.rect(self.screen, (52, 61, 82),
                         pygame.Rect(self.screen_size[0] * 0.77,
                                     self.screen_size[1] * 0.9,
                                     self.screen_size[0] - 1,
                                     (self.screen_size[1] - 1) * 0.1),
                         width=2)

        #overlay top background
        pygame.draw.rect(
            self.screen, (93, 100, 117),
            pygame.Rect(0, 0, self.screen_size[0], self.screen_size[1] * 0.04))
        #overlay top background border
        pygame.draw.rect(self.screen, (52, 61, 82),
                         pygame.Rect(0, 0, self.screen_size[0],
                                     self.screen_size[1] * 0.04),
                         width=2)

        #special ability square
        pygame.draw.rect(
            self.screen, (68, 77, 85),
            pygame.Rect(self.screen_size[0] * 0.79,
                        self.screen_size[1] * 0.915,
                        self.screen_size[0] * 0.09,
                        self.screen_size[1] * 0.07))
        pygame.draw.rect(
            self.screen, (68, 77, 85),
            pygame.Rect(self.screen_size[0] * 0.89,
                        self.screen_size[1] * 0.915,
                        self.screen_size[0] * 0.09,
                        self.screen_size[1] * 0.07))
        #special ability border
        pygame.draw.rect(self.screen, (52, 61, 70),
                         pygame.Rect(self.screen_size[0] * 0.79,
                                     self.screen_size[1] * 0.915,
                                     self.screen_size[0] * 0.09,
                                     self.screen_size[1] * 0.07),
                         width=2)
        pygame.draw.rect(self.screen, (52, 61, 70),
                         pygame.Rect(self.screen_size[0] * 0.89,
                                     self.screen_size[1] * 0.915,
                                     self.screen_size[0] * 0.09,
                                     self.screen_size[1] * 0.07),
                         width=2)

    def draw_health_HUD(self):
        #Healthbar background
        pygame.draw.rect(
            self.screen, (68, 77, 85),
            pygame.Rect(self.screen_size[0] * 0.02,
                        self.screen_size[1] * 0.915,
                        self.screen_size[0] * 0.735,
                        self.screen_size[1] * 0.03))

        # Update and draw healthbar
        if self.player.get_health() > 30:
            pygame.draw.rect(
                self.screen, (90, 186, 74),
                pygame.Rect(self.screen_size[0] * 0.02,
                            self.screen_size[1] * 0.915,
                            (self.player.get_health() / 100) *
                            self.screen_size[0] * 0.735,
                            self.screen_size[1] * 0.03))
        else:
            pygame.draw.rect(
                self.screen, (255, 45, 12),
                pygame.Rect(self.screen_size[0] * 0.02,
                            self.screen_size[1] * 0.915,
                            (self.player.get_health() / 100) *
                            self.screen_size[0] * 0.735,
                            self.screen_size[1] * 0.03))

        for i in range(0, 10):
            pygame.draw.rect(self.screen, (52, 61, 70),
                             pygame.Rect(
                                 self.screen_size[0] * 0.018 +
                                 i * self.screen_size[0] * 0.0734,
                                 self.screen_size[1] * 0.915,
                                 self.screen_size[0] * 0.0734,
                                 (self.screen_size[1] - 1) * 0.03),
                             width=2)

        pygame.draw.rect(self.screen, (52, 61, 70),
                         pygame.Rect(self.screen_size[0] * 0.02,
                                     self.screen_size[1] * 0.915,
                                     self.screen_size[0] * 0.735,
                                     self.screen_size[1] * 0.03),
                         width=1)

        ## The green bar represents our health
        ## When the healthbar turns gray it means that user has been hit and has lost health

    def draw_energy_HUD(self):
        #Energybar background
        pygame.draw.rect(
            self.screen, (68, 77, 85),
            pygame.Rect(self.screen_size[0] * 0.02,
                        self.screen_size[1] * 0.955,
                        self.screen_size[0] * 0.735,
                        self.screen_size[1] * 0.03))
        #Energybar gul
        pygame.draw.rect(
            self.screen, (115, 220, 255),
            pygame.Rect(
                self.screen_size[0] * 0.02, self.screen_size[1] * 0.955,
                (self.player.get_energy() / 10) * self.screen_size[0] * 0.735,
                self.screen_size[1] * 0.03))
        #Energybar brackets
        for i in range(0, 10):
            pygame.draw.rect(self.screen, (52, 61, 70),
                             pygame.Rect(
                                 self.screen_size[0] * 0.018 +
                                 i * self.screen_size[0] * 0.0734,
                                 self.screen_size[1] * 0.955,
                                 self.screen_size[0] * 0.0734,
                                 (self.screen_size[1] - 1) * 0.03),
                             width=2)
        #Overlay
        pygame.draw.rect(self.screen, (52, 61, 70),
                         pygame.Rect(self.screen_size[0] * 0.02,
                                     self.screen_size[1] * 0.955,
                                     self.screen_size[0] * 0.735,
                                     self.screen_size[1] * 0.03),
                         width=1)

    def draw_score(self):

        score = self.font.render('Score ' + str(self.score), 0,
                                 (255, 255, 255))
        level = self.font.render('Level ' + str(self.generator.get_level()), 0,
                                 (255, 255, 255))
        x = self.font.render('x', 0, (255, 255, 255))

        amount_coins = self.font.render(JsonLoader.get_coins(JsonLoader), 0,
                                        (255, 255, 255))

        self.screen.blit(self.coin, (90, 6))
        self.screen.blit(x, (98, 4))
        self.screen.blit(amount_coins, (108, 4))
        self.screen.blit(score, (6, 4))
        self.screen.blit(level, (self.screen_size[0] - 70, 4))

    def draw_HUD(self):
        for object in Mediator.to_be_removed:
            if object.get_object_ID() == 'enemy':
                self.score += 1

        self.draw_overlay_HUD()
        self.draw_energy_HUD()
        self.draw_health_HUD()
        self.draw_score()

    def resource_path(self, relative_path):
        try:
            # PyInstaller creates a temp folder and stores path in _MEIPASS
            base_path = sys._MEIPASS
        except Exception:
            base_path = os.path.abspath(".")

        return os.path.join(base_path, relative_path)
Ejemplo n.º 20
0
def makeBonus(btype):
    if btype == 'B':
        # portail vers le niveau suivant
        Config.bonus_actif[btype] = True
        Config.bonus_timer[btype] = pygame.time.get_ticks()
        if Config.bonus_adds[btype] == None:
            # premier passage, on créé le sprite (placé à droite)
            sgate = Spritesheet.SpriteSheet(
                'portail-next-level.png', 7, 1,
                (Config.bords.rectR.x, Config.bords.height - 88), True)
            Config.bonus_adds[btype] = sgate
        Config.bonus_adds[btype].setImage(0, 0)
        Config.bonus_adds[btype].state = "openning"
        Config.bonus_adds[btype].ticks = pygame.time.get_ticks()
    elif btype == 'C':
        # la balle colle à la raquette
        Config.bonus_actif[btype] = True
        Config.bonus_timer[btype] = pygame.time.get_ticks()
        Config.player.setStickyMode(True)
    elif btype == 'D':
        # 3 balles de plus
        for i in range(3):
            ball = Ball.Ball(
                None, 7.0,
                random.choice([
                    'ball-02.png', 'ball-03.png', 'ball-04.png', 'ball-05.png'
                ]), 'ball-black.png')
            # on place la balle à la position de la balle actuelle
            ball.x = float(Config.balls[0].x - ball.rect.width)
            ball.y = float(Config.balls[0].y - ball.rect.height - 1)
            ball.rect.centerx = ball.x + ball.rect.width
            ball.rect.centery = ball.y + ball.radius
            ball.state = "moving"
            Config.balls.append(ball)
    elif btype == 'E':
        # raquette plus grande
        Config.bonus_actif[btype] = True
        Config.bonus_timer[btype] = pygame.time.get_ticks()
        Config.player.setLargeSize()
    elif btype == 'F':
        # champs de force sous la raquette
        Config.bonus_actif[btype] = True
        Config.bonus_timer[btype] = pygame.time.get_ticks()
        if Config.bonus_adds[btype] == None:
            # premier passage, on créé le sprite
            ffield = Spritesheet.SpriteSheet(
                'champs-de-force.png', 1, 2,
                (Config.bords.rectL.x + Config.bords.rectL.w,
                 Config.bords.height - 17), True)
            Config.bonus_adds[btype] = ffield
        Config.bonus_adds[btype].ticks = pygame.time.get_ticks()
    elif btype == 'L':
        # lasers sur la raquette
        Config.bonus_actif[btype] = True
        Config.bonus_timer[btype] = pygame.time.get_ticks()
        Config.player.setLaserMode(True)
    elif btype == 'M':
        # les balles deviennent noires et détruisent toutes les briques
        # sans rebondir dessus
        Config.bonus_actif[btype] = True
        Config.bonus_timer[btype] = pygame.time.get_ticks()
        for i in range(len(Config.balls)):
            Config.balls[i].setMegaBall()
    elif btype == 'P':
        # une vie en plus
        Config.life += 1
    elif btype == 'R':
        # raquette plus petite
        Config.bonus_actif[btype] = True
        Config.bonus_timer[btype] = pygame.time.get_ticks()
        Config.player.setSmallSize()
    elif btype == 'S':
        # les balles ralentissent
        for i in range(len(Config.balls)):
            Config.balls[i].speed /= 2.0
        Config.bonus_actif[btype] = True
        Config.bonus_timer[btype] = pygame.time.get_ticks()
    elif btype == 'T':
        if not Config.player.dualMode:
            # on a 2 raquettes légèrement espacées
            Config.bonus_actif[btype] = True
            Config.bonus_timer[btype] = pygame.time.get_ticks()
            Config.player.setDualMode(True)
    elif btype == 'V':
        # la vitesse des balles augmente
        for i in range(len(Config.balls)):
            Config.balls[i].speed += 1.0
        Config.bonus_actif[btype] = True
        Config.bonus_timer[btype] = pygame.time.get_ticks()
#settings
screen_width = 640
screen_height = 480
sprite_height = 42
sprite_width = 24
scale = 8
TITLE = "Walk Cycle"
BGCOLOR = CC_BLUE

pygame.init()
screen = pygame.display.set_mode([screen_width, screen_height])
pygame.display.set_caption(TITLE)

spritesheet = Spritesheet("walk_cycle_8_frame.bmp",
                          sprite_height=sprite_height,
                          sprite_width=sprite_width,
                          colorkey=(255, 255, 255),
                          scale=8)

animation_frames = spritesheet.load_animation(0, 8)

animation_index = 0
animation_max_index = 5

# Used to manage how fast the screen updates
clock = pygame.time.Clock()
# Loop until the user clicks the close button.
done = False
# -------- Main Program Loop -----------
while not done:
    for event in pygame.event.get():
Ejemplo n.º 22
0
def update():
    # les générateurs
    for i in range(len(Config.generator)):
        if Config.generator[i].state == "openning":
            # il s'ouvre
            # animation, une image toutes les 100ms
            ct = pygame.time.get_ticks()
            if (ct - Config.generator[i].ticks) > 100:
                Config.generator[i].ticks = ct
                if Config.generator[i].cr >= (Config.generator[i].rows - 1):
                    if len(Config.enemys) < Config.max_E:
                        # complètement ouvert, un ennemi arrive (centré en bas du générateur)
                        sname = random.choice([
                            'enemy-01.png', 'enemy-02.png', 'enemy-03.png',
                            'enemy-04.png', 'enemy-05.png', 'enemy-06.png'
                        ])
                        enemy = Spritesheet.SpriteSheet(
                            sname, 5, 5,
                            (Config.generator[i].rect.midbottom[0] - 22,
                             Config.generator[i].rect.midbottom[1]), True)
                        # on regarde si on peut le placer
                        dopl = True
                        for b in range(len(Config.bricks)):
                            if Config.bricks[b].rect.y > enemy.rect.bottom:
                                # les briques restantes sont plus basses, on sort
                                break
                            offset = (int(enemy.x - Config.bricks[b].rect.x),
                                      int(enemy.y - Config.bricks[b].rect.y))
                            result = Config.bricks[b].mask.overlap(
                                enemy.mask, offset)
                            if result:
                                # des briques gênent l'apparition
                                dopl = False
                                break
                        if dopl:
                            enemy.speed = 1.0
                            # vers le bas par défaut, au hasard
                            enemy.angle = math.radians(
                                random.choice([
                                    random.randrange(300, 330),
                                    -random.randrange(300, 330)
                                ]))
                            enemy.ticks = pygame.time.get_ticks()
                            enemy.state = "moving"
                            # pour l'explosion si touché par une balle
                            enemy.xplod = None
                            # le score si touché par une balle
                            enemy.npts = 50
                            Config.enemys.append(enemy)
                    # on referme le générateur
                    Config.generator[i].state = "closed"
                    Config.generator[i].setImage(0, 0)
                else:
                    Config.generator[i].setImage(Config.generator[i].cc,
                                                 Config.generator[i].cr + 1)

    # les ennemis
    for i in range(len(Config.enemys)):
        if Config.enemys[i].state == "moving":
            # animation, une image toutes les 100ms
            ct = pygame.time.get_ticks()
            if (ct - Config.enemys[i].ticks) > 100:
                Config.enemys[i].ticks = ct
                cc = Config.enemys[i].cc + 1
                cr = Config.enemys[i].cr
                if cc >= (Config.enemys[i].cols - 1):
                    if cr >= (Config.enemys[i].rows - 1):
                        cr = 0
                        cc = 0
                    else:
                        cr += 1
                        cc = 0
                Config.enemys[i].setImage(cc, cr)

            # déplacements
            newpos = Tools.Vector2(
                Config.enemys[i].x +
                math.sin(Config.enemys[i].angle) * Config.enemys[i].speed,
                Config.enemys[i].y +
                math.cos(Config.enemys[i].angle) * Config.enemys[i].speed)

            # vérif collisions
            # bord haut?
            if newpos.y <= Config.bords.rectT.h:
                newpos.y = float(Config.bords.rectT.h + 1)
                Config.enemys[i].angle = math.pi - Config.enemys[i].angle
            # bord gauche?
            if newpos.x <= Config.bords.rectL.w:
                newpos.x = float(Config.bords.rectL.w + 1)
                Config.enemys[i].angle = -Config.enemys[i].angle
            # bord droit?
            if newpos.x > Config.bords.rectR.x - Config.enemys[i].rect.width:
                newpos.x = float(Config.bords.rectR.x -
                                 Config.enemys[i].rect.width - 1)
                Config.enemys[i].angle = -Config.enemys[i].angle
            # en bas?
            if newpos.y > Config.bords.height:
                # l'ennemi est détruit
                Config.enemys.pop(i)
                break

            if Config.bossLevel:
                # le boss?
                offset = (int(newpos.x - Config.boss.sps.rect.x),
                          int(newpos.y - Config.boss.sps.rect.y))
                result = Config.boss.sps.mask.overlap(Config.enemys[i].mask,
                                                      offset)
                if result:
                    (newpos.x, newpos.y,
                     Config.enemys[i].angle) = checkBorder_E(
                         Config.enemys[i], Config.boss.sps.rect, newpos.x,
                         newpos.y)
                # les bras?
                if Config.boss.sps_brL is not None:
                    offset = (int(newpos.x - Config.boss.sps_brL.rect.x),
                              int(newpos.y - Config.boss.sps_brL.rect.y))
                    result = Config.boss.sps_brL.mask.overlap(
                        Config.enemys[i].mask, offset)
                    if result:
                        (newpos.x, newpos.y,
                         Config.enemys[i].angle) = checkBorder_E(
                             Config.enemys[i], Config.boss.sps_brL.rect,
                             newpos.x, newpos.y)
                if Config.boss.sps_brR is not None:
                    offset = (int(newpos.x - Config.boss.sps_brR.rect.x),
                              int(newpos.y - Config.boss.sps_brR.rect.y))
                    result = Config.boss.sps_brR.mask.overlap(
                        Config.enemys[i].mask, offset)
                    if result:
                        (newpos.x, newpos.y,
                         Config.enemys[i].angle) = checkBorder_E(
                             Config.enemys[i], Config.boss.sps_brR.rect,
                             newpos.x, newpos.y)
            else:
                # une brique?
                for b in range(len(Config.bricks)):
                    offset = (int(newpos.x - Config.bricks[b].rect.x),
                              int(newpos.y - Config.bricks[b].rect.y))
                    result = Config.bricks[b].mask.overlap(
                        Config.enemys[i].mask, offset)
                    if result:
                        (newpos.x, newpos.y,
                         Config.enemys[i].angle) = checkBorder_E(
                             Config.enemys[i], Config.bricks[b].rect, newpos.x,
                             newpos.y)
                        break

            # nouvelle position
            Config.enemys[i].x = newpos.x
            Config.enemys[i].y = newpos.y
            Config.enemys[i].rect.x = Config.enemys[i].x
            Config.enemys[i].rect.y = Config.enemys[i].y

        elif Config.enemys[i].state == "xplod":
            # touché par une balle, l'ennemi explose
            if Config.enemys[i].xplod is None:
                # premier passage, on charge la page de sprites
                Config.enemys[i].xplod = Spritesheet.SpriteSheet(
                    'explosion.png', 12, 1, (Config.enemys[i].rect.centerx,
                                             Config.enemys[i].rect.centery),
                    True)
                Config.enemys[i].ticks = pygame.time.get_ticks()

            # animation, une image toutes les 100ms
            ct = pygame.time.get_ticks()
            if (ct - Config.enemys[i].ticks) > 100:
                Config.enemys[i].ticks = ct
                if Config.enemys[i].xplod.cc >= (Config.enemys[i].xplod.cols -
                                                 1):
                    # explosion terminée, l'ennemi disparait
                    Config.score += Config.enemys[i].npts
                    Config.enemys.pop(i)
                    break
                else:
                    Config.enemys[i].xplod.setImage(
                        Config.enemys[i].xplod.cc + 1,
                        Config.enemys[i].xplod.cr)
Ejemplo n.º 23
0
TITLE = "Nyan Cat"
BGCOLOR = CC_BLUE

screen = pygame.display.set_mode([screen_width, screen_height])
pygame.display.set_caption(TITLE)

all_sprites_list = pygame.sprite.Group()

# Loop until the user clicks the close button.
done = False

# Used to manage how fast the screen updates
clock = pygame.time.Clock()

spritesheet = Spritesheet("nyan_cat_c64.bmp", colorkey=(72, 58, 170), scale=8)

head = spritesheet.merge_images(spritesheet.load_animation(6, 6),
                                spritesheet.load_animation(0, 6))
tail = spritesheet.merge_images(spritesheet.load_animation(18, 6),
                                spritesheet.load_animation(12, 6))
body = spritesheet.merge_images(spritesheet.load_animation(24, 1),
                                spritesheet.load_animation(25, 1))

star = spritesheet.load_animation(26, 6)

animation_index = 0
animation_max_index = 5
# -------- Main Program Loop -----------
while not done:
    for event in pygame.event.get():
Ejemplo n.º 24
0
    "north": (440, 800),
    "west": (930, 440),
    "south": (440, 80)
}

parser = JsonParser.Parser()
parser.parse("GameConfig/config.json")
musicActive = parser.settings['musicSettings']['music']
sfxActive = parser.settings['musicSettings']['SFX']
character = parser.settings['levelSettings']['player']
currentLevel = parser.settings['levelSettings']['starting_level']

previousLevel = 0
toExit = None

ss = Spritesheet.spritesheet("Sprites/" + character + "/character.png")
alpha = (0, 0, 0, 0)

parser.parse("Sprites/" + character + "/walkCycle.json")

charWalkCycleDown = parser.settings['Down']
charWalkCycleUp = parser.settings['Up']
charWalkCycleRight = parser.settings['Right']
charWalkCycleLeft = parser.settings['Left']

cycles = [
    charWalkCycleDown, charWalkCycleLeft, charWalkCycleRight, charWalkCycleUp
]
for element in cycles:
    for r in range(len(element)):
        element[r] = ss.image_at(tuple(element[r]), alpha)
Ejemplo n.º 25
0
class Enemy(GameObject):

    ## Choose enemy sprite ##
    def __init__(self, enemy_xpos, enemy_ypos, enemy_ID, object_ID, screen):

        self.ss = Spritesheet(
            self.resource_path('Game/sprites/SpaceShipAsset.png'))
        self.sound = Soundplayer()
        self.ss2 = Spritesheet(
            self.resource_path('Game/sprites/bullets/allTheBullets.png'))
        self.img = self.ss.image_at(pygame.Rect(2, 41, 12, 12))
        self.img_bullet = self.ss2.image_at(pygame.Rect(1, 1, 1, 1))
        # Gul enemy
        if enemy_ID == 0:
            self.img = self.ss.image_at(pygame.Rect(2, 41, 12, 12))
            self.img_bullet = self.ss2.image_at(pygame.Rect(166, 8, 16, 16))
            self.img_bullet = pygame.transform.scale(self.img_bullet, (8, 8))

            self.bullet_speed = 1
            self.bullet_interval = 60
        # Grøn enemy
        elif enemy_ID == 1:
            self.img = self.ss.image_at(pygame.Rect(19, 44, 14, 9))
            self.img_bullet = self.ss2.image_at(pygame.Rect(225, 8, 16, 16))
            self.img_bullet = pygame.transform.scale(self.img_bullet, (8, 8))

            self.bullet_speed = 2
            self.bullet_interval = 75
        #Blå enemy
        elif enemy_ID == 2:
            self.img = self.ss.image_at(pygame.Rect(39, 44, 9, 10))
            self.img_bullet = self.ss2.image_at(pygame.Rect(187, 8, 16, 16))
            self.img_bullet = pygame.transform.scale(self.img_bullet, (8, 8))

            self.bullet_speed = 2.5
            self.bullet_interval = 90

        self.enemy_bullet_cooldown = random.randint(0, 60)
        self.enemy_timer = 0

        self.x_direction = [1, -1]
        self.enemy_x = enemy_xpos
        self.enemy_y = enemy_ypos
        self.enemy_speed_x = 0.6 * self.x_direction[random.randint(0, 1)]
        self.enemy_speed_y = 0.1
        self.enemy_health = 20

        self.enemy_damage_cooldown = 6

        self.enemy_rect = self.img.get_rect()
        self.object_ID = object_ID
        self.screen = screen

        self.alive = True

        self.temp_image = self.img.copy()
        self.temp_image.set_alpha(120)
        self.temp_image.fill((254, 254, 254), None, BLEND_RGB_MAX)

    def get_rect(self):
        return self.enemy_rect

    def get_enemy_health(self):
        return self.enemy_health

    def enemy_draw(self):
        if self.enemy_damage_cooldown < 6:
            self.screen.blit(self.img, (self.enemy_x, self.enemy_y))
            self.screen.blit(self.temp_image, (self.enemy_x, self.enemy_y))
        else:
            self.screen.blit(self.img, (self.enemy_x, self.enemy_y))

    ##Make sure the enemy stays in the screen
    def enemy_move(self):
        self.enemy_timer += 1
        self.enemy_damage_cooldown += 1

        self.enemy_x += self.enemy_speed_x
        if self.enemy_x >= 300 - 14 or self.enemy_x <= 0:
            self.enemy_speed_x *= -1

        self.enemy_y += self.enemy_speed_y
        if self.enemy_y >= 400:
            Mediator.to_be_removed.append(self)

        self.enemy_rect = self.img.get_rect(x=self.enemy_x, y=self.enemy_y)

        if self.enemy_timer > self.bullet_interval + self.enemy_bullet_cooldown:
            self.enemy_bullet_cooldown = random.randint(0, 60)
            self.enemy_timer = 0
            Mediator.all_game_objects.append(
                EnemyBullet(
                    self.enemy_x, self.enemy_y + 4,
                    random.uniform(-1 * (self.bullet_speed / 4),
                                   1 * (self.bullet_speed / 4)),
                    self.bullet_speed, True, self.img_bullet, 'e_bullet',
                    self.screen))

    def loop(self):
        self.enemy_move()

        hit_count = self.collision('f_bullet', self.enemy_rect)
        if hit_count >= 1:
            self.enemy_damage_cooldown = 0
            Soundplayer.enemy_hit_sound(Soundplayer())
            self.enemy_health -= float(
                Upgrades.get_level_bullet_damage(
                    Upgrades,
                    JsonLoader.get_bullet_damage(JsonLoader))) * hit_count
            self.showing_image = self.temp_image

        if self.enemy_health <= 0.0:
            Mediator.to_be_removed.append(self)
            self.alive = False

            if random.randint(0, 9) == 5:
                Mediator.all_game_objects.append(
                    Collectables(self.enemy_x, self.enemy_y, self.screen,
                                 'coin'))
            JsonLoader.updateJsonFile(JsonLoader, 'enemy')

    def draw(self):
        self.enemy_draw()