Pygame 游戏开始:使用回车键启动游戏
import pygame
pygame.init()
# 设置游戏窗口大小
WINDOW_WIDTH, WINDOW_HEIGHT = 800, 600
WINDOW_SIZE = (WINDOW_WIDTH, WINDOW_HEIGHT)
# 创建游戏窗口
screen = pygame.display.set_mode(WINDOW_SIZE)
pygame.display.set_caption('Press Enter to Start Game')
# 设置游戏字体
font = pygame.font.SysFont('Arial', 48)
# 游戏主循环
running = True
start_game = False
while running:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
start_game = True
# 绘制游戏界面
screen.fill((255, 255, 255))
if not start_game:
# 绘制游戏标题
title_text = font.render('Press Enter to Start Game', True, (0, 0, 0))
title_rect = title_text.get_rect(center=(WINDOW_WIDTH//2, WINDOW_HEIGHT//2))
screen.blit(title_text, title_rect)
else:
# 游戏逻辑和绘制代码在这里
# 刷新屏幕
pygame.display.update()
pygame.quit()
在这个示例中,我们使用了一个start_game变量来表示游戏是否已经开始。在游戏主循环中,如果start_game为False,则显示游戏标题并等待玩家按下回车键开始游戏;如果start_game为True,则执行游戏逻辑和绘制代码。当玩家按下回车键时,我们将start_game设置为True,这样游戏就开始了。
原文地址: https://www.cveoy.top/t/topic/mDAV 著作权归作者所有。请勿转载和采集!