用Python写个魂斗罗,另附30个Python小游戏源码

举报
Python小二 发表于 2022/05/02 23:52:53 2022/05/02
【摘要】 今天分享一个经典小游戏魂斗罗的 Python 版实现。部分代码实现如下: class Game_Player(): def __init__(self,game_settings,screen):   self.screen = screen &n...

今天分享一个经典小游戏魂斗罗的 Python 版实现。部分代码实现如下:


   
   
  1. class Game_Player():
  2.  def __init__(self,game_settings,screen):
  3.   self.screen = screen
  4.   self.game_settings = game_settings
  5.   self.image = pygame.image.load('images/PR/player.png')
  6.   self.rect = self.image.get_rect()
  7.   self.screen_rect = self.screen.get_rect()
  8.   self.screen_center_pos = self.screen_rect.centerx
  9.   self.rect.centerx = self.screen_rect.centerx
  10.   self.rect.bottom = 380
  11.   self.center = float(self.rect.centerx)
  12.   self.moving_right = False
  13.   self.moving_left = False
  14.   self.player_moving = False
  15.   self.pos_i = 0.0#跑步画面改变速度
  16.   self.pos_j = 0.0#跳跃的改变初始速度
  17.   self.pos_n = 0.0#射击
  18.   self.pos_d = 0.0
  19.   self.player_direction = 1#1为右,-1为左
  20.   self.player_down = False
  21.   self.player_up = False
  22.   self.player_jump = False
  23.   self.player_start_Y = 380#开始人物的高度
  24.   self.player_Y = self.player_start_Y
  25.   self.player_shooting = False
  26.  def update(self):
  27.   if self.game_settings.player_is_alive:
  28.    if self.moving_right and self.rect.right < self.screen_rect.right:
  29.     if self.game_settings.boss_appear:
  30.      self.center += self.game_settings.player_speed
  31.     elif self.center > self.screen_center_pos:
  32.      self.center += 0
  33.     else:
  34.      self.center += self.game_settings.player_speed
  35.    if self.moving_left and self.rect.left > 0:
  36.     self.center -= self.game_settings.player_speed
  37.    self.rect.centerx = self.center
  38.    self.update_image_moving()
  39.   else:
  40.    self.update_die()
  41.  def update_die(self):
  42.   self.names = locals()
  43.   self.players = []
  44.   for self.i in range(1,3):
  45.    self.names['player_image%s' %self.i] = pygame.image.load('images/PR/death%s.png' %self.i)
  46.    self.players.append(self.names['player_image%s' %self.i])
  47.   self.image = self.players[int(self.pos_d)]
  48.   self.pos_d += 0.1
  49.   if self.pos_d > 2.0:
  50.    self.pos_d = 0.0
  51.    self.game_settings.player_die_end = True
  52.  def update_image_moving_direction(self,direction):#循环图片
  53.   if self.player_shooting == True:
  54.    self.names = locals()
  55.    self.players = []
  56.    for self.i in range(1,4):
  57.     self.names['player_image%s' %self.i] = pygame.image.load('images/'+direction+'/shooting%s.png' %self.i)
  58.     self.players.append(self.names['player_image%s' %self.i])
  59.    self.image = self.players[int(self.pos_n)]
  60.    self.pos_n += 0.1#射击时跑步速率
  61.    if self.pos_n > 3.0:#射击时跑步有3个动作
  62.     self.pos_n = 0.0
  63.   else:
  64.    self.names = locals()
  65.    self.players = []
  66.    for self.i in range(1,6):
  67.     self.names['player_image%s' %self.i] = pygame.image.load('images/'+direction+'/player%s.png' %self.i)
  68.     self.players.append(self.names['player_image%s' %self.i])
  69.    self.image = self.players[int(self.pos_i)]
  70.    self.pos_i += 0.1#跑步速率
  71.    if self.pos_i > 5.0:#跑步有5个动作
  72.     self.pos_i = 0.0
  73.  def update_image_jump(self,direction):#循环图片
  74.   if self.game_settings.player_is_alive:
  75.    if self.player_jump == True:
  76.     self.names1 = locals()
  77.     self.players1 = []
  78.     for self.j in range(1,5):
  79.      self.names1['player_image%s' %self.j] = pygame.image.load('images/'+direction+'/jump%s.png' %self.j)
  80.      self.players1.append(self.names1['player_image%s' %self.j])
  81.     self.image = self.players1[int(self.pos_j)]
  82.     self.pos_j += 0.3#跳跃旋转速率
  83.     if self.pos_j > 4.0:#跳跃有4个动作
  84.      self.pos_j = 0.0
  85.   else:
  86.    self.update_die()
  87.  def update_image_moving(self):
  88.   if self.player_moving:
  89.    if self.moving_right:
  90.     self.update_image_moving_direction('PR')#PR是向右的图片
  91.    elif self.moving_left:
  92.     self.update_image_moving_direction('PL')
  93.  def get_player_state(self,player_state):#检测player的状态是否为趴下,向上,跳跃等
  94.   if self.player_direction == 1:
  95.    self.image = pygame.image.load('images/PR/'+player_state+'.png')
  96.   if self.player_direction == -1:
  97.    self.image = pygame.image.load('images/PL/'+player_state+'.png')
  98.   self.rect = self.image.get_rect()
  99.   self.rect.centerx = self.center
  100.  def get_player_down(self):
  101.   self.get_player_state('down')
  102.   self.rect.bottom = self.player_start_Y
  103.   self.screen.blit(self.image,self.rect)
  104.  def get_player_up(self):
  105.   self.get_player_state('up')
  106.   self.rect.bottom = self.player_start_Y
  107.   self.screen.blit(self.image,self.rect)
  108.  def get_player_jump(self):
  109.   self.get_player_state('jump1')
  110.   self.rect.bottom = self.player_Y
  111.   if self.game_settings.jump_vel < 0:
  112.    self.game_settings.jump_vel += 0.6#跳跃上升的增加的速率
  113.   elif self.game_settings.jump_vel > 0:
  114.    self.game_settings.jump_vel += 0.8#跳跃下降增加的速率
  115.   self.player_Y += self.game_settings.jump_vel
  116.   if self.player_Y > self.player_start_Y:
  117.    self.player_jump = False
  118.    self.player_Y = self.player_start_Y
  119.    self.game_settings.jump_vel = -14.0#恢复跳跃开始的速度
  120.    if self.player_direction == 1:
  121.     self.image = pygame.image.load('images/PR/player.png')
  122.     self.reset_player()
  123.    if self.player_direction == -1:
  124.     self.image = pygame.image.load('images/PL/player.png')
  125.     self.reset_player()
  126.   if self.player_jump == True:#判断是否处于跳跃状态来决定是否旋转跳跃的图像
  127.    if self.player_direction == 1:
  128.     self.update_image_jump('PR')
  129.    if self.player_direction == -1:
  130.     self.update_image_jump('PL')
  131.   self.screen.blit(self.image,self.rect)
  132.  def reset_player(self):
  133.   self.rect = self.image.get_rect()
  134.   self.rect.centerx = self.center
  135.   self.rect.bottom = self.player_start_Y
  136.   self.screen.blit(self.image,self.rect)
  137.  def blitme(self):
  138.   if self.player_jump:
  139.    self.get_player_jump()
  140.   elif self.player_down:
  141.    self.get_player_down()
  142.   elif self.player_up:
  143.    self.get_player_up()
  144.   else:
  145.    self.reset_player()
  146.  def revive_player(self):
  147.   self.center = self.screen_rect.centerx
  148.   self.game_settings.player_is_alive = True
8f85716022137d4471070b67bbfba461.png

实现效果如下:

魂斗罗

操作说明:

  • A:向左

  • D:向右

  • W:跳起

  • S:趴下

  • J:射击

  • P:退出程序

目前游戏还是比较初级的版本,有兴趣的小伙伴可以自行修改完善。

 小游戏源码在下方公号后台回复py小游戏获取~

文章来源: ityard.blog.csdn.net,作者:Python小二,版权归原作者所有,如需转载,请联系作者。

原文链接:ityard.blog.csdn.net/article/details/124521784

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。