-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
executable file
·211 lines (172 loc) · 5.86 KB
/
app.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/env python
#-*- coding: utf8 -*-
from animations import *
from event_manage import EventManager
from locals import *
from player import Player
from pygame.locals import *
from render import Render
from stuff_on_map import *
from utils import *
import argparse
import config
import controllers
import gettext
import glob
import language
import logging
import menu
import os
import pygame
import pygame.joystick
import sys
import textures
import time
import world
import world_map
gettext.install('tank4eta')
eventer = EventManager()
def main(args):
os.chdir(os.path.dirname(os.path.realpath(__file__)))
desired_lang = os.environ.get('LANGUAGE', 'en')[:2]
cfg = config.Parse()
if 'language' in cfg:
desired_lang = cfg['language']
else:
cfg['language'] = desired_lang
language.select(desired_lang)
# Hint the window manager to put the window in the center of the screen
os.putenv('SDL_VIDEO_CENTERED', '1')
pygame.init()
pygame.joystick.init()
for i in range(pygame.joystick.get_count()):
pygame.joystick.Joystick(i).init()
# Ignore mouse motion (greatly reduces resources when not needed)
pygame.event.set_blocked(pygame.MOUSEMOTION)
pygame.mouse.set_visible(False)
available_maps = glob.glob(os.path.join(MAPS_DIR, '*.map'))
available_maps = [os.path.basename(m) for m in available_maps]
available_maps.sort()
selected = {
'players_count': 1,
'map': available_maps[0],
'exit': False,
'toggle_fullscreen': None,
'language': None,
'fullscreen': False,
'new_settings_applied': False,
}
render = Render(fullscreen=cfg.get('fullscreen', False))
if args.debug:
render.show_fps = True
while 42:
selected['new_settings_applied'] = False
selected = menu.main_menu(cfg, render, available_maps, selected)
if selected['exit']:
break
if selected['toggle_fullscreen'] is not None:
render.toggle_full_screen(selected['toggle_fullscreen'])
selected['toggle_fullscreen'] = None
continue
if selected['language'] is not None:
language.select(selected['language'])
selected['language'] = None
continue
if selected['new_settings_applied']:
continue
pygame.mixer.init(buffer=512)
game_loop(
cfg,
render,
players_count=selected['players_count'],
map_name=selected['map']
)
pygame.mixer.stop()
render.quit()
pygame.quit()
sys.exit(0)
def game_loop(cfg, render, players_count, map_name):
texture_loader = textures.Loader()
play_map = world_map.Map(map_path(map_name), render, texture_loader)
play_map.build_grid()
BulletExplosion.load_animation(texture_loader)
FullSizeExplosion.load_animation(texture_loader)
PanzerTankMovement.load_animation(texture_loader)
BasicTankMovement.load_animation(texture_loader)
EnemyOneMovement.load_animation(texture_loader)
EnemyTwoMovement.load_animation(texture_loader)
players = []
for i in range(players_count):
player = Player()
player.name = _('Player %d') % i
players.append(player)
keyboard_controllers = controllers.keyboard_controls()
for i in range(pygame.joystick.get_count()):
if i >= len(players):
break
j = pygame.joystick.Joystick(i)
players[i].controller = controllers.Gamepad(j)
for player in players:
if player.controller is not None:
continue
player.controller = next(keyboard_controllers)
for i, position in enumerate(play_map.player_starts):
if i >= players_count:
break
tank = Tank(position, texture_loader)
players[i].tank = tank
for player in players:
if player.tank is None:
raise Exception(_("Not enough start points for players!"))
render.clear_screen()
game_world = world.World(play_map, players, texture_loader)
game_world.init()
clock = pygame.time.Clock()
end_message = None
while 42:
deltat = clock.tick(FRAMES)
events = eventer.get_events()
if eventer.game_stopped():
pygame.mixer.pause()
selected = menu.pause_menu(cfg, render)
if selected == menu.PauseMenu.PAUSE_MENU_QUIT:
end_message = _("You Gave Up! Why?")
break
else:
render.clear_screen()
render.draw_background()
pygame.mixer.unpause()
clock.tick()
continue
game_state = game_world.tick(deltat, events)
if game_state == GAME_OVER:
end_message = _("GAME OVER. You've lost!")
break
if game_state == GAME_WON:
end_message = _("Yey! You've won!")
break
render.update_fps(clock)
render.draw(game_world.get_drawables())
for player in players:
if player.tank is not None:
player.tank.stop()
while game_world.active_animations_count() > 0:
deltat = clock.tick(FRAMES)
events = eventer.get_events()
game_world.tick_only_animations(deltat, events)
render.update_fps(clock)
render.draw(game_world.get_drawables())
stats = game_world.get_end_game_stats()
render.draw_end_game_screen(end_message, stats)
time.sleep(2)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='The game with tanks!.')
parser.add_argument('-D', dest='debug', action='store_true',
default=False,
help='Start the game in debug mode')
args = parser.parse_args()
loggingLevel = logging.ERROR
if args.debug:
loggingLevel = logging.DEBUG
logging.basicConfig(level=loggingLevel)
main(args)