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 动态获取当前运行的类名和函数名的方法
Apr 15 Python
全面理解Python中self的用法
Jun 04 Python
python实现QQ批量登录功能
Jun 19 Python
python爬虫增加访问量的方法
Aug 22 Python
python global关键字的用法详解
Sep 05 Python
python编写计算器功能
Oct 25 Python
Python Web静态服务器非堵塞模式实现方法示例
Nov 21 Python
如何在Django中使用聚合的实现示例
Mar 23 Python
如何基于线程池提升request模块效率
Apr 18 Python
解决运行django程序出错问题 'str'object has no attribute'_meta'
Jul 15 Python
Python数据分析库pandas高级接口dt的使用详解
Dec 11 Python
python内置模块之上下文管理contextlib
Jun 14 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下使用SMTP发邮件的代码
2008/01/10 PHP
用PHP进行MySQL删除记录操作代码
2008/06/07 PHP
php实现的遍历文件夹下所有文件,编辑删除
2010/01/05 PHP
javascript编程起步(第四课)
2007/02/27 Javascript
jQuery 页面 Mask实现代码
2010/01/09 Javascript
15款优秀的jQuery导航菜单插件分享
2011/07/19 Javascript
jQuery层动画定位滑动效果的方法
2015/04/30 Javascript
JS数字抽奖游戏实现方法
2015/05/04 Javascript
WebGL利用FBO完成立方体贴图效果完整实例(附demo源码下载)
2016/01/26 Javascript
javascript数组遍历的方法实例分析
2016/09/13 Javascript
jQuery EasyUI 组件加上“清除”功能实例详解
2017/04/11 jQuery
Vue源码中要const _toStr = Object.prototype.toString的原因分析
2018/12/09 Javascript
Vue实现类似Spring官网图片滑动效果方法
2019/03/01 Javascript
基于javascript处理二进制图片流过程详解
2020/06/08 Javascript
JavaScript 获取滚动条位置并将页面滑动到锚点
2021/02/08 Javascript
Python3实现的腾讯微博自动发帖小工具
2013/11/11 Python
给Python初学者的一些编程技巧
2015/04/03 Python
python3制作捧腹网段子页爬虫
2017/02/12 Python
10分钟用python搭建一个超好用的CMDB系统
2019/07/17 Python
python hashlib加密实现代码
2019/10/17 Python
python 实现绘制整齐的表格
2019/11/18 Python
3种python调用其他脚本的方法
2020/01/06 Python
Django之富文本(获取内容,设置内容方式)
2020/05/21 Python
tensorflow图像裁剪进行数据增强操作
2020/06/30 Python
Python Pandas数据分析工具用法实例
2020/11/05 Python
matplotlib对象拾取事件处理的实现
2021/01/14 Python
数字天堂软件测试面试题
2012/12/23 面试题
仪器仪表检测毕业生自荐信
2013/10/31 职场文书
专业技术职务聘任书
2014/03/29 职场文书
党的群众路线教育实践活动个人对照检查材料(公安)
2014/11/05 职场文书
单位工作证明范本
2015/06/15 职场文书
2016秋季田径运动会广播稿
2015/12/21 职场文书
职业生涯规划书之大学四年
2019/08/07 职场文书
python 如何做一个识别率百分百的OCR
2021/05/29 Python
HTML+JS实现在线朗读器
2022/02/15 Javascript
Golang MatrixOne使用介绍和汇编语法
2022/04/19 Golang