基于pygame做的小游戏,pygame开发的游戏

  基于pygame做的小游戏,pygame开发的游戏

  Python飞机大战系列文章(按顺序)

  (A)通过pygame将您自己的图像添加到游戏中

  (B)让游戏角色通过pygame移动。

  (C)通过pygame处理用户的鼠标和键盘操作(事件和监控)

  (4)详细解释pygame中的精灵和精灵群。

  (E)通过pygame建立游戏框架

  (6)通过pygame使游戏的背景图像交替滚动。

  (7)使用pygame设置飞机战中敌机的速度和位置。

  (8)通过pygame操纵游戏角色的移动

  (9)通过pygame让游戏角色发射子弹

  (10)py game的碰撞检测

  文章阐明了主程序的职责和文件职责的代码。

  游戏框架的构建

  搭建游戏框架,首先要明确主程序的职责。

  定义主程序的职责从前面的案例可以知道,一款游戏的主程序的职责可以分为两部分。

  游戏初始化游戏循环

  现在,根据明确的职责,我们设计PlaneGame类如下:

  澄清文件责任

  平面_主要:

  封装主游戏类。

  创建游戏对象。

  开始游戏。

  平面_精灵:

  封装游戏中需要用到的所有精灵子类。

  提供游戏相关工具。

  【注意:如果在开发中遇到固定值(比如本例中的屏幕尺寸和每秒帧频),最好不要直接使用固定值,而是使用常量的命名规则,常量所有的字母都使用大写,单词与单词之间使用下划线连接。如果需要调整值,只需要修改常量的定义,实现统一修改]

  然后代码现在可以得到一个简单的框架(代码还没有完成)。

  其中,plane_sprites的内容是:

  import Game # Constant screen _ rect=py Game . rect(0,0,480,00)# Constant frame _ per _ sec of refreshed frame rate=60 class games prite(py Game . sprite . sprite):“《位面大战游戏精灵》”def __init__(self,image_name,Speed=1): #调用父类super()的初始化方法。__init__() #定义对象属性self . image=py game . image . load(image _ name)self . rect=self . image . get _ rect()self . speed=speed def update(self):#在屏幕垂直方向移动self . rect . y=self . speed plane _ main的内容,如下所示:

  从plane _ sprites导入游戏*类plane game (object): 飞机对战主游戏 def __init__(self): print(游戏初始化)#创建游戏窗口self . screen=py game . display . set _ mode(screen _ rect . size)#创建游戏时钟self . clock=py game . time . clock()#调用私有方法,并创建sprite组self。_ _ create _ sprites()def _ _ create _ sprites(self):pass def start _ game(self):print(游戏开始)而True: #设置刷新帧率self。clock.tick (frame _ per _ sec) #事件监控自身。__event_handler() #冲突检测自身。__check_collide() #更新/绘制精灵组自身。_ _ Update _ sprites()# Update py game . display . Update()def _ _ event _ handler(self):for event in py game . event . get():#判断是否退出游戏if event . type==py game . quit:plane game。_ _ game _ over()def _ _ check _ collide(self):passdef _ _ update _ sprites(self):pass #不使用对象属性和类属性,所以定义为静态方法@ static method def _ _ game _ over():print( game over )py game . quit()exit()if _ _ name _= _ _ main _ :# create game object game=plane game()# start game . start _ game()

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: