2022年 11月 9日

用python实现简单的飞机大战小游戏

1、下载一个包

PySide2-5.15.2.1-5.15.2-cp35.cp36.cp37.cp38.cp39.cp310-none-win_amd64

如果下载错误,就先下载shiboken2-5.15.2.1-5.15.2-cp35.cp36.cp37.cp38.cp39.cp310-none-win_amd64补丁,然后再下载Pyside2。

记得都放在同一个工程下。

2、准备好飞机大战中所需要的图片资源

(1)自身飞机

(2)飞机子弹

(3)背景

 

(4)敌机

 

将准备好的图片放入工程中的images文件夹中,且命名好

3、代码

我们一共准备六个模块

(1)主函数main模块.py

  1. from PySide2 import QtCore, QtWidgets, QtGui
  2. from settings import Settings
  3. from map import Map
  4. from hero import Hero
  5. from enemy import Enemy
  6. from random import randint
  7. class MainScene(QtWidgets.QWidget):
  8. def __init__(self):
  9. super().__init__()
  10. self.settings = Settings()
  11. self.setWindowTitle(self.settings.game_title)
  12. self.setWindowIcon(QtGui.QIcon(self.settings.game_ico))
  13. self.setFixedSize(self.settings.game_width, self.settings.game_height)
  14. self.map = Map() # 构造一个地图对象
  15. self.hero = Hero() # 构造一个英雄对象
  16. # 初始化敌机
  17. self.enemys = []
  18. self.enemy_interval = 0
  19. for i in range(self.settings.enemy_num):
  20. self.enemys.append(Enemy())
  21. self.timer_main = QtCore.QTimer(self)
  22. self.timer_main.setInterval(20)
  23. self.timer_main.timeout.connect(self.run)
  24. self.timer_main.start()
  25. self.presskeys = []
  26. self.timer_key = QtCore.QTimer(self)
  27. self.timer_key.setInterval(50)
  28. self.timer_key.timeout.connect(self.handleKey)
  29. def enemyToScene(self):
  30. self.enemy_interval += 1
  31. if self.enemy_interval < 30:
  32. return
  33. self.enemy_interval = 0
  34. for i in range(self.settings.enemy_num):
  35. if self.enemys[i].free:
  36. self.enemys[i].free = False
  37. self.enemys[i].x = randint(0, self.settings.game_width-self.enemys[i].img.width())
  38. self.enemys[i].y = -self.enemys[i].img.height()
  39. break
  40. def collisionDetect(self):
  41. for i in range(self.settings.enemy_num):
  42. if self.enemys[i].free:
  43. continue
  44. for j in range(self.settings.bullet_num):
  45. if self.hero.bullets[j].free:
  46. continue
  47. if self.enemys[i].rect.intersects(self.hero.bullets[j].rect):
  48. self.enemys[i].free = True
  49. self.hero.bullets[j].free = True
  50. def handleKey(self):
  51. x = self.hero.x
  52. y = self.hero.y
  53. if self.presskeys.count(QtCore.Qt.Key_Left) > 0:
  54. x -= self.hero.speed
  55. if self.presskeys.count(QtCore.Qt.Key_Right) > 0:
  56. x += self.hero.speed
  57. if self.presskeys.count(QtCore.Qt.Key_Space) > 0:
  58. self.hero.shoot()
  59. if self.presskeys.count(QtCore.Qt.Key_Q) > 0:
  60. self.close()
  61. self.hero.move(x, y)
  62. def keyPressEvent(self, event):
  63. '''按键按下时,自动调用此函数'''
  64. self.presskeys.append(event.key())
  65. if not self.timer_key.isActive():
  66. self.timer_key.start()
  67. def keyReleaseEvent(self, event):
  68. '''松开按键时,自动调用'''
  69. if self.timer_key.isActive() and len(self.presskeys) == 0:
  70. self.timer_key.stop()
  71. self.handleKey()
  72. while event.key() in self.presskeys:
  73. self.presskeys.remove(event.key())
  74. def updatePosition(self):
  75. self.map.move()
  76. '''更新子弹的位置'''
  77. for i in range(self.settings.bullet_num):
  78. if not self.hero.bullets[i].free:
  79. self.hero.bullets[i].move()
  80. '''更新敌机位置'''
  81. for i in range(self.settings.enemy_num):
  82. if not self.enemys[i].free:
  83. self.enemys[i].move()
  84. def run(self):
  85. self.enemyToScene()
  86. self.updatePosition()
  87. self.update()
  88. self.collisionDetect()
  89. def paintEvent(self, event):
  90. painter = QtGui.QPainter(self)
  91. painter.drawPixmap(0, self.map.y1, self.map.img1)
  92. painter.drawPixmap(0, self.map.y2, self.map.img2)
  93. painter.drawPixmap(self.hero.x, self.hero.y, self.hero.img)
  94. '''绘制子弹'''
  95. for i in range(self.settings.bullet_num):
  96. if not self.hero.bullets[i].free:
  97. painter.drawPixmap(self.hero.bullets[i].x, self.hero.bullets[i].y, self.hero.bullets[i].img)
  98. '''绘制敌机'''
  99. for i in range(self.settings.enemy_num):
  100. if not self.enemys[i].free:
  101. painter.drawPixmap(self.enemys[i].x, self.enemys[i].y, self.enemys[i].img)
  102. if __name__ == "__main__":
  103. app = QtWidgets.QApplication([])
  104. w = MainScene()
  105. w.show()
  106. app.exec_()

