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


Posted in Python onNovember 21, 2018

本文为大家分享了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()

结果图:

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

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

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python中字典和JSON互转操作实例
Jan 19 Python
Python脚本实现格式化css文件
Apr 08 Python
理解Python中函数的参数
Apr 27 Python
Python自动调用IE打开某个网站的方法
Jun 03 Python
Python注释详解
Jun 01 Python
python调用Delphi写的Dll代码示例
Dec 05 Python
为什么str(float)在Python 3中比Python 2返回更多的数字
Oct 16 Python
Django框架实现的分页demo示例
May 25 Python
python求最大值最小值方法总结
Jun 25 Python
基于Python实现ComicReaper漫画自动爬取脚本过程解析
Nov 11 Python
python 递归调用返回None的问题及解决方法
Mar 16 Python
Python图像处理库PIL详细使用说明
Apr 06 Python
使用50行Python代码从零开始实现一个AI平衡小游戏
Nov 21 #Python
pygame游戏之旅 调用按钮实现游戏开始功能
Nov 21 #Python
pygame游戏之旅 按钮上添加文字的方法
Nov 21 #Python
Face++ API实现手势识别系统设计
Nov 21 #Python
详解django自定义中间件处理
Nov 21 #Python
pygame游戏之旅 添加游戏界面按键图形
Nov 20 #Python
pygame游戏之旅 添加游戏介绍
Nov 20 #Python
You might like
2款PHP无限级分类实例代码
2015/11/11 PHP
PHP实现的分解质因数操作示例
2018/08/01 PHP
jQuery实现id模糊查询的小例子
2013/03/19 Javascript
JS子父窗口互相操作取值赋值的方法介绍
2013/05/11 Javascript
使用JS或jQuery模拟鼠标点击a标签事件代码
2014/03/10 Javascript
JavaScript实现MIPS乘法模拟的方法
2015/04/17 Javascript
jQuery遍历DOM的父级元素、子级元素和同级元素的方法总结
2016/07/07 Javascript
JavaScript无阻塞加载和defer、async详解
2017/02/26 Javascript
yarn的使用与升级Node.js的方法详解
2017/06/04 Javascript
微信小程序使用Promise简化回调
2018/02/06 Javascript
抖音上用记事本编写爱心小程序教程
2019/04/17 Javascript
vue中实现Monaco Editor自定义提示功能
2019/07/05 Javascript
微信小程序 弹窗输入组件的实现解析
2019/08/12 Javascript
Vue使用预渲染代替SSR的方法
2020/07/02 Javascript
[46:23]完美世界DOTA2联赛PWL S2 FTD vs Magma 第一场 11.20
2020/11/23 DOTA
Python 实现异步调用函数的示例讲解
2018/10/14 Python
浅谈Python的list中的选取范围
2018/11/12 Python
用Python解决x的n次方问题
2019/02/08 Python
解决python3.5 正常安装 却不能直接使用Tkinter包的问题
2019/02/22 Python
Python基本数据结构与用法详解【列表、元组、集合、字典】
2019/03/23 Python
浅谈Pytorch中的torch.gather函数的含义
2019/08/18 Python
使用Dajngo 通过代码添加xadmin用户和权限(组)
2020/07/03 Python
Python压缩模块zipfile实现原理及用法解析
2020/08/14 Python
pyx文件 生成pyd 文件用于 cython调用的实现
2021/03/04 Python
使用HTML5里的classList操作CSS类
2016/06/28 HTML / CSS
印尼在线精品店:Berrybenka.com
2016/10/22 全球购物
全球在线商店:BerryLook
2019/04/14 全球购物
法国体育用品商店:GO Sport
2019/10/23 全球购物
Pandora德国官网:购买潘多拉手链、戒指、项链和耳环
2020/02/20 全球购物
介绍一下Mysql的存储引擎
2015/02/12 面试题
应届生服务员求职信
2013/10/31 职场文书
2015年五四青年节演讲稿
2015/03/18 职场文书
2015年消防工作总结
2015/04/24 职场文书
格林童话读书笔记
2015/06/30 职场文书
消防安全培训工作总结
2015/10/23 职场文书
Python趣味挑战之给幼儿园弟弟生成1000道算术题
2021/05/28 Python