-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgame.py
176 lines (142 loc) · 5.18 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
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
# -*- coding: utf-8 -*-
#
# Game.py, Ice Emblem's main game class.
#
# Copyright 2015 Elia Argentieri <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
import pygame
import pygame.locals as p
import gc
import display
import gui
import utils
import ai
import room
import rooms
import colors as c
import fonts as f
import state as s
class NextTurnTransition(gui.Label):
def __init__(self, team):
super().__init__(_("%s phase") % team.name, f.MAIN_MENU, txt_color=team.color,
layout=room.Layout(gravity=room.Gravity.FILL),
background=room.Background(color=c.BLACK), allowed_events=[p.MOUSEBUTTONDOWN, p.KEYDOWN])
self.next_team = team
def begin(self):
super().begin()
bg = display.window.copy().convert()
bg.set_alpha(100)
self.background.image = bg
pygame.mixer.music.fadeout(1000)
if isinstance(self.next_team, ai.AI):
self.next = AITurn()
else:
self.next = PlayerTurn()
self.set_timeout(2000, self.handle_timeout)
def handle_timeout(self, _event):
self.done = True
def handle_mousebuttondown(self, event):
if event.button == 1:
self.done = True
def handle_keydown(self, event):
if event.key in [p.K_SPACE, p.K_RETURN]:
self.done = True
class Turn(gui.LinearLayout):
def __init__(self, **kwargs):
super().__init__(wait=True, orientation=gui.Orientation.HORIZONTAL, **kwargs)
self.add_children(s.loaded_map, gui.Sidebar(self, die_when_done=False))
def begin(self):
self.team = s.units_manager.active_team
self.team.play_music('map')
self.bind_keys((p.K_BACKSPACE, ), self.end_turn)
def handle_keydown(self, event):
if event.key == pygame.K_ESCAPE:
self.pause_menu()
def loop(self, _events, dt):
"""
Main loop.
"""
super().loop(_events, dt)
if s.winner is not None:
self.done = True
self.next = rooms.Fadeout(duration=1000, next=rooms.VictoryScreen(next=rooms.Fadeout(duration=2000)))
elif self.team.is_turn_over() and not self.next:
self.end_turn()
def pause_menu(self):
menu_entries = [
(_('Return to Game'), None),
(_('Return to Main Menu'), self.reset),
(_('Return to O.S.'), utils.return_to_os)
]
display.darken(200)
menu = gui.Menu(menu_entries, f.MAIN, layout=room.Layout(gravity=room.Gravity.CENTER), dismiss_callback=True,
clear_screen=None)
room.run_room(menu)
def reset(self, *_):
room.run_room(rooms.Fadeout(1000))
room.stop()
def end_turn(self, *args):
s.loaded_map.reset_selection()
next_team = s.units_manager.switch_turn()
self.next = NextTurnTransition(next_team)
self.set_timeout(1000, self.mark_done)
def end(self):
s.loaded_map.reset_selection()
class PlayerTurn(Turn):
def __init__(self):
super().__init__(allowed_events=[p.MOUSEBUTTONDOWN, p.KEYDOWN, p.MOUSEMOTION])
def end_turn(self, *args):
if self.team.is_turn_over():
super().end_turn()
self.set_timeout(100, self.mark_done)
else:
modal = gui.Modal(_("Are you sure you want to end your turn? There are still units that can move."),
f.SMALL, layout=room.Layout(gravity=gui.Gravity.CENTER), dismiss_callback=True,
clear_screen=None)
room.run_room(modal)
if modal.answer:
super().end_turn()
self.set_timeout(100, self.mark_done)
class AITurn(Turn):
def begin(self):
super().begin()
self.actions = iter(self.team)
def loop(self, _events, dt):
super().loop(_events, dt)
if len(s.loaded_map.children) == 0 and not self.next:
try:
action = next(self.actions)
s.loaded_map.do_action(action)
except StopIteration:
self.team.end_turn()
def main_menu():
room.run(rooms.SplashScreen())
room.run(rooms.Fadeout(2000))
def actual_game():
room.run(NextTurnTransition(s.units_manager.active_team))
s.loaded_map = None
s.units_manager = None
s.winner = None
gc.collect()
def play(map_file):
if map_file is None:
main_menu()
else:
s.load_map(map_file)
while True:
actual_game()
main_menu()