示例#1
0
    def __init__(self, location, *groups):
        super(Enemy, self).__init__(*groups)
        self.spriteDefinition = SpriteDefinition('evoman/images/EnemySprites.png', 0, 0, 43, 59)
        self.updateSprite(SpriteConstants.STANDING, SpriteConstants.LEFT)

        self.rect = pygame.rect.Rect(location, self.image.get_size())
        self.direction = -1
        self.max_life = 100
        self.life = self.max_life
        self.resting = 0
        self.dy = 0
        self.alternate = 1
        self.just_shoot = 0
        self.imune = 0
        self.timeenemy = 0
        self.twists = []
        self.hurt = 0
        self.shooting = 0
        self.gun_cooldown = 0
示例#2
0
    def __init__(self, location, enemyn, level, *groups):
        super(Player, self).__init__(*groups)

        self.spriteDefinition = SpriteDefinition(
            'evoman/images/EvoManSprites.png', 0, 0, 43, 59)
        self.updateSprite(SpriteConstants.STANDING, SpriteConstants.RIGHT)

        self.rect = pygame.rect.Rect(location, self.image.get_size())
        self.resting = 0
        self.dy = 0
        self.direction = 1
        self.alternate = 1
        self.gun_cooldown = 0
        self.max_life = 100
        self.life = self.max_life
        self.atacked = 0
        self.hurt = 0
        self.shooting = 0
        self.inwater = 0
        self.twists = []
        self.vx = 0
        self.vy = 0
        self.hy = 0
        self.sensors = None
示例#3
0
    def __init__(self, location, *groups):
        super(Enemy, self).__init__(*groups)
        self.spriteDefinition = SpriteDefinition('evoman/images/EnemySprites.png', 0, 0, 43, 59)
        self.updateSprite(SpriteConstants.STANDING, SpriteConstants.LEFT) 

        self.rect = pygame.rect.Rect(location, self.image.get_size())
        self.direction = -1
        self.max_life = 100
        self.life = self.max_life	
        self.resting = 0
        self.dy = 0
        self.twists = []
        self.alternate = 1
        self.imune = 0
        self.timeenemy = 0
        self.hurt = 0
        self.shooting = 0
        self.gun_cooldown = 0
示例#4
0
    def __init__(self, location, enemyn, level, *groups):
        super(Player, self).__init__(*groups)


        self.spriteDefinition = SpriteDefinition('evoman/images/EvoManSprites.png', 0, 0, 43, 59)
        self.updateSprite(SpriteConstants.STANDING, SpriteConstants.RIGHT) 
           
        self.rect = pygame.rect.Rect(location, self.image.get_size())
        self.resting = 0
        self.dy = 0
        self.direction = 1
        self.alternate = 1
        self.gun_cooldown = 0
        self.max_life = 100
        self.life = self.max_life
        self.atacked = 0
        self.hurt = 0
        self.shooting = 0
        self.inwater = 0
        self.twists = []
        self.vx = 0
        self.vy = 0
        self.hy = 0
        self.sensors = None
示例#5
0
class Enemy(pygame.sprite.Sprite):


    def __init__(self, location, *groups):
        super(Enemy, self).__init__(*groups)
        self.spriteDefinition = SpriteDefinition('evoman/images/EnemySprites.png', 0, 0, 43, 59)
        self.updateSprite(SpriteConstants.STANDING, SpriteConstants.LEFT) 

        self.rect = pygame.rect.Rect(location, self.image.get_size())
        self.direction = -1
        self.max_life = 100
        self.life = self.max_life	
        self.resting = 0
        self.dy = 0
        self.alternate = 1
        self.imune = 0
        self.timeenemy = 0
        self.twists = []
        self.bullets = 0
        self.hurt = 0
        self.shooting = 0
        self.gun_cooldown = 0
        self.gun_cooldown2 = 0

       


    def update(self, dt, game):
        
        if game.time==1:
            # puts enemy in random initial position
            if game.randomini == 'yes':
                self.rect.x = numpy.random.choice([640,500,400,300])


        # defines game mode for player actionv
        if game.enemymode == 'static': # enemy controlled by static movements 
 
            if self.timeenemy>=4 and self.timeenemy<=20 and  self.timeenemy%4 == 0:
                atack1 = 1
            else:
                atack1 = 0
 
            
            atack2 = 1 #useless
        
 
            if self.timeenemy==4:
                atack2 = 1
            else:
                atack2 = 0

            if self.timeenemy==5:
                atack3 = 1
            else:
                atack3 = 0

            if self.timeenemy>=50 and  self.timeenemy<80:
                atack4 = 1
            else:
                atack4 = 0

            if self.timeenemy == 50:
                atack5 = 1
            else:
                atack5 = 0

            if self.timeenemy == 100:
                atack6 = 1
            else:
                atack6 = 0


        elif game.enemymode == 'ai': # Player controlled by AI algorithm.
         
 

            # calls the controller providing game sensors 
            actions = game.enemy_controller.control(self.sensors.get(game), game.econt)  
            if len(actions) < 6:
                game.print_logs("ERROR: Enemy 1 controller must return 6 decision variables.")
                sys.exit(0)  
          
            atack1 = actions[0]
            atack2 = actions[1]
            atack3 = actions[2]
            atack4 = actions[3]   
            atack5 = actions[4]
            atack6 = actions[5]   


            if atack1 == 1 and not self.gun_cooldown2:
                atack1 = 1
            else:
                atack1 = 0


            if atack3 == 1 and not self.gun_cooldown:
                atack3 = 1
            else:
                atack3 = 0


        # marks the flag indicating to the player that the map is on water environment 
        game.player.inwater = 1
        
        # if the 'start game' marker is 1 
        if game.start == 1:

            # increments enemy timer 
            self.timeenemy += 1

            # copies last position state of the enemy 	
            last = self.rect.copy()

            # calculates a relative distance factor, between the player and enemy to set up the jumping strengh 
            aux_dist = (abs(game.player.rect.right - self.rect.right)/490.0)+0.3    
             
            # shoots 5 bullets positioned over the same range  
            if atack1 == 1:

                self.shooting = 5

                self.gun_cooldown2 = 3                       
  

                # bullets sound effect 
                if game.sound == "on" and game.playermode == "human":
                    sound = pygame.mixer.Sound('evoman/sounds/scifi011.wav')
                    c = pygame.mixer.Channel(3)
                    c.set_volume(10)
                    c.play(sound)
                   
                rand = numpy.random.randint(0, 25, 1)
                self.twists.append(Bullet_e7((self.rect.x,self.rect.y), self.direction, len(self.twists), game.sprite_e))


            # throws from 1 to 3 bubbles, starting at slighly different positions 
 
            if self.bullets == 0: # if the bubblues have gone away, enemy is abble to realease new bubbles.
 
                rand = 2 
                for i in range(0,rand):
                    if atack3 == 1:

                        self.gun_cooldown = 3

                        self.bullets += 1
                        self.twists.append(Bullet_e72((self.rect.x+self.direction*i*30  ,self.rect.y-i*30), self.direction, len(self.twists), game.sprite_e))

            # decreases time for bullets limitation 
            self.gun_cooldown = max(0, self.gun_cooldown - dt)

            # decreases time for bullets limitation 
            self.gun_cooldown2 = max(0, self.gun_cooldown2 - dt)

            # enemy moves during some time, after standing still for a while 
            if atack4 == 1:   
                   self.rect.x += self.direction * 600 * aux_dist * dt * 0.7
 
            #  enemy jumps while is moving 
            if self.resting == 1 and atack5 == 1:   
                self.dy = -1500
                self.resting = 0

            # at the end of the atack cicle, enemy turns over the players direction. 
            if atack6 == 1:  
               if game.enemymode == 'static':  
                   if game.player.rect.right < self.rect.left:
                       self.direction = -1
                   if game.player.rect.left > self.rect.right:
                        self.direction = 1
               else:
                   self.direction = self.direction * -1

               # reinicializes enemy timer 
               self.timeenemy = 0

            #  gravity 
            self.dy = min(400, self.dy + 100)
            self.rect.y += self.dy * dt * 0.4     

            #  changes the image when enemy jumps or stands up 
            if self.resting == 0:
               if self.direction == -1:
                   self.updateSprite(SpriteConstants.JUMPING, SpriteConstants.LEFT)
               else:
                   self.updateSprite(SpriteConstants.JUMPING, SpriteConstants.RIGHT)
            else:
               if self.direction == -1:
                   self.updateSprite(SpriteConstants.STANDING, SpriteConstants.LEFT)
               else:
                   self.updateSprite(SpriteConstants.STANDING, SpriteConstants.RIGHT)

            # checks collision of the player with the enemy 
            if self.rect.colliderect(game.player.rect):
                
                # choses what sprite penalise according to config
                if game.contacthurt == "player":
                    game.player.life = max(0, game.player.life-(game.level*0.3))
                if game.contacthurt == "enemy":
                    game.enemy.life = max(0, game.enemy.life-(game.level*0.3))

                
                game.player.rect.x +=  self.direction *  50 * dt   # pushes player when he collides with the enemy 
 
                # limits the player to stand on the screen space even being pushed 
                if game.player.rect.x < 60:
                    game.player.rect.x = 60
                if game.player.rect.x > 620:
                    game.player.rect.x = 620

                if self.rect.x < 70:  
                    self.rect.x = 70  
                if self.rect.x > 610:  
                    self.rect.x = 610  
 
                
                game.player.hurt = 5 # Sets flag to change the player image when he is hurt.

            # controls screen walls and platforms limits agaist enemy 
            new = self.rect
            self.resting = 0
            for cell in game.tilemap.layers['triggers'].collide(new, 'blockers'): 
	
                blockers = cell['blockers']
    
                if 'l' in blockers and last.right <= cell.left and new.right > cell.left:
                    new.right = cell.left
 
                if 'r' in blockers and last.left >= cell.right and new.left < cell.right:
                    new.left = cell.right
                		
                if 't' in blockers and last.bottom <= cell.top and new.bottom > cell.top:
                    self.resting = 1
                    new.bottom = cell.top
                    self.dy = 0
                    
                if 'b' in blockers and last.top >= cell.bottom and new.top < cell.bottom:
                    new.top = cell.bottom 

            # hurt enemy image 
            if self.hurt > 0:
                if self.direction == -1:
                   self.updateSprite(SpriteConstants.HURTING, SpriteConstants.LEFT)
                else:
                   self.updateSprite(SpriteConstants.HURTING, SpriteConstants.RIGHT)

            self.hurt -=1

            # changes bullets images according to the enemy direction 
            if self.shooting > 0:
                if self.direction == -1:
                    self.updateSprite(SpriteConstants.SHOOTING, SpriteConstants.LEFT)
                else:
                    self.updateSprite(SpriteConstants.SHOOTING, SpriteConstants.RIGHT)
 
            self.shooting -= 1
            self.shooting = max(0,self.shooting)

    def updateSprite(self, state, direction):
        self.image = self.spriteDefinition.getImage(state, direction)
示例#6
0
class Enemy(pygame.sprite.Sprite):

 

    def __init__(self, location, *groups):
        super(Enemy, self).__init__(*groups)
        self.spriteDefinition = SpriteDefinition('evoman/images/EnemySprites.png', 0, 0, 43, 59)
        self.updateSprite(SpriteConstants.STANDING, SpriteConstants.LEFT) 

        self.rect = pygame.rect.Rect(location, self.image.get_size())
        self.direction = -1
        self.max_life = 100
        self.life = self.max_life	
        self.resting = 0
        self.dy = 0
        self.twists = []
        self.alternate = 1
        self.fireflash = 0
        self.imune = 0
        self.rect.x = 550
        self.timeenemy = 0
        self.hurt = 0
        self.shooting = 0
        self.gun_cooldown = 0
        self.rect.right = 580

 

    def update(self, dt, game):
        

        if game.time==1:
            # puts enemy in random initial position
            if game.randomini == 'yes':
                self.rect.x = numpy.random.choice([640,500,400,300])


        # defines game mode for player action 
        if game.enemymode == 'static': # enemy controlled by static movements 
 
            if self.timeenemy == 2:
                atack1 = 1
            else:
                atack1 = 0

            if self.timeenemy> 50:
                atack2 = 1
            else:
                atack2 = 0

            if self.timeenemy == 3:
                atack3 = 1
            else:
                atack3 = 0

            if (self.fireflash>=1 and self.fireflash <=40):
                atack4 = 1
            else:
                atack4 = 0

        elif game.enemymode == 'ai': # player controlled by AI algorithm 
 

            # calls the controller providing game sensors 
            actions = game.enemy_controller.control(self.sensors.get(game), game.econt)  
            if len(actions) < 4:
                game.print_logs("ERROR: Enemy 1 controller must return 4 decision variables.")
                sys.exit(0)  
          
            atack1 = actions[0]
            atack2 = actions[1]
            atack3 = actions[2]
            atack4 = actions[3]

            if atack3 == 1 and not self.gun_cooldown:
                atack3 = 1
            else:
                atack3 = 0



        # if the 'start game' marker is 1 
        if game.start == 1:	
           
            self.timeenemy += 1 # increments enemy timer 
       	
            last = self.rect.copy()  # copies last position state of the enemy

  
            # when player atacks, enemy turns into fire and goes towards his direction 
            if game.player.atacked == 1 and self.fireflash == 0:
                self.fireflash = 100
            else:
                self.fireflash = max(0,self.fireflash -1)

            if  atack4 == 1:
                self.rect.x += self.direction * 600 * dt  

                if self.fireflash == 1:
                    self.direction = self.direction * -1

                if  self.rect.colliderect(game.player.rect):
                    self.fireflash = 0

            # otherwise he just keeps shooting towards the player direction 
            elif self.fireflash == 0:
                       
                if atack1 == 1 and self.resting == 1: 
                    self.dy = -900
                    self.resting = 0
   
                self.imune = 0  # enemy is not imune to player's shooting anymore 

                # images of the enemy standing up 
                if self.direction == -1:
                    self.updateSprite(SpriteConstants.STANDING, SpriteConstants.LEFT)
                else:
                    self.updateSprite(SpriteConstants.STANDING, SpriteConstants.RIGHT)  
                          
            # reinicializes timer and turns to the players direction 
            if atack2 == 1:
                self.timeenemy = 1

                if game.enemymode == 'static':
                    if game.player.rect.right < self.rect.left:
                        self.direction = -1   
                    elif game.player.rect.left > self.rect.right:
                        self.direction = 1
                else:
                   self.direction = self.direction *-1


            # checks collision of the player with the enemy 
            if self.rect.colliderect(game.player.rect):
                
                # choses what sprite penalise according to config
                if game.contacthurt == "player":
                    game.player.life = max(0, game.player.life-(game.level*0.3))
                if game.contacthurt == "enemy":
                    game.enemy.life = max(0, game.enemy.life-(game.level*0.3))

                # pushes player when he collides with the enemy 
                game.player.rect.x +=  self.direction *  50 * dt  

                # limits the player to stand on the screen space even being pushed 
                if game.player.rect.x < 60:
                    game.player.rect.x = 60
                if game.player.rect.x > 620:
                    game.player.rect.x = 620

                # sets flag to change the player image when he is hurt 
                game.player.hurt = 5
 
             # gravity 
            self.dy = min(400, self.dy + 100)
            self.rect.y += self.dy * dt

            # controls screen walls and platforms limits agaist enemy 
            new = self.rect
            self.resting = 0
            for cell in game.tilemap.layers['triggers'].collide(new, 'blockers'): 
	
                blockers = cell['blockers']
    
                if 'l' in blockers and last.right <= cell.left and new.right > cell.left:
                    new.right = cell.left
 
                if 'r' in blockers and last.left >= cell.right and new.left < cell.right:
                    new.left = cell.right
                		
                if 't' in blockers and last.bottom <= cell.top and new.bottom > cell.top:
                    self.resting = 1
                    new.bottom = cell.top
                    self.dy = 0
                    
                if 'b' in blockers and last.top >= cell.bottom and new.top < cell.bottom:
                    new.top = cell.bottom 


            # enemy shoots 3 bullets  
            if atack3 == 1:

                self.shooting = 5
 
                self.gun_cooldown = 5

                # if enemy is not turned into fire, shoots, otherwise stops the time counter for a while.
                if self.fireflash == 0:

                    # bullets sound effect 
                    if game.sound == "on" and game.playermode == "human":
                        sound = pygame.mixer.Sound('evoman/sounds/scifi011.wav')
                        c = pygame.mixer.Channel(3)
                        c.set_volume(10)
                        c.play(sound)

               
                    for i in range (0,3):  
                        self.twists.append(Bullet_e4((self.rect.x ,self.rect.y ), self.direction, i, len(self.twists), game.sprite_e))
                else :
                    self.timeenemy -= 1

            
            self.gun_cooldown = max(0, self.gun_cooldown - dt)  # decreases time for bullets limitation.

           # changes bullets images according to the enemy direction 
            if self.shooting > 0:
                if self.direction == -1:
                    self.updateSprite(SpriteConstants.SHOOTING, SpriteConstants.LEFT)
                else:
                    self.updateSprite(SpriteConstants.SHOOTING, SpriteConstants.RIGHT)
 
            self.shooting -= 1
            self.shooting = max(0,self.shooting)

            #  changes the image when enemy is hurt and imune, as a fireball 
            if self.imune == 1:
                if game.time%2==0:
                    self.image = pygame.image.load('evoman/images/fireball.png')
                else:
                    self.image = pygame.image.load('evoman/images/fireball2.png')

            self.hurt -=1 

    def updateSprite(self, state, direction):
        self.image = self.spriteDefinition.getImage(state, direction)
