Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· OpenAIAi2 WildChat-4.8M (AI2)

我用python製作了一個pygame,但是我想將遊玩過程錄音錄影要怎麼做,也就是遊戲一開始到關閉會自動存檔

  1. User

    我用python製作了一個pygame,但是我想將遊玩過程錄音錄影要怎麼做,也就是遊戲一開始到關閉會自動存檔

  2. Assistant

    將遊玩過程錄音錄影,可以使用Python的第三方庫OpenCV和PyAudio。透過OpenCV,可以截取畫面並將其逐幀保存成視頻檔案,然後使用PyAudio來錄音和保存音訊檔案。

    以下是一個簡單的範例,為了展示截屏功能,本範例使用pygame製作一個簡單的可控制方塊的遊戲。事實上這個範例並不完整,會沒有音效部份,然而將程式碼改為符合你的需要應該會容易許多。

    import cv2
    import numpy as np
    import pyaudio
    import wave
    import pygame
    import sys
    
    # 設定視頻參數
    fps = 10.0
    fourcc = cv2.VideoWriter_fourcc(*"MJPG")
    frame_size = (640, 480)
    out_video = cv2.VideoWriter("out.avi", fourcc, fps, frame_size)
    
    # 設定錄音參數
    chunk = 1024
    sample_format = pyaudio.paInt16
    channels = 2
    fs = 44100
    seconds = 10
    out_audio = wave.open("out.wav", "wb")
    out_audio.setnchannels(channels)
    out_audio.setsampwidth(pyaudio.get_sample_size(sample_format))
    out_audio.setframerate(fs)
    
    pygame.init()
    
    # 創建視窗
    screen = pygame.display.set_mode(frame_size)
    
    # 創建方塊
    block_size = 50
    block_x, block_y = frame_size[0] // 2 - block_size // 2, frame_size[1] // 2 - block_size // 2
    block_speed = 5
    block_color = (255, 255, 0)
    block = pygame.Rect(block_x, block_y, block_size, block_size)
    
    # 開始錄音和錄屏
    audio = pyaudio.PyAudio()
    stream = audio.open(format=sample_format, channels=channels, rate=fs, frames_per_buffer=chunk, input=True)
    frame_count = 0
    
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                # 關閉錄音和錄屏
                stream.stop_stream()
                stream.close()
                audio.terminate()
                out_audio.close()
                out_video.release()
                pygame.quit()
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    block.x -= block_speed
                elif event.key == pygame.K_RIGHT:
                    block.x += block_speed
                elif event.key == pygame.K_UP:
                    block.y -= block_speed
                elif event.key == pygame.K_DOWN:
                    block.y += block_speed
    
        # 填充背景色
        screen.fill((0, 0, 0))
    
        # 畫出方塊
        pygame.draw.rect(screen, block_color, block)
    
        # 更新視窗
        pygame.display.update()
    
        # 截圖並寫入視頻
        frame = np.array(pygame.surfarray.array3d(screen))
        out_video.write(frame)
    
        # 錄音並寫入音訊檔案
        data = stream.read(chunk)
        out_audio.writeframes(data)
    
        # 控制視頻帧率
        pygame.time.Clock().tick(fps)
    
    

    在程式碼中,我們使用OpenCV的VideoWriter物件out_video來保存視頻;PyAudio的wave物件out_audio來保存音訊。每次遊戲更新時,我們會截取畫面(frame),將其寫入視頻檔案,也會讀取音訊數據(data),將其寫入音訊檔案中。

    最後需要注意,本範例並不是一個完整的pygame遊戲,並不完全符合你的需要,你需要修改程式碼以符合你的實際需求。

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.