服务器之家

服务器之家 > 正文

python飞机大战游戏实例讲解

时间:2021-08-08 00:18     来源/作者:小妮浅浅

记得刚学python那会,作过一个飞机大战小项目,这个项目非常经典,可以帮助初学者提高动手能力,今天把它分享出来。

一、项目介绍

先放几张图片

python飞机大战游戏实例讲解

python飞机大战游戏实例讲解

python飞机大战游戏实例讲解

python飞机大战游戏实例讲解

二、项目实现

1.首先安装库

  1. pip install pygame

2.主要python代码

  1. import pygame
  2.  
  3. from pygame.locals import *
  4. import random
  5. https://blog.csdn.net/qq_36079986/article/details/110395731
  6. class HeroPlane(object):# 创建一个飞机的英雄类
  7. def __init__(self,screen_temp): # 始化方法
  8. self.x = 200
  9. self.y = 500
  10. self.image = pygame.image.load('./img/hero1.png')
  11. self.screen = screen_temp# 因为在display方法中使用到了screen方法# 但是在类中没有 也不能调用 # 所以说 以传参的方法传递到类中
  12. self.bullet_list = []
  13. def display(self):# 创建一个展现飞机的方法
  14. herp_x1, herp_y1, herp_x2, herp_y2, herp_x3, herp_y3 = -100, -100, -100, -100, -100, -100
  15. self.screen.blit(self.image,(self.x, self.y))
  16. for bullet in self.bullet_list:###遍历子弹类对象,赋值给bullet
  17. herp_x1, herp_y1, herp_x2, herp_y2, herp_x3, herp_y3 = bullet.display()###### bullet对象调用display方法, 显示子弹
  18. bullet.move()
  19. if self.bullet_list[0].y < 0:
  20. del self.bullet_list[0]
  21. return herp_x1, herp_y1, herp_x2, herp_y2, herp_x3, herp_y3
  22. def move_left(self):
  23. self.x -= 50
  24. def move_right(self):
  25. self.x += 50
  26. def move_up(self):
  27. self.y -= 50
  28. def move_down(self):
  29. self.y += 50
  30. def fire(self):
  31. print(len(self.bullet_list),'个子弹##########################################')
  32. self.bullet_list.append(HeroBullet(self.screen,self.x, self.y))
  33. class HeroBullet(object):
  34. def __init__(self,screen_temp,x_temp,y_temp):
  35. self.x = x_temp
  36. self.y = y_temp
  37. self.image = pygame.image.load('./img/bullet.png')
  38. self.screen = screen_temp
  39. def display(self):
  40. self.screen.blit(self.image,(self.x - 22, self.y-22))
  41. self.screen.blit(self.image, (self.x + 39, self.y-22))
  42. self.screen.blit(self.image, (self.x + 100, self.y-22))
  43. return self.x - 22, self.y - 22, self.x + 39, self.y - 22, self.x + 100, self.y - 22
  44. def move(self):
  45. self.y -= 1
  46. class EnemyPlane(object):
  47. def __init__(self,screen_temp):
  48. self.ra = random.randint(0,2)
  49. self.x = random.randint(0,400)
  50. self.y = 0
  51. self.image = pygame.image.load('./img/enemy1.png')
  52. if 0<=self.x<=540:
  53. if self.ra == 0:
  54. self.flag = "left"
  55. elif self.ra == 1:
  56. self.flag = "right"
  57. else:
  58. self.flag = 'middle'
  59. self.screen.blit(self.image, (self.x, self.y))
  60. if self.x < 0:
  61. self.flag = "left"
  62. elif self.x > 450:
  63. self.flag = 'right'
  64. if self.flag == "left":
  65. self.x += 2
  66. elif self.flag == 'right':
  67. self.x -= 2
  68. self.y += 0.1
  69. if (random.randint(0,50) == 1):
  70. self.bullet_list.append(EnemyBullet(self.screen, self.x, self.y))
  71. # print(len(self.bullet_list),'颗敌方子弹')
  72. for bullet in self.bullet_list:
  73. bullet.display()
  74. if bullet.y > 700:
  75. del bullet
  76. class EnemyBullet(object):
  77. def __init__(self,screen_temp,x,y):
  78. self.x = x + 30
  79. self.y = y + 90
  80. self.image = pygame.image.load('./img/bullet-1.gif')
  81. self.y+= 10
  82. def key_control(hero_temp):
  83. for event in pygame.event.get(): # 判断输入类型
  84. if event.type == QUIT:#判断是否是点击了退出按钮
  85. print("exit")
  86. exit()
  87. elif event.type == KEYDOWN:#判断是否是按下了键# 如果是键盘事件(键盘按下事件)
  88. if event.key == K_a or event.key == K_LEFT:
  89. hero_temp.move_left()
  90. elif event.key == K_d or event.key == K_RIGHT:
  91. hero_temp.move_right()
  92. elif event.key == K_w or event.key == K_UP:
  93. hero_temp.move_up()
  94. elif event.key == K_s or event.key == K_DOWN:
  95. hero_temp.move_down()
  96. elif event.key == K_SPACE:#检测按键是否是空格键
  97. hero_temp.fire()
  98. def main():
  99. screen = pygame.display.set_mode((480, 650), 0, 32) # 使用screen资源来创建背景# 返回值是一个资源
  100. pygame.display.set_caption("aircraft game") # 设置窗口标题
  101. background = pygame.image.load('./img/background.png')## ./ 当前路径 ../ 上一级路径 / 根目录
  102. hero = HeroPlane(screen)
  103. enemy_list = []
  104. j = 0
  105. while True:
  106. screen.blit(background, (0, 0)) # 资源中的blit方法能够加载游戏的画面
  107. herp_x1, herp_y1, herp_x2, herp_y2, herp_x3, herp_y3 = hero.display() #####刷新
  108. if len(enemy_list) <= 10:
  109. enemy_list.append(EnemyPlane(screen))
  110. if enemy_list[0].y >= 700:
  111. del enemy_list[0]
  112. for enemy_each in enemy_list :
  113. # enemy_each.fire()
  114. enemy_each.move()
  115. enemy_each.display()
  116. # print(len(enemy_list), '个敌机')
  117. if (enemy_each.x <= herp_x1 -10 <= enemy_each.x + 80 and enemy_each.y -10<= herp_y1<= enemy_each.y + 90) or (enemy_each.x -10 <= herp_x2<= enemy_each.x + 80 and enemy_each.y -10<= herp_y2<= enemy_each.y + 90) or (enemy_each.x-10 <= herp_x3<= enemy_each.x + 80 and enemy_each.y -10<= herp_y3<= enemy_each.y + 90):
  118. enemy_list.remove(enemy_each)
  119. # print(len(enemy_list), '个敌机')
  120. print('jizhon')
  121. # print(hero.x,type(hero.x))
  122. if j % 100 == 5:
  123. print(len(enemy_list),'个敌机')
  124. pygame.display.update() ### 加载游戏
  125. key_control(hero)# 调用键盘事件函数
  126. j += 1
  127. # print(herp_x1, herp_y1, herp_x2, herp_y2, herp_x3, herp_y3)
  128. if __name__ == '__main__':
  129. main()

