pygame游戏之旅 添加游戏暂停功能

来源:中文源码网    浏览: 次    日期:2019年11月5日

【下载文档:  pygame游戏之旅 添加游戏暂停功能.txt 】

(友情提示:右键点上行txt文档名->目标另存为)

pygame游戏之旅 添加游戏暂停功能本文为大家分享了pygame游戏之旅的第13篇,供大家参考,具体内容如下

定义暂停函数:

def paused():

largeText = pygame.font.SysFont('comicsansms',115)

TextSurf, TextRect = text_objects('Paused', largeText)

TextRect.center = ((display_width/2),(display_height/2))

gameDisplay.blit(TextSurf, TextRect)

while pause:

for event in pygame.event.get():

print(event)

if event.type == pygame.QUIT:

pygame.quit()

quit()

## gameDisplay.fill(white)

button("Continue", 150, 450, 100, 50, green, bright_green,game_loop)

button("Quit",550, 450, 100, 50, red, bright_red,quitgame)

pygame.display.update()

clock.tick(15)重新定义原来的crah函数:

def crash():

largeText = pygame.font.SysFont('comicsansms',115)

TextSurf, TextRect = text_objects('You Crashed!', largeText)

TextRect.center = ((display_width/2),(display_height/2))

gameDisplay.blit(TextSurf, TextRect)

while True:

for event in pygame.event.get():

print(event)

if event.type == pygame.QUIT:

pygame.quit()

quit()

## gameDisplay.fill(white)

button("Play Again", 150, 450, 100, 50, green, bright_green,game_loop)

button("Quit",550, 450, 100, 50, red, bright_red,quitgame)

pygame.display.update()

clock.tick(15)源代码:import pygame

import time

import random

pygame.init()

white = (255,255,255)

black = (0,0,0)

gray = (128,128,128)

red = (200,0,0)

green = (0,200,0)

bright_red = (255,0,0)

bright_green = (0,255,0)

blue = (0,0,255)car_width = 100

display_width = 800

display_height = 600

gameDisplay = pygame.display.set_mode( (display_width,display_height) )

pygame.display.set_caption('A bit Racey')

clock = pygame.time.Clock()

carImg = pygame.image.load('car.png')

pause = False

##crash = True

def things_dodged(count):

font = pygame.font.SysFont(None, 25)

text = font.render("Dodged:"+str(count), True, black)

gameDisplay.blit(text,(0,0))

def things(thingx, thingy, thingw, thingh, color):

pygame.draw.rect(gameDisplay, color, [thingx, thingy, thingw, thingh])

def car(x, y):

gameDisplay.blit(carImg, (x,y))

def text_objects(text, font):

textSurface = font.render(text, True, black)

return textSurface, textSurface.get_rect()

def crash():

largeText = pygame.font.SysFont('comicsansms',115)

TextSurf, TextRect = text_objects('You Crashed!', largeText)

TextRect.center = ((display_width/2),(display_height/2))

gameDisplay.blit(TextSurf, TextRect)

while True:

for event in pygame.event.get():

print(event)

if event.type == pygame.QUIT:

pygame.quit()

quit()

## gameDisplay.fill(white)

button("Play Again", 150, 450, 100, 50, green, bright_green,game_loop)

button("Quit",550, 450, 100, 50, red, bright_red,quitgame)

pygame.display.update()

clock.tick(15)

def button (msg, x, y, w, h, ic, ac, action=None):

mouse =pygame.mouse.get_pos()

click = pygame.mouse.get_pressed()

print(click)

if x + w > mouse[0] > x and y + h > mouse[1] > y:

pygame.draw.rect(gameDisplay, ac, (x,y,w,h))

if click[0] == 1 and action != None:

action()

## if action == "play":

## action()

## if action == "quit":

## pygame.quit()

## quit()

else:

pygame.draw.rect(gameDisplay, ic, (x,y,w,h))

smallText = pygame.font.SysFont('comicsansms', 20)

textSurf, textRect = text_objects(msg, smallText)

textRect.center = ( (x+(w/2)), (y+(h/2)))

gameDisplay.blit(textSurf, textRect)

def quitgame():