示例#7
0
class Enemy(pygame.sprite.Sprite):


    def __init__(self, location, *groups):
        super(Enemy, self).__init__(*groups)
        self.spriteDefinition = SpriteDefinition('evoman/images/EnemySprites.png', 0, 0, 43, 59)
        self.updateSprite(SpriteConstants.STANDING, SpriteConstants.LEFT) 

        self.rect = pygame.rect.Rect(location, self.image.get_size())
        self.direction = -1
        self.max_life = 100
        self.life = self.max_life	
        self.resting = 0
        self.dy = 0
        self.alternate = 1
        self.just_shoot = 0
        self.imune = 0
        self.timeenemy = 0
        self.twists = []
        self.hurt = 0
        self.shooting = 0
        self.gun_cooldown = 0



    def update(self, dt, game):
                
        
        if game.time==1:

            # puts enemy in random initial position
            if game.randomini == 'yes':
                self.rect.x = numpy.random.choice([640,500,400,300])

        # defines game mode for player action 
        if game.enemymode == 'static': # enemy controlled by static movements 
 
            if (self.timeenemy >= 1 and self.timeenemy <10)  or (self.timeenemy >= 20 and self.timeenemy <30):
                atack1 = 1
            else:
                atack1 = 0

            if self.timeenemy == 1  or self.timeenemy == 20:
                atack2 = 1
            else:
                atack2 = 0

            if self.timeenemy == 9  or self.timeenemy == 29:
                atack3 = 1
            else:
                atack3 = 0

            if self.timeenemy >=40 and self.timeenemy <50:
                atack4 = 1
            else:
                atack4 = 0

            if self.timeenemy  == 50:
                atack5 = 1
            else:
                atack5 = 0
  
            if ( (abs(self.rect.left-game.player.rect.left)<=200 or abs(self.rect.right-game.player.rect.right)<=200)  ) and not self.gun_cooldown:
                atack6 = 1
            else:
                atack6 = 0



        elif game.enemymode == 'ai': # player controlled by AI algorithm 
  
 

            # calls the controller providing game sensors 
            actions = game.enemy_controller.control(self.sensors.get(game), game.econt)  
            if len(actions) < 6:
                game.print_logs("ERROR: Enemy 1 controller must return 6 decision variables.")
                sys.exit(0)  
          
            atack1 = actions[0]
            atack2 = actions[1]
            atack3 = actions[2]
            atack4 = actions[3]   
            atack5 = actions[4]
            atack6 = actions[5]      



            if atack6 == 1 and not self.gun_cooldown:
                atack6 = 1
            else:
                atack6 = 0


        # If the 'start game' marker is 1.
        if game.start == 1:
           
            self.timeenemy += 1 # increments enemy timer 
     	
            last = self.rect.copy() # copies last position state of the enemy 

            # jumps over the player. It happens twice.  
            if  atack1== 1 :   
                # moves on the axis x 
                self.rect.x += self.direction * 730 * dt                 
                if self.resting == 1 and atack2 == 1:
                    self.dy = -900
                    self.resting = 0

                # enemy turns to the players direction   
                if atack3 == 1:

                    if game.enemymode == 'static':
                        if game.player.rect.right < self.rect.left:
                            self.direction = -1
                        if game.player.rect.left > self.rect.right:
                            self.direction = 1
                    else:
                        self.direction = self.direction * -1

            # runs in the player's direction, after jumping twice 
            elif atack4 == 1 :
                self.rect.x += self.direction * 900 * dt
 
            # reinicializes enemy timer 
            elif atack5 == 1:
                self.timeenemy = 0

            # throws a bullet over the player when enemy is jumping and right over him 
            if self.resting == 0 and self.just_shoot == 0 and atack6 == 1:

                self.shooting = 5   
 
                self.gun_cooldown = 5

                # bullets sound effect 
                if game.sound == "on" and game.playermode == "human":
                    sound = pygame.mixer.Sound('evoman/sounds/scifi011.wav')
                    c = pygame.mixer.Channel(3)
                    c.set_volume(10)
                    c.play(sound)

                self.just_shoot = 1
    
                rand = 3
                # shoots from 1 to 3 bullets 
                for i in range(0,rand):
                    self.twists.append(Bullet_e8((self.rect.x+(i*60) ,self.rect.y ), i, self.direction, len(self.twists), game.sprite_e))

            # decreases time for bullets limitation 
            self.gun_cooldown = max(0, self.gun_cooldown - dt)

            # animation, running enemy images alternation 
            if self.direction > 0:
                direction = SpriteConstants.RIGHT
            else:
                direction = SpriteConstants.LEFT

            if self.alternate == 1:
                self.updateSprite(SpriteConstants.START_RUNNING, direction)
            if self.alternate == 4 or self.alternate == 10:
                self.updateSprite(SpriteConstants.RUNNING_STEP1, direction)
            if self.alternate == 7:
                self.updateSprite(SpriteConstants.RUNNING_STEP2, direction)
                   
            self.alternate += 1
            if self.alternate > 12:
                self.alternate = 1    

            #  changes the image when enemy jumps 
            if self.resting == 0:
               if self.direction == -1:
                   self.updateSprite(SpriteConstants.JUMPING, SpriteConstants.LEFT)
               else:
                   self.updateSprite(SpriteConstants.JUMPING, SpriteConstants.RIGHT) 

            # checks collision of the player with the enemy 
            if self.rect.colliderect(game.player.rect):
                
                # choses what sprite penalise according to config
                if game.contacthurt == "player":
                    game.player.life = max(0, game.player.life-(game.level*0.3))
                if game.contacthurt == "enemy":
                    game.enemy.life = max(0, game.enemy.life-(game.level*0.3))

                # sets flag to change the player image when he is hurt 
                game.player.hurt = 5

            #  gravity  
            self.dy = min(400, self.dy + 100)
            self.rect.y += self.dy * dt
           
            # controls screen walls and platforms limits agaist enemy 
            new = self.rect
            self.resting = 0
            for cell in game.tilemap.layers['triggers'].collide(new, 'blockers'): 
	
                blockers = cell['blockers']
    
                if 't' in blockers and last.bottom <= cell.top and new.bottom > cell.top:
                    self.resting = 1
                    new.bottom = cell.top
                    self.dy = 0
                    self.just_shoot = 0
                    
                if 'b' in blockers and last.top >= cell.bottom and new.top < cell.bottom:
                    new.top = cell.bottom 

                if 'l' in blockers and last.right <= cell.left and new.right > cell.left  and last.bottom>cell.top:
                    new.right = cell.left
 
                if 'r' in blockers and last.left >= cell.right and new.left < cell.right   and last.bottom>cell.top:
                    new.left = cell.right
                		
            # hurt enemy animation 
            if self.hurt > 0:
                if self.direction == -1:
                   self.updateSprite(SpriteConstants.HURTING, SpriteConstants.LEFT)
                else:
                   self.updateSprite(SpriteConstants.HURTING, SpriteConstants.RIGHT)

            self.hurt -=1

            # changes bullets images according to the enemy direction 
            if self.shooting > 0:
                if self.direction == -1:
                    self.updateSprite(SpriteConstants.SHOOTING, SpriteConstants.LEFT)
                else:
                    self.updateSprite(SpriteConstants.SHOOTING, SpriteConstants.RIGHT)
 
            self.shooting -= 1
            self.shooting = max(0,self.shooting)

 
    def updateSprite(self, state, direction):
        self.image = self.spriteDefinition.getImage(state, direction)
class Enemy(pygame.sprite.Sprite):
    def __init__(self, location, *groups):
        super(Enemy, self).__init__(*groups)
        self.spriteDefinition = SpriteDefinition(
            'evoman/images/EnemySprites.png', 0, 0, 43, 59)
        self.updateSprite(SpriteConstants.STANDING, SpriteConstants.LEFT)

        self.rect = pygame.rect.Rect(location, self.image.get_size())
        self.direction = -1
        self.max_life = 100
        self.life = self.max_life
        self.resting = 0
        self.dy = 0
        self.alternate = 1
        self.direction_floor = 1
        self.imune = 0
        self.move = 0
        self.countmove = 0
        self.rect.x = 500
        self.timeenemy = 0
        self.twists = []
        self.hurt = 0
        self.shooting = 0
        self.gun_cooldown = 0

    def update(self, dt, game):

        if game.time == 1:
            # puts enemy in random initial position
            if game.randomini == 'yes':
                self.rect.x = numpy.random.choice([640, 500, 400, 300])

        # Defines game mode for player action.
        if game.enemymode == 'static':  # Enemy controlled by static movements.

            if self.resting == 1 and self.timeenemy >= 95 and self.timeenemy <= 110:
                atack1 = 1
            else:
                atack1 = 0

            if self.resting == 0:
                atack2 = 1
            else:
                atack2 = 0

            if (game.player.rect.right < game.enemy.rect.left
                    and abs(game.player.rect.right - game.enemy.rect.left) <=
                    50) or (game.enemy.rect.right < game.player.rect.left
                            and abs(game.enemy.rect.right -
                                    game.player.rect.left) <= 50):
                atack3 = 1
            else:
                atack3 = 0

        elif game.enemymode == 'ai':  # Player controlled by AI algorithm.

            # calls the controller providing game sensors
            actions = game.enemy_controller.control(self.sensors.get(game),
                                                    game.econt)
            if len(actions) < 3:
                game.print_logs(
                    "ERROR: Enemy 1 controller must return 3 decision variables."
                )
                sys.exit(0)

            atack1 = actions[0]
            atack2 = actions[1]
            atack3 = actions[2]

            if atack2 == 1 and not self.gun_cooldown:
                atack2 = 1
            else:
                atack2 = 0

        # if the 'start game' marker is 1
        if game.start == 1:

            self.timeenemy += 1  # increments enemy timer

            # moving floor, changes the movement direction from time to time.
            for cell in game.tilemap.layers['triggers'].collide(
                    game.player.rect, 'blockers'):

                blockers = cell['blockers']
                if 't' in blockers:

                    game.player.rect.x += self.direction_floor * 100 * dt  # moves player over the moving floor

                    # limits player inside the screen
                    if game.player.rect.left < 60:
                        game.player.rect.left = 61
                    if game.player.rect.right > 665:
                        game.player.rect.right = 665

                if game.time % 120 == 0:
                    self.direction_floor = self.direction_floor * -1

            last = self.rect.copy()  # copies last position state of the enemy

            # if player gets too close to the enemy, he jumps to the other side.
            if (self.resting == 1 and atack3 == 1):
                self.move = 1
                self.dy = -900
                self.resting = 0

            if self.move == 1:
                self.rect.x += self.direction * 900 * dt

            if self.move == 1 and self.rect.x < 200:
                self.rect.x = 200
                self.direction = self.direction * -1
                self.move = 0
            if self.move == 1 and self.rect.x > 500:
                self.rect.x = 500
                self.direction = self.direction * -1
                self.move = 0

            # jumps from time to time or when player atacks
            if ((self.resting == 1 and atack1 == 1)
                    or (self.resting == 1 and game.player.atacked == 1)):
                self.dy = -900
                self.resting = 0

            # releases until 4 bullets (decided randomly) after jumping or when player atacks
            if (atack2 == 1 and not self.gun_cooldown):

                self.shooting = 5

                self.gun_cooldown = 3

                # bullets sound effect
                if game.sound == "on" and game.playermode == "human":
                    sound = pygame.mixer.Sound('evoman/sounds/scifi011.wav')
                    c = pygame.mixer.Channel(3)
                    c.set_volume(10)
                    c.play(sound)

                aux = numpy.random.randint(1, 4)
                for i in range(0, aux):
                    self.twists.append(
                        Bullet_e5((self.rect.x + (self.direction *
                                                  (i * 30)), self.rect.top +
                                   (self.direction * (i * 20))),
                                  self.direction, game.player.rect,
                                  len(self.twists), game.sprite_e))

                self.timeenemy = 0  # reinicializes enemy timer

            if game.player.atacked == 1:
                # bullets sound effect
                if game.sound == "on" and game.playermode == "human":
                    sound = pygame.mixer.Sound('evoman/sounds/scifi011.wav')
                    c = pygame.mixer.Channel(3)
                    c.set_volume(10)
                    c.play(sound)

                self.twists.append(
                    Bullet_e5((self.rect.x, self.rect.top), self.direction,
                              game.player.rect, len(self.twists),
                              game.sprite_e))

            self.gun_cooldown = max(
                0, self.gun_cooldown -
                dt)  # decreases time for bullets limitation

            # animation, running enemy images alternation.
            if self.direction > 0:
                direction = SpriteConstants.RIGHT
            else:
                direction = SpriteConstants.LEFT

            if self.alternate == 1:
                self.updateSprite(SpriteConstants.START_RUNNING, direction)
            if self.alternate == 4 or self.alternate == 10:
                self.updateSprite(SpriteConstants.RUNNING_STEP1, direction)
            if self.alternate == 7:
                self.updateSprite(SpriteConstants.RUNNING_STEP2, direction)

            self.alternate += 1
            if self.alternate > 12:
                self.alternate = 1

            #  changes the image when enemy jumps
            if self.resting == 0:
                if self.direction == -1:
                    self.updateSprite(SpriteConstants.JUMPING,
                                      SpriteConstants.LEFT)
                else:
                    self.updateSprite(SpriteConstants.JUMPING,
                                      SpriteConstants.RIGHT)

            # checks collision of the player with the enemy
            if self.rect.colliderect(game.player.rect):

                # choses what sprite penalise according to config
                if game.contacthurt == "player":
                    game.player.life = max(
                        0, game.player.life - (game.level * 0.3))
                if game.contacthurt == "enemy":
                    game.enemy.life = max(0,
                                          game.enemy.life - (game.level * 0.3))

                # pushes player when he collides with the enemy
                game.player.rect.x += self.direction * 50 * dt

                # limits the player to stand on the screen space even being pushed.
                if game.player.rect.x < 60:
                    game.player.rect.x = 60
                if game.player.rect.x > 620:
                    game.player.rect.x = 620

            # gravity
            self.dy = min(400, self.dy + 100)
            self.rect.y += self.dy * dt

            # controls screen walls and platforms limits agaist enemy
            new = self.rect
            self.resting = 0
            for cell in game.tilemap.layers['triggers'].collide(
                    new, 'blockers'):

                blockers = cell['blockers']

                if 'l' in blockers and last.right <= cell.left and new.right > cell.left:
                    new.right = cell.left

                if 'r' in blockers and last.left >= cell.right and new.left < cell.right:
                    new.left = cell.right

                if 't' in blockers and last.bottom <= cell.top and new.bottom > cell.top:
                    self.resting = 1
                    new.bottom = cell.top
                    self.dy = 0

                if 'b' in blockers and last.top >= cell.bottom and new.top < cell.bottom:
                    new.top = cell.bottom

            # hurt enemy animation
            if self.hurt > 0:
                if self.direction == -1:
                    self.updateSprite(SpriteConstants.HURTING,
                                      SpriteConstants.LEFT)
                else:
                    self.updateSprite(SpriteConstants.HURTING,
                                      SpriteConstants.RIGHT)

            self.hurt -= 1

            # changes bullets images according to the enemy direction
            if self.shooting > 0:
                if self.direction == -1:
                    self.updateSprite(SpriteConstants.SHOOTING,
                                      SpriteConstants.LEFT)
                else:
                    self.updateSprite(SpriteConstants.SHOOTING,
                                      SpriteConstants.RIGHT)

            self.shooting -= 1
            self.shooting = max(0, self.shooting)

    def updateSprite(self, state, direction):
        self.image = self.spriteDefinition.getImage(state, direction)