三、说明:代码功能都在注释里面,我就不做过多解释了;与本项目有关的图片数据我传到资源里面了,大家可以免费下载,我把代码和数据都打包到一起了,大家下载下来导入库就可以直接一些了。

实例补充

  1. import pygame
  2.  
  3. class MyPlane(pygame.sprite.Sprite):
  4. def __init__(self,bg_size):
  5. pygame.sprite.Sprite.__init__(self)
  6.  
  7. self.image1 = pygame.image.load(r'E:\learn pygame\image\me1.png').convert_alpha()
  8. self.image2 = pygame.image.load(r'E:\learn pygame\image\me2.png').convert_alpha()
  9. self.image3 = pygame.image.load(r'E:\learn pygame\image\me_protect.png').convert_alpha()
  10. self.destroy_images = []
  11. self.destroy_images.extend([\
  12. pygame.image.load(r'E:\learn pygame\image\me_destroy_1.png').convert_alpha(), \
  13. pygame.image.load(r'E:\learn pygame\image\me_destroy_2.png').convert_alpha(), \
  14. pygame.image.load(r'E:\learn pygame\image\me_destroy_3.png').convert_alpha(), \
  15. pygame.image.load(r'E:\learn pygame\image\me_destroy_4.png').convert_alpha() \
  16. ])
  17. self.rect = self.image1.get_rect()
  18. self.width,self.height = bg_size[0],bg_size[1]
  19. self.rect.left,self.rect.top = (self.width - self.rect.width) // 2,self.height - self.rect.height - 60
  20. self.speed = 10
  21. self.active = True
  22. self.invincible = False
  23. self.mask = pygame.mask.from_surface(self.image1)
  24.  
  25. def moveUp(self):
  26. if self.rect.top > 0:
  27. self.rect.top -= self.speed
  28. else:
  29. self.rect.top = 0
  30.  
  31. def moveDown(self):
  32. if self.rect.bottom < self.height - 30:
  33. self.rect.top += self.speed
  34. else:
  35. self.rect.bottom = self.height - 30
  36.  
  37. def moveLeft(self):
  38. if self.rect.left > 0:
  39. self.rect.left -= self.speed
  40. else:
  41. self.rect.left = 0
  42.  
  43. def moveRight(self):
  44. if self.rect.right < self.width:
  45. self.rect.left += self.speed
  46. else:
  47. self.rect.right = self.width
  48.  
  49. def reset(self):
  50. self.rect.left,self.rect.top = (self.width - self.rect.width) // 2,self.height - self.rect.height - 30
  51. self.active = True
  52. self.invincible = True

到此这篇关于python飞机大战游戏实例讲解的文章就介绍到这了,更多相关飞机大战游戏python小项目内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

原文链接:https://www.py.cn/jishu/jichu/21578.html

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021德云社封箱演出完整版 2021年德云社封箱演出在线看
2021德云社封箱演出完整版 2021年德云社封箱演出在线看 2021-03-15
返回顶部