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实现二叉堆
Feb 03 Python
python 调用win32pai 操作cmd的方法
May 28 Python
利用python实现简单的邮件发送客户端示例
Dec 23 Python
浅谈Python使用Bottle来提供一个简单的web服务
Dec 27 Python
python如何生成各种随机分布图
Aug 27 Python
浅谈Python traceback的优雅处理
Aug 31 Python
python数据处理 根据颜色对图片进行分类的方法
Dec 08 Python
基于Python实现人脸自动戴口罩系统
Feb 06 Python
开启Django博客的RSS功能的实现方法
Feb 17 Python
vscode写python时的代码错误提醒和自动格式化的方法
May 07 Python
Python 操作SQLite数据库的示例
Oct 16 Python
Python进程间的通信之语法学习
Apr 11 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
德生H-501的评价与改造
2021/03/02 无线电
apache php模块整合操作指南
2012/11/16 PHP
优化WordPress中文章与评论的时间显示
2016/01/12 PHP
php简单实现批量上传图片的方法
2016/05/09 PHP
基于Web标准的UI组件 — 树状菜单(2)
2006/09/18 Javascript
jquery读取xml文件实现省市县三级联动的方法
2015/05/29 Javascript
JS实现闭包中的沙箱模式示例
2017/09/07 Javascript
JavaScript实现为事件句柄绑定监听函数的方法分析
2017/11/14 Javascript
详解最新vue-cli 2.9.1的webpack存在问题
2017/12/16 Javascript
JavaScript设计模式之构造函数模式实例教程
2018/07/02 Javascript
使用nodejs实现JSON文件自动转Excel的工具(推荐)
2020/06/24 NodeJs
[01:04:14]OG vs Winstrike 2018国际邀请赛小组赛BO2 第二场 8.19
2018/08/21 DOTA
Python的print用法示例
2014/02/11 Python
调试Python程序代码的几种方法总结
2015/04/28 Python
virtualenv 指定 python 解释器的版本方法
2018/10/25 Python
pyttsx3实现中文文字转语音的方法
2018/12/24 Python
Appium+Python自动化测试之运行App程序示例
2019/01/23 Python
bluepy 一款python封装的BLE利器简单介绍
2019/06/25 Python
python tkinter组件使用详解
2019/09/16 Python
numpy创建单位矩阵和对角矩阵的实例
2019/11/29 Python
Pytorch 中的optimizer使用说明
2021/03/03 Python
AmazeUI 加载进度条的实现示例
2020/08/20 HTML / CSS
iHerb台湾:维生素、保健品和健康产品
2018/01/31 全球购物
任课老师推荐信范文
2013/11/24 职场文书
2014年巴西世界杯口号
2014/06/05 职场文书
小学三好学生事迹材料
2014/08/15 职场文书
2014年重阳节敬老活动方案
2014/09/16 职场文书
会计师事务所实习证明
2014/11/16 职场文书
亲属关系公证书样本
2015/01/23 职场文书
幼儿园迎新生欢迎词
2015/09/30 职场文书
《为人民服务》教学反思
2016/02/20 职场文书
用JS实现飞机大战小游戏
2021/06/09 Javascript
如何理解python接口自动化之logging日志模块
2021/06/15 Python
关于ObjectUtils.isEmpty() 和 null 的区别
2022/02/28 Java/Android
vue3语法糖内的defineProps及defineEmits
2022/04/14 Vue.js
Win11快速关闭所有广告推荐
2022/04/19 数码科技