-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbattleships_gui.py
309 lines (265 loc) · 11.5 KB
/
battleships_gui.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import turtle
import const
class BattleshipsGraphics:
"""
Display instance.
"""
def __init__(self, gridSize):
self.turtle = turtle.Turtle()
self.screen = self.turtle.getscreen()
self.screen.setup(width=1200, height=750, startx=None, starty=None)
self.screen.register_shape(const.MISSED_GIF)
self.screen.register_shape(const.HIT_GIF)
self.screen.register_shape(const.WINNER_GIF)
self.screen.register_shape(const.LOSER_GIF)
self.screen.bgpic(const.BOARD_BKG_GIF)
self.turtle.speed(0)
self.turtle.color("black")
self.squareSize = 40
self.gridSize = gridSize
self.turtle.tracer(1, 0)
self.turtle.goto(0, 0)
self.turtle.showturtle()
def setScreenSize(self, width, height):
self.screen.setup(width=width, height=height,
startx=None, starty=None)
def setSquareSize(self, squareSize):
self.squareSize = squareSize
# Draw a line from (x1, y1) to (x2, y2)
def _drawLine(self, x1, y1, x2, y2):
self.turtle.penup()
self.turtle.goto(x1, y1)
self.turtle.pendown()
self.turtle.goto(x2, y2)
self.turtle.penup()
# Write a text at the specified location (x, y)
def _writeText(self, s, x, y, font=("Arial", 16, "bold")):
self.turtle.penup() # Pull the pen up
self.turtle.goto(x, y)
self.turtle.pendown() # Pull the pen down
self.turtle.write(s, align='center', font=font) # Write a string
self.turtle.penup()
# Draw a point at the specified location (x, y)
def _drawPoint(self, x, y):
self.turtle.penup() # Pull the pen up
self.turtle.goto(x, y)
self.turtle.pendown() # Pull the pen down
self.turtle.begin_fill() # Begin to fill color in a shape
self.turtle.circle(3)
self.turtle.end_fill() # Fill the shape
self.turtle.penup()
# Draw a circle at centered at (x, y) with the specified radius
def _fillCircle(self, x, y, radius):
self.turtle.penup() # Pull the pen up
self.turtle.fill(True)
self.turtle.goto(x, y - radius)
self.turtle.pendown() # Pull the pen down
self.turtle.circle(radius)
self.turtle.fill(False)
self.turtle.penup()
# Draw a rectangle at (x, y) with the specified width and height
def _fillRectangle(self, x, y, width, height):
self.turtle.penup() # Pull the pen up
self.turtle.fill(True)
self.turtle.setheading(0)
self.turtle.goto(x, y)
self.turtle.pendown() # Pull the pen down
self.turtle.forward(width)
self.turtle.right(90)
self.turtle.forward(height)
self.turtle.right(90)
self.turtle.forward(width)
self.turtle.right(90)
self.turtle.forward(height)
self.turtle.penup() # Pull the pen up
self.turtle.fill(False)
self.turtle.setheading(0)
self.turtle.penup()
def drawBoat(self, board, row, col):
width = self.screen.window_width()
height = self.screen.window_height()
marginh = (height - self.squareSize * self.gridSize)/2
marginw = (width - 2 * self.squareSize * self.gridSize)/4
middleh = -marginh / 3 # Vertical offset
top = self.squareSize * self.gridSize / 2 + middleh
if board == 'left':
left = -self.gridSize * self.squareSize - marginw
else:
left = marginw
self.turtle.color("black", "darkgrey")
self._fillRectangle(left + col * self.squareSize,
top - row * self.squareSize,
self.squareSize, self.squareSize)
self.turtle.penup()
def drawMiss(self, board, row, col):
width = self.screen.window_width()
height = self.screen.window_height()
marginh = (height - self.squareSize * self.gridSize) / 2
marginw = (width - 2 * self.squareSize * self.gridSize) / 4
middleh = -marginh / 3 # Vertical offset
top = self.squareSize * self.gridSize / 2 + middleh
if board == 'left':
left = -self.gridSize * self.squareSize - marginw
else:
left = marginw
self.turtle.color("black", "blue")
self.turtle.shape(const.MISSED_GIF)
self.turtle.penup()
self.turtle.goto(left + col * self.squareSize + self.squareSize / 2,
top - row * self.squareSize - self.squareSize / 2)
self.turtle.pendown()
self.turtle.stamp()
self.turtle.shape("blank")
self.turtle.penup()
def drawHit(self, board, row, col):
width = self.screen.window_width()
height = self.screen.window_height()
marginh = (height - self.squareSize * self.gridSize) / 2
marginw = (width - 2 * self.squareSize * self.gridSize) / 4
middleh = -marginh / 3 # Vertical offset
top = self.squareSize * self.gridSize / 2 + middleh
if board == 'left':
left = -self.gridSize * self.squareSize - marginw
else:
left = marginw
self.turtle.color("black", "red")
self.turtle.shape(const.HIT_GIF)
self.turtle.penup()
self.turtle.goto(left + col * self.squareSize + self.squareSize / 2,
top - row * self.squareSize - self.squareSize / 2)
self.turtle.pendown()
self.turtle.stamp()
self.turtle.shape("blank")
self.turtle.penup()
def drawPlayer(self, name, description, board):
width = self.screen.window_width()
height = self.screen.window_height()
marginw = (width - 2 * self.squareSize * self.gridSize) / 4
marginh = (height - self.squareSize * self.gridSize) / 2
middleh = -marginh / 3 # Vertical offset
bottom = -self.squareSize * self.gridSize / 2 + middleh
if board == 'left':
center = -self.gridSize * self.squareSize / 2 - marginw
else:
center = self.gridSize * self.squareSize / 2 + marginw
self.turtle.color("green", "green")
self._writeText(name, center, bottom - 30)
self._writeText(description, center, bottom - 80,
font=("Arial", 12, "normal"))
self.turtle.penup()
def drawWinner(self, board):
width = self.screen.window_width()
if board == 'left':
win = -width / 4
lost = width / 4
else:
win = width / 4
lost = -width / 4
self.turtle.shape(const.WINNER_GIF)
self.turtle.penup()
self.turtle.goto(win, 0)
self.turtle.pendown()
self.turtle.stamp()
self.turtle.shape(const.LOSER_GIF)
self.turtle.penup()
self.turtle.goto(lost, 0)
self.turtle.pendown()
self.turtle.stamp()
self.turtle.shape("blank")
self.turtle.penup()
def drawScore(self, leftPlayerScore, rightPlayerScore):
width = self.screen.window_width()
marginw = (width - 2 * self.squareSize * self.gridSize) / 4
self.turtle.color("green", "black")
self._fillRectangle(-marginw / 2, 30, marginw, 40)
self._writeText((str(leftPlayerScore) + " - " + str(rightPlayerScore)),
0, 0)
self.turtle.penup()
def drawBoards(self):
width = self.screen.window_width()
height = self.screen.window_height()
squareSize = self.squareSize
gridSize = self.gridSize
marginh = (height - self.squareSize * self.gridSize)/2
marginw = (width - 2 * self.squareSize * self.gridSize)/4
middleh = -marginh / 3 # Vertical offset
bottom = -self.squareSize * self.gridSize / 2 + middleh
top = self.squareSize * self.gridSize / 2 + middleh
self.turtle.color("cyan", "black")
self.turtle.shape("blank")
self.turtle.pensize(3)
rowLabels = ['A', 'B', 'C', 'D', 'E', 'F',
'G', 'H', 'I', 'J', 'K', 'L']
colLabels = ['1', '2', '3', '4', '5', '6',
'7', '8', '9', '10', '11', '12']
# Drawing left board
leftL = -gridSize * squareSize - marginw
leftR = -marginw
leftM = -squareSize * gridSize / 2 - marginw
for row in range(0, 7):
self._drawLine(leftL, top - row * squareSize,
leftM, top - row * squareSize)
for row in range(6, 13):
self._drawLine(leftL, top - row * squareSize,
leftR, top - row * squareSize)
for col in range(0, 7):
self._drawLine(leftL + col * squareSize, top,
leftL + col * squareSize, bottom)
for col in range(6, 13):
self._drawLine(leftL + col * squareSize, middleh,
leftL + col * squareSize, bottom)
for row in range(1, 7):
self._writeText(rowLabels[row - 1], leftL - squareSize / 2,
top - row * squareSize + squareSize / 5,
font=("Arial", 12, "normal"))
for row in range(7, 13):
self._writeText(rowLabels[row - 1], leftL - squareSize / 2,
top - row * squareSize + squareSize / 5,
font=("Arial", 12, "normal"))
for col in range(1, 7):
self._writeText(colLabels[col - 1],
leftL + col * squareSize - squareSize / 2,
top + squareSize / 3,
font=("Arial", 12, "normal"))
for col in range(7, 13):
self._writeText(colLabels[col - 1],
leftL + col * squareSize - squareSize / 2,
middleh + squareSize / 3,
font=("Arial", 12, "normal"))
# Drawing right board
rightL = marginw
rightR = gridSize * squareSize + marginw
rightM = squareSize * gridSize / 2 + marginw
for row in range(0, 7):
self._drawLine(rightL, top - row * squareSize,
rightM, top - row * squareSize)
for row in range(6, 13):
self._drawLine(rightL, top - row * squareSize,
rightR, top - row * squareSize)
for col in range(0, 7):
self._drawLine(rightL + col * squareSize, top,
rightL + col * squareSize, bottom)
for col in range(6, 13):
self._drawLine(rightL + col * squareSize, middleh,
rightL + col * squareSize, bottom)
for row in range(1, 7):
self._writeText(rowLabels[row - 1],
rightL - squareSize / 2,
top - row * squareSize + squareSize / 3,
font=("Arial", 12, "normal"))
for row in range(7, 13):
self._writeText(rowLabels[row - 1],
rightL - squareSize / 2,
top - row * squareSize + squareSize / 5,
font=("Arial", 12, "normal"))
for col in range(1, 7):
self._writeText(colLabels[col - 1],
rightL + col * squareSize - squareSize / 2,
top + squareSize / 3,
font=("Arial", 12, "normal"))
for col in range(7, 13):
self._writeText(colLabels[col - 1],
rightL + col * squareSize - squareSize / 2,
middleh + squareSize / 3,
font=("Arial", 12, "normal"))
self.turtle.penup()