参考:用python实现简单的飞机大战小游戏_化华桦的博客-CSDN博客_python飞机大战
敌机图片:
我方飞机:
子弹:
背景图:
主函数代码main.py:
from PySide2 import QtCore, QtWidgets, QtGui
from settings import Settings
from map import Map
from hero import Hero
from enemy import Enemy
from random import randint
class MainScene(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.settings = Settings()
self.setWindowTitle(self.settings.game_title)
self.setWindowIcon(QtGui.QIcon(self.settings.game_ico))
self.setFixedSize(self.settings.game_width, self.settings.game_height)
self.map = Map() # 构造一个地图对象
self.hero = Hero() # 构造一个英雄对象
# 初始化敌机
self.enemys = []
self.enemy_interval = 0
for i in range(self.settings.enemy_num):
self.enemys.append(Enemy())
self.timer_main = QtCore.QTimer(self)
self.timer_main.setInterval(20)
self.timer_main.timeout.connect(self.run)
self.timer_main.start()
self.presskeys = []
self.timer_key = QtCore.QTimer(self)
self.timer_key.setInterval(50)
self.timer_key.timeout.connect(self.handleKey)
def enemyToScene(self):
self.enemy_interval += 1
if self.enemy_interval < 30:
return
self.enemy_interval = 0
for i in range(self.settings.enemy_num):
if self.enemys[i].free:
self.enemys[i].free = False
self.enemys[i].x = randint(0, self.settings.game_width-self.enemys[i].img.width())
self.enemys[i].y = -self.enemys[i].img.height()
break
def collisionDetect(self):
for i in range(self.settings.enemy_num):
if self.enemys[i].free:
continue
for j in range(self.settings.bullet_num):
if self.hero.bullets[j].free:
continue
if self.enemys[i].rect.intersects(self.hero.bullets[j].rect):
self.enemys[i].free = True
self.hero.bullets[j].free = True
def handleKey(self):
x = self.hero.x
y = self.hero.y
if self.presskeys.count(QtCore.Qt.Key_Left) > 0:
x -= self.hero.speed
if self.presskeys.count(QtCore.Qt.Key_Right) > 0:
x += self.hero.speed
if self.presskeys.count(QtCore.Qt.Key_Space) > 0:
self.hero.shoot()
if self.presskeys.count(QtCore.Qt.Key_Q) > 0:
self.close()
self.hero.move(x, y)
def keyPressEvent(self, event):
'''按键按下时,自动调用此函数'''
self.presskeys.append(event.key())
if not self.timer_key.isActive():
self.timer_key.start()
def keyReleaseEvent(self, event):
'''松开按键时,自动调用'''
if self.timer_key.isActive() and len(self.presskeys) == 0:
self.timer_key.stop()
self.handleKey()
while event.key() in self.presskeys:
self.presskeys.remove(event.key())
def updatePosition(self):
self.map.move()
'''更新子弹的位置'''
for i in range(self.settings.bullet_num):
if not self.hero.bullets[i].free:
self.hero.bullets[i].move()
'''更新敌机位置'''
for i in range(self.settings.enemy_num):
if not self.enemys[i].free:
self.enemys[i].move()
def run(self):
self.enemyToScene()
self.updatePosition()
self.update()
self.collisionDetect()
def paintEvent(self, event):
painter = QtGui.QPainter(self)
painter.drawPixmap(0, self.map.y1, self.map.img1)
painter.drawPixmap(0, self.map.y2, self.map.img2)
painter.drawPixmap(self.hero.x, self.hero.y, self.hero.img)
'''绘制子弹'''
for i in range(self.settings.bullet_num):
if not self.hero.bullets[i].free:
painter.drawPixmap(self.hero.bullets[i].x, self.hero.bullets[i].y, self.hero.bullets[i].img)
'''绘制敌机'''
for i in range(self.settings.enemy_num):
if not self.enemys[i].free:
painter.drawPixmap(self.enemys[i].x, self.enemys[i].y, self.enemys[i].img)
if __name__ == "__main__":
app = QtWidgets.QApplication([])
w = MainScene()
w.show()
app.exec_()
参数设定代码settings.py:
app.ico图片为游戏运行界面左上角图标,随便用那张图片都行,例如直接使用我方飞机图片重命名即可
class Settings:
def __init__(self):
self.game_title = "python飞机大战"
self.game_ico = r"D:\images\app.ico"
self.game_width = 512
self.game_height = 700
self.map_path = r"D:\images\img_bg_level_2.png"
self.map_speed = 2
self.hero_path = r"D:\images\hero2.png"
self.hero_speed = 20
self.bullet_path = r"D:\images\bullet_11.png"
self.bullet_speed = 10
self.bullet_num = 10
self.enemy_path = r"D:\images\img-plane_5.png"
self.enemy_speed = 3
self.enemy_num = 10
敌机代码enemy.py:
from PySide2 import QtGui
from settings import Settings
class Enemy:
def __init__(self):
self.settings = Settings()
self.img = QtGui.QPixmap(self.settings.enemy_path)
self.rect = self.img.rect()
self.x = 0
self.y = 0
self.free = True
self.speed = self.settings.enemy_speed
def move(self):
if self.free:
return
self.y += self.speed
self.rect.moveTo(self.x, self.y)
if self.y >= self.settings.game_height:
self.free = True
我方飞机代码hero.py:
from PySide2 import QtGui
from settings import Settings
from bullet import Bullet
class Hero:
def __init__(self):
self.settings = Settings()
self.img = QtGui.QPixmap(self.settings.hero_path)
self.x = self.settings.game_width*0.5 - self.img.width()*0.5
self.y = self.settings.game_height - self.img.height()
self.speed = self.settings.hero_speed
# 创建一个弹夹,放入10颗子弹
self.bullets = []
for i in range(self.settings.bullet_num):
self.bullets.append(Bullet())
def move(self, x, y):
'''飞机移动'''
self.x = x
self.y = y
def shoot(self):
'''飞机发射子弹,按空格键时,调用此函数'''
for i in range(self.settings.bullet_num):
if self.bullets[i].free:
self.bullets[i].free = False
self.bullets[i].x = self.x + self.img.width()*0.5 - self.bullets[i].img.width()*0.5
self.bullets[i].y = self.y - 30
break
地图代码map.py:
from PySide2 import QtGui
from settings import Settings
class Map:
def __init__(self):
self.settings = Settings()
self.img1 = QtGui.QPixmap(self.settings.map_path)
self.img2 = QtGui.QPixmap(self.settings.map_path)
self.y1 = -self.settings.game_height
self.y2 = 0
def move(self):
self.y1 += self.settings.map_speed
if self.y1 >= 0:
self.y1 = -self.settings.game_height
self.y2 += self.settings.map_speed
if self.y2 >= self.settings.game_height:
self.y2 = 0
子弹代码bullet.py:
from PySide2 import QtGui
from settings import Settings
class Bullet:
def __init__(self):
self.settings = Settings()
self.img = QtGui.QPixmap(self.settings.bullet_path)
self.rect = self.img.rect()
self.speed = self.settings.bullet_speed
self.x = 0
self.y = 0
self.free = True
def move(self):
if self.free:
return
self.y -= self.speed
self.rect.moveTo(self.x, self.y)
if self.y <= -self.rect.height():
self.free = True
运行方式:运行main.py即可
注意点一:
如果直接用文章中的背景图保存到本地后运行代码,运行结果如下:
于是自己替换了背景图片,和样例中的背景图调整像素到了一样大小:512x768(游戏界面参数设定是512和700,背景图大小有差别不影响,自己觉得可以就行):
替换背景后运行结果如下:
运行结果视频展示(方向键左右移动,空格射击):
20221011_182548
注意点二:
需要安装Pyside2依赖库,直接pip install Pyside2即可,如果出现
qt.qpa.plugin: Could not load the Qt platform plugin "windows" in "" even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.
Available platform plugins are: minimal, offscreen, windows.错误
参照以下方法解决
关于qt.qpa.plugin: Could not load the Qt platform plugin “windows“ in ““ even though it was found.问题_ASS-ASH的博客-CSDN博客