资源简介 程序设计 14 游戏模块 跨平台的Python模块 专为电子游戏开发设计 包含图像、声音等多种元素控制 建立在SDL基础上 允许实时电子游戏研发 不被低级语言束缚 pygame.cdrom访问光驱 pygame.color颜色 pygame.cursors加载光标 pygame.display访问显示设备 pygame.draw绘制形状、线和点 pygame.event管理事件 pygame.font使用字体 pygame.image加载和存储图片 pygame.joystick使用游戏手柄 pygame.key读取键盘按键 pygame.mixer声音 pygame.movie播放视频 pygame.music播放音频 pygame.overlay访问高级视频叠加 pygame.rect管理矩形区域 pygame.scrap本地剪贴板访问 pygame.sndarray操作声音数据 pygame.sprite操作移动图像 pygame.surface管理图像和屏幕 pygame.surfarray管理点阵图像数据 pygame.time管理时间和帧信息 pygame.transform缩放和移动图像 pygame.mouse鼠标 在控制面板的高级系统设置中点击环境变量,找到path变量,在其末尾输入本机安装Python的路径。例如:C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\Scripts 或在安装软件时,选择“添加到路径” 打开cmd 输入pip install pygame 系统将自动下载相应的安装包并自动安装。 环境变量配置 安装pygame模块 开始游戏编程 2 3 1 1 2 安装 笑脸爆炸 笑脸爆炸 基本知识 核心功能 导入 初始化 加载图片 响应点击 随机大小 反弹功能 显示设置 事件处理 图片运动 游戏循环 游戏退出 “画点” import pygame pygame.init() screen=pygame.display.set_mode([800,600]) pygame.display.set_caption("画点") GREEN=(0,255,0) #RGB色彩模式,每段0~255 radius=10 keep_going=True while keep_going: for event in pygame.event.get(): if event.type==pygame.QUIT: keep_going=False pygame.draw.circle(screen,GREEN,(100,100),radius) pygame.display.update() pygame.quit() #pygame.event.get()获取用户执行事件列表,从而处理例如鼠标点击、按键、关闭等事件操作。 #退出程序 #绘制圆形 #刷新显示 #设定窗口大小 #设定窗口标题 #导入模块 #初始化 “随机彩点” import pygame pygame.init() screen=pygame.display.set_mode([800,600]) pygame.display.set_caption("画点") GREEN=(0,255,0) #RGB色彩模式,每段0~255 radius=10 keep_going=True while keep_going: for event in pygame.event.get(): if event.type==pygame.QUIT: keep_going=False pygame.draw.circle(screen,GREEN,(100,100),radius) pygame.display.update() pygame.quit() COLOR=(int(random.random()*255),int(random.random()*255),int(random.random() *255)) radius=random.randrange(1,10) (int(random.random()*800),int(random.random()*600)) 1 “点击绘点” import pygame pygame.init() screen=pygame.display.set_mode([800,600]) pygame.display.set_caption("画点") GREEN=(0,255,0) #RGB色彩模式,每段0~255 radius=10 keep_going=True while keep_going: for event in pygame.event.get(): if event.type==pygame.QUIT: keep_going=False pygame.draw.circle(screen,GREEN,(100,100),radius) pygame.display.update() pygame.quit() 2 if event.type==pygame.MOUSEBUTTONDOWN: spot=event.pos #获取鼠标点击位置 pygame.draw.circle(screen,COLOR,spot,radius) “加载图片” import pygame pygame.init() screen=pygame.display.set_mode([800,600]) pygame.display.set_caption("画点") GREEN=(0,255,0) #RGB色彩模式,每段0~255 radius=10 keep_going=True while keep_going: for event in pygame.event.get(): if event.type==pygame.QUIT: keep_going=False pygame.draw.circle(screen,GREEN,(100,100),radius) pygame.display.update() pygame.quit() 3 if event.type==pygame.MOUSEBUTTONDOWN: spot=event.pos screen.blit(pic,spot) pic=pygame.image.load("smile.png") #图片需要和文件在同一目录 #点击画笑脸(加载图片),书写时注意大小写 import pygame #导入模块 import random pygame.init() #初始化 screen=pygame.display.set_mode([800,600]) pygame.display.set_caption("画笑脸") pic=pygame.image.load("smile.png")#图片需要和文件在同一目录 keep_going=True while keep_going: for event in pygame.event.get(): if event.type==pygame.QUIT: keep_going=False if event.type==pygame.MOUSEBUTTONDOWN: spot=event.pos #获取鼠标点击位置 screen.blit(pic,spot) pygame.display.update() pygame.quit() 笑脸爆炸 笑脸爆炸 基本知识 核心功能 导入 初始化 加载图片 响应点击 随机大小 反弹功能 显示设置 事件处理 图片运动 游戏循环 游戏退出 弹球? 速度与运动? 位置参数、速度参数 关键1 反弹判断 关键2 设计挡板对象 关键3 import pygame #导入模块 pygame.init() #初始化 screen=pygame.display.set_mode([800,600]) pygame.display.set_caption("弹球") keep_going=True pic=pygame.image.load("ball.png") BLACK=(0,0,0) picx=0 picy=0 while keep_going: #Game loop for event in pygame.event.get(): if event.type==pygame.QUIT: keep_going=False picx +=1 picy +=1 screen.fill(BLACK) #用来擦除之前绘制的图形 screen.blit(pic,(picx,picy)) pygame.display.update() pygame.quit() #如果图片不透明要加上这两句,以免边角出现黑边 #colorkey=pic.get_at((0,0)) #pic.set_colorket(colorkey) “运动小球” “时间控制” import pygame #导入模块 pygame.init() #初始化 screen=pygame.display.set_mode([800,600]) pygame.display.set_caption("弹球") keep_going=True pic=pygame.image.load("ball.png") picx=0 picy=0 BLACK=(0,0,0) timer=pygame.time.Clock() #引入计时器 while keep_going: #Game loop for event in pygame.event.get(): if event.type==pygame.QUIT: keep_going=False picx +=1 picy +=1 screen.fill(BLACK) screen.blit(pic,(picx,picy)) pygame.display.update() timer.tick(60)#每秒60帧 pygame.quit() “反弹研究” speedx speedy 与左/右边界接触时,speedx_____________,speedy_____________ 与上/下边界接触时,speedx_____________,speedy_____________ *(-1) 不变 *(-1) 不变 “反弹研究” import pygame pygame.init() screen=pygame.display.set_mode([800,600]) pygame.display.set_caption("弹球") keep_going=True pic=pygame.image.load("ball.png") picx=0 picy=0 BLACK=(0,0,0) timer=pygame.time.Clock() speedx=5 speedy=5 while keep_going: #Game loop for event in pygame.event.get(): if event.type==pygame.QUIT: keep_going=False picx +=speedx picy +=speedy if picx<=0 or picx+pic.get_width()>800: speedx*=-1 if picy<=0 or picy+pic.get_height()>600: speedy*=-1 screen.fill(BLACK) screen.blit(pic,(picx,picy)) pygame.display.update() timer.tick(60) pygame.quit() import pygame pygame.init() screen=pygame.display.set_mode([800,600]) pygame.display.set_caption("笑脸弹球") keep_going=True pic=pygame.image.load("Smile.png") picx=0 picy=0 BLACK=(0,0,0) #定义背景颜色:黑色 timer=pygame.time.Clock() speedx=5 speedy=5 while keep_going: for event in pygame.event.get(): if event.type==pygame.QUIT: keep_going=False picx +=speedx picy +=speedy if picx<=0 or picx+pic.get_width()>800: speedx*=-1 if picy<=0 or picy+pic.get_height()>600: speedy*=-1 screen.fill(BLACK) if event.type==pygame.MOUSEBUTTONDOWN: picx,picy=event.pos #获取鼠标点击位置 screen.blit(pic,(picx,picy)) pygame.display.update() timer.tick(60) pygame.quit() “点击改变位置” 1. 完成课上的两组实例:绘制彩点、弹动笑脸。 作业 2. 思考要实现弹球游戏还需要哪些功能代码? 要实现笑脸爆炸呢?(下次课我们一同来实现!) 展开更多...... 收起↑ 资源预览