-
Notifications
You must be signed in to change notification settings - Fork 0
/
gameEngine.py
206 lines (182 loc) · 6.78 KB
/
gameEngine.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
import random
from typing import Optional
from candy import Candy
from candyColor import ALL_CANDY_COLORS, CandyColor
from customExceptions import BrokenGameLogicException, GameOverException
from direction import HORIZONTAL_DIRECTIONS, VERTICAL_DIRECTIONS, Direction
from gameCellKind import CANDY_SET, GameCellKind
from getAssetForCell import getAssetForCell, getColorForCell
from position import Position
from pyGameWindowController import PyGameWindowController
from snake import Snake
from pygame import rect
from candyMap import CandyMap
from constant import ALL_POSSIBLE_POSITIONS, CELL_RENDERER, \
GAME_GRID_Y_SIZE_IN_GAME_CELLS, GAME_GRID_X_SIZE_IN_GAME_CELLS
from soundProcessing import SoundKind, getSoundBy
class GameEngine:
def __init__(self):
self.__windowController = PyGameWindowController()
self.__playerScore: int = 0
self.__snakeLength: int = 0
def runGameLoop(self):
self.__windowController.clearWindow()
self.__setInitialGameState()
while True:
rectsToRerender = self.__makeGameIteration(
self.__getNextHeadDirection(
self.__windowController.parsePyGameEvents()
)
)
self.__windowController.updadeScreen(*rectsToRerender)
# pass
def renderGameOverScreen(self, deathReason: str):
self.__windowController.renderGameOverScreen(
deathReason,
self.__playerScore,
self.__snakeLength
)
def __makeGameIteration(self, direction: Direction):
if self.__snake.willSnakeBiteItselfAfterMove(direction):
raise GameOverException('Snake tried to bite itself')
if self.__willSnakeStepOutOfGameField(direction):
raise GameOverException('Snake tried to bite border of game field')
candy = self.__candiesField.getCandyBy(
self.__snake.headPosition.getNewPositionShiftedInto(direction)
)
doesHeadFacesCandy = candy is not None
if doesHeadFacesCandy:
getSoundBy(SoundKind.CANDY_EATEN).play()
return [
*(
self.__renderCell(position)
for position in self.__snake.makeStepAndGetPositionsToRerender(
direction,
doesHeadFacesCandy
)
),
*self.__makeGameIterationForCandies(
candy,
),
self.__windowController.renderScoreIntoGameWindow(
self.__playerScore
)
]
def __makeGameIterationForCandies(
self,
candy: Optional[Candy],
):
doesHeadFacesCandy = candy is not None
if doesHeadFacesCandy:
self.__playerScore += candy.size
self.__snakeLength += 1
self.__candiesField.removeCandyBy(candy.position)
removedCandyPositions = self.__candiesField.reduceAllCandiesSizeByOne()
if doesHeadFacesCandy:
removedCandyPositions.append(candy.position)
return [
*(self.__renderCell(candy.position)
for candy in self.__candiesField.getAllCandies()),
*(self.__renderCell(position)
for position in removedCandyPositions),
*(self.__renderCell(self.__generateCandy().position)
for _ in removedCandyPositions)
]
def __generateCandy(self):
position = random.choice(tuple(
ALL_POSSIBLE_POSITIONS
- {candy.position for candy in self.__candiesField.getAllCandies()}
- set(self.__snake.allNodesPositions)
))
snakeWayToReachPosition = (
abs(position.x - self.__snake.headPosition.x)
+ abs(position.y - self.__snake.headPosition.y)
)
return self.__candiesField.createNewCandy(
position,
random.choice(ALL_CANDY_COLORS),
int(snakeWayToReachPosition * 2)
+ random.randrange(10)
+ 1
)
def __getNextHeadDirection(
self,
directionFromKeyboard: Optional[Direction]
) -> Direction:
if (
(
self.__snake.headDirection in HORIZONTAL_DIRECTIONS
and directionFromKeyboard in HORIZONTAL_DIRECTIONS
) or (
self.__snake.headDirection in VERTICAL_DIRECTIONS
and directionFromKeyboard in VERTICAL_DIRECTIONS
) or (
directionFromKeyboard is None
)
):
return self.__snake.headDirection
return directionFromKeyboard
def __renderCell(
self,
position: Position,
overridedCellKind: Optional[GameCellKind] = None,
cellRenderer: str = CELL_RENDERER
):
cellKind: GameCellKind = self.__getCellKindBy(
position,
overridedCellKind
)
rectToRerender: Optional[rect.Rect] = None
if cellRenderer == 'color':
rectToRerender = self.__windowController.drawColor(
getColorForCell(cellKind),
position
)
elif cellRenderer == 'asset':
rectToRerender = self.__windowController.drawAsset(
getAssetForCell(cellKind),
position
)
if cellKind in CANDY_SET:
candy: Candy = self.__candiesField.getCandyBy(
position) # type: ignore
self.__windowController.drawWhiteTextOnCell(
f'{candy.size}',
position
)
if rectToRerender:
return rectToRerender
else:
raise BrokenGameLogicException('Unknown CELL_RENDERER')
def __willSnakeStepOutOfGameField(self, direction: Direction):
position = self.__snake.headPosition.getNewPositionShiftedInto(
direction
)
return (
position.x < 0
or position.y < 0
or position.x >= GAME_GRID_X_SIZE_IN_GAME_CELLS
or position.y >= GAME_GRID_Y_SIZE_IN_GAME_CELLS
)
def __getCellKindBy(
self,
position: Position,
overridedCellKind: Optional[GameCellKind] = None,
):
return (
overridedCellKind
or self.__snake.getGameCellKindBy(position)
or self.__candiesField.getGameCellKindBy(position)
or GameCellKind.VOID
)
def __setInitialGameState(self):
self.__snake = Snake()
self.__candiesField = CandyMap()
self.__windowController.updadeScreen(
*(self.__renderCell(self.__generateCandy().position)
for _ in range(5)),
*(self.__renderCell(position)
for position in self.__snake.allNodesPositions)
)
self.__playerScore: int = 0
self.__snakeLength: int = 3