pygame游戏开发教程:在屏幕上渲染文字与实现碰撞报错显示

周叙
周叙
发布时间:2026-10-13| 6人浏览过
原创

本文详解在pygame游戏中添加文字显示的方法。通过定义text_objects函数处理字体渲染,结合message_display函数在屏幕中央展示错误信息,并实现crash接口触发碰撞后的文字提示与游戏重置流程。

本系列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()

在message_display内部,调用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 相关产品和应用

全部下载
Selenium

Selenium

4.51.0 · Windows

用于浏览器自动化和 Web 测试的 Python 工具。

scikit-learn

scikit-learn

1.9.1 · Windows

常用 Python 机器学习算法与模型评估工具库。

Black

Black

26.10.1 · Windows

强调一致性的 Python 自动代码格式化工具。

Ruff

Ruff

0.17.1 · macOS

由 Rust 编写的高速 Python 代码检查和格式化工具。

pytest

pytest

9.1.1 · Windows

简洁而强大的 Python 自动化测试框架。

FastAPI

FastAPI

0.146.0 · Windows

高性能 Python API 框架,支持类型提示和自动接口文档。

Flask

Flask

3.1.3 · Windows

轻量灵活的 Python Web 应用框架。

Django

Django

6.1.2 · Windows

成熟的 Python Web 框架,适合构建内容站点和业务系统。

Spyder

Spyder

6.1.7 · Windows

面向科学计算与数据分析的 Python 集成开发环境。

SciPy

SciPy

1.18.1 · Windows

面向科学、工程和技术计算的 Python 算法库。