forked from wojciech1871/Castle_game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameView.py
194 lines (156 loc) · 6.8 KB
/
GameView.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
import pygame
from pygame.locals import *
import pickle
from GameModel import *
from GameMenu import *
if not pygame.font:
print("Warning, fonts disabled")
if not pygame.mixer:
print("Warning, sound disabled")
class Ball(pygame.sprite.Sprite):
resolution = (35, 35)
color = None
def __init__(self):
super().__init__()
self.image = None
self.rect = None
def on_init(self):
self.image = pygame.transform.scale(self.image, (self.resolution[0], self.resolution[1]))
self.rect = self.image.get_rect()
class WhiteBall(Ball):
color = GameColor.WHITE
def __init__(self):
super().__init__()
self.image = FunContainer.load_image("white-ball.jpg", -1)
self.on_init()
class BlackBall(Ball):
color = GameColor.BLACK
def __init__(self):
super().__init__()
self.image = FunContainer.load_image("black-ball.jpg", -1)
self.on_init()
class BallsContainer(pygame.sprite.RenderPlain):
def __init__(self, ballsList):
super().__init__()
self.ballsList = ballsList
class GameView:
windowWidth = GameMenu.windowWidth
windowHeight = GameMenu.windowHeight
marginWidth = 10
marginHeight = 10
numOfCells = GameModel.numOfCells
cellWidth = np.floor_divide(windowWidth-2*marginWidth, numOfCells)
marginWidth += np.floor_divide(np.remainder(windowWidth-2*marginWidth, numOfCells), 2)
cellHeight = np.floor_divide(windowHeight-2*marginHeight, numOfCells)
marginHeight += np.floor_divide(np.remainder(windowHeight-2*marginHeight, numOfCells), 2)
linesColor = (25, 25, 110)
def __init__(self, screen: pygame.Surface, gameModel: GameModel):
super().__init__()
self.gameModel = gameModel
self.board = self.board_init()
self.screen = screen
self.background = FunContainer.load_image("background.jpg")
self.background = pygame.transform.scale(self.background, (self.windowWidth, self.windowHeight))
self.draw_lines()
self.draw_thrones()
self.draw_walls()
self.gauntlet = None
self.blackBalls = None
self.whiteBalls = None
self.balls_init()
self.init_draw()
def balls_init(self):
if self.gameModel.player1.color == GameColor.BLACK:
blackBalls = self.gameModel.player1.balls
whiteBalls = self.gameModel.player2.balls
else:
blackBalls = self.gameModel.player2.balls
whiteBalls = self.gameModel.player1.balls
self.blackBalls = BallsContainer(blackBalls)
self.whiteBalls = BallsContainer(whiteBalls)
for position in self.blackBalls.ballsList:
blackBall = BlackBall()
blackBall.rect.center = Rect(self.board[position]).center
self.blackBalls.add(blackBall)
for position in self.whiteBalls.ballsList:
whiteBall = WhiteBall()
whiteBall.rect.center = Rect(self.board[position]).center
self.whiteBalls.add(whiteBall)
def balls_update(self):
activeColor = self.gameModel.activePlayer.color
if self.gameModel.player1Color == GameColor.WHITE:
self.whiteBalls.ballsList = self.gameModel.player1.balls
self.blackBalls.ballsList = self.gameModel.player2.balls
else:
self.whiteBalls.ballsList = self.gameModel.player2.balls
self.blackBalls.ballsList = self.gameModel.player1.balls
if activeColor == GameColor.WHITE:
numOfSprites = len(self.blackBalls)
numOfBalls = len(self.blackBalls.ballsList)
balls = self.whiteBalls
opballs = self.blackBalls
else:
numOfSprites = len(self.whiteBalls)
numOfBalls = len(self.whiteBalls.ballsList)
balls = self.blackBalls
opballs = self.whiteBalls
if numOfBalls != numOfSprites:
opballs.sprites()[0].kill()
for i in range(len(balls)):
balls.sprites()[i].rect.center = Rect(self.board[balls.ballsList[i]]).center
def init_draw(self):
self.screen.blit(self.background, (0, 0))
self.blackBalls.draw(self.screen)
self.whiteBalls.draw(self.screen)
def board_init(self):
board = np.array([[Rect([0]*4)]*self.numOfCells]*self.numOfCells)
for i in range(self.numOfCells):
for j in range(self.numOfCells):
board[j][i] = Rect(i*self.cellWidth+self.marginWidth, j*self.cellHeight+self.marginHeight, self.cellWidth-1, self.cellHeight-1)
return board
def cartesian2board(self, pos):
x = np.floor_divide(pos[1] - self.marginWidth, self.cellWidth)
if x >= self.numOfCells:
x = self.numOfCells - 1
elif x <= 0:
x = 0
y = np.floor_divide(pos[0] - self.marginHeight, self.cellHeight)
if y >= self.numOfCells:
y = self.numOfCells - 1
elif y <= 0:
y = 0
return x, y
def draw_lines(self):
for i in range(self.numOfCells):
start = Rect(self.board[i][0]).center
stop = Rect(self.board[i][self.numOfCells-1]).center
pygame.draw.line(self.background, self.linesColor, start, stop, 1)
for j in range(self.numOfCells):
start = Rect(self.board[0][j]).center
stop = Rect(self.board[self.numOfCells-1][j]).center
pygame.draw.line(self.background, self.linesColor, start, stop, 1)
def draw_thrones(self):
resolution = (60, 60)
blueThrone = FunContainer.load_image("blue-throne.jpg", -1)
redThrone = FunContainer.load_image("red-throne.jpg", -1)
blueThrone = pygame.transform.scale(blueThrone, resolution)
redThrone = pygame.transform.scale(redThrone, resolution)
FunContainer.center_blit(self.background, blueThrone, Rect(self.board[self.gameModel.player1ThronePos]))
FunContainer.center_blit(self.background, redThrone, Rect(self.board[self.gameModel.player2ThronePos]))
def draw_walls(self):
resolution = (42, 42)
wallImage = FunContainer.load_image("wall.jpg")
wallImage = pygame.transform.scale(wallImage, resolution)
for i in range(self.numOfCells):
for j in range(self.numOfCells):
if self.gameModel.wallsMap[i][j]:
FunContainer.center_blit(self.background, wallImage, Rect(self.board[(i, j)]))
def view_update(self):
self.screen.blit(self.background, self.gauntlet.rect, self.gauntlet.rect)
self.blackBalls.clear(self.screen, self.background)
self.blackBalls.draw(self.screen)
self.whiteBalls.clear(self.screen, self.background)
self.whiteBalls.draw(self.screen)
self.gauntlet.update()
self.screen.blit(self.gauntlet.image, self.gauntlet.rect)
pygame.display.update()