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记录详细调用堆栈日志的方法
May 05 Python
Python中处理字符串之endswith()方法的使用简介
May 18 Python
python抓取并保存html页面时乱码问题的解决方法
Jul 01 Python
python3学习之Splash的安装与实例教程
Jul 09 Python
Python使用pandas和xlsxwriter读写xlsx文件的方法示例
Apr 09 Python
在Pytorch中计算自己模型的FLOPs方式
Dec 30 Python
Pytorch 实现sobel算子的卷积操作详解
Jan 10 Python
Django ModelForm操作及验证方式
Mar 30 Python
如何在python中执行另一个py文件
Apr 30 Python
详解python定时简单爬取网页新闻存入数据库并发送邮件
Nov 27 Python
pip 20.3 新版本发布!即将抛弃 Python 2.x(推荐)
Dec 16 Python
Python实现微信表情包炸群功能
Jan 28 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
php调用Google translate_tts api实现代码
2013/08/07 PHP
PHP中调用SVN命令更新网站方法
2015/01/07 PHP
php类的扩展和继承用法实例
2015/06/20 PHP
JS Timing
2007/04/21 Javascript
Javascript的一种模块模式
2008/03/22 Javascript
基于jQuery的图片大小自动适应实现代码
2010/11/17 Javascript
javascript处理表单示例(javascript提交表单)
2014/04/28 Javascript
封装了jQuery的Ajax请求全局配置
2015/02/05 Javascript
javascript获取文档坐标和视口坐标
2015/05/26 Javascript
JavaScript截取指定长度字符串点击可以展开全部代码
2015/12/04 Javascript
使用jQuery实现两个div中按钮互换位置的实例代码
2017/09/21 jQuery
Vue-Router2.X多种路由实现方式总结
2018/02/09 Javascript
webpack中如何加载静态文件的方法步骤
2019/05/18 Javascript
Js通过AES加密后PHP用Openssl解密的方法
2019/07/12 Javascript
vue-cli点击实现全屏功能
2020/03/07 Javascript
[04:50]DOTA2亚洲邀请赛小组赛第四日 TOP10精彩集锦
2015/02/02 DOTA
[48:41]VP vs VG Supermajor小组赛 B组胜者组决赛 BO3 第二场 6.2
2018/06/03 DOTA
python实现字符串和字典的转换
2018/09/29 Python
浅谈numpy生成数组的零值问题
2018/11/12 Python
解决tensorflow模型压缩的问题_踩坑无数,总算搞定
2021/03/02 Python
德国箱包网上商店:koffer24.de
2016/07/27 全球购物
美国眼镜网站:LensCrafters
2020/01/19 全球购物
Levi’s西班牙官方网站:李维斯,著名的牛仔裤品牌
2020/08/20 全球购物
高中毕业的自我鉴定
2013/12/09 职场文书
采购部部门职责
2013/12/15 职场文书
乡镇网格化管理实施方案
2014/03/23 职场文书
房屋委托书范本
2014/04/04 职场文书
客户经理竞聘演讲稿
2014/05/15 职场文书
售后服务承诺书模板
2014/05/21 职场文书
毕业生对母校寄语
2015/02/26 职场文书
采购部年度工作总结
2015/08/13 职场文书
Java图书管理系统,课程设计必用(源码+文档)
2021/06/30 Java/Android
十大公认最好看的动漫:《咒术回战》在榜,《钢之炼金术师》第一
2022/03/18 日漫
Python数据处理的三个实用技巧分享
2022/04/01 Python
Centos7 Shell编程之正则表达式、文本处理工具详解
2022/08/05 Servers
前端传参数进行Mybatis调用mysql存储过程执行返回值详解
2022/08/14 MySQL