pygame.quit()

quit()

def unpause():

global pause

pause = False

def paused():

largeText = pygame.font.SysFont('comicsansms',115)

TextSurf, TextRect = text_objects('Paused', largeText)

TextRect.center = ((display_width/2),(display_height/2))

gameDisplay.blit(TextSurf, TextRect)

while pause:

for event in pygame.event.get():

print(event)

if event.type == pygame.QUIT:

pygame.quit()

quit()

## gameDisplay.fill(white)

button("Continue", 150, 450, 100, 50, green, bright_green,unpause)

button("Quit",550, 450, 100, 50, red, bright_red,quitgame)

pygame.display.update()

clock.tick(15)

def game_intro():

global pasue

pause = False

intro = True

while intro:

for event in pygame.event.get():

print(event)

if event.type == pygame.QUIT:

pygame.quit()

quit()

gameDisplay.fill(white)

largeText = pygame.font.SysFont('comicsansms',115)

TextSurf, TextRect = text_objects('A bit Racey', largeText)

TextRect.center = ((display_width/2),(display_height/2))

gameDisplay.blit(TextSurf, TextRect)

button("GO", 150, 450, 100, 50, green, bright_green,game_loop)

button("Quit",550, 450, 100, 50, red, bright_red,quitgame)

pygame.display.update()

clock.tick(15)

def game_loop():

global pause

x = display_width * 0.45

y = display_height * 0.8

x_change = 0

dodged = 0 gameExit = False

thing_startx = random.randrange(0, display_width)

thing_starty = -600

thing_speed = 7

thing_width = 100

thing_height = 100

while not gameExit:

for event in pygame.event.get():

if event.type == pygame.QUIT:

pygame.quit()

quit()

if event.type == pygame.KEYDOWN:

if event.key == pygame.K_LEFT:

x_change = -5

elif event.key == pygame.K_RIGHT:

x_change = 5

elif event.key == pygame.K_p:

pause = True

paused()

if event.type == pygame.KEYUP:

if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:

x_change = 0

print(event)

x += x_change

gameDisplay.fill(white)

things(thing_startx, thing_starty, thing_width, thing_height, black)

thing_starty += thing_speed

car(x,y)

things_dodged(dodged)

if x > display_width - car_width or x < 0:

gameExit = True

if thing_starty > display_height:

thing_starty = 0 - thing_height

thing_startx = random.randrange(0, display_width)

dodged += 1

thing_speed += 1

thing_width += (dodged * 1.2)

if y < thing_starty + thing_height:

print('y crossover')

if x > thing_startx and x < thing_startx + thing_width or x + car_width > thing_startx and x + car_width < thing_startx + thing_width:

print('x crossover')

crash()

pygame.display.update()

clock.tick(60)

#crash()

game_intro()

game_loop()

pygame.quit()

quit()结果图:以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持中文源码网。

亲,试试微信扫码分享本页! *^_^*

