-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathUserInterface.py
More file actions
147 lines (131 loc) · 6.42 KB
/
Copy pathUserInterface.py
File metadata and controls
147 lines (131 loc) · 6.42 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
import copy
import pygame
from pygame.math import Vector2
from pygame.rect import Rect
from Blocks import *
from MapLoader import *
from GameRule import GameRuleObserver
from GameState import *
from Settings import *
from StatisticsReader import *
class UserInterface():
def __init__(self, map):
# pygame.init()
self._map = map
self.worldSize = Vector2(WORLD_MAX_X, WORLD_MAX_Y)
self._cellSize = Vector2(CELL_SIZE_X, CELL_SIZE_Y)
_windowSize = self.worldSize.elementwise() * self._cellSize
self._window = pygame.display.set_mode((int(_windowSize.x), int(_windowSize.y)))
pygame.display.set_caption(GAME_TITLE + " - " + str(type(map).__name__))
self._gameState = GameState(self._map.load_map())
self._clock = pygame.time.Clock()
self._running = True
self._moveCommand = Vector2(0, 0)
self._isCountSign = False
self._loadingPicture = pygame.image.load(self._map.load_picture())
self._countdown_time = COUNTDOWN
if type(self._map) == MapMainMenu:
pygame.mixer.music.load("music/menu.ogg")
elif type(self._map) == MapAbout:
pygame.mixer.music.load("music/map.ogg")
else:
pygame.mixer.music.load("music/garden.ogg")
pygame.mixer.music.play(-1)
def check_win_state(self):
return self._gameState.playerState
def check_lose_state(self):
return not self._gameState.aliveState
def _update(self):
testObserver = GameRuleObserver(self._gameState)
for i in range(len(self._gameState.units)):
if self._gameState.units[i].is_text():
self._gameState.units[i].texture.set_alpha(ALPHA)
testObserver.endow(self._gameState.isBlockList)
testObserver.transform(self._gameState.isBlockList)
if self._gameState.playerState:
self._running = False
if testObserver.is_win(self._gameState.isBlockList):
self._gameState.playerState = True
if not self._isCountSign \
and type(self._map).__name__ != "MapMainMenu" \
and type(self._map).__name__ != "MapAbout":
winCountList = check_win_count(STATISTICS_FILE_PATH)
mapId = int(type(self._map).__name__.split('Map')[1]) - 1
update_win_count(STATISTICS_FILE_PATH, type(self._map).__name__)
if winCountList[mapId] == 0:
update_first_win_time(STATISTICS_FILE_PATH, type(self._map).__name__)
self._isCountSign = True
if testObserver.is_lose(self._gameState.units):
self._gameState.aliveState = False
if not self._isCountSign \
and type(self._map).__name__ != "MapMainMenu" \
and type(self._map).__name__ != "MapAbout":
update_lose_count(STATISTICS_FILE_PATH, type(self._map).__name__)
self._isCountSign = True
for unit in self._gameState.units:
if unit.is_control() and self._moveCommand != Vector2(0, 0):
testObserver.move(unit, self._moveCommand)
def _process_input(self):
self._moveCommand = Vector2(0, 0)
for event in pygame.event.get():
if event.type == pygame.QUIT:
self._running = False
break
elif event.type == pygame.KEYDOWN:
if event.type == pygame.K_ESCAPE:
self._running = False
break
elif event.key == pygame.K_RIGHT or event.key == pygame.K_d:
self._moveCommand = RIGHT_DIRECTION
elif event.key == pygame.K_LEFT or event.key == pygame.K_a:
self._moveCommand = LEFT_DIRECTION
elif event.key == pygame.K_DOWN or event.key == pygame.K_s:
self._moveCommand = BOTTOM_DIRECTION
elif event.key == pygame.K_UP or event.key == pygame.K_w:
self._moveCommand = TOP_DIRECTION
elif event.key == pygame.K_r:
self._gameState = GameState(self._map.load_map())
self._isCountSign = False
elif event.key == pygame.K_SPACE:
self._running = False
break
def _loading(self):
while self._countdown_time > 0:
for event in pygame.event.get():
if event.type == pygame.QUIT:
self._running = False
break
elif event.type == pygame.KEYDOWN:
self._countdown_time = 0
self._window.blit(self._loadingPicture, Vector2(0, 0))
self._window.blit(pygame.image.load("pics/anykey.png"), Vector2(380, 500))
if type(self._map).__name__ != "MapMainMenu" and type(self._map).__name__ != "MapAbout":
self._render_unit(FlagBlock("WIN_COUNT_FLAG", Vector2(2, 0), passable=False))
self._window.blit(pygame.image.load("pics/text_win_count.png"), Vector2(30, 0))
self._window.blit(pygame.image.load("pics/icon_skull.png"), Vector2(2, 30))
self._window.blit(pygame.image.load("pics/text_lose_count.png"), Vector2(30, 30))
self._window.blit(pygame.image.load("pics/icon_clock.png"), Vector2(2, 60))
self._window.blit(pygame.image.load("pics/text_time_win.png"), Vector2(30, 60))
statistics_display(self._window, int(type(self._map).__name__.split("Map")[1])-1)
pygame.display.update()
self._countdown_time -= 1
def _render_unit(self, unit):
unit.cartoon()
self._window.blit(unit.texture, unit.location)
def _render(self):
self._window.fill(BACKGROUND_COLOR) # 黑色背景
for unit in self._gameState.units:
self._render_unit(unit)
if not self._gameState.aliveState:
self._window.blit(pygame.image.load("pics/R.png"), Vector2(200, 12))
self._window.blit(pygame.image.load("pics/button_restart_0_1.png"), Vector2(230, -3))
self._window.blit(pygame.image.load("pics/space.png"), Vector2(500, 0))
self._window.blit(pygame.image.load("pics/main_menu.png"), Vector2(595, -1))
pygame.display.update()
def run(self):
while self._running:
self._process_input()
self._loading()
self._update()
self._render()
self._clock.tick(60)