class Enemy(pygame.sprite.Sprite):
    def __init__(self, location, *groups):

        super(Enemy, self).__init__(*groups)

        self.spriteDefinition = SpriteDefinition(
            'evoman/images/EnemySprites.png', 0, 0, 43, 59)
        self.updateSprite(SpriteConstants.STANDING, SpriteConstants.LEFT)

        self.rect = pygame.rect.Rect(location, self.image.get_size())
        self.direction = -1
        self.max_life = 100
        self.life = self.max_life
        self.resting = 0
        self.dy = 0
        self.twists = []
        self.alternate = 1
        self.imune = 0
        self.timeenemy = 0
        self.hurt = 0
        self.shooting = 0
        self.gun_cooldown = 0

    def update(self, dt, game):

        if game.time == 1:
            # puts enemy in random initial position
            if game.randomini == 'yes':
                self.rect.x = numpy.random.choice([630, 610, 560, 530])

        # defines game mode for player action.
        if game.enemymode == 'static':  # enemy controlled by static movements

            if (self.timeenemy >= 210
                    and self.timeenemy <= 250) or (self.timeenemy >= 260
                                                   and self.timeenemy <= 300):
                atack1 = 1
            else:
                atack1 = 0

            if self.timeenemy == 210 or self.timeenemy == 260:
                atack2 = 1
            else:
                atack2 = 0

            if self.timeenemy > 300:
                atack3 = 1
            else:
                atack3 = 0

            if (self.timeenemy == 40) or (self.timeenemy
                                          == 110) or (self.timeenemy == 180):
                atack4 = 1
            else:
                atack4 = 0

        elif game.enemymode == 'ai':  # player controlled by AI algorithm

            # calls the controller providing game sensors
            actions = game.enemy_controller.control(self.sensors.get(game),
                                                    game.econt)
            if len(actions) < 4:
                game.print_logs(
                    "ERROR: Enemy 1 controller must return 4 decision variables."
                )
                sys.exit(0)

            atack1 = actions[0]
            atack2 = actions[1]
            atack3 = actions[2]
            atack4 = actions[3]

            if atack4 == 1 and not self.gun_cooldown:
                atack4 = 1
            else:
                atack4 = 0

            if atack1 == 1 and self.resting == 1:
                atack1 = 1
            else:
                atack1 = 0

        # if the 'start game' marker is 1
        if game.start == 1:

            # increments enemy timer
            self.timeenemy += 1

            # copies last position state of the enemy
            last = self.rect.copy()

            # movements of the enemy on the axis x. Happens 2 to each side.
            if atack1 == 1:
                self.rect.x += self.direction * 200 * dt

                # jumps
                if atack2 == 1:
                    self.dy = -900
                    self.resting = 0

            # animation, running enemy images alternatetion.
                if self.direction > 0:
                    direction = SpriteConstants.RIGHT
                else:
                    direction = SpriteConstants.LEFT

                if self.alternate == 1:
                    self.updateSprite(SpriteConstants.START_RUNNING, direction)
                if self.alternate == 4 or self.alternate == 10:
                    self.updateSprite(SpriteConstants.RUNNING_STEP1, direction)
                if self.alternate == 7:
                    self.updateSprite(SpriteConstants.RUNNING_STEP2, direction)

                self.alternate += 1
                if self.alternate > 12:
                    self.alternate = 1

                #  changes the image when enemy jumps
                if self.resting == 0:
                    if self.direction == -1:
                        self.updateSprite(SpriteConstants.JUMPING,
                                          SpriteConstants.LEFT)
                    else:
                        self.updateSprite(SpriteConstants.JUMPING,
                                          SpriteConstants.RIGHT)

            else:
                # animation, standing up images
                if self.direction == -1:
                    self.updateSprite(SpriteConstants.STANDING,
                                      SpriteConstants.LEFT)
                else:
                    self.updateSprite(SpriteConstants.STANDING,
                                      SpriteConstants.RIGHT)

            # restart enemy timer and turns the enemy around
            if atack3 == 1:
                self.timeenemy = 0
                self.direction = self.direction * -1

            # checks collision of the player with the enemy
            if self.rect.colliderect(game.player.rect):

                # choses what sprite penalise according to config
                if game.contacthurt == "player":
                    game.player.life = max(0,
                                           game.player.life - (game.level * 1))
                if game.contacthurt == "enemy":
                    game.enemy.life = max(0,
                                          game.enemy.life - (game.level * 1))

                game.player.hurt = 5  # sets flag to change the player image when he is hurt.

            # gravity
            self.dy = min(400, self.dy + 100)
            self.rect.y += self.dy * dt

            # controls screen walls and platforms limits agaist enemy.
            new = self.rect
            self.resting = 0
            for cell in game.tilemap.layers['triggers'].collide(
                    new, 'blockers'):

                blockers = cell['blockers']

                if 'l' in blockers and last.right <= cell.left and new.right > cell.left:
                    new.right = cell.left

                if 'r' in blockers and last.left >= cell.right and new.left < cell.right:
                    new.left = cell.right

                if 't' in blockers and last.bottom <= cell.top and new.bottom > cell.top:
                    self.resting = 1
                    new.bottom = cell.top
                    self.dy = 0

                if 'b' in blockers and last.top >= cell.bottom and new.top < cell.bottom:
                    new.top = cell.bottom

            # enemy shoots
            if atack4 == 1:

                self.shooting = 5

                self.gun_cooldown = 3

                # bullets sound effect
                if game.sound == "on" and game.playermode == "human":
                    sound = pygame.mixer.Sound('evoman/sounds/scifi011.wav')
                    c = pygame.mixer.Channel(3)
                    c.set_volume(10)
                    c.play(sound)

                # shoots 6 bullets placed in a fixed range
                for i in range(0, 6):
                    self.twists.append(
                        Bullet_e2((self.rect.x + 10, self.rect.bottom),
                                  self.direction, i, len(self.twists),
                                  game.sprite_e))

            # decreases time for bullets limitation
            self.gun_cooldown = max(0, self.gun_cooldown - dt)

            # hurt enemy animation
            if self.hurt > 0:
                if self.direction == -1:
                    self.updateSprite(SpriteConstants.HURTING,
                                      SpriteConstants.LEFT)
                else:
                    self.updateSprite(SpriteConstants.HURTING,
                                      SpriteConstants.RIGHT)

            self.hurt -= 1

            # changes bullets images according to the enemy direction
            if self.shooting > 0:
                if self.direction == -1:
                    self.updateSprite(SpriteConstants.SHOOTING,
                                      SpriteConstants.LEFT)
                else:
                    self.updateSprite(SpriteConstants.SHOOTING,
                                      SpriteConstants.RIGHT)

            self.shooting -= 1
            self.shooting = max(0, self.shooting)

    def updateSprite(self, state, direction):
        self.image = self.spriteDefinition.getImage(state, direction)
示例#10
0
class Enemy(pygame.sprite.Sprite):
    def __init__(self, location, *groups):
        super(Enemy, self).__init__(*groups)
        self.spriteDefinition = SpriteDefinition(
            'evoman/images/EnemySprites.png', 0, 0, 43, 59)
        self.updateSprite(SpriteConstants.STANDING, SpriteConstants.LEFT)

        self.rect = pygame.rect.Rect(location, self.image.get_size())
        self.direction = -1
        self.max_life = 100
        self.life = self.max_life
        self.resting = 0
        self.dy = 0
        self.twists = []
        self.alternate = 1
        self.imune = 0
        self.timeenemy = 0
        self.hurt = 0
        self.shooting = 0
        self.gun_cooldown = 0

    def update(self, dt, game):

        if game.time == 1:
            # puts enemy in random initial position
            if game.randomini == 'yes':
                self.rect.x = numpy.random.choice([640, 500, 400, 300])

        # defines game mode for player action
        if game.enemymode == 'static':  # enemy controlled by static movements

            if self.timeenemy >= 120 and self.timeenemy <= 140:
                atack1 = 1
            else:
                atack1 = 0

            if self.timeenemy == 130:
                atack2 = 1
            else:
                atack2 = 0

            if self.timeenemy > 140:
                atack3 = 1
            else:
                atack3 = 0

            if self.timeenemy == 30:
                atack4 = 1
            else:
                atack4 = 0

        elif game.enemymode == 'ai':  # player controlled by AI algorithm

            # calls the controller providing game sensors
            actions = game.enemy_controller.control(self.sensors.get(game),
                                                    game.econt)
            if len(actions) < 4:
                game.print_logs(
                    "ERROR: Enemy 1 controller must return 4 decision variables."
                )
                sys.exit(0)

            atack1 = actions[0]
            atack2 = actions[1]
            atack3 = actions[2]
            atack4 = actions[3]

            if atack4 == 1 and not self.gun_cooldown:
                atack4 = 1
            else:
                atack4 = 0

        # if 'start game' is true
        if game.start == 1:

            self.timeenemy += 1  # increments enemy timer

            # copies last position state of the enemy
            last = self.rect.copy()

            # movements of the enemy on the axis x
            if atack1 == 1:

                self.rect.x += self.direction * 180 * dt  # goes forward
                # jumps
                if atack2 == 1 and self.resting == 1:
                    self.dy = -700
                    self.resting = 0

                # animation, running enemy images alternatetion
                if self.direction > 0:
                    direction = SpriteConstants.RIGHT
                else:
                    direction = SpriteConstants.LEFT

                if self.alternate == 1:
                    self.updateSprite(SpriteConstants.START_RUNNING, direction)
                if self.alternate == 4 or self.alternate == 10:
                    self.updateSprite(SpriteConstants.RUNNING_STEP1, direction)
                if self.alternate == 7:
                    self.updateSprite(SpriteConstants.RUNNING_STEP2, direction)

                self.alternate += 1
                if self.alternate > 12:
                    self.alternate = 1

                #  changes the image when enemy jumps
                if self.resting == 0:
                    if self.direction == -1:
                        self.updateSprite(SpriteConstants.JUMPING,
                                          SpriteConstants.LEFT)
                    else:
                        self.updateSprite(SpriteConstants.JUMPING,
                                          SpriteConstants.RIGHT)

            else:
                # animation, standing up images
                if self.direction == -1:
                    self.updateSprite(SpriteConstants.STANDING,
                                      SpriteConstants.LEFT)
                else:
                    self.updateSprite(SpriteConstants.STANDING,
                                      SpriteConstants.RIGHT)

            # restart enemy's timer from time to time, so that he stops moving.
            if atack3 == 1:
                self.timeenemy = 20
                # puts the enemy turned to the player's direction
                if game.enemymode == 'static':
                    if game.player.rect.right < self.rect.left:
                        self.direction = -1
                    elif game.player.rect.left > self.rect.right:
                        self.direction = 1
                else:
                    self.direction = self.direction * -1

            # checks collision of the player with the enemy
            if self.rect.colliderect(game.player.rect):

                # choses what sprite penalise according to config
                if game.contacthurt == "player":
                    game.player.life = max(0,
                                           game.player.life - (game.level * 1))
                if game.contacthurt == "enemy":
                    game.enemy.life = max(0,
                                          game.enemy.life - (game.level * 1))

                # pushes player when he collides with the enemy
                game.player.rect.x += self.direction * 50 * dt

                # limits the player to stand on the screem space even being pushed
                if game.player.rect.x < 60:
                    game.player.rect.x = 60
                if game.player.rect.x > 620:
                    game.player.rect.x = 620

                # sets flag to change the player image when he is hurt
                game.player.hurt = 5

            # gravity
            self.dy = min(400, self.dy + 100)
            self.rect.y += self.dy * dt

            # controls screen walls and platforms limits agaist enemy
            new = self.rect
            self.resting = 0
            for cell in game.tilemap.layers['triggers'].collide(
                    new, 'blockers'):

                blockers = cell['blockers']

                if 'l' in blockers and last.right <= cell.left and new.right > cell.left:
                    new.right = cell.left

                if 'r' in blockers and last.left >= cell.right and new.left < cell.right:
                    new.left = cell.right

                if 't' in blockers and last.bottom <= cell.top and new.bottom > cell.top:
                    self.resting = 1
                    new.bottom = cell.top
                    self.dy = 0

                if 'b' in blockers and last.top >= cell.bottom and new.top < cell.bottom:
                    new.top = cell.bottom

            # enemy shoots
            if atack4 == 1:

                self.shooting = 5
                self.gun_cooldown = 3

                # bullets sound effect
                if game.sound == "on" and game.playermode == "human":
                    sound = pygame.mixer.Sound('evoman/sounds/scifi011.wav')
                    c = pygame.mixer.Channel(3)
                    c.set_volume(10)
                    c.play(sound)

                # shoots 4 bullets placed in fixed places - bullets coming from the enemy.
                for i in range(0, 4):

                    ay = [-10, -10, 20, -45]

                    if self.direction > 0:
                        ax = [-24, 50, 1, 1]
                        self.twists.append(
                            Bullet_e3(
                                (self.rect.x + ax[i], self.rect.y - ay[i]), 1,
                                'h', len(self.twists), game.sprite_e))
                    else:
                        ax = [25, -50, -7, -7]
                        self.twists.append(
                            Bullet_e3(
                                (self.rect.x - ax[i], self.rect.y - ay[i]), -1,
                                'h', len(self.twists), game.sprite_e))

                # shoots 4 bullets placed in fixed places - bullets coming from the top of the screen
                aux = 100
                for i in range(0, 4):
                    self.twists.append(
                        Bullet_e3((aux, 100), 1, 'v', len(self.twists),
                                  game.sprite_e))
                    aux = aux + 150

            # decreases time for bullets limitation
            self.gun_cooldown = max(0, self.gun_cooldown - dt)

            # hurt enemy animation
            if self.hurt > 0:
                if self.direction == -1:
                    self.updateSprite(SpriteConstants.HURTING,
                                      SpriteConstants.LEFT)
                else:
                    self.updateSprite(SpriteConstants.HURTING,
                                      SpriteConstants.RIGHT)

            self.hurt -= 1

            # changes bullets images according to the enemy direction
            if self.shooting > 0:
                if self.direction == -1:
                    self.updateSprite(SpriteConstants.SHOOTING,
                                      SpriteConstants.LEFT)
                else:
                    self.updateSprite(SpriteConstants.SHOOTING,
                                      SpriteConstants.RIGHT)

            self.shooting -= 1
            self.shooting = max(0, self.shooting)

    def updateSprite(self, state, direction):
        self.image = self.spriteDefinition.getImage(state, direction)
