-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathView.py
87 lines (67 loc) · 2.62 KB
/
View.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 as pg
from EventManager import *
from Model import GameEngine
import Const
class GraphicalView:
'''
Draws the state of GameEngine onto the screen.
'''
background = pg.Surface(Const.ARENA_SIZE)
def __init__(self, ev_manager: EventManager, model: GameEngine):
'''
This function is called when the GraphicalView is created.
For more specific objects related to a game instance
, they should be initialized in GraphicalView.initialize()
'''
self.ev_manager = ev_manager
ev_manager.register_listener(self)
self.model = model
self.screen = pg.display.set_mode(Const.WINDOW_SIZE)
pg.display.set_caption(Const.WINDOW_CAPTION)
self.background.fill(Const.BACKGROUND_COLOR)
def initialize(self):
'''
This method is called when a new game is instantiated.
'''
pass
def notify(self, event):
'''
Called by EventManager when a event occurs.
'''
if isinstance(event, EventInitialize):
self.initialize()
elif isinstance(event, EventEveryTick):
self.display_fps()
cur_state = self.model.state_machine.peek()
if cur_state == Const.STATE_MENU: self.render_menu()
elif cur_state == Const.STATE_PLAY: self.render_play()
elif cur_state == Const.STATE_STOP: self.render_stop()
elif cur_state == Const.STATE_ENDGAME: self.render_endgame()
def display_fps(self):
'''
Display the current fps on the window caption.
'''
pg.display.set_caption(f'{Const.WINDOW_CAPTION} - FPS: {self.model.clock.get_fps():.2f}')
def render_menu(self):
# draw background
self.screen.fill(Const.BACKGROUND_COLOR)
# draw text
font = pg.font.Font(None, 36)
text_surface = font.render("Press [space] to start ...", 1, pg.Color('gray88'))
text_center = (Const.ARENA_SIZE[0] / 2, Const.ARENA_SIZE[1] / 2)
self.screen.blit(text_surface, text_surface.get_rect(center=text_center))
pg.display.flip()
def render_play(self):
# draw background
self.screen.fill(Const.BACKGROUND_COLOR)
# draw players
for player in self.model.players:
center = list(map(int, player.position))
pg.draw.circle(self.screen, Const.PLAYER_COLOR[player.player_id], center, Const.PLAYER_RADIUS)
pg.display.flip()
def render_stop(self):
pass
def render_endgame(self):
# draw background
self.screen.fill(Const.BACKGROUND_COLOR)
pg.display.flip()