赞
踩
创建项目文件plane war,在项目文件里放入写游戏脚本需要用到的图片文件images以及新建一个新的文件plane放入py文件(main.py和sprites.py)
images文件压缩包链接:
https://pan.baidu.com/s/1w8xlEhW1JaCGzmjDIs0gyA
提取码:w873
下一步,在pycharm的终端输入
pip install pygame
安装好编写游戏需要调用到的模块
输入 python -m pygame.examples.aliens 进行验证
出现如上界面即pygame模块安装成功
主函数代码:
- from sprites import*
-
-
- class PlaneGame:
- """飞机作战主游戏类"""
- def __init__(self):
- print("游戏初始化")
-
- # 1.创建游戏窗口
- self.screen = pygame.display.set_mode(SCREEN_RECT.size)
- # 2.创建游戏时钟
- self.clock = pygame.time.Clock()
- # 3.创建精灵和精灵组
- self.__create_sprites()
- pygame.time.set_timer(HERO_FIRE_EVENT, BULLET_INTERVAL_TIME)
- pygame.time.set_timer(CREATE_ENEMY_EVENT, ENEMY_INTERVAL_TIME)
-
- def __create_sprites(self):
- """创建精灵和精灵组"""
- bg1 = Background()
- bg2 = Background(True)
- self.back_group = pygame.sprite.Group(bg1, bg2)
-
- self.hero = Hero()
- self.hero_group = pygame.sprite.Group(self.hero)
-
- self.enemy_group = pygame.sprite.Group()
-
- def __update_sprites(self):
- """更新精灵和精灵组"""
- self.back_group.update()
- self.back_group.draw(self.screen)
-
- self.hero_group.update()
- self.hero_group.draw(self.screen)
-
- self.hero.bullet_group.update()
- self.hero.bullet_group.draw(self.screen)
-
- self.enemy_group.update()
- self.enemy_group.draw(self.screen)
-
- def start_game(self):
- """开始游戏"""
- print("开始游戏。。。")
-
- while True:
- # 1.设置刷新帧率
- self.clock.tick(60)
- # 2.事件监听
- self.__event_handler()
- # 3.碰撞检测
- self.__check_collide()
- # 4.更新精灵和精灵组
- self.__update_sprites()
- # 5.更新屏幕显示
- pygame.display.update()
-
- def __event_handler(self):
- """事件监听"""
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- PlaneGame.__game_over()
- elif event.type == HERO_FIRE_EVENT:
- self.hero.fire()
- elif event.type == CREATE_ENEMY_EVENT:
- self.enemy_group.add(Enemy())
-
- keys_pressed = pygame.key.get_pressed()
-
- if keys_pressed[pygame.K_RIGHT]:
- self.hero.speed = HERO_SPEED
- elif keys_pressed[pygame.K_LEFT]:
- self.hero.speed = -HERO_SPEED
- else:
- self.hero.speed = 0
-
- def __check_collide(self):
- """碰撞检测"""
- # 子弹摧毁敌机
- pygame.sprite.groupcollide(self.hero.bullet_group, self.enemy_group, True, True)
- # 敌机摧毁英雄
- enemies = pygame.sprite.spritecollide(self.hero, self.enemy_group, True)
-
- if len(enemies):
- self.hero.kill()
- self.__game_over()
-
- @staticmethod
- def __game_over():
- """游戏结束"""
- print("游戏结束。。。")
-
- pygame.quit()
- exit()
-
-
- if __name__ == '__main__':
- game = PlaneGame()
- game.start_game()
精灵组和精灵组函数完整代码:
- # -*- coding:utf-8 -*-
- import pygame
- from random import randint
-
- # 游戏屏幕大小
- SCREEN_RECT = pygame.Rect(0, 0, 480, 700)
-
- # 背景
- BACK_IMAGE_PATH = "./images/background.png"
- BACK_SPEED = 1 # 背景向下移动
-
- # 子弹
- BULLET_IMAGE_PATH = "./images/bullet.png"
- BULLET_SPEED = -2 # 子弹向上移动
- BULLET_INTERVAL_TIME = 500 # 子弹产生时间差
-
- # 英雄
- HERO_IMAGE_PATH = "./images/hero.png"
- HERO_START_BOTTOM_HEIGHT = 120
- HERO_BULLET_NUMBER = 3
- HERO_BULLET_DISTANCE = 20
- HERO_FIRE_EVENT = pygame.USEREVENT
- HERO_SPEED = 2
-
- # 敌机
- ENEMY_IMAGE_PATH = "./images/enemy.png"
- ENEMY_MIN_SPEED = 1
- ENEMY_MAX_SPEED = 1
- CREATE_ENEMY_EVENT = pygame.USEREVENT + 1
- ENEMY_INTERVAL_TIME = 1000 # 敌机产生的时间 间隔
-
-
- class GameSprite(pygame.sprite.Sprite):
- """游戏精灵基类"""
- def __init__(self, image_path, speed=0):
- super().__init__()
-
- self.image = pygame.image.load(image_path)
- self.rect = self.image.get_rect()
- self.speed = speed
-
- def update(self, *args):
- self.rect.y += self.speed
-
-
- class Background(GameSprite):
- """游戏背景精灵"""
-
- def __init__(self, is_alt=False):
- super().__init__(BACK_IMAGE_PATH, BACK_SPEED)
-
- # 判断是否为交替图片,如果是,将图片设置到屏幕顶端
- if is_alt:
- self.rect.y = -self.rect.height
-
- def update(self, *args):
- super().update()
-
- # 判断是否移出屏幕,如果移出屏幕,将图像设置到屏幕上方
- if self.rect.y >= SCREEN_RECT.height:
- self.rect.y = -self.rect.height
-
-
- class Bullet(GameSprite):
- """子弹精灵"""
-
- def __init__(self):
- super().__init__(BULLET_IMAGE_PATH, BULLET_SPEED)
-
- def update(self, *args):
- super().update()
-
- # 判断是否移出屏幕,如果移出屏幕,将图像设置到屏幕上方
- if self.rect.bottom < 0:
- self.kill()
-
-
- class Hero(GameSprite):
- """英雄精灵"""
-
- def __init__(self):
- super().__init__(HERO_IMAGE_PATH)
-
- self.rect.centerx = SCREEN_RECT.centerx
- self.rect.bottom = SCREEN_RECT.bottom - HERO_START_BOTTOM_HEIGHT
-
- self.bullet_group = pygame.sprite.Group()
-
- def update(self, *args):
- self.rect.x += self.speed
-
- if self.rect.left < 0:
- self.rect.left = 0
- if self.rect.right > SCREEN_RECT.right:
- self.rect.right = SCREEN_RECT.right
-
- def fire(self):
- for i in range(HERO_BULLET_NUMBER):
- hero_bullet = Bullet()
-
- hero_bullet.rect.bottom = self.rect.y - i * HERO_BULLET_DISTANCE
- hero_bullet.rect.centerx = self.rect.centerx
-
- self.bullet_group.add(hero_bullet)
-
-
- class Enemy(GameSprite):
- """敌机精灵"""
-
- def __init__(self):
- super().__init__(ENEMY_IMAGE_PATH)
-
- self.speed = randint(ENEMY_MIN_SPEED, ENEMY_MAX_SPEED)
-
- self.rect.bottom = 0
- self.rect.x = randint(0, SCREEN_RECT.width - self.rect.width)
-
- def update(self, *args):
- super().update()
-
- if self.rect.y >= SCREEN_RECT.height:
- self.kill()
将游戏打包成exe格式
第一步: 终端安装pyinstaller模块
pip install pyinstaller
可以在终端输入pip list 查看是否有安装有pyinstaller模块
第二步 用cd操作切入保存py代码文件的目录
第三步 在终端输入打包命令
pyinstaller -F -w main.py sprites.py(要打包的脚本文件)
-F的含义是直接打包成exe,不生成dll等依赖文件
-w的含义是不带控制台的打包,就是生成的程序没有黑边
生成的exe在dist里
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。