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实现的百度贴吧网络爬虫实例
Apr 17 Python
在Python中处理XML的教程
Apr 29 Python
win10环境下python3.5安装步骤图文教程
Feb 03 Python
PyQt5使用QTimer实现电子时钟
Jul 29 Python
Django 实现xadmin后台菜单改为中文
Nov 15 Python
python基于celery实现异步任务周期任务定时任务
Dec 30 Python
python标准库os库的函数介绍
Feb 12 Python
python 解决tqdm模块不能单行显示的问题
Feb 19 Python
python 实现控制鼠标键盘
Nov 27 Python
pandas按照列的值排序(某一列或者多列)
Dec 13 Python
深入探讨opencv图像矫正算法实战
May 21 Python
Python批量解压&压缩文件夹的示例代码
Apr 04 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
ftp类(example.php)
2006/10/09 PHP
基于mysql的论坛(7)
2006/10/09 PHP
PHP 常用函数库和一些实用小技巧
2009/01/01 PHP
PHP调用Twitter的RSS的实现代码
2010/03/10 PHP
php 模拟 asp.net webFrom 按钮提交事件的思路及代码
2013/12/02 PHP
php实现在多维数组中查找特定value的方法
2015/07/29 PHP
一个简单的js树形菜单
2011/12/09 Javascript
兼容所有浏览器的js复制插件Zero使用介绍
2014/03/19 Javascript
在JS中操作时间之getUTCMilliseconds()方法的使用
2015/06/10 Javascript
JavaScript实现获取某个元素相邻兄弟节点的prev与next方法
2016/01/25 Javascript
利用JS判断字符串是否含有数字与特殊字符的方法小结
2016/11/25 Javascript
JavaScript、C# URL编码、解码总结
2017/01/21 Javascript
bootstrap动态添加面包屑(breadcrumb)及其响应事件的方法
2017/05/25 Javascript
AngularJS 最常用的八种功能(基础知识)
2017/06/26 Javascript
JavaScript编写的网页小游戏,很给力
2017/08/18 Javascript
vue实现组件之间传值功能示例
2018/07/13 Javascript
浅谈Vue.js中如何实现自定义下拉菜单指令
2019/01/06 Javascript
微信小程序返回箭头跳转到指定页面实例解析
2019/10/08 Javascript
js实现随机点名程序
2020/09/17 Javascript
JS实现简单的表格增删
2020/01/16 Javascript
通过js实现压缩图片上传功能
2020/02/25 Javascript
Node.js设置定时任务之node-schedule模块的使用详解
2020/04/28 Javascript
简单了解什么是神经网络
2017/12/23 Python
python基础教程项目三之万能的XML
2018/04/02 Python
Python考拉兹猜想输出序列代码实践
2019/07/05 Python
Python定时器线程池原理详解
2020/02/26 Python
记一次django内存异常排查及解决方法
2020/08/07 Python
Python使用Selenium模拟浏览器自动操作功能
2020/09/08 Python
一款超酷的js+css3实现的3D标签云特效兼容ie7/8/9
2013/11/18 HTML / CSS
重大事项社会稳定风险评估方案
2014/06/15 职场文书
求职简历自荐信
2014/06/18 职场文书
2014年团队工作总结
2014/11/24 职场文书
我的长征观后感
2015/06/09 职场文书
歌舞青春观后感
2015/06/10 职场文书
pytorch交叉熵损失函数的weight参数的使用
2021/05/24 Python
div与span之间的区别与使用介绍
2021/12/06 HTML / CSS