You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Pygame俯视射击游戏主菜单设置视频背景?

解决Pygame主菜单视频背景独立窗口问题

你当前用Kivy的VideoWindow创建了独立的视频播放窗口,这和Pygame的游戏窗口属于两个独立进程,自然无法作为菜单背景。正确的做法是在Pygame窗口内部直接解码并渲染视频帧,再叠加菜单UI元素。

方案:使用OpenCV读取视频帧并在Pygame中渲染

先安装依赖库:

pip install opencv-python

修改后的完整主菜单代码:

import pygame
import sys
import cv2
import numpy as np

# 初始化Pygame基础配置
pygame.init()
DISPLAY_WIDTH = 800
DISPLAY_HEIGHT = 600
display = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT))
clock = pygame.time.Clock()

def main_menu():
    click = False
    font2 = pygame.font.Font('Pixel.TTF', 64)
    
    # 加载按钮图片(保留透明通道)
    play_btn = pygame.image.load('Play.png').convert_alpha()
    options_btn = pygame.image.load('Options.png').convert_alpha()
    quit_btn = pygame.image.load('Quit.png').convert_alpha()
    
    # 初始化视频捕获
    video_cap = cv2.VideoCapture('MenuVideo.mp4')
    if not video_cap.isOpened():
        print("无法加载视频文件")
        return
    
    while True:
        # 读取视频帧,循环播放
        ret, frame = video_cap.read()
        if not ret:
            video_cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
            continue
        
        # 转换视频帧格式适配Pygame
        frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        frame_surface = pygame.surfarray.make_surface(frame_rgb)
        # 修正OpenCV帧上下颠倒问题并缩放至窗口大小
        frame_surface = pygame.transform.rotate(frame_surface, 180)
        frame_surface = pygame.transform.scale(frame_surface, (DISPLAY_WIDTH, DISPLAY_HEIGHT))
        
        # 绘制视频背景
        display.blit(frame_surface, (0, 0))
        
        # 绘制菜单标题
        title_text = font2.render('MAIN MENU', True, (255, 255, 255))
        display.blit(title_text, (DISPLAY_WIDTH//2 - title_text.get_width()//2, 30))
        
        # 获取鼠标位置
        mx, my = pygame.mouse.get_pos()
        
        # 定义按钮区域
        play_rect = play_btn.get_rect(topleft=(50, 100))
        options_rect = options_btn.get_rect(topleft=(50, 200))
        quit_rect = quit_btn.get_rect(topleft=(50, 300))
        
        # 处理按钮点击逻辑
        if play_rect.collidepoint(mx, my) and click:
            video_cap.release()
            game()
        if options_rect.collidepoint(mx, my) and click:
            video_cap.release()
            options()
        if quit_rect.collidepoint(mx, my) and click:
            video_cap.release()
            pygame.quit()
            sys.exit()
        
        # 绘制按钮
        display.blit(play_btn, play_rect)
        display.blit(options_btn, options_rect)
        display.blit(quit_btn, quit_rect)
        
        click = False
        
        # 事件处理
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                video_cap.release()
                pygame.quit()
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    video_cap.release()
                    pygame.quit()
                    sys.exit()
            if event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1:
                    click = True
        
        pygame.display.update()
        clock.tick(60)

def game():
    # 你的游戏逻辑实现
    pass

def options():
    # 你的设置菜单逻辑实现
    pass

if __name__ == "__main__":
    main_menu()

关键说明

  • 彻底放弃Kivy的独立窗口方案,将视频渲染整合进Pygame主循环
  • 通过OpenCV读取视频帧,转换色彩空间并修正方向后,渲染为窗口背景
  • 先绘制视频背景,再叠加菜单UI元素,确保菜单显示在背景上方
  • 切换界面或退出时释放视频捕获资源,避免内存泄漏

内容的提问来源于stack exchange,提问作者Maddox Styles

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 08:09:22