pygame游戏之旅 游戏中添加显示文字
作者:观月执白 时间:2023-03-26 02:56:23
本文为大家分享了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
标签:pygame,添加文字
0
投稿
猜你喜欢
使用pycallgraph分析python代码函数调用流程以及框架解析
2023-06-05 08:52:56
随Linux开机自动启动mysql
2009-12-29 10:14:00
使用python实现定时报天气的示例代码
2021-12-27 14:56:10
asp之自动闭合HTML/ubb标签函数+简单注释
2008-09-29 12:21:00
简单的分页代码js实现
2024-04-25 13:10:29
不要忽略了颜色的可用性
2009-03-05 18:19:00
细化解析:MySQL 搜索中的大小写敏感性
2008-11-27 15:53:00
Pycharm运行时总是跳出Python Console问题
2023-06-22 11:40:34
Python Base64编码和解码操作
2022-10-20 06:26:05
比较经典技术普及帖 以你刚才在淘宝上买了一件东西
2022-01-19 06:59:15
关于Python字符编码与二进制不得不说的一些事
2022-01-07 14:56:46
PHP实现判断二叉树是否对称的方法
2023-06-28 13:24:10
一篇文章教你用python画动态爱心表白
2021-02-10 04:14:38
参考sql2012存储过程写的统计所有用户表尺寸大小的示例
2024-01-25 05:50:18
pytorch中的卷积和池化计算方式详解
2021-03-31 19:26:32
如何利用JavaScript读取excel文件并绘制echarts图形
2023-08-27 07:51:16
MySQL函数与存储过程字符串长度限制的解决
2024-01-16 13:17:01
Python 实现Mac 屏幕截图详解
2021-06-16 13:43:52
python rolling regression. 使用 Python 实现滚动回归操作
2021-01-11 09:38:07
分页技术原理与实现之分页的意义及方法(一)
2024-01-26 14:29:16