示例#11
0
class Enemy(pygame.sprite.Sprite):
    def __init__(self, location, *groups):
        super(Enemy, self).__init__(*groups)
        self.spriteDefinition = SpriteDefinition(
            'evoman/images/EnemySprites.png', 0, 0, 43, 59)
        self.updateSprite(SpriteConstants.STANDING, SpriteConstants.LEFT)

        self.rect = pygame.rect.Rect(location, self.image.get_size())
        self.direction = -1
        self.max_life = 100
        self.life = self.max_life
        self.resting = 0
        self.dy = 0
        self.twists = []
        self.alternate = 1
        self.just_shoot = 0
        self.imune = 0
        self.timeenemy = 0
        self.hurt = 0
        self.shooting = 0
        self.gun_cooldown = 0

    def update(self, dt, game):

        if game.time == 1:
            # puts enemy in random initial position
            if game.randomini == 'yes':
                self.rect.x = numpy.random.choice([640, 500, 400, 300])

        # Defines game mode for player action.
        if game.enemymode == 'static':  # Enemy controlled by static movements.

            if self.timeenemy == 105:
                atack1 = 1
            else:
                atack1 = 0

            if ((abs(self.rect.left - game.player.rect.left) <= 1
                 or abs(self.rect.right - game.player.rect.right) <= 1)
                    or self.dy > 200):
                atack2 = 1
            else:
                atack2 = 0

            atack3 = 0

        elif game.enemymode == 'ai':  # Player controlled by AI algorithm.

            # calls the controller providing game sensors
            actions = game.enemy_controller.control(self.sensors.get(game),
                                                    game.econt)
            if len(actions) < 3:
                game.print_logs(
                    "ERROR: Enemy 1 controller must return 3 decision variables."
                )
                sys.exit(0)

            atack1 = actions[0]
            atack2 = actions[1]
            atack3 = actions[2]

            if atack2 == 1 and not self.gun_cooldown:
                atack2 = 1
            else:
                atack2 = 0

        # if the 'start game' marker is 1
        if game.start == 1:

            self.timeenemy += 1  # increments enemy timer

            last = self.rect.copy()  # copies last position state of the enemy

            # movements of the enemy on the axis x until a limit (turning aroud)

            if self.rect.left < 60:
                self.direction = self.direction * -1
                self.rect.left = 60
            if self.rect.right > 680:
                self.direction = self.direction * -1
                self.rect.right = 680

            # calculating the relative distance between enemy and player to set the jumping strengh
            aux_dist = (abs(game.player.rect.right - self.rect.right) /
                        490.0) + 0.1

            # when atacking, enemy may accelarate his movement.
            if self.dy < 0:
                self.rect.x += self.direction * (1500 * aux_dist) * dt
            else:
                self.rect.x += self.direction * 180 * dt

            #  jumps over the player. It happens from time to time, or when the player shoots.
            if ((self.resting == 1 and atack1 == 1)
                    or (self.resting == 1 and game.player.atacked == 1)):

                if game.enemymode == 'static':
                    # enemy turns to the players direction.
                    if game.player.rect.right <= self.rect.left:
                        self.direction = -1
                    if game.player.rect.left >= self.rect.right:
                        self.direction = 1

                # reinicializes enemy timer
                self.timeenemy = 0

                self.dy = -1500 * aux_dist
                self.resting = 0

            if atack3 == 1 and game.enemymode == 'ai':
                self.direction = self.direction * -1

            # throws a bullet over the player when enemy is jumping and right over him
            if self.resting == 0 and self.just_shoot == 0 and atack2 == 1:

                self.shooting = 5
                self.gun_cooldown = 3

                # bullets sound effect
                if game.sound == "on" and game.playermode == "human":
                    sound = pygame.mixer.Sound('evoman/sounds/scifi011.wav')
                    c = pygame.mixer.Channel(3)
                    c.set_volume(10)
                    c.play(sound)

                self.just_shoot = 1

                self.twists.append(
                    Bullet_e6((self.rect.x, self.rect.y), self.direction,
                              len(self.twists), game.sprite_e))

            self.gun_cooldown = max(
                0, self.gun_cooldown -
                dt)  # decreases time for bullets limitation

            # animation, running enemy images alternation
            if self.direction > 0:
                direction = SpriteConstants.RIGHT
            else:
                direction = SpriteConstants.LEFT

            if self.alternate == 1:
                self.updateSprite(SpriteConstants.START_RUNNING, direction)
            if self.alternate == 4 or self.alternate == 10:
                self.updateSprite(SpriteConstants.RUNNING_STEP1, direction)
            if self.alternate == 7:
                self.updateSprite(SpriteConstants.RUNNING_STEP2, direction)

            self.alternate += 1
            if self.alternate > 12:
                self.alternate = 1

            #  changes the image when enemy jumps
            if self.resting == 0:
                if self.direction == -1:
                    self.updateSprite(SpriteConstants.JUMPING,
                                      SpriteConstants.LEFT)
                else:
                    self.updateSprite(SpriteConstants.JUMPING,
                                      SpriteConstants.RIGHT)

            # checks collision of the player with the enemy
            if self.rect.colliderect(game.player.rect):

                # choses what sprite penalise according to config
                if game.contacthurt == "player":
                    game.player.life = max(
                        0, game.player.life - (game.level * 0.3))
                if game.contacthurt == "enemy":
                    game.enemy.life = max(0,
                                          game.enemy.life - (game.level * 0.3))

                game.player.rect.x += self.direction * 50 * dt  # pushes player when he collides with the enemy

                # limits the player to stand on the screen space even being pushed
                if game.player.rect.x < 60:
                    game.player.rect.x = 60
                if game.player.rect.x > 620:
                    game.player.rect.x = 620

                game.player.hurt = 5  # sets flag to change the player image when he is hurt

            #  gravity
            self.dy = min(400, self.dy + 100)
            self.rect.y += self.dy * dt

            # controls screen walls and platforms limits agaist enemy
            new = self.rect
            self.resting = 0
            for cell in game.tilemap.layers['triggers'].collide(
                    new, 'blockers'):

                blockers = cell['blockers']

                if 'l' in blockers and last.right <= cell.left and new.right > cell.left:
                    new.right = cell.left

                if 'r' in blockers and last.left >= cell.right and new.left < cell.right:
                    new.left = cell.right

                if 't' in blockers and last.bottom <= cell.top and new.bottom > cell.top:
                    self.resting = 1
                    new.bottom = cell.top
                    self.dy = 0
                    self.just_shoot = 0

                if 'b' in blockers and last.top >= cell.bottom and new.top < cell.bottom:
                    new.top = cell.bottom

            # hurt enemy animation
            if self.hurt > 0:
                if self.direction == -1:
                    self.updateSprite(SpriteConstants.HURTING,
                                      SpriteConstants.LEFT)
                else:
                    self.updateSprite(SpriteConstants.HURTING,
                                      SpriteConstants.RIGHT)

            self.hurt -= 1

            # changes bullets images according to the enemy direction
            if self.shooting > 0:
                if self.direction == -1:
                    self.updateSprite(SpriteConstants.SHOOTING,
                                      SpriteConstants.LEFT)
                else:
                    self.updateSprite(SpriteConstants.SHOOTING,
                                      SpriteConstants.RIGHT)

            self.shooting -= 1
            self.shooting = max(0, self.shooting)

    def updateSprite(self, state, direction):
        self.image = self.spriteDefinition.getImage(state, direction)
示例#12
0
class Enemy(pygame.sprite.Sprite):
    def __init__(self, location, *groups):
        super(Enemy, self).__init__(*groups)
        self.spriteDefinition = SpriteDefinition(
            'evoman/images/EnemySprites.png', 0, 0, 43, 59)
        self.updateSprite(SpriteConstants.STANDING, SpriteConstants.LEFT)

        self.rect = pygame.rect.Rect(location, self.image.get_size())
        self.direction = -1
        self.max_life = 100
        self.life = self.max_life
        self.resting = 0
        self.dy = 0
        self.twists = []
        self.alternate = 1
        self.fireflash = 0
        self.imune = 0
        self.rect.x = 550
        self.timeenemy = 0
        self.hurt = 0
        self.shooting = 0
        self.gun_cooldown = 0
        self.rect.right = 580

    def update(self, dt, game):

        if game.time == 1:
            # puts enemy in random initial position
            if game.randomini == 'yes':
                self.rect.x = numpy.random.choice([640, 500, 400, 300])

        # defines game mode for player action
        if game.enemymode == 'static':  # enemy controlled by static movements

            if self.timeenemy == 2:
                atack1 = 1
            else:
                atack1 = 0

            if self.timeenemy > 50:
                atack2 = 1
            else:
                atack2 = 0

            if self.timeenemy == 3:
                atack3 = 1
            else:
                atack3 = 0

            if (self.fireflash >= 1 and self.fireflash <= 40):
                atack4 = 1
            else:
                atack4 = 0

        elif game.enemymode == 'ai':  # player controlled by AI algorithm

            # calls the controller providing game sensors
            actions = game.enemy_controller.control(self.sensors.get(game),
                                                    game.econt)
            if len(actions) < 4:
                game.print_logs(
                    "ERROR: Enemy 1 controller must return 4 decision variables."
                )
                sys.exit(0)

            atack1 = actions[0]
            atack2 = actions[1]
            atack3 = actions[2]
            atack4 = actions[3]

            if atack3 == 1 and not self.gun_cooldown:
                atack3 = 1
            else:
                atack3 = 0

        # if the 'start game' marker is 1
        if game.start == 1:

            self.timeenemy += 1  # increments enemy timer

            last = self.rect.copy()  # copies last position state of the enemy

            # when player atacks, enemy turns into fire and goes towards his direction
            if game.player.atacked == 1 and self.fireflash == 0:
                self.fireflash = 100
            else:
                self.fireflash = max(0, self.fireflash - 1)

            if atack4 == 1:
                self.rect.x += self.direction * 600 * dt

                if self.fireflash == 1:
                    self.direction = self.direction * -1

                if self.rect.colliderect(game.player.rect):
                    self.fireflash = 0

            # otherwise he just keeps shooting towards the player direction
            elif self.fireflash == 0:

                if atack1 == 1 and self.resting == 1:
                    self.dy = -900
                    self.resting = 0

                self.imune = 0  # enemy is not imune to player's shooting anymore

                # images of the enemy standing up
                if self.direction == -1:
                    self.updateSprite(SpriteConstants.STANDING,
                                      SpriteConstants.LEFT)
                else:
                    self.updateSprite(SpriteConstants.STANDING,
                                      SpriteConstants.RIGHT)

            # reinicializes timer and turns to the players direction
            if atack2 == 1:
                self.timeenemy = 1

                if game.enemymode == 'static':
                    if game.player.rect.right < self.rect.left:
                        self.direction = -1
                    elif game.player.rect.left > self.rect.right:
                        self.direction = 1
                else:
                    self.direction = self.direction * -1

            # checks collision of the player with the enemy
            if self.rect.colliderect(game.player.rect):

                # choses what sprite penalise according to config
                if game.contacthurt == "player":
                    game.player.life = max(
                        0, game.player.life - (game.level * 0.3))
                if game.contacthurt == "enemy":
                    game.enemy.life = max(0,
                                          game.enemy.life - (game.level * 0.3))

                # pushes player when he collides with the enemy
                game.player.rect.x += self.direction * 50 * dt

                # limits the player to stand on the screen space even being pushed
                if game.player.rect.x < 60:
                    game.player.rect.x = 60
                if game.player.rect.x > 620:
                    game.player.rect.x = 620

                # sets flag to change the player image when he is hurt
                game.player.hurt = 5

            # gravity
            self.dy = min(400, self.dy + 100)
            self.rect.y += self.dy * dt

            # controls screen walls and platforms limits agaist enemy
            new = self.rect
            self.resting = 0
            for cell in game.tilemap.layers['triggers'].collide(
                    new, 'blockers'):

                blockers = cell['blockers']

                if 'l' in blockers and last.right <= cell.left and new.right > cell.left:
                    new.right = cell.left

                if 'r' in blockers and last.left >= cell.right and new.left < cell.right:
                    new.left = cell.right

                if 't' in blockers and last.bottom <= cell.top and new.bottom > cell.top:
                    self.resting = 1
                    new.bottom = cell.top
                    self.dy = 0

                if 'b' in blockers and last.top >= cell.bottom and new.top < cell.bottom:
                    new.top = cell.bottom

            # enemy shoots 3 bullets
            if atack3 == 1:

                self.shooting = 5

                self.gun_cooldown = 5

                # if enemy is not turned into fire, shoots, otherwise stops the time counter for a while.
                if self.fireflash == 0:

                    # bullets sound effect
                    if game.sound == "on" and game.playermode == "human":
                        sound = pygame.mixer.Sound(
                            'evoman/sounds/scifi011.wav')
                        c = pygame.mixer.Channel(3)
                        c.set_volume(10)
                        c.play(sound)

                    for i in range(0, 3):
                        self.twists.append(
                            Bullet_e4(
                                (self.rect.x, self.rect.y), self.direction, i,
                                len(self.twists), game.sprite_e))
                else:
                    self.timeenemy -= 1

            self.gun_cooldown = max(
                0, self.gun_cooldown -
                dt)  # decreases time for bullets limitation.

            # changes bullets images according to the enemy direction
            if self.shooting > 0:
                if self.direction == -1:
                    self.updateSprite(SpriteConstants.SHOOTING,
                                      SpriteConstants.LEFT)
                else:
                    self.updateSprite(SpriteConstants.SHOOTING,
                                      SpriteConstants.RIGHT)

            self.shooting -= 1
            self.shooting = max(0, self.shooting)

            #  changes the image when enemy is hurt and imune, as a fireball
            if self.imune == 1:
                if game.time % 2 == 0:
                    self.image = pygame.image.load(
                        'evoman/images/fireball.png')
                else:
                    self.image = pygame.image.load(
                        'evoman/images/fireball2.png')

            self.hurt -= 1

    def updateSprite(self, state, direction):
        self.image = self.spriteDefinition.getImage(state, direction)
示例#13
0
class Enemy(pygame.sprite.Sprite):


    def __init__(self, location, *groups):
        super(Enemy, self).__init__(*groups)
        self.spriteDefinition = SpriteDefinition('evoman/images/EnemySprites.png', 0, 0, 43, 59)
        self.updateSprite(SpriteConstants.STANDING, SpriteConstants.LEFT)

        self.rect = pygame.rect.Rect(location, self.image.get_size())
        self.direction = -1
        self.max_life = 100
        self.life = self.max_life
        self.resting = 0
        self.dy = 0
        self.alternate = 1
        self.just_shoot = 0
        self.imune = 0
        self.timeenemy = 0
        self.twists = []
        self.hurt = 0
        self.shooting = 0
        self.gun_cooldown = 0


    def update(self, dt, game):


        if game.time==1:

            # puts enemy in random initial position
            if game.randomini == 'yes':
                self.rect.x = numpy.random.choice([640,500,400,300])

        # defines game mode for player action
        if game.enemymode == 'static': # enemy controlled by static movements

            if (self.timeenemy >= 1 and self.timeenemy <10)  or (self.timeenemy >= 20 and self.timeenemy <30):
                atack1 = 1
            else:
                atack1 = 0

            if self.timeenemy == 1  or self.timeenemy == 20:
                atack2 = 1
            else:
                atack2 = 0

            if self.timeenemy == 9  or self.timeenemy == 29:
                atack3 = 1
            else:
                atack3 = 0

            if self.timeenemy >=40 and self.timeenemy <50:
                atack4 = 1
            else:
                atack4 = 0

            if self.timeenemy  == 50:
                atack5 = 1
            else:
                atack5 = 0

            if ( (abs(self.rect.left-game.player.rect.left)<=200 or abs(self.rect.right-game.player.rect.right)<=200)  ) and not self.gun_cooldown:
                atack6 = 1
            else:
                atack6 = 0



        elif game.enemymode == 'ai': # player controlled by AI algorithm



            # calls the controller providing game sensors
            actions = game.enemy_controller.control(self.sensors.get(game), game.econt)
            if len(actions) < 6:
                game.print_logs("ERROR: Enemy 1 controller must return 6 decision variables.")
                sys.exit(0)

            atack1 = actions[0]
            atack2 = actions[1]
            atack3 = actions[2]
            atack4 = actions[3]
            atack5 = actions[4]
            atack6 = actions[5]



            if atack6 == 1 and not self.gun_cooldown:
                atack6 = 1
            else:
                atack6 = 0


        # If the 'start game' marker is 1.
        if game.start == 1:

            self.timeenemy += 1 # increments enemy timer

            last = self.rect.copy() # copies last position state of the enemy

            # jumps over the player. It happens twice.
            if  atack1== 1 :
                # moves on the axis x
                self.rect.x += self.direction * 730 * dt
                if self.resting == 1 and atack2 == 1:
                    self.dy = -900
                    self.resting = 0

                # enemy turns to the players direction
                if atack3 == 1:

                    if game.enemymode == 'static':
                        if game.player.rect.right < self.rect.left:
                            self.direction = -1
                        if game.player.rect.left > self.rect.right:
                            self.direction = 1
                    else:
                        self.direction = self.direction * -1

            # runs in the player's direction, after jumping twice
            elif atack4 == 1 :
                self.rect.x += self.direction * 900 * dt

            # reinicializes enemy timer
            elif atack5 == 1:
                self.timeenemy = 0

            # throws a bullet over the player when enemy is jumping and right over him
            if self.resting == 0 and self.just_shoot == 0 and atack6 == 1:

                self.shooting = 5

                self.gun_cooldown = 5

                # bullets sound effect
                if game.sound == "on" and game.playermode == "human":
                    sound = pygame.mixer.Sound('evoman/sounds/scifi011.wav')
                    c = pygame.mixer.Channel(3)
                    c.set_volume(10)
                    c.play(sound)

                self.just_shoot = 1

                rand = 3
                # shoots from 1 to 3 bullets
                for i in range(0,rand):
                    self.twists.append(Bullet_e8((self.rect.x+(i*60) ,self.rect.y ), i, self.direction, len(self.twists), game.sprite_e))

            # decreases time for bullets limitation
            self.gun_cooldown = max(0, self.gun_cooldown - dt)

            # animation, running enemy images alternation
            if self.direction > 0:
                direction = SpriteConstants.RIGHT
            else:
                direction = SpriteConstants.LEFT

            if self.alternate == 1:
                self.updateSprite(SpriteConstants.START_RUNNING, direction)
            if self.alternate == 4 or self.alternate == 10:
                self.updateSprite(SpriteConstants.RUNNING_STEP1, direction)
            if self.alternate == 7:
                self.updateSprite(SpriteConstants.RUNNING_STEP2, direction)

            self.alternate += 1
            if self.alternate > 12:
                self.alternate = 1

            #  changes the image when enemy jumps
            if self.resting == 0:
               if self.direction == -1:
                   self.updateSprite(SpriteConstants.JUMPING, SpriteConstants.LEFT)
               else:
                   self.updateSprite(SpriteConstants.JUMPING, SpriteConstants.RIGHT)

            # checks collision of the player with the enemy
            if self.rect.colliderect(game.player.rect):

                # choses what sprite penalise according to config
                if game.contacthurt == "player":
                    game.player.life = max(0, game.player.life-(game.level*0.3))
                if game.contacthurt == "enemy":
                    game.enemy.life = max(0, game.enemy.life-(game.level*0.3))

                # sets flag to change the player image when he is hurt
                game.player.hurt = 5

            #  gravity
            self.dy = min(400, self.dy + 100)
            self.rect.y += self.dy * dt

            # controls screen walls and platforms limits agaist enemy
            new = self.rect
            self.resting = 0
            for cell in game.tilemap.layers['triggers'].collide(new, 'blockers'):

                blockers = cell['blockers']

                if 't' in blockers and last.bottom <= cell.top and new.bottom > cell.top:
                    self.resting = 1
                    new.bottom = cell.top
                    self.dy = 0
                    self.just_shoot = 0

                if 'b' in blockers and last.top >= cell.bottom and new.top < cell.bottom:
                    new.top = cell.bottom

                if 'l' in blockers and last.right <= cell.left and new.right > cell.left  and last.bottom>cell.top:
                    new.right = cell.left

                if 'r' in blockers and last.left >= cell.right and new.left < cell.right   and last.bottom>cell.top:
                    new.left = cell.right

            # hurt enemy animation
            if self.hurt > 0:
                if self.direction == -1:
                   self.updateSprite(SpriteConstants.HURTING, SpriteConstants.LEFT)
                else:
                   self.updateSprite(SpriteConstants.HURTING, SpriteConstants.RIGHT)

            self.hurt -=1

            # changes bullets images according to the enemy direction
            if self.shooting > 0:
                if self.direction == -1:
                    self.updateSprite(SpriteConstants.SHOOTING, SpriteConstants.LEFT)
                else:
                    self.updateSprite(SpriteConstants.SHOOTING, SpriteConstants.RIGHT)

            self.shooting -= 1
            self.shooting = max(0,self.shooting)


    def updateSprite(self, state, direction):
        self.image = self.spriteDefinition.getImage(state, direction)
