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执行系统命令的常见方法(全)
Oct 22 Python
python3 unicode列表转换为中文的实例
Oct 26 Python
[原创]Python入门教程3. 列表基本操作【定义、运算、常用函数】
Oct 30 Python
Python3将数据保存为txt文件的方法
Sep 12 Python
详解python中docx库的安装过程
Nov 08 Python
PyTorch中的padding(边缘填充)操作方式
Jan 03 Python
Python可以实现栈的结构吗
May 27 Python
Python dict的常用方法示例代码
Jun 23 Python
解决tensorflow 释放图,删除变量问题
Jun 23 Python
tensorflow 2.1.0 安装与实战教程(CASIA FACE v5)
Jun 30 Python
Python可视化工具如何实现动态图表
Oct 23 Python
python中pdb模块实例用法
Jan 15 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
DOTA2【瓜皮时刻】Vol.91 RTZ山史最惨“矿难”
2021/03/05 DOTA
Yii使用CLinkPager分页实例详解
2014/07/23 PHP
Joomla调用系统自带编辑器的实现方法
2016/05/05 PHP
老生常谈PHP面向对象之标识映射
2017/06/21 PHP
使用jquery实现图文切换效果另加特效
2013/01/20 Javascript
基于jquery扩展漂亮的CheckBox(自己编写)
2013/11/19 Javascript
js淡入淡出的图片轮播效果代码分享
2015/08/24 Javascript
基于Jquery代码实现手风琴菜单
2015/11/19 Javascript
js 性能优化之算法和流程控制
2017/02/15 Javascript
解决vue打包之后静态资源图片失效的问题
2018/02/21 Javascript
Angular入口组件(entry component)与声明式组件的区别详解
2018/04/09 Javascript
vuejs使用axios异步访问时用get和post的实例讲解
2018/08/09 Javascript
详解webpack4.x之搭建前端开发环境
2019/03/28 Javascript
python实现类似ftp传输文件的网络程序示例
2014/04/08 Python
Python的Urllib库的基本使用教程
2015/04/30 Python
详细讲解Python中的文件I/O操作
2015/05/24 Python
python 使用socket传输图片视频等文件的实现方式
2019/08/07 Python
python解析yaml文件过程详解
2019/08/30 Python
Python Des加密解密如何实现软件注册码机器码
2020/01/08 Python
python itsdangerous模块的具体使用方法
2020/02/17 Python
linux 下selenium chrome使用详解
2020/04/02 Python
python 读取二进制 显示图片案例
2020/04/24 Python
python 数据分析实现长宽格式的转换
2020/05/18 Python
python利用递归方法实现求集合的幂集
2020/09/07 Python
使用HTML5的表单验证的简单示例
2015/09/09 HTML / CSS
工业设计专业个人求职信范文
2013/12/28 职场文书
求职简历中自我评价
2014/01/28 职场文书
幸福家庭事迹材料
2014/02/03 职场文书
大学生社团活动总结
2014/04/26 职场文书
品酒会策划方案
2014/05/26 职场文书
优秀共青团员事迹材料
2014/12/25 职场文书
《7的乘法口诀》教学反思
2016/02/18 职场文书
Python中递归以及递归遍历目录详解
2021/10/24 Python
IIS服务器中设置HTTP重定向访问HTTPS
2022/04/29 Servers
SpringBoot使用ip2region获取地理位置信息的方法
2022/06/21 Java/Android
MySQL新手入门进阶语句汇总
2022/09/23 MySQL