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的迭代器和生成器
Jul 29 Python
Python生成任意范围任意精度的随机数方法
Apr 09 Python
Python cookbook(字符串与文本)针对任意多的分隔符拆分字符串操作示例
Apr 19 Python
python字典值排序并取出前n个key值的方法
Oct 17 Python
对pandas将dataframe中某列按照条件赋值的实例讲解
Nov 29 Python
python基于递归解决背包问题详解
Jul 03 Python
Python异常处理例题整理
Jul 07 Python
将Pytorch模型从CPU转换成GPU的实现方法
Aug 19 Python
Python箱型图绘制与特征值获取过程解析
Oct 22 Python
基于python实现生成指定大小txt文档
Jul 20 Python
python opencv实现简易画图板
Aug 27 Python
解决python 输出到csv 出现多空行的情况
Mar 24 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
Apache, PHP在Windows 9x/NT下的安装与配置 (二)
2006/10/09 PHP
php中fsockopen用法实例
2015/01/05 PHP
php将服务端的文件读出来显示在web页面实例
2016/10/31 PHP
php源码之将图片转化为data/base64数据流实例详解
2016/11/27 PHP
javascript 写类方式之一
2009/07/05 Javascript
教您去掉ie网页加载进度条的方法
2010/12/09 Javascript
基于Jquery的淡入淡出的特效基础练习
2010/12/13 Javascript
JQuery事件e参数的方法preventDefault()取消默认行为
2013/09/26 Javascript
jQuery之字体大小的设置方法
2014/02/27 Javascript
Javascript学习笔记之 函数篇(三) : 闭包和引用
2014/11/23 Javascript
2014 HTML5/CSS3热门动画特效TOP10
2014/12/07 Javascript
jqueryUI里拖拽排序示例分析
2015/02/26 Javascript
javascript中offset、client、scroll的属性总结
2015/08/13 Javascript
浅析Javascript匿名函数与自执行函数
2016/02/06 Javascript
基于javascript实现tab选项卡切换特效调试笔记
2016/03/30 Javascript
详解JavaScript的另类写法
2016/04/11 Javascript
input 禁止输入特殊字符的四种实现方式
2016/08/24 Javascript
详解Node.Js如何处理post数据
2016/09/19 Javascript
Angular路由简单学习
2016/12/26 Javascript
简单快速的实现js计算器功能
2017/08/17 Javascript
微信小程序HTTP接口请求封装的实现
2019/02/21 Javascript
基于 jQuery 实现键盘事件监听控件
2019/04/04 jQuery
详解Vue组件之间通信的七种方式
2019/04/14 Javascript
解决vuex数据异步造成初始化的时候没值报错问题
2019/11/13 Javascript
原生js实现九宫格拖拽换位
2021/01/26 Javascript
Python random模块用法解析及简单示例
2017/12/18 Python
使用Python给头像加上圣诞帽或圣诞老人小图标附源码
2019/12/25 Python
Python调用Windows API函数编写录音机和音乐播放器功能
2020/01/05 Python
python3实现将json对象存入Redis以及数据的导入导出
2020/07/16 Python
美国浴缸、水槽和水龙头购物网站:Vintage Tub & Bath
2019/11/05 全球购物
公司员工检讨书
2014/02/08 职场文书
2014教师党员个人自我评议
2014/09/20 职场文书
四风对照检查剖析材料
2014/10/07 职场文书
企业党员岗位承诺书
2015/04/27 职场文书
Springboot使用Spring Data JPA实现数据库操作
2021/06/30 Java/Android
「地球外少年少女」BD发售宣传CM公开
2022/03/21 日漫