Today, I'd like to share a classic little game soul duel Python Version implementation . Part of the code is implemented as follows :
class Game_Player(): def __init__(self,game_settings,screen):
self.screen = screen self.game_settings = game_settings
self.image = pygame.image.load('images/PR/player.png')
self.rect = self.image.get_rect() self.screen_rect = self.screen.get_rect()
self.screen_center_pos = self.screen_rect.centerx
self.rect.centerx = self.screen_rect.centerx self.rect.bottom = 380
self.center = float(self.rect.centerx) self.moving_right = False
self.moving_left = False self.player_moving = False
self.pos_i = 0.0# Running screen changes speed self.pos_j = 0.0# Change initial speed of jump self.pos_n = 0.0# Shooting
self.pos_d = 0.0 self.player_direction = 1#1 Right ,-1 Left
self.player_down = False self.player_up = False self.player_jump = False
self.player_start_Y = 380# Start character 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.rect.right < self.screen_rect.right:
if self.game_settings.boss_appear:
self.center += self.game_settings.player_speed
elif self.center > self.screen_center_pos: self.center += 0 else:
self.center += self.game_settings.player_speed
if self.moving_left and self.rect.left > 0:
self.center -= self.game_settings.player_speed
self.rect.centerx = self.center self.update_image_moving() else:
self.update_die() def update_die(self): self.names = locals()
self.players = [] for self.i in range(1,3):
self.names['player_image%s' %self.i] = pygame.image.load('images/PR/death%s.png' %self.i)
self.players.append(self.names['player_image%s' %self.i])
self.image = self.players[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):# Circular picture
if self.player_shooting == True: self.names = locals()
self.players = [] for self.i in range(1,4):
self.names['player_image%s' %self.i] = pygame.image.load('images/'+direction+'/shooting%s.png' %self.i)
self.players.append(self.names['player_image%s' %self.i])
self.image = self.players[int(self.pos_n)] self.pos_n += 0.1# Running speed during shooting
if self.pos_n > 3.0:# It's hard to run when shooting 3 Action self.pos_n = 0.0 else:
self.names = locals() self.players = [] for self.i in range(1,6):
self.names['player_image%s' %self.i] = pygame.image.load('images/'+direction+'/player%s.png' %self.i)
self.players.append(self.names['player_image%s' %self.i])
self.image = self.players[int(self.pos_i)] self.pos_i += 0.1# Running speed
if self.pos_i > 5.0:# Running has 5 Action self.pos_i = 0.0
def update_image_jump(self,direction):# Circular picture
if self.game_settings.player_is_alive: if self.player_jump == True:
self.names1 = locals() self.players1 = [] for self.j in range(1,5):
self.names1['player_image%s' %self.j] = pygame.image.load('images/'+direction+'/jump%s.png' %self.j)
self.players1.append(self.names1['player_image%s' %self.j])
self.image = self.players1[int(self.pos_j)] self.pos_j += 0.3# Jump rotation rate
if self.pos_j > 4.0:# Jump there 4 Action 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 It's a picture to the right
elif self.moving_left: self.update_image_moving_direction('PL')
def get_player_state(self,player_state):# testing player Is the state of lying down , Upward , Jump, etc
if self.player_direction == 1:
self.image = pygame.image.load('images/PR/'+player_state+'.png')
if self.player_direction == -1:
self.image = pygame.image.load('images/PL/'+player_state+'.png')
self.rect = self.image.get_rect() self.rect.centerx = self.center
def get_player_down(self): self.get_player_state('down')
self.rect.bottom = self.player_start_Y
self.screen.blit(self.image,self.rect) def get_player_up(self):
self.get_player_state('up') self.rect.bottom = self.player_start_Y
self.screen.blit(self.image,self.rect) def get_player_jump(self):
self.get_player_state('jump1') self.rect.bottom = self.player_Y
if self.game_settings.jump_vel < 0:
self.game_settings.jump_vel += 0.6# The rate at which jumps rise
elif self.game_settings.jump_vel > 0:
self.game_settings.jump_vel += 0.8# The rate at which jump drops and increases
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 beginning of the jump
if self.player_direction == 1:
self.image = pygame.image.load('images/PR/player.png')
self.reset_player() if self.player_direction == -1:
self.image = pygame.image.load('images/PL/player.png')
self.reset_player() if self.player_jump == True:# Judge whether it is in the jumping state to determine whether to rotate the jumping image
if self.player_direction == 1: self.update_image_jump('PR')
if self.player_direction == -1: self.update_image_jump('PL')
self.screen.blit(self.image,self.rect) def reset_player(self):
self.rect = self.image.get_rect() self.rect.centerx = self.center
self.rect.bottom = self.player_start_Y
self.screen.blit(self.image,self.rect) 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.center = self.screen_rect.centerx
self.game_settings.player_is_alive = True
The results are as follows :
Contra
Operating instructions :
*
A: towards the left
*
D: towards the right
*
W: Jump
*
S: Get down
*
J: Shooting
*
P: Exit program
At present, the game is still a relatively primary version , Interested partners can modify and improve themselves .
The source code of the small game is replied in the background of the public account below py Small game acquisition ~
Technology