(2)资源调用模块setting.py

  1. class Settings:
  2. def __init__(self):
  3. self.game_title = "python飞机大战"
  4. self.game_ico = r"D:\周末实训\飞机大战\images\app.ico"
  5. self.game_width = 512
  6. self.game_height = 700
  7. self.map_path = r"D:\周末实训\飞机大战\images\img_bg_level_2.jpg"
  8. self.map_speed = 2
  9. self.hero_path = r"D:\周末实训\飞机大战\images\hero2.png"
  10. self.hero_speed = 20
  11. self.bullet_path = r"D:\周末实训\飞机大战\images\bullet_11.png"
  12. self.bullet_speed = 10
  13. self.bullet_num = 10
  14. self.enemy_path = r"D:\周末实训\飞机大战\images\img-plane_5.png"
  15. self.enemy_speed = 3
  16. self.enemy_num = 10

(3)地图模块map.py

  1. from PySide2 import QtGui
  2. from settings import Settings
  3. class Map:
  4. def __init__(self):
  5. self.settings = Settings()
  6. self.img1 = QtGui.QPixmap(self.settings.map_path)
  7. self.img2 = QtGui.QPixmap(self.settings.map_path)
  8. self.y1 = -self.settings.game_height
  9. self.y2 = 0
  10. def move(self):
  11. self.y1 += self.settings.map_speed
  12. if self.y1 >= 0:
  13. self.y1 = -self.settings.game_height
  14. self.y2 += self.settings.map_speed
  15. if self.y2 >= self.settings.game_height:
  16. self.y2 = 0

(4)自身飞机操作模块hero.py

  1. from PySide2 import QtGui
  2. from settings import Settings
  3. from bullet import Bullet
  4. class Hero:
  5. def __init__(self):
  6. self.settings = Settings()
  7. self.img = QtGui.QPixmap(self.settings.hero_path)
  8. self.x = self.settings.game_width*0.5 - self.img.width()*0.5
  9. self.y = self.settings.game_height - self.img.height()
  10. self.speed = self.settings.hero_speed
  11. # 创建一个弹夹,放入10颗子弹
  12. self.bullets = []
  13. for i in range(self.settings.bullet_num):
  14. self.bullets.append(Bullet())
  15. def move(self, x, y):
  16. '''飞机移动'''
  17. self.x = x
  18. self.y = y
  19. def shoot(self):
  20. '''飞机发射子弹,按空格键时,调用此函数'''
  21. for i in range(self.settings.bullet_num):
  22. if self.bullets[i].free:
  23. self.bullets[i].free = False
  24. self.bullets[i].x = self.x + self.img.width()*0.5 - self.bullets[i].img.width()*0.5
  25. self.bullets[i].y = self.y - 30
  26. break

(5)发射子弹模块bullet.py

  1. from PySide2 import QtGui
  2. from settings import Settings
  3. class Bullet:
  4. def __init__(self):
  5. self.settings = Settings()
  6. self.img = QtGui.QPixmap(self.settings.bullet_path)
  7. self.rect = self.img.rect()
  8. self.speed = self.settings.bullet_speed
  9. self.x = 0
  10. self.y = 0
  11. self.free = True
  12. def move(self):
  13. if self.free:
  14. return
  15. self.y -= self.speed
  16. self.rect.moveTo(self.x, self.y)
  17. if self.y <= -self.rect.height():
  18. self.free = True

(6)敌机模块enemy.py

  1. from PySide2 import QtGui
  2. from settings import Settings
  3. class Enemy:
  4. def __init__(self):
  5. self.settings = Settings()
  6. self.img = QtGui.QPixmap(self.settings.enemy_path)
  7. self.rect = self.img.rect()
  8. self.x = 0
  9. self.y = 0
  10. self.free = True
  11. self.speed = self.settings.enemy_speed
  12. def move(self):
  13. if self.free:
  14. return
  15. self.y += self.speed
  16. self.rect.moveTo(self.x, self.y)
  17. if self.y >= self.settings.game_height:
  18. self.free = True

4、注意

代码没有问题,在同一工程目录下建6个.py文件然后将代码复制进去就行了

如果有报错,一般就是我上面说的包没有下载或者调用成功,

也可能是图片路径问题。

代码比较少,功能也不是很多,相比于正规游戏这个肯定是没得比的,如果有问题,还望各位大佬多加指点

操作方式:飞机方向由左右两键控制,空格发射子弹,Q退出游戏