Pygame

Pygame是一种游戏开发引擎。Pygame适合用于游戏逻辑验证,游戏入门以及系统演示验证

python VR开发 python游戏开发引擎_开发框架


 

python -m pygame.examples.aliens

运行pygame自带的一个小游戏

 

Pygame最小开发框架

python VR开发 python游戏开发引擎_python VR开发_02

在最小开发框架中,获取事件并逐列响应与屏幕刷新是一对无限循环的关系

不断跟踪对游戏的不同输入,并给出不断的响应,同时刷新效果来保证这个响应让用户看得见

极简开发框架

python VR开发 python游戏开发引擎_python VR开发_03

 

最小开发框架的逐步解释

python VR开发 python游戏开发引擎_游戏_04

python VR开发 python游戏开发引擎_开发框架_05

python VR开发 python游戏开发引擎_游戏_06

python VR开发 python游戏开发引擎_开发框架_07

python VR开发 python游戏开发引擎_开发框架_08

 

最小开发框架的代码

开发游戏最好用Pycharm

python VR开发 python游戏开发引擎_小游戏_09

python VR开发 python游戏开发引擎_开发框架_10

 

import pygame, syspygame.init()screen = pygame.display.set_mode((600,400)) pygame.display.set_caption("Pygame游戏之旅") while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() pygame.display.update()

python VR开发 python游戏开发引擎_Python_11

python VR开发 python游戏开发引擎_游戏_12

 

壁球小游戏(展示型)与图像的基本使用

python VR开发 python游戏开发引擎_游戏_13

python VR开发 python游戏开发引擎_开发框架_14

python VR开发 python游戏开发引擎_Python_15

python VR开发 python游戏开发引擎_开发框架_16

 

python VR开发 python游戏开发引擎_小游戏_17

为什么要引入Rect对象,因为Rect对象有很多属性,对于我们编写程序操作很方便

python VR开发 python游戏开发引擎_游戏_18

矩形对象是图像对象的正切矩形
后边的所有的移动操作以及对边缘的反弹操作都是针对这个矩形的

 

python VR开发 python游戏开发引擎_开发框架_19

 

python VR开发 python游戏开发引擎_开发框架_20

 

python VR开发 python游戏开发引擎_python VR开发_21

python VR开发 python游戏开发引擎_python VR开发_22

 

 

import pygame, sys pygame.init() size = width, height =600, 400 speed = [1,1] BLACK = 0,0,0 screen = pygame.display.set_mode(size) pygame.display.set_caption("Pygame壁球") ball = pygame.image.load("PYG02-ball.gif") ballrect = ball.get_rect() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() ballrect = ballrect.move(speed[0], speed[1]) if ballrect.left < 0 or ballrect.right > width: speed[0] = -speed[0] if ballrect.top < 0 or ballrect.bottom > height: speed[1] = -speed[1] screen.fill(BLACK) screen.blit(ball,ballrect) pygame.display.update()

python VR开发 python游戏开发引擎_开发框架_23

 

壁球小游戏(节奏型)与屏幕帧率设置

python VR开发 python游戏开发引擎_Python_24

python VR开发 python游戏开发引擎_小游戏_25

 

python VR开发 python游戏开发引擎_小游戏_26

python VR开发 python游戏开发引擎_小游戏_27

 

import pygame, sys pygame.init() size = width, height =600, 400 speed = [1,1] BLACK = 0,0,0 screen = pygame.display.set_mode(size) pygame.display.set_caption("Pygame壁球") ball = pygame.image.load("PYG02-ball.gif") ballrect = ball.get_rect() fps = 300 fclock = pygame.time.Clock() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() ballrect = ballrect.move(speed[0], speed[1]) if ballrect.left < 0 or ballrect.right > width: speed[0] = -speed[0] if ballrect.top < 0 or ballrect.bottom > height: speed[1] = -speed[1] screen.fill(BLACK) screen.blit(ball,ballrect) pygame.display.update() fclock.tick(fps)

python VR开发 python游戏开发引擎_Python_28

 

壁球小游戏(操纵型)与键盘的基本使用

python VR开发 python游戏开发引擎_开发框架_29

python VR开发 python游戏开发引擎_Python_30

python VR开发 python游戏开发引擎_开发框架_31

 

import pygame, sys pygame.init() size = width, height =600, 400 speed = [1,1] BLACK = 0,0,0 screen = pygame.display.set_mode(size) pygame.display.set_caption("Pygame壁球") ball = pygame.image.load("PYG02-ball.gif") ballrect = ball.get_rect() fps = 300 fclock = pygame.time.Clock() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: speed[0] = speed[0] if speed[0] == 0 else (abs(speed[0] - 1)*int(speed[0]/abs(speed[0]))) elif event.key == pygame.K_RIGHT: speed[0] = speed[0] + 1 if speed[0] > 0 else speed[0] - 1 elif event.key == pygame.K_UP: speed[1] = speed[1] + 1 if speed[1] > 0 else speed[1] - 1 elif event.key == pygame.K_DOWN: speed[1] = speed[1] if speed[1] == 0 else (abs(speed[1] - 1) * int(speed[1]/abs(speed[1]))) ballrect = ballrect.move(speed[0], speed[1]) if ballrect.left < 0 or ballrect.right > width: speed[0] = -speed[0] if ballrect.top < 0 or ballrect.bottom > height: speed[1] = -speed[1] screen.fill(BLACK) screen.blit(ball,ballrect) pygame.display.update() fclock.tick(fps)

 

 

