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对象转JSON字符串的方法
Apr 27 Python
Django读取Mysql数据并显示在前端的实例
May 27 Python
Python实现动态添加属性和方法操作示例
Jul 25 Python
Tensorflow 同时载入多个模型的实例讲解
Jul 27 Python
python高阶爬虫实战分析
Jul 29 Python
python获取中文字符串长度的方法
Nov 14 Python
pytorch 转换矩阵的维数位置方法
Dec 08 Python
python实现LBP方法提取图像纹理特征实现分类的步骤
Jul 11 Python
Python和Sublime整合过程图示
Dec 25 Python
Matplotlib绘制雷达图和三维图的示例代码
Jan 07 Python
python如何操作mysql
Aug 17 Python
PyCharm 解决找不到新打开项目的窗口问题
Jan 15 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 n个不重复的随机数生成代码
2009/06/23 PHP
PHPExcel读取Excel文件的实现代码
2011/12/06 PHP
PHP图片自动裁切应付不同尺寸的显示
2014/10/16 PHP
PHP+mysql防止SQL注入的方法小结
2019/04/27 PHP
分别用marquee和div+js实现首尾相连循环滚动效果,仅3行代码
2011/09/21 Javascript
Jquery中的$.each获取各种返回类型数据的使用方法
2015/05/03 Javascript
jQuery异步上传文件插件ajaxFileUpload详细介绍
2015/05/19 Javascript
IE8 内存泄露(内存一直增长 )的原因及解决办法
2016/04/06 Javascript
基于jQuery制作小图标上下滑动特效
2017/01/18 Javascript
layui固定下拉框的显示条数(有滚动条)的方法
2019/09/10 Javascript
[36:33]Ti4 循环赛第四日 附加赛NEWBEE vs Mouz
2014/07/13 DOTA
Python实现Tab自动补全和历史命令管理的方法
2015/03/12 Python
Python的Django框架中从url中捕捉文本的方法
2015/07/20 Python
如何高效使用Python字典的方法详解
2017/08/31 Python
python学生管理系统代码实现
2020/04/05 Python
python dataframe常见操作方法:实现取行、列、切片、统计特征值
2018/06/09 Python
Python内存读写操作示例
2018/07/18 Python
python3调用百度翻译API实现实时翻译
2018/08/16 Python
Django开发的简易留言板案例详解
2018/12/04 Python
python Pandas库基础分析之时间序列的处理详解
2019/07/13 Python
django 自定义过滤器(filter)处理较为复杂的变量方法
2019/08/12 Python
Python实现图片批量加入水印代码实例
2019/11/30 Python
美国知名的网上鞋类及相关服装零售商:Shoes.com
2017/05/06 全球购物
英国莱斯特松木橡木家具网上商店:Choice Furniture Superstore
2019/07/05 全球购物
英国哈罗德园艺:Harrod Horticultural
2020/03/31 全球购物
为什么要有struct关键字
2012/05/08 面试题
银行实习的自我鉴定
2013/12/10 职场文书
《乞巧》教学反思
2014/02/27 职场文书
物业总经理助理岗位职责
2014/06/29 职场文书
小兵张嘎电影观后感
2015/06/03 职场文书
2016年度员工工作表现评语
2015/12/02 职场文书
超市啤酒狂欢夜策划方案范文!
2019/07/03 职场文书
教您怎么制定西餐厅运营方案 ?
2019/07/05 职场文书
python基于机器学习预测股票交易信号
2021/05/25 Python
Python实战之OpenCV实现猫脸检测
2021/06/26 Python
Python实战实现爬取天气数据并完成可视化分析详解
2022/06/16 Python