pygame游戏之旅 游戏中添加显示文字
作者:观月执白 发布时间:2023-03-26 02:56:23
标签:pygame,添加文字
本文为大家分享了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()
结果图:
来源:https://blog.csdn.net/pianzang5201/article/details/78303954


猜你喜欢
- 默认情况下IDE goland 是不支持protobuf协议文件类型".proto"的,为了更快高效的编写proto文件
- 前言项目中要实现多选,就想到用插件,选择了bootstrap-select。附上官网api链接,http://silviomoreto.gi
- 如下所示:import numpy as npZ=np.random.random((5,5))Zmax,Zmin=Z.max(axis=0
- 1.小猫运动游戏源码# @Author : 辣条'''多行注释本程序运行后会有一只小猫向前走安装模块 pip ins
- 前言:在数据库中,数据表是数据库中最重要、最基本的操作对象,是数据存储的基本单位。数据表被定义为列的集合,数据在表中是按照行和列的格式来存储
- 1. Golint(1)安装golintgit clone https://github.com/golang/lint.git
- 你和用户之间的网站堆栈(简化版)在TXJS大会的最后一天,一个开发者问我:面向对象的CSS没有给你留下一大堆基于表现的class名?网络堆栈
- 一、实例将以下列表的backup_unit_id全部提取出来示例:dbs = [{ &nbs
- 我们在压力测试过程会收集到很多log,怎样快速从中找到有用信息呢?让python脚本帮我们做这部分工作吧!废话不说,上代码环境:win10
- python程序运行中,可由程序抛出异常。异常触发:使用raise命令抛出异常,即可使用异常基类Exception,也可使用自定义异常类(继
- 一、用Python创建一个新文件,内容是从0到9的整数, 每个数字占一行:#python >>>f=open('f
- 最近重温了一下位图分割的相关内容,发现网络上位图分割原理讲得已经很清楚了,但是代码多为C++实现或者Matlab实现,因为需要Python的
- sympy有个vector 模块,里面提供了求解标量场、向量场的梯度、散度、旋度等计算,官方参考连接:http://docs.sympy.o
- 第一步.开启mysql慢查询方式一:修改配置文件Windows:Windows 的配置文件为 my.ini,一般在 MySQL 的安装目录下
- 如下所示:import numpy as npimport pandas as pdfrom pandas import Sereis, D
- 三遍记忆,让你记住海量素材的准确位置和用途,提高其可用性.这仅仅是一个示意图.在以往的日子里,我做到了只要脑袋里冒出一个想法,立刻就能知道我
- 本文实例讲述了Python设计模式之代理模式。分享给大家供大家参考,具体如下:代理模式在一般形式上是一个类函数接口。代理可以是这些事物的接口
- 双击编辑功能如何实现:例如:标题 (鼠标双击“标题”文字 即出现可编辑的输入框形式及提交按钮) <!D
- 铃铃铃…… 上课了老师在黑板写着这么一个标题 《Python: 你所不知道的星号 * 用法》同学A: 呃,星号不就
- 一、问题描述如题,在使用pip install xxx的方法安装python库,或者是基于python的软件时,报错“E