python暂停和恢复游戏_pygame游戏之旅 添加游戏暂停功能相关推荐

  1. 用 JAVA 开发游戏连连看(之四)添加更多的功能

    之四)添加更多的功能 计分功能 大体上我们的程序已经可以跑了起来,可惜,就这么玩玩也太没有意思了,总得有个计分的吧.虽然我们不知道别人是怎么计分的,可是,程序是我们自己动手写的,我的地盘我做主,看看我 ...

  2. 【Unity3D】游戏物体操作 ① ( 场景简介 | 添加游戏物体 | 操作游戏物体 | 选中游戏物体 | 场景显示效果缩放 | 重命名游戏物体 | 复制游戏物体 | 删除游戏物体 | 移动物体 )

    文章目录 一.Scene 场景简介 二.添加游戏物体 三.操作游戏物体 1.选中游戏物体 2.场景显示效果缩放 3.重命名游戏物体 4.复制游戏物体 5.删除游戏物体 6.Inspector 检查器窗 ...

  3. java连连看的按钮如何加入_用 JAVA 开发游戏连连看(之四)添加更多的功能

    之四)添加更多的功能 计分功能 大体上我们的程序已经可以跑了起来,可惜,就这么玩玩也太没有意思了,总得有个计分的吧.虽然我们不知道别人是怎么计分的,可是,程序是我们自己动手写的,我的地盘我做主,看看我 ...

  4. (1)python pyinstaller打包exe添加版本信息(2)python获取exe版本信息(3)pyqt5开发exe添加检查版本更新功能

    笔者总结不容易点个关注吧    一键三联哦!       感谢您! python pyinstaller打包exe添加版本信息 打包并添加版本信息 注意!这里有个坑 如果第二次要修改版权信息 要将fi ...

  5. MC新手入门(十三)------ 添加游戏角色

    我们使用之前介绍的游戏开发工具MC来添加游戏角色.下面我们添加两个角色,一个为动态图片的角色,一个为静态图片的角色.如图4-1-1-1: 图4-1-1-1(左静右动) 首先,准备两组图片放在MC的根目 ...

  6. 360游戏大厅打不开HTML游戏,360游戏大厅使用过程常见问题解决方法

    玩游戏出现黑屏.白屏 遇到黑屏 遇到黑屏,左上角有红X或提示Flash版本过低时,可尝试在游戏大厅的游戏工具条上点击"修复",选择一键修复后,大厅会智能安装.升级Flash. 遇到 ...

  7. 1Unity3D-开发潜行类游戏案例个人总结--俺的游戏视频上传到b站啦!

    本学期的数据可视化课程提供了丰富的选题,我选择我比较感兴趣的游戏制作,使用Unity2020版本进行开发,在这个过程中学习收获了许多. 我的游戏视频已经更新到bilibili https://www. ...

  8. python暂停和恢复游戏,暂停/恢复中间的python脚本

    我可以在用户中间暂停正在运行的python脚本(在Windows下),并在用户决定时再次恢复吗? 有一个主管理器程序生成,加载和运行其他python脚本(通过从控制台调用python script.p ...

  9. Python不能做游戏?一小时做出一个游戏!

    嗨喽-小伙伴们,我又来了, 写在前面的话: [ 一直都听他们说,python做不出好的游戏,个人是不赞同的,我只能说,python可以用来写游戏,但不适合. 举个最简单的例子,弹弓可以用来拔牙吗?当然 ...

最新文章

  1. UVA 1646 Edge Case
  2. 微信小程序INC自增自减MUL自乘问题
  3. css选择器的优先级
  4. 快速入门系列之 Scala 语言 GitChat连接
  5. 【转】10分钟精通SharePoint - VS开发模板
  6. Winform 的一个多线程绑定DataGrid数据源的例子
  7. python调用ping命令_python调用系统命令ping
  8. linux设备驱动 注册 命令6,Linux设备驱动程序学习----6.模块的参数
  9. 推荐几个.NET开源图表组件
  10. 20190718每日一句
  11. CSDN 博客前200名
  12. eps php,eps是什么文件
  13. C++11的std::is_same和std::decay使用与源码解析
  14. 尾行注释转行上注释 正则表达式
  15. 台式电脑计算机怎么用,怎么用键盘开机电脑_台式电脑键盘怎么开机-win7之家
  16. 如何让安卓手机访问内网服务器?
  17. 多个txt合并成一个txt(简单易操作)
  18. Gradle配置阿里云仓库
  19. 微信小程序基础库的问题
  20. linux文件权限的例子,Linux基础教程之linux文件权限深度解读

热门文章

  1. Leetcode--80. 删除排序数组中的重复项Ⅱ
  2. maven java管理_java – 依赖管理与maven
  3. 耶鲁大学计算机科学录取,耶鲁大学计算机科学研究生Offer及录取要求
  4. Linux二进制实用工具Binutils工具集解析()
  5. Go语言,在Ubuntu9.10和Windows安装
  6. 特殊构造(非捕获总结)
  7. php跳一跳小游戏,原生JS实现的跳一跳小游戏完整实例
  8. freedos能够编译c语言嘛,Freedos freedos核心源代码包含汇编和C语言代码 - 下载 - 搜珍网...
  9. Cocos Creator棋牌开发-部署经验总结
  10. 计算机A级作文,关于被计算机的作文(共一篇)-疾风作文网