示例#14
0
class Enemy(pygame.sprite.Sprite):




    def __init__(self, location, *groups):
        super(Enemy, self).__init__(*groups)
        self.spriteDefinition = SpriteDefinition('evoman/images/EnemySprites.png', 0, 0, 43, 59)
        self.updateSprite(SpriteConstants.STANDING, SpriteConstants.LEFT) 

        self.rect = pygame.rect.Rect(location, self.image.get_size())
        self.direction = -1
        self.max_life = 100
        self.life = self.max_life
        self.resting = 0
        self.dy = 0
        self.alternate = 1
        self.direction_floor = 1
        self.imune = 0
        self.move = 0
        self.countmove = 0
        self.rect.x = 500
        self.timeenemy = 0
        self.twists = []
        self.hurt = 0
        self.shooting = 0
        self.gun_cooldown = 0

 


    def update(self, dt, game):
                

        if game.time==1:
            # puts enemy in random initial position
            if game.randomini == 'yes':
                self.rect.x = numpy.random.choice([640,500,400,300])

        # Defines game mode for player action.
        if game.enemymode == 'static': # Enemy controlled by static movements.
 
            if self.resting == 1 and self.timeenemy >= 95 and self.timeenemy <= 110:
                atack1 = 1
            else:
                atack1 = 0

            if self.resting == 0 :
                atack2 = 1
            else:
                atack2 = 0

            if (game.player.rect.right < game.enemy.rect.left and  abs(game.player.rect.right - game.enemy.rect.left) <= 50 ) or (game.enemy.rect.right < game.player.rect.left and abs(game.enemy.rect.right - game.player.rect.left) <= 50):
                atack3 = 1
            else:
                atack3 = 0


        elif game.enemymode == 'ai': # Player controlled by AI algorithm.

 

            # calls the controller providing game sensors 
            actions = game.enemy_controller.control(self.sensors.get(game), game.econt)  
            if len(actions) < 3:
                game.print_logs("ERROR: Enemy 1 controller must return 3 decision variables.")
                sys.exit(0)  
          
            atack1 = actions[0]
            atack2 = actions[1]
            atack3 = actions[2]
 


            if atack2 == 1 and not self.gun_cooldown:
                atack2 = 1
            else:
                atack2 = 0


        # if the 'start game' marker is 1 
        if game.start == 1:

            
            self.timeenemy += 1 # increments enemy timer 

            # moving floor, changes the movement direction from time to time. 
            for cell in game.tilemap.layers['triggers'].collide(game.player.rect, 'blockers'): 
	 
                blockers = cell['blockers']
                if 't' in blockers:
                     
                    game.player.rect.x += self.direction_floor * 100 * dt # moves player over the moving floor

                    # limits player inside the screen
                    if game.player.rect.left < 60:
                        game.player.rect.left = 61	
                    if game.player.rect.right > 665:
                        game.player.rect.right = 665

                if game.time%120 == 0:
                    self.direction_floor = self.direction_floor *-1                    

            
            last = self.rect.copy() # copies last position state of the enemy 	
 
            # if player gets too close to the enemy, he jumps to the other side.  
            if (self.resting == 1 and atack3 == 1):
                self.move = 1 
                self.dy = -900
                self.resting = 0

            if self.move == 1:
                self.rect.x += self.direction * 900 * dt

            if self.move == 1 and self.rect.x<200:
                self.rect.x = 200
                self.direction = self.direction  * -1
                self.move = 0
            if self.move == 1 and  self.rect.x>500:
                self.rect.x = 500
                self.direction = self.direction  * -1
                self.move = 0


            # jumps from time to time or when player atacks 
            if ( (self.resting == 1 and atack1 == 1) or ( self.resting == 1 and game.player.atacked == 1)):
                self.dy = -900
                self.resting = 0 

            # releases until 4 bullets (decided randomly) after jumping or when player atacks  
            if (atack2 == 1 and not self.gun_cooldown) :

                self.shooting = 5

                self.gun_cooldown = 3

                # bullets sound effect 
                if game.sound == "on" and game.playermode == "human":
                    sound = pygame.mixer.Sound('evoman/sounds/scifi011.wav')
                    c = pygame.mixer.Channel(3)
                    c.set_volume(10)
                    c.play(sound)
                
                aux = numpy.random.randint(1,4,1)     
                for i in range(0,aux):
                    self.twists.append(Bullet_e5((self.rect.x + (self.direction*(i*30)) ,self.rect.top + (self.direction*(i*20))  ), self.direction, game.player.rect , len(self.twists), game.sprite_e))
 
                    
                self.timeenemy = 0 # reinicializes enemy timer 

            if game.player.atacked == 1:
                # bullets sound effect 
                if game.sound == "on" and game.playermode == "human":
                    sound = pygame.mixer.Sound('evoman/sounds/scifi011.wav')
                    c = pygame.mixer.Channel(3)
                    c.set_volume(10)
                    c.play(sound)
                  
                self.twists.append(Bullet_e5((self.rect.x ,self.rect.top ), self.direction, game.player.rect , len(self.twists), game.sprite_e))

    
            self.gun_cooldown = max(0, self.gun_cooldown - dt)   # decreases time for bullets limitation 
  
            # animation, running enemy images alternation.
            if self.direction > 0:
                direction = SpriteConstants.RIGHT
            else:
                direction = SpriteConstants.LEFT

            if self.alternate == 1:
                self.updateSprite(SpriteConstants.START_RUNNING, direction)
            if self.alternate == 4 or self.alternate == 10:
                self.updateSprite(SpriteConstants.RUNNING_STEP1, direction)
            if self.alternate == 7:
                self.updateSprite(SpriteConstants.RUNNING_STEP2, direction)
                  
            self.alternate += 1
            if self.alternate > 12:
                self.alternate = 1    

            #  changes the image when enemy jumps 
            if self.resting == 0:
                if self.direction == -1:
                   self.updateSprite(SpriteConstants.JUMPING, SpriteConstants.LEFT)
                else:
                   self.updateSprite(SpriteConstants.JUMPING, SpriteConstants.RIGHT)
   
            # checks collision of the player with the enemy 
            if self.rect.colliderect(game.player.rect):
                
                # choses what sprite penalise according to config
                if game.contacthurt == "player":
                    game.player.life = max(0, game.player.life-(game.level*0.3))
                if game.contacthurt == "enemy":
                    game.enemy.life = max(0, game.enemy.life-(game.level*0.3))

                # pushes player when he collides with the enemy 
                game.player.rect.x +=  self.direction *  50 * dt  

                # limits the player to stand on the screen space even being pushed.
                if game.player.rect.x < 60:
                    game.player.rect.x = 60
                if game.player.rect.x > 620:
                    game.player.rect.x = 620 

            # gravity 
            self.dy = min(400, self.dy + 100)
            self.rect.y += self.dy * dt

            # controls screen walls and platforms limits agaist enemy 
            new = self.rect
            self.resting = 0
            for cell in game.tilemap.layers['triggers'].collide(new, 'blockers'): 
	
                blockers = cell['blockers']
    
                if 'l' in blockers and last.right <= cell.left and new.right > cell.left:
                    new.right = cell.left
 
                if 'r' in blockers and last.left >= cell.right and new.left < cell.right:
                    new.left = cell.right
                		
                if 't' in blockers and last.bottom <= cell.top and new.bottom > cell.top:
                    self.resting = 1
                    new.bottom = cell.top
                    self.dy = 0
                    
                if 'b' in blockers and last.top >= cell.bottom and new.top < cell.bottom:
                    new.top = cell.bottom 

            # hurt enemy animation 
            if self.hurt > 0:
                if self.direction == -1:
                   self.updateSprite(SpriteConstants.HURTING, SpriteConstants.LEFT)
                else:
                   self.updateSprite(SpriteConstants.HURTING, SpriteConstants.RIGHT)

            self.hurt -=1

           # changes bullets images according to the enemy direction 
            if self.shooting > 0:
                if self.direction == -1:
                    self.updateSprite(SpriteConstants.SHOOTING, SpriteConstants.LEFT)
                else:
                    self.updateSprite(SpriteConstants.SHOOTING, SpriteConstants.RIGHT)
 
            self.shooting -= 1
            self.shooting = max(0,self.shooting)
           
    def updateSprite(self, state, direction):
        self.image = self.spriteDefinition.getImage(state, direction) 
示例#15
0
class Player(pygame.sprite.Sprite):



    def __init__(self, location, enemyn, level, *groups):
        super(Player, self).__init__(*groups)


        self.spriteDefinition = SpriteDefinition('evoman/images/EvoManSprites.png', 0, 0, 43, 59)
        self.updateSprite(SpriteConstants.STANDING, SpriteConstants.RIGHT) 
           
        self.rect = pygame.rect.Rect(location, self.image.get_size())
        self.resting = 0
        self.dy = 0
        self.direction = 1
        self.alternate = 1
        self.gun_cooldown = 0
        self.max_life = 100
        self.life = self.max_life
        self.atacked = 0
        self.hurt = 0
        self.shooting = 0
        self.inwater = 0
        self.twists = []
        self.vx = 0
        self.vy = 0
        self.hy = 0
        self.sensors = None
 

       	   
    def update(self, dt, game):
 
         
        # if the enemies are not atacking with the freezing atack (prevents player from making any movements or atacking) and also the 'start game' marker is 1.
        if game.freeze_p == 0  and game.start == 1:

            # checks water environment flag to regulate movements speed 
            if self.inwater == 1:
                self.vx = 0.5
                self.vy = 0.5
                self.hy = -2000
            else: 
                self.vx = 1
                self.vy = 1
                self.hy = -900

            # defines game mode for player action 

            if game.playermode == 'human': # player controlled by keyboard/joystick  

                # if joystick is connected, initializes it.
                if game.joy > 0:
                    joystick = pygame.joystick.Joystick(0)
                    joystick.init()

                # tests if the button/key was pressed or released.

                press = 0
                release = 0
                for event in  game.event:                    
                    if event.type == pygame.JOYBUTTONDOWN or event.type == pygame.KEYDOWN:
                        press = 1
                    else:
                        press = 0

                    if event.type == pygame.JOYBUTTONUP or event.type == pygame.KEYUP:
                        release = 1
                    else:
                        release = 0
 
                # gets pressed key value 
                key = pygame.key.get_pressed()
 
                # gets joystick value for axis x (left/right) 
                left = 0
                if game.joy > 0:
                    if round(joystick.get_axis(0)) == -1:
                        left = 1
                if key[pygame.K_LEFT]:
                    left = 1
           
                right = 0
                if game.joy > 0:
                    if round(joystick.get_axis(0)) == 1:
                        right = 1
                if key[pygame.K_RIGHT]:
                    right  = 1

                # gets joystick/key value for jumping  
                jump = 0
                if game.joy > 0:
                    if int(joystick.get_button(2)) == 1 and press == 1:
                            jump = 1
                if key[pygame.K_SPACE] and press == 1:
                    jump = 1
       
                # gets joystick/key value for shooting  
                shoot = 0
                if game.joy > 0: 
                    if int(joystick.get_button(3)) == 1 and press == 1: 
                            shoot = 1
                if key[pygame.K_LSHIFT] and press == 1:
                    shoot = 1

            elif game.playermode == 'ai': # player controlled by AI algorithm 


                # calls the controller providing game sensors 
                
                actions = game.player_controller.control(self.sensors.get(game), game.pcont)  
                if len(actions) < 5:
                    game.print_logs("ERROR: Player controller must return 5 decision variables.")
                    sys.exit(0)  
          
                left = actions[0]
                right = actions[1]
                jump = actions[2]
                shoot = actions[3]
                release = actions[4]   

            # if the button is released before the jumping maximum height, them player stops going up. 
            if release == 1 and self.resting == 0:
                self.dy = 0

            # copies last position state of the player 
            last = self.rect.copy()   

            # movements on the axis x (left)                                         
            if left:

                self.rect.x -= 200 * dt * self.vx
                self.direction = -1

                # animation, running images alternation 
                if self.alternate == 1:       
                    self.updateSprite(SpriteConstants.START_RUNNING, SpriteConstants.LEFT)
                if self.alternate == 4 or self.alternate == 10:
                    self.updateSprite(SpriteConstants.RUNNING_STEP1, SpriteConstants.LEFT)
                if self.alternate == 7:
                    self.updateSprite(SpriteConstants.RUNNING_STEP2, SpriteConstants.LEFT)
                    
                self.alternate += 1
                if self.alternate > 12:
                    self.alternate = 1   
             
            # movements on the axis x (right) 
            elif right:

                self.rect.x += 200 * dt * self.vx
                self.direction = 1

                # animation, running player images alternation 
                if self.alternate == 1:       
                    self.updateSprite(SpriteConstants.START_RUNNING, SpriteConstants.RIGHT)
                if self.alternate == 4 or self.alternate == 10:
                    self.updateSprite(SpriteConstants.RUNNING_STEP1, SpriteConstants.RIGHT)
                if self.alternate == 7:
                    self.updateSprite(SpriteConstants.RUNNING_STEP2, SpriteConstants.RIGHT)
                    
                self.alternate += 1
                if self.alternate > 12:
                    self.alternate = 1     
         

            else:
                # animation, standing up images 
                if self.direction == -1:
                    self.updateSprite(SpriteConstants.STANDING, SpriteConstants.LEFT)
                else:
                    self.updateSprite(SpriteConstants.STANDING, SpriteConstants.RIGHT) 
 

            # if player is touching the floor, he is allowed to jump 
            if  self.resting == 1  and jump == 1:      
                    self.dy =  self.hy 
 
            # gravity      
            self.dy = min(400, self.dy + 100)
            self.rect.y += self.dy * dt * self.vy

            #  changes the image when player jumps 
            if self.resting == 0 :       
                if self.direction == -1:
                    self.updateSprite(SpriteConstants.JUMPING, SpriteConstants.LEFT)
                else:
                    self.updateSprite(SpriteConstants.JUMPING, SpriteConstants.RIGHT)
 
             
            new = self.rect # copies new (after movement) position state of the player
            
            # controls screen walls and platforms limits agaist player 
            self.resting = 0
            for cell in game.tilemap.layers['triggers'].collide(new, 'blockers'): 

                blockers = cell['blockers']

                if 'l' in blockers and last.right <= cell.left and new.right > cell.left and last.bottom>cell.top:
                    new.right = cell.left

                if 'r' in blockers and last.left >= cell.right and new.left < cell.right and last.bottom>cell.top:
                    new.left = cell.right

                if 't' in blockers and last.bottom <= cell.top and new.bottom > cell.top:
                    self.resting = 1 # player touches the floor 
                    new.bottom = cell.top
                    self.dy = 0

                if 'b' in blockers and last.top >= cell.bottom and new.top < cell.bottom:
                    new.top = cell.bottom

            # shoots, limiting time between bullets. 
            if  shoot == 1 and not self.gun_cooldown: 

                self.shooting = 5              
                self.atacked = 1 # marks if the player has atacked enemy 


                # creates bullets objects according to the direction. 
                if self.direction > 0:
                    self.twists.append(Bullet_p(self.rect.midright, 1, len(self.twists), game.sprite_p))
                    
                else:
                     self.twists.append(Bullet_p(self.rect.midleft, -1, len(self.twists), game.sprite_p)) 
        
                self.gun_cooldown = 0.4 # marks time to the bullet for allowing next bullets 

                # sound effects 
                if game.sound == "on" and game.playermode == "human":
                    sound = pygame.mixer.Sound('evoman/sounds/scifi003.wav')
                    c = pygame.mixer.Channel(2)
                    c.set_volume(1)
                    c.play(sound)

            else:
                self.atacked = 0 
 
            
 
            # decreases time for limitating bullets 
            self.gun_cooldown = max(0, self.gun_cooldown - dt)
 
            # hurt player animation 
            if self.hurt > 0:  
                if self.direction == -1:     
                    self.updateSprite(SpriteConstants.HURTING, SpriteConstants.LEFT)
                else:
                    self.updateSprite(SpriteConstants.HURTING, SpriteConstants.RIGHT)

            self.hurt -= 1
            self.hurt = max(0,self.hurt)
            self.shooting -= 1
            self.shooting = max(0,self.shooting)
            
            # shooting animation 
            if self.shooting > 0:
                if self.resting == 0:
                    if self.direction == -1:
                        self.updateSprite(SpriteConstants.SHOOTING_JUMPING, SpriteConstants.LEFT)
                    else:
                        self.updateSprite(SpriteConstants.SHOOTING_JUMPING, SpriteConstants.RIGHT)
                else:
                    if self.direction == -1:
                        self.updateSprite(SpriteConstants.SHOOTING, SpriteConstants.LEFT)
                    else:
                        self.updateSprite(SpriteConstants.SHOOTING, SpriteConstants.RIGHT)

            # kills player in case he touches killers stuff, like spikes.
            for cell in game.tilemap.layers['triggers'].collide(self.rect, 'killers'):
                game.player.life = 0

            # focuses screen center on player 
            game.tilemap.set_focus(new.x, new.y)
        else:
            game.tilemap.set_focus(self.rect.x, self.rect.y)



    def updateSprite(self, state, direction):
        self.image = self.spriteDefinition.getImage(state, direction)
