python 打飞机项目 ( 让敌机发射子弹 )
main.py
主程序文件
# -*- coding:utf-8 -*-
import pygame, time
from Plane import Plane
from HeroPlane import HeroPlane
from pygame.locals import *
def key_control (plane_temp):
# 获取事件,比如按键等
for event in pygame.event.get():
# 判断是否是点击了退出按钮
if event.type == QUIT:
print("exit")
exit()
# 判断是否是按下了键
elif event.type == KEYDOWN:
# 检测按键是否是a或者left
if event.key == K_a or event.key == K_LEFT:
print('left')
plane_temp.move_left()
# 检测按键是否是d或者right
elif event.key == K_d or event.key == K_RIGHT:
print('right')
plane_temp.move_right()
# 检测按键是否是空格键
elif event.key == K_SPACE:
print('space')
plane_temp.fire()
def main ():
screen = pygame.display.set_mode((480, 852), 0, 32)
background = pygame.image.load("./resource/background.png")
# 创建一个飞机对象
plane = Plane(screen, 210, 700)
# 创建敌机对象
enemyPlane = HeroPlane(screen, 0, 0)
while True:
screen.blit(background, (0, 0))
plane.display()
enemyPlane.display() # 敌机显示
enemyPlane.move() # 敌机开火
enemyPlane.fire() # 敌机开火
key_control(plane) # 键盘事件监听
pygame.display.update() # 更新窗口
time.sleep(0.01) # 延时0.01秒,防止程序内存溢出
if __name__ == '__main__':
main()
Plane.py
飞机对象文件
# -*- coding:utf-8 -*-
import pygame
from Bullet import Bullet
class Plane(object):
image = None
bullet_list = []
def __init__ (self, screen_temp, x, y):
self.screen = screen_temp
self.x = x
self.y = y
self.img_load()
# 飞机赋值图片对象
def img_load (self):
self.image = pygame.image.load("./resource/hero1.png")
def display (self):
self.screen.blit(self.image, (self.x, self.y)) # 显示飞机
for bullet in self.bullet_list: # 循环取出子弹对象
# 判断子弹是否越界
if bullet.judge():
self.bullet_list.remove(bullet) # 如果子弹越界就删除子弹
bullet.display() # 显示子弹
bullet.move() # 子弹移动步长
# 飞机向左移动偏移量
def move_left (self):
self.x -= 10
# 飞机向右移动偏移量
def move_right (self):
self.x += 10
# 将飞机创建的子弹对象存储进 bullet_list 列表中
def fire (self):
self.bullet_list.append(Bullet(self.screen, self.x, self.y))
HeroPlane.py
敌机对象文件
# -*- coding:utf-8 -*-
import pygame
import random
from EnemyBullet import EnemyBullet
class HeroPlane(object):
# 定义一个类属性用来保存
direction = 'right'
# 定义一个空列表用来保存敌机子弹对象
bullet_list = []
def __init__(self, screen_temp, x, y):
self.screen = screen_temp
self.x = x
self.y = y
self.img_load()
# 敌机背景图片赋值
def img_load(self):
self.image = pygame.image.load("./resource/enemy-1.gif")
# 显示敌机
def display(self):
self.screen.blit(self.image, (self.x, self.y))
# 敌机子弹对象
for bullet in self.bullet_list:
# 判断子弹是否越界
if bullet.judge():
self.bullet_list.remove(bullet) # 子弹越界就删除
bullet.display() # 显示敌机子弹
bullet.move() # 显示敌机子弹移动步长
# 敌机移动轨迹
def move(self):
# 敌机创建的子弹默认向右移动
if self.direction == 'right':
self.x += 5 # 每次向右移动增加 5px 的步长
elif self.direction == 'left': # 向左移动
self.x -= 5 # 每次向左移动减少 5px 的步长
# 当敌机向右移动到了边界就向左移动
if self.x > 480 - 50: # 480 是界面总宽度; 50 是飞机宽度. 所以敌机移动的距离应该是界面宽度-敌机宽度 ( 移动距离 = 界面宽度 - 敌机宽度 )
self.direction = 'left'
elif self.x <= 0: # 当敌机移动到了最左边就会继续向右移动
self.direction = 'right'
# 开火
def fire(self):
random_temp = random.randint(1, 100) # 随机生成 1 - 100的随机数
if (random_temp == 20) or (random_temp == 78): # 随机数概率
self.bullet_list.append(EnemyBullet(self.screen, self.x, self.y)) # 创建敌机子弹对象
EnemyBullet.py
敌机子弹对象
# -*- coding:utf-8 -*-
import pygame
class EnemyBullet(object):
def __init__ (self, screen_temp, x, y):
self.x = x + 30
self.y = y + 30
self.screen = screen_temp
self.img_load()
# 敌机子弹背景
def img_load(self):
self.image = pygame.image.load("./resource/bullet-1.gif")
# 显示敌机子弹
def display(self):
self.screen.blit(self.image, (self.x, self.y))
# 子弹移动步长
def move(self):
self.y += 20
# 判断子弹y轴是否已经越界
def judge(self):
if self.y > 890: # 890 界面总高度
return True
else:
return False
本作品采用《CC 协议》,转载必须注明作者和本文链接
by JeffreyBool blog :point_right: link