/
game.py
87 lines (65 loc) · 2.41 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
import pygame
import numpy as np
from pygame.locals import QUIT, KEYDOWN, K_ESCAPE, K_q, MOUSEBUTTONDOWN, MOUSEBUTTONUP
from pymunk.pygame_util import DrawOptions
from pymunk.pygame_util import to_pygame, from_pygame
from utils.settings.physics import space_damping
from utils.settings.game import WIDTH, HEIGHT
from utils.pymunk_utils import create_space, add_collisions
class Game:
def __init__(self, record=True):
self.armies = []
self.objects = []
self.video = []
self.fps = 60.0
self.done = False
self.record_mouse = False
self.mouse_traj = []
self.record = record
self.create_screen()
self.create_space()
self.draw_options = DrawOptions(self.screen)
# =============================================================================
# Instantiation
# =============================================================================
def create_screen(self):
pygame.init()
self.font = pygame.font.Font(pygame.font.get_default_font(), 8)
self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
self.clock = pygame.time.Clock()
def create_space(self):
self.space = create_space(WIDTH, HEIGHT)
self.space.damping = space_damping
add_collisions(self.space)
# =============================================================================
# Called at every frame
# =============================================================================
def update(self, dt):
self.space.step(dt)
for army in self.armies:
army.update(dt)
def draw(self, DEBUG):
# Pymunk debugging draw (constraints and stuff). N.B. Quite slow
# if DEBUG:
# self.space.debug_draw(self.draw_options)
# Drawing soldiers
for a in self.armies:
a.draw(DEBUG)
# Other stuff
self.show_fps()
# =============================================================================
# Utilities
# =============================================================================
def get_mouse_pos(self, is_pygame = True):
if is_pygame: return pygame.mouse.get_pos()
return from_pygame(pygame.mouse.get_pos(), self.screen)
def save_video(self):
from pygifsicle import optimize
import imageio
imageio.mimwrite("test_.gif", self.video, fps=self.fps)
optimize("test_.gif")
def show_fps(self):
fps = self.font.render(
str(int(self.clock.get_fps())), True, pygame.Color("black")
)
self.screen.blit(fps, (50, 50))