pygame游戏之旅 游戏中添加显示文字


Posted in Python onNovember 20, 2018

本文为大家分享了pygame游戏之旅的第5篇,供大家参考,具体内容如下

在游戏中添加显示文字:

这里自己定义一个crash函数接口:

def crash():
 message_diaplay('You Crashed')

然后实现接口函数message_display(text)

def message_diaplay(text):
 largeText = pygame.font.Font('freesansbold.ttf',115)
 TextSurf, TextRect = text_objects(text, largeText)
 TextRect.center = ((display_width/2),(display_height/2))
 gameDisplay.blit(TextSurf, TextRect)
 pygame.display.update()
 time.sleep(2)
 game_loop()

在这其中定义了一个函数text_objects(text, largeText),最后实现这个函数即可

def text_objects(text, font):
 textSurface = font.render(text, True, white)
 return textSurface, textSurface.get_rect()

全部代码:

import pygame
import time
 
pygame.init()
 
white = (255,255,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')
 
def car(x, y):
 gameDisplay.blit(carImg, (x,y))
 
 
def text_objects(text, font):
 textSurface = font.render(text, True, white)
 return textSurface, textSurface.get_rect()
 
def message_diaplay(text):
 largeText = pygame.font.Font('freesansbold.ttf',115)
 TextSurf, TextRect = text_objects(text, largeText)
 TextRect.center = ((display_width/2),(display_height/2))
 gameDisplay.blit(TextSurf, TextRect)
 pygame.display.update()
 time.sleep(2)
 game_loop()
 
def crash():
 message_diaplay('You Crashed')
 
 
def game_loop():
 x = display_width * 0.45
 y = display_height * 0.8
 x_change = 0
 
 gameExit = False
 
 while not gameExit:
  for event in pygame.event.get():
   if event.type == pygame.QUIT:
    gameExit = True
   if event.type == pygame.KEYDOWN:
    if event.key == pygame.K_LEFT:
     x_change = -5
    elif event.key == pygame.K_RIGHT:
     x_change = 5
   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)
  car(x,y)
  if x > display_width - car_width or x < 0:
   gameExit = True
  pygame.display.update()
  clock.tick(60)
crash()
#game_loop()
pygame.quit()
quit()

结果图:

pygame游戏之旅 游戏中添加显示文字

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

Python 相关文章推荐
用Python编写一个简单的Lisp解释器的教程
Apr 03 Python
Python 类的继承实例详解
Mar 25 Python
详解Python 序列化Serialize 和 反序列化Deserialize
Aug 20 Python
python中Switch/Case实现的示例代码
Nov 09 Python
pandas 小数位数 精度的处理方法
Jun 09 Python
用python 实现在不确定行数情况下多行输入方法
Jan 28 Python
Python Excel处理库openpyxl使用详解
May 09 Python
梅尔频率倒谱系数(mfcc)及Python实现
Jun 18 Python
python async with和async for的使用
Jun 20 Python
Flask框架模板继承实现方法分析
Jul 31 Python
Python模拟登录之滑块验证码的破解(实例代码)
Nov 18 Python
python中用ctypes模拟点击的实例讲解
Nov 26 Python
pygame游戏之旅 添加键盘按键的方法
Nov 20 #Python
pygame游戏之旅 载入小车图片、更新窗口
Nov 20 #Python
一文带你了解Python中的字符串是什么
Nov 20 #Python
pygame游戏之旅 创建游戏窗口界面
Nov 20 #Python
pygame游戏之旅 python和pygame安装教程
Nov 20 #Python
python2和python3的输入和输出区别介绍
Nov 20 #Python
python使用pygame框架实现推箱子游戏
Nov 20 #Python
You might like
基于php验证码函数的使用示例
2013/05/03 PHP
php实现根据词频生成tag云的方法
2015/04/17 PHP
php中array_slice和array_splice函数解析
2016/10/18 PHP
PHP mongodb操作类定义与用法示例【适合mongodb2.x和mongodb3.x】
2018/06/16 PHP
在laravel中实现将查询的对象转换为多维数组的函数
2019/10/21 PHP
jQuery CSS()方法改变现有的CSS样式
2014/08/20 Javascript
JQuery自适应窗口大小导航菜单附源码下载
2015/09/01 Javascript
BootStrap与validator 使用笔记(JAVA SpringMVC实现)
2016/09/21 Javascript
JS 实现随机验证码功能
2017/02/15 Javascript
AngularJS 支付倒计时功能实现思路
2017/06/05 Javascript
js+html5实现半透明遮罩层弹框效果
2020/08/24 Javascript
jquery.pagination.js分页使用教程
2018/10/23 jQuery
Javascript删除数组里的某个元素
2019/02/28 Javascript
关于Vue源码vm.$watch()内部原理详解
2019/04/26 Javascript
jquery+php后台实现省市区联动功能示例
2019/05/23 jQuery
如何提升vue.js中大型数据的性能
2019/06/21 Javascript
微信小程序实现日历小功能
2020/11/18 Javascript
JS实现页面侧边栏效果探究
2021/01/08 Javascript
vue实现简易计算器功能
2021/01/20 Vue.js
Python生成器(Generator)详解
2015/04/13 Python
从Python的源码浅要剖析Python的内存管理
2015/04/16 Python
举例讲解Linux系统下Python调用系统Shell的方法
2015/11/07 Python
Python中字典(dict)合并的四种方法总结
2017/08/10 Python
python虚拟环境virtualenv的安装与使用
2017/09/21 Python
解决Pycharm界面的子窗口不见了的问题
2019/01/17 Python
python从list列表中选出一个数和其对应的坐标方法
2019/07/20 Python
python打开文件的方式有哪些
2020/06/29 Python
Window10上Tensorflow的安装(CPU和GPU版本)
2020/12/15 Python
授权委托书格式
2014/07/31 职场文书
白鹤梁导游词
2015/02/06 职场文书
借款民事起诉状范文
2015/05/19 职场文书
红高粱观后感
2015/06/10 职场文书
两行代码解决Jupyter Notebook中文不能显示的问题
2021/04/24 Python
OpenCV 图像梯度的实现方法
2021/07/25 Python
python识别围棋定位棋盘位置
2021/07/26 Python
SQLServer中exists和except用法介绍
2021/12/04 SQL Server