-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
89 lines (70 loc) · 2.51 KB
/
game.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import pygame
import cv2
import time
from recorder import Recorder
from games.squares_fight import SquareFight
from util import get_window_position
pygame.init()
# game variables
WINDOW_NAME = "Python"
FPS = 60
SCREEN_WIDTH = 576
SCREEN_HEIGHT = 1016
# set up Pygame window
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), pygame.NOFRAME)
pygame.display.set_caption(WINDOW_NAME)
# force window creation by handling a few events
for _ in range(5): # process a few events to ensure window is initialized
pygame.event.get()
pygame.display.flip()
def game_loop(game, fps):
clock = pygame.time.Clock()
start_time = time.time()
game.start()
while True:
game.update() # update game state
pygame.display.flip() # update display
if time.time() - start_time > MAX_RECORDING_TIME: # stop after MAX_RECORDING_TIME seconds
break
if RECORD_MODE == "timed" and time.time() - start_time >= VIDEO_LENGTH: # stop after VIDEO_LENGTH seconds
break
if RECORD_MODE == "signal" and game.stopped() == True:
end_signal_time = time.time()
# continue for 3 more seconds
while time.time() - end_signal_time < 3:
game.update()
pygame.display.flip()
clock.tick(fps)
break
clock.tick(fps)
return time.time() - start_time # return elapsed time
# recording setup
RECORD_MODE = "signal" # "signal" or "timed"
VIDEO_LENGTH = 5 # in seconds (for "timed" mode)
MAX_RECORDING_TIME = 90 # seconds (time before restarting the process)
FOURCC = "mp4v"
OUTPUT_FILE = "output.mp4"
recorder = Recorder(fourcc=FOURCC, fps=FPS)
while True:
time.sleep(0.5)
# get window-to-be-recorded data
x, y, width, height = get_window_position(WINDOW_NAME)
print(x, y, width, height)
# start recording window
recorder.start_recording(x, y, width, height)
# create a new game instance
game = SquareFight(screen, fps=FPS, screen_width=SCREEN_WIDTH, screen_height=SCREEN_HEIGHT)
# run the game loop and measure time
start_time = time.time()
elapsed_time = game_loop(game, FPS)
# end recording
recorder.stop_recording()
if elapsed_time > MAX_RECORDING_TIME:
print("Recording exceeded 90 seconds. Restarting...")
continue # restart the process
# merge video and audio if within time limit
recorder.merge_audio_video()
break # exit loop and finalize recording
# cleanup
pygame.quit()
cv2.destroyAllWindows()