代码如下
import pygame
import sys
import random
# 初始化Pygame
pygame.init()
# 设置窗口尺寸
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 600
window_surface = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption('应援语跑马灯模拟')
# 定义颜色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
# 定义字体
font = pygame.font.SysFont('msyh.ttc', 128)
text = "I love china!" # 应援语
text_width, text_height = font.size(text)
# 定义文本的位置
text_x = WINDOW_WIDTH
text_y = (WINDOW_HEIGHT - text_height) // 2
# 主循环
while True:
# 定义字体颜色
corler = (random.randint(50, 255), random.randint(50, 255), random.randint(50, 255))
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# 清空窗口
window_surface.fill(WHITE)
# 绘制文本
text_surface = font.render(text, True, corler)
window_surface.blit(text_surface, (text_x, text_y))
# 更新文本位置
text_x -= 5 # 控制跑马灯速度
# 如果文本移出屏幕左侧,重新放置在屏幕右侧
if text_x + text_width < 0:
text_x = WINDOW_WIDTH
# 更新窗口
pygame.display.update()
# 控制帧率
pygame.time.delay(200) # 50毫秒
标签:灯牌,python,text,应援,random,surface,WINDOW,pygame,255
From: https://blog.csdn.net/u011095039/article/details/137514225