示例#16
0
class Enemy(pygame.sprite.Sprite):


    def __init__(self, location, *groups):

        super(Enemy, self).__init__(*groups)

        self.spriteDefinition = SpriteDefinition('evoman/images/EnemySprites.png', 0, 0, 43, 59)
        self.updateSprite(SpriteConstants.STANDING, SpriteConstants.LEFT) 

        self.rect = pygame.rect.Rect(location, self.image.get_size())
        self.direction = -1
        self.max_life = 100
        self.life = self.max_life	
        self.resting = 0
        self.dy = 0
        self.twists = []
        self.alternate = 1
        self.imune = 0        
        self.timeenemy = 0
        self.hurt = 0
        self.shooting = 0
        self.gun_cooldown = 0

     

    def update(self, dt, game):
  


        if game.time==1:
            # puts enemy in random initial position
            if game.randomini == 'yes':
                self.rect.x = numpy.random.choice([630,610,560,530])  


        # defines game mode for player action. 
        if game.enemymode == 'static': # enemy controlled by static movements 
 
            if (self.timeenemy >= 210  and self.timeenemy <= 250) or (self.timeenemy >= 260  and self.timeenemy <= 300):
                atack1 = 1
            else:
                atack1 = 0

            if self.timeenemy == 210 or self.timeenemy == 260:
                atack2 = 1
            else:
                atack2 = 0

            if self.timeenemy> 300:
                atack3 = 1
            else:
                atack3 = 0

            if (self.timeenemy == 40) or (self.timeenemy == 110) or (self.timeenemy == 180):
                atack4 = 1
            else:
                atack4 = 0


        elif game.enemymode == 'ai': # player controlled by AI algorithm 
 

            # calls the controller providing game sensors 
            actions = game.enemy_controller.control(self.sensors.get(game), game.econt)  
            if len(actions) < 4:
                game.print_logs("ERROR: Enemy 1 controller must return 4 decision variables.")
                sys.exit(0)  
          
            atack1 = actions[0]
            atack2 = actions[1]
            atack3 = actions[2]
            atack4 = actions[3]


            if atack4 == 1 and not self.gun_cooldown:
                atack4 = 1
            else:
                atack4 = 0

            if atack1 == 1 and self.resting == 1:
                atack1 = 1
            else:
                atack1 = 0


        # if the 'start game' marker is 1 
        if game.start == 1:	

            # increments enemy timer 
            self.timeenemy += 1

            # copies last position state of the enemy 	
            last = self.rect.copy()

            # movements of the enemy on the axis x. Happens 2 to each side.
            if atack1 == 1  :
                self.rect.x += self.direction * 200 * dt 
  
                # jumps 
                if atack2 == 1: 
                    self.dy = -900
                    self.resting = 0  
       
               # animation, running enemy images alternatetion.
                if self.direction > 0:
                    direction = SpriteConstants.RIGHT
                else:
                    direction = SpriteConstants.LEFT

                if self.alternate == 1:
                    self.updateSprite(SpriteConstants.START_RUNNING, direction)
                if self.alternate == 4 or self.alternate == 10:
                    self.updateSprite(SpriteConstants.RUNNING_STEP1, direction)
                if self.alternate == 7:
                    self.updateSprite(SpriteConstants.RUNNING_STEP2, direction)
                    
                self.alternate += 1
                if self.alternate > 12:
                    self.alternate = 1    

                #  changes the image when enemy jumps 
                if self.resting == 0:
                   if self.direction == -1:
                       self.updateSprite(SpriteConstants.JUMPING, SpriteConstants.LEFT)
                   else:
                       self.updateSprite(SpriteConstants.JUMPING, SpriteConstants.RIGHT)

            else: 
                # animation, standing up images 
                if self.direction == -1:
                    self.updateSprite(SpriteConstants.STANDING, SpriteConstants.LEFT)
                else:
                    self.updateSprite(SpriteConstants.STANDING, SpriteConstants.RIGHT)

            # restart enemy timer and turns the enemy around             
            if atack3 == 1:
                self.timeenemy = 0
                self.direction = self.direction * -1
   
            # checks collision of the player with the enemy 
            if self.rect.colliderect(game.player.rect):
 
                # choses what sprite penalise according to config
                if game.contacthurt == "player":
                    game.player.life = max(0, game.player.life-(game.level*1))
                if game.contacthurt == "enemy":
                    game.enemy.life = max(0, game.enemy.life-(game.level*1))

                game.player.hurt = 5 # sets flag to change the player image when he is hurt.
 
            # gravity  
            self.dy = min(400, self.dy + 100)
            self.rect.y += self.dy * dt

            # controls screen walls and platforms limits agaist enemy.
            new = self.rect
            self.resting = 0
            for cell in game.tilemap.layers['triggers'].collide(new, 'blockers'): 

                blockers = cell['blockers']
    
                if 'l' in blockers and last.right <= cell.left and new.right > cell.left:
                    new.right = cell.left
 
                if 'r' in blockers and last.left >= cell.right and new.left < cell.right:
                    new.left = cell.right

                if 't' in blockers and last.bottom <= cell.top and new.bottom > cell.top:
                    self.resting = 1
                    new.bottom = cell.top
                    self.dy = 0
                    
                if 'b' in blockers and last.top >= cell.bottom and new.top < cell.bottom:
                    new.top = cell.bottom 
            
            # enemy shoots 
            if atack4 == 1:

                self.shooting = 5

                self.gun_cooldown = 3

                 # bullets sound effect 
                if game.sound == "on" and game.playermode == "human":
                    sound = pygame.mixer.Sound('evoman/sounds/scifi011.wav')
                    c = pygame.mixer.Channel(3)
                    c.set_volume(10)
                    c.play(sound)

            
                # shoots 6 bullets placed in a fixed range  
                for i in range (0,6):          
                    self.twists.append(Bullet_e2((self.rect.x+10,self.rect.bottom), self.direction ,i, len(self.twists), game.sprite_e))
             
            # decreases time for bullets limitation 
            self.gun_cooldown = max(0, self.gun_cooldown - dt)
  
            # hurt enemy animation 
            if self.hurt > 0:
                if self.direction == -1:
                   self.updateSprite(SpriteConstants.HURTING, SpriteConstants.LEFT)
                else:
                   self.updateSprite(SpriteConstants.HURTING, SpriteConstants.RIGHT)

            self.hurt -=1

            # changes bullets images according to the enemy direction 
            if self.shooting > 0:
                if self.direction == -1:
                    self.updateSprite(SpriteConstants.SHOOTING, SpriteConstants.LEFT)
                else:
                    self.updateSprite(SpriteConstants.SHOOTING, SpriteConstants.RIGHT)
 
            self.shooting -= 1
            self.shooting = max(0,self.shooting)


           
    def updateSprite(self, state, direction):
        self.image = self.spriteDefinition.getImage(state, direction)
示例#17
0
class Enemy(pygame.sprite.Sprite):




    def __init__(self, location, *groups):
        super(Enemy, self).__init__(*groups)
        self.spriteDefinition = SpriteDefinition('evoman/images/EnemySprites.png', 0, 0, 43, 59)
        self.updateSprite(SpriteConstants.STANDING, SpriteConstants.LEFT) 

        self.rect = pygame.rect.Rect(location, self.image.get_size())
        self.direction = -1
        self.max_life = 100
        self.life = self.max_life
        self.resting = 0
        self.dy = 0
        self.twists = []
        self.alternate = 1
        self.just_shoot = 0
        self.imune = 0
        self.timeenemy = 0
        self.hurt = 0
        self.shooting = 0
        self.gun_cooldown = 0

        
        

    def update(self, dt, game):
                

        if game.time==1:
            # puts enemy in random initial position
            if game.randomini == 'yes':
                self.rect.x = numpy.random.choice([640,500,400,300])


        # Defines game mode for player action.
        if game.enemymode == 'static': # Enemy controlled by static movements.
 
            if self.timeenemy == 105:
                atack1 = 1
            else:
                atack1 = 0

            if ( (abs(self.rect.left-game.player.rect.left)<=1 or abs(self.rect.right-game.player.rect.right)<=1)  or self.dy>200  ):
                atack2 = 1
            else:
                atack2 = 0
 

            atack3 = 0


        elif game.enemymode == 'ai': # Player controlled by AI algorithm.
 

            # calls the controller providing game sensors 
            actions = game.enemy_controller.control(self.sensors.get(game), game.econt)  
            if len(actions) < 3:
                game.print_logs("ERROR: Enemy 1 controller must return 3 decision variables.")
                sys.exit(0)  
          
            atack1 = actions[0]
            atack2 = actions[1]
            atack3 = actions[2]
 


            if atack2 == 1 and not self.gun_cooldown:
                atack2 = 1
            else:
                atack2 = 0


        # if the 'start game' marker is 1 
        if game.start == 1:
            
            self.timeenemy += 1 # increments enemy timer 
            	
            last = self.rect.copy() # copies last position state of the enemy 

            # movements of the enemy on the axis x until a limit (turning aroud) 
            
            if self.rect.left<60: 
                self.direction = self.direction  * -1
                self.rect.left = 60
            if self.rect.right>680: 
                self.direction = self.direction  * -1
                self.rect.right = 680
             
            # calculating the relative distance between enemy and player to set the jumping strengh 
            aux_dist = (abs(game.player.rect.right - self.rect.right)/490.0)+0.1
 
            # when atacking, enemy may accelarate his movement.
            if self.dy<0:
                self.rect.x += self.direction * (1500 *aux_dist) * dt   
            else:       
                self.rect.x += self.direction * 180 * dt 

            #  jumps over the player. It happens from time to time, or when the player shoots.  
            if ((self.resting == 1 and atack1 == 1) or ( self.resting == 1 and game.player.atacked == 1)):   

                if game.enemymode == 'static':
                    # enemy turns to the players direction.     
                    if game.player.rect.right <= self.rect.left:
                        self.direction = -1
                    if game.player.rect.left >= self.rect.right:
                        self.direction = 1

                # reinicializes enemy timer 
                self.timeenemy = 0

                self.dy = -1500 * aux_dist
                self.resting = 0

            if atack3 == 1 and game.enemymode == 'ai':
                 self.direction = self.direction * -1

            # throws a bullet over the player when enemy is jumping and right over him 
            if self.resting == 0 and self.just_shoot == 0 and atack2 == 1:

                self.shooting = 5 
                self.gun_cooldown = 3

                # bullets sound effect 
                if game.sound == "on" and game.playermode == "human":
                    sound = pygame.mixer.Sound('evoman/sounds/scifi011.wav')
                    c = pygame.mixer.Channel(3)
                    c.set_volume(10)
                    c.play(sound)

                self.just_shoot = 1
               
                self.twists.append(Bullet_e6((self.rect.x ,self.rect.y ), self.direction, len(self.twists), game.sprite_e))

            
            self.gun_cooldown = max(0, self.gun_cooldown - dt) # decreases time for bullets limitation 

            # animation, running enemy images alternation 
            if self.direction > 0:
                direction = SpriteConstants.RIGHT
            else:
                direction = SpriteConstants.LEFT

            if self.alternate == 1:
                self.updateSprite(SpriteConstants.START_RUNNING, direction)
            if self.alternate == 4 or self.alternate == 10:
                self.updateSprite(SpriteConstants.RUNNING_STEP1, direction)
            if self.alternate == 7:
                self.updateSprite(SpriteConstants.RUNNING_STEP2, direction)
                   
            self.alternate += 1
            if self.alternate > 12:
                self.alternate = 1    

            #  changes the image when enemy jumps 
            if self.resting == 0:
               if self.direction == -1:
                   self.updateSprite(SpriteConstants.JUMPING, SpriteConstants.LEFT)
               else:
                   self.updateSprite(SpriteConstants.JUMPING, SpriteConstants.RIGHT)

            # checks collision of the player with the enemy 
            if self.rect.colliderect(game.player.rect):
                
                # choses what sprite penalise according to config
                if game.contacthurt == "player":
                    game.player.life = max(0, game.player.life-(game.level*0.3))
                if game.contacthurt == "enemy":
                    game.enemy.life = max(0, game.enemy.life-(game.level*0.3))
                
                game.player.rect.x +=  self.direction *  50 * dt   # pushes player when he collides with the enemy 
 
                # limits the player to stand on the screen space even being pushed 
                if game.player.rect.x < 60:
                    game.player.rect.x = 60
                if game.player.rect.x > 620:
                    game.player.rect.x = 620
                 
                game.player.hurt = 5  # sets flag to change the player image when he is hurt 

            #  gravity  
            self.dy = min(400, self.dy + 100)
            self.rect.y += self.dy * dt
           
            # controls screen walls and platforms limits agaist enemy 
            new = self.rect
            self.resting = 0
            for cell in game.tilemap.layers['triggers'].collide(new, 'blockers'): 
	
                blockers = cell['blockers']
    
                if 'l' in blockers and last.right <= cell.left and new.right > cell.left:
                    new.right = cell.left
 
                if 'r' in blockers and last.left >= cell.right and new.left < cell.right:
                    new.left = cell.right
                		
                if 't' in blockers and last.bottom <= cell.top and new.bottom > cell.top:
                    self.resting = 1
                    new.bottom = cell.top
                    self.dy = 0
                    self.just_shoot = 0
                    
                if 'b' in blockers and last.top >= cell.bottom and new.top < cell.bottom:
                    new.top = cell.bottom 

            # hurt enemy animation 
            if self.hurt > 0:
                if self.direction == -1:
                   self.updateSprite(SpriteConstants.HURTING, SpriteConstants.LEFT)
                else:
                   self.updateSprite(SpriteConstants.HURTING, SpriteConstants.RIGHT)

            self.hurt -=1

            # changes bullets images according to the enemy direction 
            if self.shooting > 0:
                if self.direction == -1:
                    self.updateSprite(SpriteConstants.SHOOTING, SpriteConstants.LEFT)
                else:
                    self.updateSprite(SpriteConstants.SHOOTING, SpriteConstants.RIGHT)
 
            self.shooting -= 1
            self.shooting = max(0,self.shooting)

 
    def updateSprite(self, state, direction):
        self.image = self.spriteDefinition.getImage(state, direction)
