文章目录
一、环境配置
1.使用工具
windows系统
pycharm软件
2.安装pygame模块
参考链接:https://jingyan.baidu.com/article/c45ad29c671fe6051753e215.html
导入模块成功即可:

二、飞机作战实例
项目准备
1.新建飞机大作战项目
2.新建两个空白py文件
3.导入游戏素材图片
源代码
1.main.py
import pygame
from plane_sprites import *
class PlaneGame(object):
def __init__(self):
pygame.init()
# 创建游戏的窗口
self.game_screen = pygame.display.set_mode(SCREEN_RECT.size)
# 创建游戏的时钟
self.clock = pygame.time.Clock()
# 调用私有方法,精灵和精灵组
self.__create_sprites()
# 设置定时器事件
pygame.time.set_timer(CREATE_ENEMY_EVENT, 1000)
pygame.time.set_timer(HERO_FIRE_EVENT, 600)
def __create_sprites(self):
# 创建背景精灵和精灵组
bg = Background()
bg1 = Background(True)
self.bg_group = pygame.sprite.Group(bg, bg1)
# 创建敌机的精灵组
self.enemy_group = pygame.sprite.Group()
# 创建英雄的精灵和精灵组
self.hero = Hero()
self.hero_group = pygame.sprite.Group(self.hero)
def start_game(self):
print("游戏开始。。。")
while True:
# 1.设置刷新帧率
self.clock.tick(FRAME_PRE_SEC)
# 2.事件监听
self.__event_handle()
# 3.碰撞检测
self.__check_collide()
# 4.更新/绘制精灵组
self.__update_sprites()
# 5.更新显示
p

最低0.47元/天 解锁文章
723





