Today we share a Python implementation of the classic game Contra.
I. Effectiveness demonstration
II. Operational instructions
A: to the left
D: to the right
W: Jump up
S: Get down!
J: Shooting
P: Exit program
At present, the game is still a relatively elementary version, interested partners can modify their own perfect.
III. Core code
class Game_Player(): def __init__(self,game_settings,screen): = screen self.game_settings = game_settings = ('images/PR/') = .get_rect() self.screen_rect = .get_rect() self.screen_center_pos = self.screen_rect.centerx = self.screen_rect.centerx = 380 = float() self.moving_right = False self.moving_left = False self.player_moving = False self.pos_i = 0.0#Running screen to change speed self.pos_j = 0.0# Jumping changes initial speed self.pos_n = 0.0#Shooting self.pos_d = 0.0 self.player_direction = 1#1 for right, -1 for left self.player_down = False self.player_up = False self.player_jump = False self.player_start_Y = 380#Starting figure height self.player_Y = self.player_start_Y self.player_shooting = False def update(self): if self.game_settings.player_is_alive: if self.moving_right and < self.screen_rect.right: if self.game_settings.boss_appear: += self.game_settings.player_speed elif > self.screen_center_pos: += 0 else: += self.game_settings.player_speed if self.moving_left and > 0: -= self.game_settings.player_speed = self.update_image_moving() else: self.update_die() def update_die(self): = locals() = [] for in range(1,3): ['player_image%s' %] = ('images/PR/death%' %) (['player_image%s' %]) = [int(self.pos_d)] self.pos_d += 0.1 if self.pos_d > 2.0: self.pos_d = 0.0 self.game_settings.player_die_end = True def update_image_moving_direction(self,direction):#Cycled images if self.player_shooting == True: = locals() = [] for in range(1,4): ['player_image%s' %] = ('images/'+direction+'/shooting%' %) (['player_image%s' %]) = [int(self.pos_n)] self.pos_n += 0.1# Running rate while shooting if self.pos_n > 3.0:#RunningWhileShooting has 3 moves self.pos_n = 0.0 else: = locals() = [] for in range(1,6): ['player_image%s' %] = ('images/'+direction+'/player%' %) (['player_image%s' %]) = [int(self.pos_i)] self.pos_i += 0.1# Running rate if self.pos_i > 5.0:#Running has 5 moves self.pos_i = 0.0 def update_image_jump(self,direction):#Cycled images if self.game_settings.player_is_alive: if self.player_jump == True: self.names1 = locals() self.players1 = [] for in range(1,5): self.names1['player_image%s' %] = ('images/'+direction+'/jump%' %) self.(self.names1['player_image%s' %]) = self.players1[int(self.pos_j)] self.pos_j += 0.3#Jump Rotation Rate if self.pos_j > 4.0:#Jumping has 4 moves self.pos_j = 0.0 else: self.update_die() def update_image_moving(self): if self.player_moving: if self.moving_right: self.update_image_moving_direction('PR')#PR is the picture to the right elif self.moving_left: self.update_image_moving_direction('PL') def get_player_state(self,player_state):# Detects if the player's state is down, up, jumping, etc. if self.player_direction == 1: = ('images/PR/'+player_state+'.png') if self.player_direction == -1: = ('images/PL/'+player_state+'.png') = .get_rect() = def get_player_down(self): self.get_player_state('down') = self.player_start_Y (,) def get_player_up(self): self.get_player_state('up') = self.player_start_Y (,) def get_player_jump(self): self.get_player_state('jump1') = self.player_Y if self.game_settings.jump_vel < 0: self.game_settings.jump_vel += 0.6# Rate of increase of jumps elif self.game_settings.jump_vel > 0: self.game_settings.jump_vel += 0.8# Rate of increase of jump descent self.player_Y += self.game_settings.jump_vel if self.player_Y > self.player_start_Y: self.player_jump = False self.player_Y = self.player_start_Y self.game_settings.jump_vel = -14.0#Restore the speed at the start of the jump if self.player_direction == 1: = ('images/PR/') self.reset_player() if self.player_direction == -1: = ('images/PL/') self.reset_player() if self.player_jump == True:# Determine if the jump state is in a jump state to decide whether to rotate the jumped image or not if self.player_direction == 1: self.update_image_jump('PR') if self.player_direction == -1: self.update_image_jump('PL') (,) def reset_player(self): = .get_rect() = = self.player_start_Y (,) def blitme(self): if self.player_jump: self.get_player_jump() elif self.player_down: self.get_player_down() elif self.player_up: self.get_player_up() else: self.reset_player() def revive_player(self): = self.screen_rect.centerx self.game_settings.player_is_alive = True
To this article on Python Pygame implementation of the classic Contra game is introduced to this article, more related Python Pygame Contra content, please search for my previous articles or continue to browse the following related articles I hope you will support me in the future!