示例#18
0
class Enemy(pygame.sprite.Sprite):


    def __init__(self, location, *groups):
             
        super(Enemy, self).__init__(*groups)

        self.spriteDefinition = SpriteDefinition('evoman/images/EnemySprites.png', 0, 0, 43, 59)
        self.updateSprite(SpriteConstants.STANDING, SpriteConstants.LEFT)

        self.rect = pygame.rect.Rect(location, self.image.get_size())
        self.direction = -1
        self.max_life = 100
        self.life = self.max_life	
        self.resting = 0
        self.dy = 0
        self.time_colis = 0
        self.alternate = 1
        self.imune = 0
        self.timeenemy = 0
        self.twists = []
        self.hurt = 0
        self.shooting = 1
        self.gun_cooldown = 0
        self.gun_cooldown2 = 0

 
   
        
    def update(self, dt, game):


        if game.time==1:
            # puts enemy in random initial position
            if game.randomini == 'yes':
                self.rect.x = numpy.random.choice([640,500,400,300])  


        # increments enemy timer 
        if game.start == 1:
            self.timeenemy += 1

        # defines game mode for player action 
        if game.enemymode == 'static': #  controlled by static movements 

            atack1 = 1

            if self.timeenemy >= 200 and self.timeenemy < 260:
                atack2 = 1
            else:
                atack2 = 0

            if self.timeenemy == 220:
                atack3 = 1
            else:
                atack3 = 0

            atack4 = 1


        elif game.enemymode == 'ai': # enemy controlled by AI algorithm 
 

            # calls the controller providing game sensors 
            actions = game.enemy_controller.control(self.sensors.get(game), game.econt)  
            if len(actions) < 4:
                game.print_logs("ERROR: Enemy 1 controller must return 4 decision variables.")
                sys.exit(0)  
          
            atack1 = actions[0]
            atack2 = actions[1]
            atack3 = actions[2]
            atack4 = actions[3] 


            # applies attack rules
            if atack2 == 1 and not self.gun_cooldown:
                atack2 = 1
            else:
                atack2 = 0

            if atack3 == 1 and not self.gun_cooldown2:
                atack3 = 1
            else:
                atack3 = 0



        # if the enemy is not atacking with the feezing atack (prevents player from making any movements) and also the 'start game' marker is 1.
        if game.freeze_e == 0 and game.start == 1:
             	
            last = self.rect.copy()# copies last position state of the enemy 
            
            if atack1 == 1:
                # moves the enemy on the axis x  
                self.rect.x += self.direction * 100 * dt

                # chases player, switching direction as he moves.
                if atack4 == 1:	
                    
                    if game.enemymode == 'static':
                        if game.player.rect.right < self.rect.left:
                            self.direction = -1   
                        elif game.player.rect.left > self.rect.right:
                             self.direction = 1
                    else:
                        self.direction = self.direction * -1 
       
            # animation, running enemy images alternation.
            if self.direction > 0:
                direction = SpriteConstants.RIGHT
            else:
                direction = SpriteConstants.LEFT

            if self.alternate == 1:
                self.updateSprite(SpriteConstants.START_RUNNING, direction)
            if self.alternate == 4 or self.alternate == 10:
                self.updateSprite(SpriteConstants.RUNNING_STEP1, direction)
            if self.alternate == 7:
                self.updateSprite(SpriteConstants.RUNNING_STEP2, direction)
                    
            self.alternate += 1
            if self.alternate > 12:
                self.alternate = 1     

            # checks collision of the player with the enemy 
            if self.rect.colliderect(game.player.rect):
                
                # sprite loses life points, according to the difficult level of the game (the more difficult, the more it loses).

                # choses what sprite penalise according to config
                if game.contacthurt == "player":
                    game.player.life = max(0, game.player.life-(game.level*1))
                if game.contacthurt == "enemy":
                    game.enemy.life = max(0, game.enemy.life-(game.level*1))
 
                # counts duration of the collision to jump from time to time during the collision 
                self.time_colis += 1
                if self.time_colis > 15:
                    self.time_colis = 0
                    self.dy = -600
                 
                # sets flag to change the player image when he is hurt 
                game.player.hurt = 5
 

            # gravity     
            self.dy = min(400, self.dy + 100)
            self.rect.y += self.dy * dt
            
            # controls screen walls and platforms limits towards enemy 
            new = self.rect
            self.resting = 0
            for cell in game.tilemap.layers['triggers'].collide(new, 'blockers'): 

                blockers = cell['blockers']
                		
                if 't' in blockers and last.bottom <= cell.top and new.bottom > cell.top:
                    self.resting = 1
                    new.bottom = cell.top
                    self.dy = 0
                    
                if 'b' in blockers and last.top >= cell.bottom and new.top < cell.bottom:
                    new.top = cell.bottom 
             
                if 'l' in blockers and last.right <= cell.left and new.right > cell.left  and last.bottom>cell.top:
                    new.right = cell.left
                    # Jumps when finds a wall in the middle plataforms.
                    if new.left<600:
                        self.dy = -600 

                if 'r' in blockers and last.left >= cell.right and new.left < cell.right and last.bottom>cell.top:
                    new.left = cell.right
                    # Jumps when finds a wall in the middle plataforms.
                    if new.left>29:
                        self.dy = -600

            #  Changes the image when enemy jumps.
            if self.resting == 0:
               if self.direction == -1:
                   self.updateSprite(SpriteConstants.JUMPING, SpriteConstants.LEFT)
               else:
                   self.updateSprite(SpriteConstants.JUMPING, SpriteConstants.RIGHT)

            # Hurt enemy animation.            
            if self.hurt > 0:
                if self.direction == -1:
                   self.updateSprite(SpriteConstants.HURTING, SpriteConstants.LEFT)
                else:
                   self.updateSprite(SpriteConstants.HURTING, SpriteConstants.RIGHT)

            self.hurt -=1


        # Enemy atack: freezes the player (preeveting him from making any movements or atacking) and also himself from moving. Freenzing endures according to the timer.
        if atack2 == 1:

            self.gun_cooldown = 6

            game.freeze_p = 1
            game.freeze_e = 1

        # Enemy shooting after freezing.
        if  atack3 == 1:

            self.shooting = 5 

            self.gun_cooldown2 = 6

            # Bullets sound effect.
            if game.sound == "on" and game.playermode == "human":

                sound = pygame.mixer.Sound('evoman/sounds/scifi011.wav')
                c = pygame.mixer.Channel(3)
                c.set_volume(10)
                c.play(sound)
            
            # Shoots 8 bullets placed in a fixed range with a little random variation in their position (x and y). 


            for i in range (0,8):
                rand = numpy.array([30,20,10,15,9,25,18,5])
                rand2 = numpy.array([1,2,3,4,5,2,4,3])
            
                rand = rand[i]
                rand2 = rand2[i]

                # Start position of the bullets vary according to the position of the enemy.
                if self.direction > 0:
                    self.twists.append(Bullet_e1((self.rect.x+(i*rand),self.rect.y+10+(i*rand2)), 1, len(self.twists), game.sprite_e))
                else:
                    self.twists.append(Bullet_e1((self.rect.x-(i*rand)+46,self.rect.y+10+(i*rand2)), -1, len(self.twists), game.sprite_e))

        # Decreases time for bullets and freezing limitation.
        self.gun_cooldown = max(0, self.gun_cooldown - dt)
        self.gun_cooldown2 = max(0, self.gun_cooldown2 - dt)

        # Changes bullets images according to the enemy direction.
        if self.shooting > 0:
            if self.direction == -1:
                self.updateSprite(SpriteConstants.SHOOTING, SpriteConstants.LEFT)
            else:
                self.updateSprite(SpriteConstants.SHOOTING, SpriteConstants.RIGHT)

        self.shooting -= 1
        self.shooting = max(0,self.shooting)

        # Releases movement.
        if  self.gun_cooldown <= 5:
            game.freeze_p = 0
            game.freeze_e = 0
 
        # Reinicializes enemy atacking timer.
        if self.timeenemy == 260:
            self.timeenemy = 0

    def updateSprite(self, state, direction):
        self.image = self.spriteDefinition.getImage(state, direction)
示例#19
0
class Player(pygame.sprite.Sprite):
    def __init__(self, location, enemyn, level, *groups):
        super(Player, self).__init__(*groups)

        self.spriteDefinition = SpriteDefinition(
            'evoman/images/EvoManSprites.png', 0, 0, 43, 59)
        self.updateSprite(SpriteConstants.STANDING, SpriteConstants.RIGHT)

        self.rect = pygame.rect.Rect(location, self.image.get_size())
        self.resting = 0
        self.dy = 0
        self.direction = 1
        self.alternate = 1
        self.gun_cooldown = 0
        self.max_life = 100
        self.life = self.max_life
        self.atacked = 0
        self.hurt = 0
        self.shooting = 0
        self.inwater = 0
        self.twists = []
        self.vx = 0
        self.vy = 0
        self.hy = 0
        self.sensors = None

    def update(self, dt, game):

        # if the enemies are not atacking with the freezing atack (prevents player from making any movements or atacking) and also the 'start game' marker is 1.
        if game.freeze_p == 0 and game.start == 1:

            # checks water environment flag to regulate movements speed
            if self.inwater == 1:
                self.vx = 0.5
                self.vy = 0.5
                self.hy = -2000
            else:
                self.vx = 1
                self.vy = 1
                self.hy = -900

            # defines game mode for player action

            if game.playermode == 'human':  # player controlled by keyboard/joystick

                # if joystick is connected, initializes it.
                if game.joy > 0:
                    joystick = pygame.joystick.Joystick(0)
                    joystick.init()

                # tests if the button/key was pressed or released.

                press = 0
                release = 0
                for event in game.event:
                    if event.type == pygame.JOYBUTTONDOWN or event.type == pygame.KEYDOWN:
                        press = 1
                    else:
                        press = 0

                    if event.type == pygame.JOYBUTTONUP or event.type == pygame.KEYUP:
                        release = 1
                    else:
                        release = 0

                # gets pressed key value
                key = pygame.key.get_pressed()

                # gets joystick value for axis x (left/right)
                left = 0
                if game.joy > 0:
                    if round(joystick.get_axis(0)) == -1:
                        left = 1
                if key[pygame.K_LEFT]:
                    left = 1

                right = 0
                if game.joy > 0:
                    if round(joystick.get_axis(0)) == 1:
                        right = 1
                if key[pygame.K_RIGHT]:
                    right = 1

                # gets joystick/key value for jumping
                jump = 0
                if game.joy > 0:
                    if int(joystick.get_button(2)) == 1 and press == 1:
                        jump = 1
                if key[pygame.K_SPACE] and press == 1:
                    jump = 1

                # gets joystick/key value for shooting
                shoot = 0
                if game.joy > 0:
                    if int(joystick.get_button(3)) == 1 and press == 1:
                        shoot = 1
                if key[pygame.K_LSHIFT] and press == 1:
                    shoot = 1

            elif game.playermode == 'ai':  # player controlled by AI algorithm

                # calls the controller providing game sensors

                actions = game.player_controller.control(
                    self.sensors.get(game), game.pcont)
                if len(actions) < 5:
                    game.print_logs(
                        "ERROR: Player controller must return 5 decision variables."
                    )
                    sys.exit(0)

                left = actions[0]
                right = actions[1]
                jump = actions[2]
                shoot = actions[3]
                release = actions[4]

            # if the button is released before the jumping maximum height, them player stops going up.
            if release == 1 and self.resting == 0:
                self.dy = 0

            # copies last position state of the player
            last = self.rect.copy()

            # movements on the axis x (left)
            if left:

                self.rect.x -= 200 * dt * self.vx
                self.direction = -1

                # animation, running images alternation
                if self.alternate == 1:
                    self.updateSprite(SpriteConstants.START_RUNNING,
                                      SpriteConstants.LEFT)
                if self.alternate == 4 or self.alternate == 10:
                    self.updateSprite(SpriteConstants.RUNNING_STEP1,
                                      SpriteConstants.LEFT)
                if self.alternate == 7:
                    self.updateSprite(SpriteConstants.RUNNING_STEP2,
                                      SpriteConstants.LEFT)

                self.alternate += 1
                if self.alternate > 12:
                    self.alternate = 1

            # movements on the axis x (right)
            elif right:

                self.rect.x += 200 * dt * self.vx
                self.direction = 1

                # animation, running player images alternation
                if self.alternate == 1:
                    self.updateSprite(SpriteConstants.START_RUNNING,
                                      SpriteConstants.RIGHT)
                if self.alternate == 4 or self.alternate == 10:
                    self.updateSprite(SpriteConstants.RUNNING_STEP1,
                                      SpriteConstants.RIGHT)
                if self.alternate == 7:
                    self.updateSprite(SpriteConstants.RUNNING_STEP2,
                                      SpriteConstants.RIGHT)

                self.alternate += 1
                if self.alternate > 12:
                    self.alternate = 1

            else:
                # animation, standing up images
                if self.direction == -1:
                    self.updateSprite(SpriteConstants.STANDING,
                                      SpriteConstants.LEFT)
                else:
                    self.updateSprite(SpriteConstants.STANDING,
                                      SpriteConstants.RIGHT)

            # if player is touching the floor, he is allowed to jump
            if self.resting == 1 and jump == 1:
                self.dy = self.hy

            # gravity
            self.dy = min(400, self.dy + 100)
            self.rect.y += self.dy * dt * self.vy

            #  changes the image when player jumps
            if self.resting == 0:
                if self.direction == -1:
                    self.updateSprite(SpriteConstants.JUMPING,
                                      SpriteConstants.LEFT)
                else:
                    self.updateSprite(SpriteConstants.JUMPING,
                                      SpriteConstants.RIGHT)

            new = self.rect  # copies new (after movement) position state of the player

            # controls screen walls and platforms limits agaist player
            self.resting = 0
            for cell in game.tilemap.layers['triggers'].collide(
                    new, 'blockers'):

                blockers = cell['blockers']

                if 'l' in blockers and last.right <= cell.left and new.right > cell.left and last.bottom > cell.top:
                    new.right = cell.left

                if 'r' in blockers and last.left >= cell.right and new.left < cell.right and last.bottom > cell.top:
                    new.left = cell.right

                if 't' in blockers and last.bottom <= cell.top and new.bottom > cell.top:
                    self.resting = 1  # player touches the floor
                    new.bottom = cell.top
                    self.dy = 0

                if 'b' in blockers and last.top >= cell.bottom and new.top < cell.bottom:
                    new.top = cell.bottom

            # shoots, limiting time between bullets.
            if shoot == 1 and not self.gun_cooldown:

                self.shooting = 5
                self.atacked = 1  # marks if the player has atacked enemy

                # creates bullets objects according to the direction.
                if self.direction > 0:
                    self.twists.append(
                        Bullet_p(self.rect.midright, 1, len(self.twists),
                                 game.sprite_p))

                else:
                    self.twists.append(
                        Bullet_p(self.rect.midleft, -1, len(self.twists),
                                 game.sprite_p))

                self.gun_cooldown = 0.4  # marks time to the bullet for allowing next bullets

                # sound effects
                if game.sound == "on" and game.playermode == "human":
                    sound = pygame.mixer.Sound('evoman/sounds/scifi003.wav')
                    c = pygame.mixer.Channel(2)
                    c.set_volume(1)
                    c.play(sound)

            else:
                self.atacked = 0

            # decreases time for limitating bullets
            self.gun_cooldown = max(0, self.gun_cooldown - dt)

            # hurt player animation
            if self.hurt > 0:
                if self.direction == -1:
                    self.updateSprite(SpriteConstants.HURTING,
                                      SpriteConstants.LEFT)
                else:
                    self.updateSprite(SpriteConstants.HURTING,
                                      SpriteConstants.RIGHT)

            self.hurt -= 1
            self.hurt = max(0, self.hurt)
            self.shooting -= 1
            self.shooting = max(0, self.shooting)

            # shooting animation
            if self.shooting > 0:
                if self.resting == 0:
                    if self.direction == -1:
                        self.updateSprite(SpriteConstants.SHOOTING_JUMPING,
                                          SpriteConstants.LEFT)
                    else:
                        self.updateSprite(SpriteConstants.SHOOTING_JUMPING,
                                          SpriteConstants.RIGHT)
                else:
                    if self.direction == -1:
                        self.updateSprite(SpriteConstants.SHOOTING,
                                          SpriteConstants.LEFT)
                    else:
                        self.updateSprite(SpriteConstants.SHOOTING,
                                          SpriteConstants.RIGHT)

            # kills player in case he touches killers stuff, like spikes.
            for cell in game.tilemap.layers['triggers'].collide(
                    self.rect, 'killers'):
                game.player.life = 0

            # focuses screen center on player
            game.tilemap.set_focus(new.x, new.y)
        else:
            game.tilemap.set_focus(self.rect.x, self.rect.y)

    def updateSprite(self, state, direction):
        self.image = self.spriteDefinition.getImage(state, direction)