Pygame屏幕绘制机制

python VR开发 python游戏开发引擎_开发框架_32

python VR开发 python游戏开发引擎_游戏_33

 

屏幕绘制的重要函数

python VR开发 python游戏开发引擎_Python_34

pygame提供的显示函数还可以支持OpenGL和硬件加速

 

Pygame屏幕尺寸和模式设置

python VR开发 python游戏开发引擎_python VR开发_35

注意:每种显示方式要配合相应的处理机制

尽管我们可以设置屏幕的模式为窗口可调、无边框或者全屏显示,同时我们也要注意如何去针对不同的模式设计相关的应对方法

例如如果只把屏幕模式设为窗口可调,而游戏的小球和游戏的区域并没有变,小球的运动区域就还是会原来那样

如果屏幕设置为无边框,就还要增设其他关闭屏幕的方法

如果把屏幕设置为全屏模式,小球的尺寸也会被拉大

python VR开发 python游戏开发引擎_Python_36

适应全屏并且Esc可退出的version

import pygame, sys pygame.init() vInfo = pygame.display.Info() size = width, height =vInfo.current_w, vInfo.current_h #感知操作系统屏幕的宽度和高度 speed = [1,1] BLACK = 0,0,0 screen = pygame.display.set_mode(size, pygame.FULLSCREEN) pygame.display.set_caption("Pygame壁球") ball = pygame.image.load("PYG02-ball.gif") ballrect = ball.get_rect() fps = 300 fclock = pygame.time.Clock() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: speed[0] = speed[0] if speed[0] == 0 else (abs(speed[0] - 1)*int(speed[0]/abs(speed[0]))) elif event.key == pygame.K_RIGHT: speed[0] = speed[0] + 1 if speed[0] > 0 else speed[0] - 1 elif event.key == pygame.K_UP: speed[1] = speed[1] + 1 if speed[1] > 0 else speed[1] - 1 elif event.key == pygame.K_DOWN: speed[1] = speed[1] if speed[1] == 0 else (abs(speed[1] - 1) * int(speed[1]/abs(speed[1]))) elif event.key == pygame.K_ESCAPE: #ESC键退出 sys.exit() ballrect = ballrect.move(speed[0], speed[1]) if ballrect.left < 0 or ballrect.right > width: speed[0] = -speed[0] if ballrect.top < 0 or ballrect.bottom > height: speed[1] = -speed[1] screen.fill(BLACK) screen.blit(ball,ballrect) pygame.display.update() fclock.tick(fps)

 

python VR开发 python游戏开发引擎_Python_37

适应窗口大小变化的version

import pygame, sys pygame.init() size = width, height =600, 400 speed = [1,1] BLACK = 0,0,0 screen = pygame.display.set_mode(size, pygame.RESIZABLE) pygame.display.set_caption("Pygame壁球") ball = pygame.image.load("PYG02-ball.gif") ballrect = ball.get_rect() fps = 300 fclock = pygame.time.Clock() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: speed[0] = speed[0] if speed[0] == 0 else (abs(speed[0] - 1)*int(speed[0]/abs(speed[0]))) elif event.key == pygame.K_RIGHT: speed[0] = speed[0] + 1 if speed[0] > 0 else speed[0] - 1 elif event.key == pygame.K_UP: speed[1] = speed[1] + 1 if speed[1] > 0 else speed[1] - 1 elif event.key == pygame.K_DOWN: speed[1] = speed[1] if speed[1] == 0 else (abs(speed[1] - 1) * int(speed[1]/abs(speed[1]))) elif event.key == pygame.K_ESCAPE: #ESC键退出 sys.exit() elif event.type == pygame.VIDEORESIZE: size = width, height = event.size[0], event.size[1] screen = pygame.display.set_mode(size, pygame.RESIZABLE) ballrect = ballrect.move(speed[0], speed[1]) if ballrect.left < 0 or ballrect.right > width: speed[0] = -speed[0] if ballrect.top < 0 or ballrect.bottom > height: speed[1] = -speed[1] screen.fill(BLACK) screen.blit(ball,ballrect) pygame.display.update() fclock.tick(fps)

 

Sprite精灵类

       在pygame.sprite模块里面包含了一个名为Sprite类,他是pygame本身自带的一个精灵。但是这个类的功能比较少,因此我们新建一个类对其继承,在sprite类的基础上丰富,以方便我们的使用。

sprite类提供了碰撞检测方法

pygame.sprite.collide_rect()   True就是碰撞了,False是没碰撞 

 

 

 

pygame用blit()实现动画效果

surface.blit(image,(x,y),rect)  在这里surface.blit()这个方法应该大家都很熟悉了,我们就是利用第三个参数,也就是绘制区域的变化实现的动画.我们将图像的一部分绘制出来。如果加上一个简单的循环,让绘制区域的位置发生变化。那么就可以实现动画效果啦