python飞机大战游戏背景(python实现飞机大战游戏)
类别:脚本大全 浏览量:2867
时间:2021-10-13 00:09:22 python飞机大战游戏背景
python实现飞机大战游戏飞机大战(Python)代码分为两个python文件,工具类和主类,需要安装pygame模块,能完美运行(网上好多不完整的,调试得心累。实现出来,成就感还是满满的),如图所示:
完整代码如下:
1.工具类plane_sprites.py
|
import random import pygame # 屏幕大小的常量 SCREEN_RECT = pygame.Rect( 0 , 0 , 480 , 700 ) # 刷新的帧率 FRAME_PER_SEC = 60 # 创建敌机的定时器常量 CREATE_ENEMY_EVENT = pygame.USEREVENT # 英雄发射子弹事件 HERO_FIRE_EVENT = pygame.USEREVENT + 1 class GameSprite(pygame.sprite.Sprite): """飞机大战游戏精灵""" def __init__( self , image_name, speed = 1 ): super ().__init__() # 调用父类的初始化方法 self .image = pygame.image.load(image_name) # 定义对象的属性 self .rect = self .image.get_rect() self .speed = speed def update( self ): # 在屏幕的垂直方向上移动 self .rect.y + = self .speed class Background(GameSprite): """游戏背景精灵""" def __init__( self , is_alt = False ): super ().__init__( "C:/Users/Administrator/Desktop/background.jpg" alt="python飞机大战游戏背景(python实现飞机大战游戏)" border="0" /> if is_alt: # 2. 判断是否是交替图像,如果是,需要设置初始位置 self .rect.y = - self .rect.height def update( self ): super ().update() # 1. 调用父类的方法实现 if self .rect.y > = SCREEN_RECT.height: # 2. 判断是否移出屏幕,如果移出屏幕,将图像设置到屏幕的上方 self .rect.y = - self .rect.height class Enemy(GameSprite): """敌机精灵""" def __init__( self ): super ().__init__( "C:/Users/Administrator/Desktop/enemy1.jpg" alt="python飞机大战游戏背景(python实现飞机大战游戏)" border="0" /> self .speed = random.randint( 1 , 3 ) # 2. 指定敌机的初始随机速度 1 ~ 3 self .rect.bottom = 0 # 3. 指定敌机的初始随机位置 max_x = SCREEN_RECT.width - self .rect.width self .rect.x = random.randint( 0 , max_x) def update( self ): super ().update() # 1. 调用父类方法,保持垂直方向的飞行 # 2. 判断是否飞出屏幕,如果是,需要从精灵组删除敌机 if self .rect.y > = SCREEN_RECT.height: self .kill() # print("飞出屏幕,需要从精灵组删除...") # kill方法可以将精灵从所有精灵组中移出,精灵就会被自动销毁 def __del__( self ): # print("敌机挂了 %s" % self.rect) pass class Hero(GameSprite): """英雄精灵""" def __init__( self ): super ().__init__( "C:/Users/Administrator/Desktop/me1.jpg" alt="python飞机大战游戏背景(python实现飞机大战游戏)" border="0" /> self .rect.centerx = SCREEN_RECT.centerx self .rect.bottom = SCREEN_RECT.bottom - 120 self .bullets = pygame.sprite.Group() def update( self ): self .rect.x + = self .speed if self .rect.x < 0 : self .rect.x = 0 elif self .rect.right > SCREEN_RECT.right: self .rect.right = SCREEN_RECT.right def fire( self ): print ( "发射子弹..." ) for i in ( 0 , 1 , 2 ): bullet = Bullet() bullet.rect.bottom = self .rect.y - i * 20 bullet.rect.centerx = self .rect.centerx self .bullets.add(bullet) class Bullet(GameSprite): """子弹精灵""" def __init__( self ): super ().__init__( "C:/Users/Administrator/Desktop/bullet1.jpg" alt="python飞机大战游戏背景(python实现飞机大战游戏)" border="0" /> def update( self ): super ().update() if self .rect.bottom < 0 : self .kill() def __del__( self ): print ( "子弹被销毁..." ) |
2.主类plane_main.py
|
import pygame from plane_sprites import * class PlaneGame( object ): """飞机大战主游戏""" def __init__( self ): print ( "游戏初始化" ) self .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, 500 ) def __create_sprites( self ): bg1 = Background() bg2 = Background( True ) self .back_group = pygame.sprite.Group(bg1, bg2) self .enemy_group = pygame.sprite.Group() self .hero = Hero() self .hero_group = pygame.sprite.Group( self .hero) def start_game( self ): print ( "游戏开始..." ) while True : self .clock.tick(FRAME_PER_SEC) self .__event_handler() self .__check_collide() self .__update_sprites() pygame.display.update() def __event_handler( self ): for event in pygame.event.get(): if event. type = = pygame.QUIT: PlaneGame.__game_over() elif event. type = = CREATE_ENEMY_EVENT: # print("敌机出场...") # 创建敌机精灵 enemy = Enemy() self .enemy_group.add(enemy) elif event. type = = HERO_FIRE_EVENT: self .hero.fire() # elif event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT: # print("向右移动...") keys_pressed = pygame.key.get_pressed() if keys_pressed[pygame.K_RIGHT]: self .hero.speed = 2 elif keys_pressed[pygame.K_LEFT]: self .hero.speed = - 2 else : self .hero.speed = 0 def __check_collide( self ): for event in pygame.event.get(): if event. type = = pygame.QUIT: PlaneGame.__game_over() elif event. type = = CREATE_ENEMY_EVENT: enemy = Enemy() self .enemy_group.add(enemy) elif event. type = = HERO_FIRE_EVENT: self .hero.fire() keys_pressed = pygame.key.get_pressed() if keys_pressed[pygame.K_RIGHT]: self .hero.speed = 2 elif keys_pressed[pygame.K_LEFT]: self .hero.speed = - 2 else : self .hero.speed = 0 def __check_collide( self ): pygame.sprite.groupcollide( self .hero.bullets, self .enemy_group, True , True ) enemies = pygame.sprite.spritecollide( self .hero, self .enemy_group, True ) if len (enemies) > 0 : self .hero.kill() PlaneGame.__game_over() def __update_sprites( self ): self .back_group.update() self .back_group.draw( self .screen) self .enemy_group.update() self .enemy_group.draw( self .screen) self .hero_group.update() self .hero_group.draw( self .screen) self .hero.bullets.update() self .hero.bullets.draw( self .screen) @staticmethod def __game_over(): print ( "游戏结束" ) pygame.quit() exit() if __name__ = = '__main__' : game = PlaneGame() game.start_game() |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持开心学习网。
原文链接:https://blog.csdn.net/weixin_44193909/article/details/88964137
您可能感兴趣
- python字符串之基本操作小练习(Python数据类型之String字符串实例详解)
- python获取json结果保存文本(Python JSON格式数据的提取和保存的实现)
- python循环语句嵌套使用(Python分支语句与循环语句应用实例分析)
- python元组和列表的定义(Python基本数据结构与用法详解列表、元组、集合、字典)
- python栈和队列(Python 实现数据结构中的栈队列)
- python列表切片和分片(Python列表切片操作实例总结)
- python高级面试题及答案(python面试题小结附答案实例代码)
- python3.8爬虫需要的包(python爬取基于m3u8协议的ts文件并合并)
- python群聊提示(python-itchat 统计微信群、好友数量,及原始消息数据的实例)
- python下的sql处理(python中aioysql异步操作MySQL的方法)
- python的4种数字变量(Python将字符串常量转化为变量方法总结)
- pythonssh登录服务器(对python 通过ssh访问数据库的实例详解)
- python代码返回值(Python 函数返回值的示例代码)
- python加密与解密(python实现简单加密解密机制)
- python表格导出为图片(python生成带有表格的图片实例)
- python如何判断文件是否结束(python判断文件是否存在,不存在就创建一个的实例)
- 冬天来了手脚冰凉 真不是因为上辈子你是折翼的天使(冬天来了手脚冰凉)
- 0 1 岁婴儿最强作息指南,照着做养出天使宝宝(01岁婴儿最强作息指南)
- 沪上这16所高校 萌新 礼包开箱 哪一款让你心动(沪上这16所高校萌新)
- 她救了被绑架的他,而这一切竟是一场阴谋...(她救了被绑架的他)
- 冬季养殖这6种阴生植物,方便又好养,你家有么(冬季养殖这6种阴生植物)
- 阴生植物为什么不怕照不到阳光(阴生植物为什么不怕照不到阳光)
热门推荐
- 如何判断数据是json还是字符串(JSON.stringify的多种用法总结)
- html5input标签的默认属性(移动端HTML5 input常见问题小结)
- dedecms屏蔽规则(织梦dedecms修改任意管理员漏洞处理方法)
- php内置加密函数(php过滤htmlspecialchars 函数实现把预定义的字符转换为 HTML 实体用法分析)
- windows下nginx负载均衡配置(使用nginx配置访问wgcloud的方法)
- reactnative动态设置值(react native实现监控手势上下拉动效果)
- linux常用的参数类型和参数代码(浅析Linux resolv.conf)
- APP运营该注重的细节
- dedecms中的有些功能如何修改(DedeCms参考手册、函数及文件大全)
- 织梦cms文章页如何跳转(织梦CMS文档读取频道信息失败,无法进行后续操作!)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9