-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameViewer.py
188 lines (164 loc) · 6.61 KB
/
GameViewer.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
import pygame, sys, tile,time,os
WHITE=(255,255,255)
BLACK=(0,0,0)
YELLOW=(255,255,0)
RED=(255,0,0)
GREEN=(0,255,0)
GREY=(128,128,128)
class GameViewer:
def __init__(self,rows,cols,size, controller, tileMatrix):
self.rows=rows
self.cols=cols
self.size=size
self.screen = None
self.controller = controller
self.tileMatrix = tileMatrix
workingDir = os.path.dirname(__file__)
self.fontFile = os.path.join(workingDir, 'INVASION2000.ttf')
def setup(self):
pygame.init()
#print(pygame.font.get_fonts())
self.screen = pygame.display.set_mode((740,self.size))
self.font = pygame.font.Font(self.fontFile, 32)
#self.draw(self.tileMatrix,0,0)
def run(self):
pos=[]
done = False
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running=False
pygame.quit
sys.exit()
if pygame.mouse.get_pressed()[0]:
pos=pygame.mouse.get_pos()
if pos[0]>self.size:
pass
else:
done = True
break
if done:
break
return self.get_clicked_tile(pos)
def draw_grid(self):
height=self.size//self.rows
width=self.size//self.cols
for i in range(self.rows):
pygame.draw.line(self.screen,(BLACK),(0,i*height),(self.size,i*height))
for j in range(self.cols):
pygame.draw.line(self.screen,(BLACK),(j*width,0),(j*width,self.size))
def draw_tiles(self):
self.screen.fill(GREY)
height=self.size//self.rows
width=self.size//self.cols
for i in range(self.rows):
for j in range(self.cols):
if self.tileMatrix[i][j] == "W":
pygame.draw.rect(self.screen,WHITE,((j*width),(i*height),width,height))
elif self.tileMatrix[i][j] == "B":
pygame.draw.rect(self.screen,BLACK,((j*width),(i*height),width,height))
else:
pygame.draw.rect(self.screen,GREEN,((j*width),(i*height),width,height))
#pygame.draw.rect(self.screen,GREY,((8*width),(i*height),width,height))
def draw_text(self,whitePlayerName, blackPlayerName,whitePlayerPoints,blackPlayerPoints):
text = self.font.render(blackPlayerName, True, BLACK)
textRect = text.get_rect()
textRect.center = (675, 20)
self.screen.blit(text, textRect)
text = self.font.render(str(blackPlayerPoints), True, BLACK)
textRect = text.get_rect()
textRect.center = (675,60)
self.screen.blit(text, textRect)
text = self.font.render(whitePlayerName, True, WHITE)
textRect = text.get_rect()
textRect.center = (675,200)
self.screen.blit(text, textRect)
text = self.font.render(str(whitePlayerPoints), True, WHITE)
textRect = text.get_rect()
textRect.center = (675,260)
self.screen.blit(text, textRect)
def get_clicked_tile(self,pos):
height= height=self.size//self.rows
width=self.size//self.cols
x,y= pos
row=y//height
col=x//width
return [row,col]
def draw(self, tileMatrix,whitePlayerName,blackPlayerName, whitePlayerPoints,blackPlayerPoints):
self.tileMatrix = tileMatrix
self.draw_tiles()
self.draw_grid()
self.draw_text(whitePlayerName,blackPlayerName,whitePlayerPoints,blackPlayerPoints)
pygame.display.update()
def showPossibleMoves(self, posMoves):
height=self.size//self.rows
width=self.size//self.cols
for move in posMoves:
points=str(move.getPoints())
text = self.font.render(points, True, BLACK)
textRect = text.get_rect()
textRect.center = ((width*move.getPos()[1])+(width/2), (height*move.getPos()[0])+(height/2))
self.screen.blit(text, textRect)
pygame.display.update()
def drawGameOver(self,winner):
font = pygame.font.Font(self.fontFile,80)
if not winner:
text =font.render("IT´S A TIE", True, RED)
else:
text =font.render(winner+" WON!!!", True, RED)
textRect = text.get_rect()
textRect.center = (300, 250)
self.screen.blit(text, textRect)
font = pygame.font.Font(self.fontFile,30)
text =font.render("(click anywhere to start over)", True, RED)
textRect = text.get_rect()
textRect.center = (300, 320)
self.screen.blit(text, textRect)
pygame.display.update()
self.waitForInput()
self.controller.NewGame()
def startScreen(self):
pygame.draw.rect(self.screen,BLACK, (0,0, 380,600))
pygame.draw.rect(self.screen,WHITE, (380,0, 380,600))
font = pygame.font.Font(self.fontFile,60)
text = font.render("WELCOME TO", True, RED)
textRect = text.get_rect()
textRect.center = (380, 250)
self.screen.blit(text, textRect)
text = font.render("OTHELLO", True, RED)
textRect = text.get_rect()
textRect.center = (380, 320)
self.screen.blit(text, textRect)
pygame.display.update()
pos=self.waitForInput()
pygame.draw.rect(self.screen,BLACK, (0,0, 380,600))
pygame.draw.rect(self.screen,WHITE, (380,0, 380,600))
font = pygame.font.Font(self.fontFile,60)
text = font.render("Pick a Color", True, RED)
textRect = text.get_rect()
textRect.center = (380, 250)
self.screen.blit(text, textRect)
font = pygame.font.Font(self.fontFile, 30)
text = font.render("(Black goes first )", True, RED)
textRect = text.get_rect()
textRect.center = (380, 320)
self.screen.blit(text, textRect)
pygame.display.update()
pos=self.waitForInput()
if pos[0]<380:
return "B"
else:
return "W"
def waitForInput(self):
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running=False
pygame.quit
sys.exit()
if pygame.mouse.get_pressed()[0]:
pos=pygame.mouse.get_pos()
if pos[0]>self.size:
pass
else:
return pos