示例#20
0
class Enemy(pygame.sprite.Sprite):


    def __init__(self, location, *groups):
        super(Enemy, self).__init__(*groups)
        self.spriteDefinition = SpriteDefinition('evoman/images/EnemySprites.png', 0, 0, 43, 59)
        self.updateSprite(SpriteConstants.STANDING, SpriteConstants.LEFT) 

        self.rect = pygame.rect.Rect(location, self.image.get_size())
        self.direction = -1
        self.max_life = 100
        self.life = self.max_life	
        self.resting = 0
        self.dy = 0
        self.twists = []
        self.alternate = 1
        self.imune = 0
        self.timeenemy = 0
        self.hurt = 0
        self.shooting = 0
        self.gun_cooldown = 0

         

    def update(self, dt, game):


        if game.time==1:
            # puts enemy in random initial position
            if game.randomini == 'yes':
                self.rect.x = numpy.random.choice([640,500,400,300])

        # defines game mode for player action 
        if game.enemymode == 'static': # enemy controlled by static movements 
 
            if self.timeenemy >= 120  and self.timeenemy <= 140:
                atack1 = 1
            else:
                atack1 = 0

            if self.timeenemy == 130:
                atack2 = 1
            else:
                atack2 = 0

            if self.timeenemy> 140:
                atack3 = 1
            else:
                atack3 = 0

            if self.timeenemy == 30:
                atack4 = 1
            else:
                atack4 = 0


        elif game.enemymode == 'ai': # player controlled by AI algorithm 
 

            # calls the controller providing game sensors 
            actions = game.enemy_controller.control(self.sensors.get(game), game.econt)  
            if len(actions) < 4:
                game.print_logs("ERROR: Enemy 1 controller must return 4 decision variables.")
                sys.exit(0)  
          
            atack1 = actions[0]
            atack2 = actions[1]
            atack3 = actions[2]
            atack4 = actions[3]


            if atack4 == 1 and not self.gun_cooldown:
                atack4 = 1
            else:
                atack4 = 0

        
        # if 'start game' is true 
        if game.start == 1:	
            
            self.timeenemy += 1 # increments enemy timer 

            
            # copies last position state of the enemy 	
            last = self.rect.copy()

            # movements of the enemy on the axis x  
            if atack1 == 1:
                
                self.rect.x += self.direction * 180 * dt  # goes forward 
                # jumps  
                if atack2 == 1 and self.resting == 1: 
                    self.dy = -700
                    self.resting = 0
  
                # animation, running enemy images alternatetion 
                if self.direction > 0:
                    direction = SpriteConstants.RIGHT
                else:
                    direction = SpriteConstants.LEFT

                if self.alternate == 1:
                    self.updateSprite(SpriteConstants.START_RUNNING, direction)
                if self.alternate == 4 or self.alternate == 10:
                    self.updateSprite(SpriteConstants.RUNNING_STEP1, direction)
                if self.alternate == 7:
                    self.updateSprite(SpriteConstants.RUNNING_STEP2, direction)
                    
                self.alternate += 1
                if self.alternate > 12:
                    self.alternate = 1    

                #  changes the image when enemy jumps 
                if self.resting == 0:
                   if self.direction == -1:
                       self.updateSprite(SpriteConstants.JUMPING, SpriteConstants.LEFT)
                   else:
                       self.updateSprite(SpriteConstants.JUMPING, SpriteConstants.RIGHT) 

            else: 
                # animation, standing up images 
                if self.direction == -1:
                   self.updateSprite(SpriteConstants.STANDING, SpriteConstants.LEFT)
                else:
                   self.updateSprite(SpriteConstants.STANDING, SpriteConstants.RIGHT)

                           
            # restart enemy's timer from time to time, so that he stops moving.    
            if atack3 == 1:
                self.timeenemy = 20
                # puts the enemy turned to the player's direction 
                if game.enemymode == 'static':
                    if game.player.rect.right < self.rect.left:
                        self.direction = -1   
                    elif game.player.rect.left > self.rect.right:
                        self.direction = 1
                else:
                    self.direction = self.direction * -1

            # checks collision of the player with the enemy 
            if self.rect.colliderect(game.player.rect):
                
                # choses what sprite penalise according to config
                if game.contacthurt == "player":
                    game.player.life = max(0, game.player.life-(game.level*1))
                if game.contacthurt == "enemy":
                    game.enemy.life = max(0, game.enemy.life-(game.level*1))

                # pushes player when he collides with the enemy 
                game.player.rect.x +=  self.direction *  50 * dt
  
                # limits the player to stand on the screem space even being pushed 
                if game.player.rect.x < 60:
                    game.player.rect.x = 60
                if game.player.rect.x > 620:
                    game.player.rect.x = 620

                # sets flag to change the player image when he is hurt 
                game.player.hurt = 5
  
            # gravity
            self.dy = min(400, self.dy + 100)
            self.rect.y += self.dy * dt

            # controls screen walls and platforms limits agaist enemy 
            new = self.rect
            self.resting = 0
            for cell in game.tilemap.layers['triggers'].collide(new, 'blockers'): 
	
                blockers = cell['blockers']
    
                if 'l' in blockers and last.right <= cell.left and new.right > cell.left:
                    new.right = cell.left
 
                if 'r' in blockers and last.left >= cell.right and new.left < cell.right:
                    new.left = cell.right
                		
                if 't' in blockers and last.bottom <= cell.top and new.bottom > cell.top:
                    self.resting = 1
                    new.bottom = cell.top
                    self.dy = 0
                    
                if 'b' in blockers and last.top >= cell.bottom and new.top < cell.bottom:
                    new.top = cell.bottom 


            # enemy shoots 
            if atack4 == 1:

                self.shooting = 5
                self.gun_cooldown = 3

                # bullets sound effect 
                if game.sound == "on" and game.playermode == "human":
                    sound = pygame.mixer.Sound('evoman/sounds/scifi011.wav')
                    c = pygame.mixer.Channel(3)
                    c.set_volume(10)
                    c.play(sound)
 
                # shoots 4 bullets placed in fixed places - bullets coming from the enemy.
                for i in range (0,4): 

                    ay = [-10,-10,20,-45]

                    if self.direction > 0:
                        ax = [-24,50,1,1]
                        self.twists.append(Bullet_e3((self.rect.x+ax[i],self.rect.y-ay[i]), 1, 'h', len(self.twists), game.sprite_e))
                    else:
                        ax = [25,-50,-7,-7]
                        self.twists.append(Bullet_e3((self.rect.x-ax[i],self.rect.y-ay[i]), -1, 'h', len(self.twists), game.sprite_e))

                # shoots 4 bullets placed in fixed places - bullets coming from the top of the screen
                aux = 100
                for i in range (0,4):   
                    self.twists.append(Bullet_e3((aux,100), 1, 'v',len(self.twists),game.sprite_e))
                    aux = aux + 150
            
            # decreases time for bullets limitation 
            self.gun_cooldown = max(0, self.gun_cooldown - dt)

            # hurt enemy animation 
            if self.hurt > 0:
                if self.direction == -1:
                   self.updateSprite(SpriteConstants.HURTING, SpriteConstants.LEFT)
                else:
                   self.updateSprite(SpriteConstants.HURTING, SpriteConstants.RIGHT)

            self.hurt -=1

            # changes bullets images according to the enemy direction 
            if self.shooting > 0:
                if self.direction == -1:
                    self.updateSprite(SpriteConstants.SHOOTING, SpriteConstants.LEFT)
                else:
                    self.updateSprite(SpriteConstants.SHOOTING, SpriteConstants.RIGHT)
 
            self.shooting -= 1
            self.shooting = max(0,self.shooting)


    def updateSprite(self, state, direction):
        self.image = self.spriteDefinition.getImage(state, direction)
class Enemy(pygame.sprite.Sprite):
    def __init__(self, location, *groups):

        super(Enemy, self).__init__(*groups)

        self.spriteDefinition = SpriteDefinition(
            'evoman/images/EnemySprites.png', 0, 0, 43, 59)
        self.updateSprite(SpriteConstants.STANDING, SpriteConstants.LEFT)

        self.rect = pygame.rect.Rect(location, self.image.get_size())
        self.direction = -1
        self.max_life = 100
        self.life = self.max_life
        self.resting = 0
        self.dy = 0
        self.time_colis = 0
        self.alternate = 1
        self.imune = 0
        self.timeenemy = 0
        self.twists = []
        self.hurt = 0
        self.shooting = 1
        self.gun_cooldown = 0
        self.gun_cooldown2 = 0

    def update(self, dt, game):

        if game.time == 1:
            # puts enemy in random initial position
            if game.randomini == 'yes':
                self.rect.x = numpy.random.choice([640, 500, 400, 300])

        # increments enemy timer
        if game.start == 1:
            self.timeenemy += 1

        # defines game mode for player action
        if game.enemymode == 'static':  #  controlled by static movements

            atack1 = 1

            if self.timeenemy >= 200 and self.timeenemy < 260:
                atack2 = 1
            else:
                atack2 = 0

            if self.timeenemy == 220:
                atack3 = 1
            else:
                atack3 = 0

            atack4 = 1

        elif game.enemymode == 'ai':  # enemy controlled by AI algorithm

            # calls the controller providing game sensors
            actions = game.enemy_controller.control(self.sensors.get(game),
                                                    game.econt)
            if len(actions) < 4:
                game.print_logs(
                    "ERROR: Enemy 1 controller must return 4 decision variables."
                )
                sys.exit(0)

            atack1 = actions[0]
            atack2 = actions[1]
            atack3 = actions[2]
            atack4 = actions[3]

            # applies attack rules
            if atack2 == 1 and not self.gun_cooldown:
                atack2 = 1
            else:
                atack2 = 0

            if atack3 == 1 and not self.gun_cooldown2:
                atack3 = 1
            else:
                atack3 = 0

        # if the enemy is not atacking with the feezing atack (prevents player from making any movements) and also the 'start game' marker is 1.
        if game.freeze_e == 0 and game.start == 1:

            last = self.rect.copy()  # copies last position state of the enemy

            if atack1 == 1:
                # moves the enemy on the axis x
                self.rect.x += self.direction * 100 * dt

                # chases player, switching direction as he moves.
                if atack4 == 1:

                    if game.enemymode == 'static':
                        if game.player.rect.right < self.rect.left:
                            self.direction = -1
                        elif game.player.rect.left > self.rect.right:
                            self.direction = 1
                    else:
                        self.direction = self.direction * -1

            # animation, running enemy images alternation.
            if self.direction > 0:
                direction = SpriteConstants.RIGHT
            else:
                direction = SpriteConstants.LEFT

            if self.alternate == 1:
                self.updateSprite(SpriteConstants.START_RUNNING, direction)
            if self.alternate == 4 or self.alternate == 10:
                self.updateSprite(SpriteConstants.RUNNING_STEP1, direction)
            if self.alternate == 7:
                self.updateSprite(SpriteConstants.RUNNING_STEP2, direction)

            self.alternate += 1
            if self.alternate > 12:
                self.alternate = 1

            # checks collision of the player with the enemy
            if self.rect.colliderect(game.player.rect):

                # sprite loses life points, according to the difficult level of the game (the more difficult, the more it loses).

                # choses what sprite penalise according to config
                if game.contacthurt == "player":
                    game.player.life = max(0,
                                           game.player.life - (game.level * 1))
                if game.contacthurt == "enemy":
                    game.enemy.life = max(0,
                                          game.enemy.life - (game.level * 1))

                # counts duration of the collision to jump from time to time during the collision
                self.time_colis += 1
                if self.time_colis > 15:
                    self.time_colis = 0
                    self.dy = -600

                # sets flag to change the player image when he is hurt
                game.player.hurt = 5

            # gravity
            self.dy = min(400, self.dy + 100)
            self.rect.y += self.dy * dt

            # controls screen walls and platforms limits towards enemy
            new = self.rect
            self.resting = 0
            for cell in game.tilemap.layers['triggers'].collide(
                    new, 'blockers'):

                blockers = cell['blockers']

                if 't' in blockers and last.bottom <= cell.top and new.bottom > cell.top:
                    self.resting = 1
                    new.bottom = cell.top
                    self.dy = 0

                if 'b' in blockers and last.top >= cell.bottom and new.top < cell.bottom:
                    new.top = cell.bottom

                if 'l' in blockers and last.right <= cell.left and new.right > cell.left and last.bottom > cell.top:
                    new.right = cell.left
                    # Jumps when finds a wall in the middle plataforms.
                    if new.left < 600:
                        self.dy = -600

                if 'r' in blockers and last.left >= cell.right and new.left < cell.right and last.bottom > cell.top:
                    new.left = cell.right
                    # Jumps when finds a wall in the middle plataforms.
                    if new.left > 29:
                        self.dy = -600

            #  Changes the image when enemy jumps.
            if self.resting == 0:
                if self.direction == -1:
                    self.updateSprite(SpriteConstants.JUMPING,
                                      SpriteConstants.LEFT)
                else:
                    self.updateSprite(SpriteConstants.JUMPING,
                                      SpriteConstants.RIGHT)

            # Hurt enemy animation.
            if self.hurt > 0:
                if self.direction == -1:
                    self.updateSprite(SpriteConstants.HURTING,
                                      SpriteConstants.LEFT)
                else:
                    self.updateSprite(SpriteConstants.HURTING,
                                      SpriteConstants.RIGHT)

            self.hurt -= 1

        # Enemy atack: freezes the player (preeveting him from making any movements or atacking) and also himself from moving. Freenzing endures according to the timer.
        if atack2 == 1:

            self.gun_cooldown = 6

            game.freeze_p = 1
            game.freeze_e = 1

        # Enemy shooting after freezing.
        if atack3 == 1:

            self.shooting = 5

            self.gun_cooldown2 = 6

            # Bullets sound effect.
            if game.sound == "on" and game.playermode == "human":

                sound = pygame.mixer.Sound('evoman/sounds/scifi011.wav')
                c = pygame.mixer.Channel(3)
                c.set_volume(10)
                c.play(sound)

            # Shoots 8 bullets placed in a fixed range with a little random variation in their position (x and y).

            for i in range(0, 8):
                rand = numpy.array([30, 20, 10, 15, 9, 25, 18, 5])
                rand2 = numpy.array([1, 2, 3, 4, 5, 2, 4, 3])

                rand = rand[i]
                rand2 = rand2[i]

                # Start position of the bullets vary according to the position of the enemy.
                if self.direction > 0:
                    self.twists.append(
                        Bullet_e1((self.rect.x + (i * rand), self.rect.y + 10 +
                                   (i * rand2)), 1, len(self.twists),
                                  game.sprite_e))
                else:
                    self.twists.append(
                        Bullet_e1(
                            (self.rect.x - (i * rand) + 46, self.rect.y + 10 +
                             (i * rand2)), -1, len(self.twists),
                            game.sprite_e))

        # Decreases time for bullets and freezing limitation.
        self.gun_cooldown = max(0, self.gun_cooldown - dt)
        self.gun_cooldown2 = max(0, self.gun_cooldown2 - dt)

        # Changes bullets images according to the enemy direction.
        if self.shooting > 0:
            if self.direction == -1:
                self.updateSprite(SpriteConstants.SHOOTING,
                                  SpriteConstants.LEFT)
            else:
                self.updateSprite(SpriteConstants.SHOOTING,
                                  SpriteConstants.RIGHT)

        self.shooting -= 1
        self.shooting = max(0, self.shooting)

        # Releases movement.
        if self.gun_cooldown <= 5:
            game.freeze_p = 0
            game.freeze_e = 0

        # Reinicializes enemy atacking timer.
        if self.timeenemy == 260:
            self.timeenemy = 0

    def updateSprite(self, state, direction):
        self.image = self.spriteDefinition.getImage(state, direction)