pygame简介
pygame是被设计用来写游戏的python模块集合,pygame是在优秀的SDL库之上开发的功能性包。使用python可以导入pygame来开发具有全部特性的游戏和多媒体软件,pygame是极度轻便的并且可以运行在几乎所有的平台和操作系统上。
pygame的安装
pip install pygame
不了解如何安装的可以看下面的的文章
主要功能实现与详解
pygame内置许多方便的函数,很多功能直接调用即可
显示界面
import pygame
# 导入对应的包
pygame.init()
# 初始化包函数
screen = pygame.display.set_mode((800, 600))
# 创建窗口,后面的参数代表窗口大小
pygame.display.set_caption("简易飞机大战")
# 定义标题,后面的参数代表标题名称
while True:
# 一直不间断的运行游戏窗口
pygame.display.update()
# 刷新游戏界面
接收按键事件并响应事件
import pygame
from pygame.locals import *
# 将locals中定义好的所有全局变量导入
def control():
for event in pygame.event.get():
# 判断事件队列中是否有事件发生
if event.type == QUIT:
# 判断是否选中窗口的退出按钮
python.quit()
# 退出pygame模块
exit()
# 结束运行中的代码
elif event.type == KEYDOWN:
# 按键按下时
if event.key == K_a:
# 按住A时
elif event.type == KEYUP:
# 按键松开时
if event.key == K_a:
# 松开A时
碰撞测试
def collision_check(a, b):
temp1 = (b.x <= a.x + a.width <= b.x + b.width)
temp2 = (b.y <= a.y + a.height <= b.y + b.height)
return temp1 and temp2
a与b分别为两个可能碰撞的图形,因为可能是a撞b或b撞a所以需要
if collision_check(a,b) or collision_check(b,a):
print("发生碰撞")
碰撞测试原理图
在界面上显示图片
screen.blit(img, (x, y))
# 界面的变量名为screen
# 对应的图片路径为img
# 添加后坐标x
# 添加后坐标y
显示文字
my_font = pygame.font.SysFont("SimHei", 60)
# 设置文字类型与文字字号
text = my_font.render("分数:"+str(num), True, color)
# 设置文字内容和颜色,True代表是否抗锯齿,增加字体质量
screen.blit(text, (10, 0))
# 设置字体显示位置
完整代码
使用面向对象编程思想进行书写,定义飞机类,敌人飞机类,我方飞机类,界面类,事件函数,子弹类。与pygame包配合最终实现简易飞机大战
aircraft(飞机类)
import pygame
# 飞机类
class Aircraft(object):
x = 0
y = 0
screen = ''
img = ''
def __init__(self, x, y, screen, path):
# 构造方法
self.x = x
self.y = y
self.screen = screen
self.img = pygame.image.load(path)
hero(主角飞机类)
from aircraft import Aircraft
from bullet import Bullet
import time
import key
'''
主角飞机类
'''
class Hero(Aircraft):
# Aircraft代表父类
bullets = []
key = {"left": False, "right": False, "space": False}
# 代表对应的按键是否被按下
def __init__(self, screen):
# 构造方法
Aircraft.__init__(self, 320, 520, screen, "../img/hero.png")
# 继承父类
def display(self, enemy):
# 显示图片
self.screen.blit(self.img, (self.x, self.y))
if len(self.bullets) > 0:
# 如果列表中有子弹
for b in self.bullets:
if b[1] <= 0:
self.bullets.remove(b)
# 当子弹越界时,删除子弹
for i in range(0, len(self.bullets)):
x = self.bullets[i][0]
y = self.bullets[i][1]
bullet = Bullet(x, y - 2, self.screen)
# 根据定位重新显示子弹
bullet.display()
# 重新显示子弹
self.bullets[i][1] = y - 2
# 子弹坐标修改
if key.collision_check(bullet, enemy) or key.collision_check(enemy, bullet):
# 是否碰撞
return True
return False
def move(self):
if self.key["left"]:
self.x -= 2.5
elif self.key["right"]:
self.x += 2.5
if self.x > 800:
self.x = 0
# 越界处理
elif self.x < 0:
self.x = 800
# 越界处理
def key_down(self, direction):
# 按下按键时
self.key[direction] = True
def key_up(self, direction):
# 松开按键时
self.key[direction] = False
def bullet(self):
bullet = Bullet(self.x+40, self.y-20, self.screen)
bullet.display()
mob = [self.x+40, self.y-20]
# 生成子弹
self.bullets.append(mob)
# 把子弹放入列表中
enemy(敌人飞机类)
import pygame
import random
from aircraft import Aircraft
import time
'''
敌人飞机类
'''
class Enemy(Aircraft):
width = 57
height = 43
def __init__(self, x, y, screen):
Aircraft.__init__(self, x, y, screen, "../img/enemy.png")
def display(self):
# 显示图片
self.screen.blit(self.img, (self.x, self.y))
def move(self):
i = random.randint(1, 100)
if i == 2 or i == 4:
self.x += 10
elif i == 3 or i == 5:
self.x -= 10
if self.x > 800:
self.x = 0
# 越界处理
elif self.x < 0:
self.x = 800
# 越界处理
views(界面类)
import pygame
# pygame包
import time
# python自带的时间包
from hero import Hero
# 从hero文件导入Hero类
from enemy import Enemy
# 从enemy文件导入Enemy类
import key
# 导入key文件
import random
# 导入python自带的随机数包
'''
主界面
'''
def start():
num = 0
pygame.init()
# 初始化函数
screen = pygame.display.set_mode((800, 600))
# 创建窗口
pygame.display.set_caption("简易飞机大战")
# 定义标题
hero = Hero(screen)
# 创建主角对象
flag = 0
# 判断敌人飞机是否存活
my_font = pygame.font.SysFont("SimHei", 60)
# 设置文本格式
color = 0, 255, 0
while True:
# 游戏不断循环运行
if not flag:
# 当地图上没有飞机时
x = random.randint(100, 700)
# 生成100 到 700 的随机数
y = random.randint(10, 150)
# 生成 10 到1 50 的随机数
enemy = Enemy(x, y, screen)
# 创建敌人飞机
flag = 1
# 代表已经有飞机
screen.fill((0, 0, 0))
# 填充背景色的RGB
text = my_font.render("分数:"+str(num), True, color)
# 显示文本
screen.blit(text, (10, 0))
if flag:
# 自动移动敌人飞机
enemy.display()
# 显示
enemy.move()
# 移动
t = hero.display(enemy)
# 显示主角飞机
if t:
flag = 0
num += 1
hero.move()
# 移动主角飞机
key.control(hero)
# 获取事件
pygame.display.update()
# 刷新游戏界面
time.sleep(0.01)
# 避免移动量过大,让CPU进行微小的睡眠
bullet(子弹类)
import pygame
'''
子弹类
'''
class Bullet(object):
# 子弹类
x = 0
y = 0
width = 5
height = 11
screen = ''
img = ''
def __init__(self, x, y, screen):
self.x = x
self.y = y
self.screen = screen
self.img = pygame.image.load("../img/bullet.png")
def display(self):
self.screen.blit(self.img, (self.x, self.y))
key(事件函数)
import pygame
from pygame.locals import *
'''
获取事件
'''
def control(hero):
# 用于获取事件
for event in pygame.event.get():
# 判断是否有按键事件发生
if event.type == QUIT:
# 判断是否选中退出按钮
pygame.quit()
# 退出pygame
exit()
# 退出系统
elif event.type == KEYDOWN:
if event.key == K_a:
hero.key_down("left")
elif event.key == K_d:
hero.key_down("right")
elif event.key == K_SPACE:
hero.bullet()
elif event.type == KEYUP:
if event.key == K_a:
hero.key_up("left")
elif event.key == K_d:
hero.key_up("right")
def collision_check(a, b):
temp1 = (b.x <= a.x + a.width <= b.x + b.width)
temp2 = (b.y <= a.y + a.height <= b.y + b.height)
return temp1 and temp2
main(程序入口)
import views
'''
程序入口
'''
if __name__ == '__main__':
views.start()
程序演示
写在最后
本程序中部分教程与素材均来源于CSDN,博客园,Github,侵删
关于pygame更多介绍可以访问这个大佬的CSDN:来自江南的你
Comments | 4 条评论
快递代发,礼品代发就发5a单号网www.danhw.com
淘宝空包代发单号网www.kuaidzj.com
单号购买 快递购买 空包代发www.uudanhaowang.com
最便宜快递单号网 快递空包网www.88danhw.com