forked from EricTsengTy/Challenge2021-Homework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.py
More file actions
185 lines (152 loc) · 5.6 KB
/
Model.py
File metadata and controls
185 lines (152 loc) · 5.6 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import random
import pygame as pg
from EventManager import *
import Const
class StateMachine(object):
'''
Manages a stack based state machine.
peek(), pop() and push() perform as traditionally expected.
peeking and popping an empty stack returns None.
TL;DR. Just for game state recording.
'''
def __init__(self):
self.statestack = []
def peek(self):
'''
Returns the current state without altering the stack.
Returns None if the stack is empty.
'''
try:
return self.statestack[-1]
except IndexError:
# empty stack
return None
def pop(self):
'''
Returns the current state and remove it from the stack.
Returns None if the stack is empty.
'''
try:
return self.statestack.pop()
except IndexError:
# empty stack
return None
def push(self, state):
'''
Push a new state onto the stack.
Returns the pushed value.
'''
self.statestack.append(state)
return state
def clear(self):
'''
Clear the stack.
'''
self.statestack = []
class GameEngine:
'''
The main game engine. The main loop of the game is in GameEngine.run()
'''
def __init__(self, ev_manager: EventManager):
'''
This function is called when the GameEngine is created.
For more specific objects related to a game instance
, they should be initialized in GameEngine.initialize()
'''
self.ev_manager = ev_manager
ev_manager.register_listener(self)
self.state_machine = StateMachine()
def initialize(self):
'''
This method is called when a new game is instantiated.
'''
self.clock = pg.time.Clock()
self.state_machine.push(Const.STATE_MENU)
self.players = [Player(0), Player(1)]
self.attack_side = 1
self.exchange_countdown = Const.t
def notify(self, event: BaseEvent):
'''
Called by EventManager when a event occurs.
'''
if isinstance(event, EventInitialize):
self.initialize()
elif isinstance(event, EventEveryTick):
# Peek the state of the game and do corresponding work
cur_state = self.state_machine.peek()
if cur_state == Const.STATE_MENU:
self.update_menu()
elif cur_state == Const.STATE_PLAY:
self.update_objects()
self.timer -= 1
if self.timer == 0:
self.ev_manager.post(EventTimesUp())
elif cur_state == Const.STATE_ENDGAME:
self.update_endgame()
elif isinstance(event, EventStateChange):
if event.state == Const.STATE_POP:
if self.state_machine.pop() is None:
self.ev_manager.post(EventQuit())
else:
self.state_machine.push(event.state)
elif isinstance(event, EventQuit):
self.running = False
elif isinstance(event, EventPlayerMove):
self.players[event.player_id].move_direction(event.direction)
elif isinstance(event, EventTimesUp):
self.state_machine.push(Const.STATE_ENDGAME)
def update_menu(self):
'''
Update the objects in welcome scene.
For example: game title, hint text
'''
pass
def update_objects(self):
'''
Update the objects not controlled by user.
For example: obstacles, items, special effects
'''
pass
def update_endgame(self):
'''
Update the objects in endgame scene.
For example: scoreboard
'''
pass
def run(self):
'''
The main loop of the game is in this function.
This function activates the GameEngine.
'''
self.running = True
# Tell every one to start
self.ev_manager.post(EventInitialize())
self.timer = Const.GAME_LENGTH
while self.running:
self.ev_manager.post(EventEveryTick())
self.clock.tick(Const.FPS)
dist_sq = (self.players[0].position.x-self.players[1].position.x) ** 2 + (self.players[0].position.y-self.players[1].position.y) ** 2
dist = dist_sq ** 0.5
if dist <= 2*Const.PLAYER_RADIUS:
self.state_machine.push(Const.STATE_ENDGAME)
self.exchange_countdown = (self.timer/Const.FPS) % Const.t
if self.timer != Const.GAME_LENGTH and self.timer != 0 and self.exchange_countdown == 0:
self.attack_side ^= 1
#print(self.attack_side)
for player in self.players:
player.speed = Const.SPEED_ATTACK if player.player_id == self.attack_side else Const.SPEED_DEFENSE
class Player:
def __init__(self, player_id):
self.player_id = player_id
self.position = Const.PLAYER_INIT_POSITION[player_id] # is a pg.Vector2
self.speed = Const.SPEED_ATTACK if player_id == 1 else Const.SPEED_DEFENSE
def move_direction(self, direction: str):
'''
Move the player along the direction by its speed.
Will automatically clip the position so no need to worry out-of-bound moving.
'''
# Modify position of player
self.position += self.speed / Const.FPS * Const.DIRECTION_TO_VEC2[direction]
# clipping
self.position.x = max(0, min(Const.ARENA_SIZE[0], self.position.x))
self.position.y = max(0, min(Const.ARENA_SIZE[1], self.position.y))