-
Notifications
You must be signed in to change notification settings - Fork 1
/
single_player_complete.py
277 lines (219 loc) · 6.9 KB
/
single_player_complete.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
from microbit import *
import music
import random
PLAYER = 7
SHIP = 9
WATER = 2
THRESHOLD = 400
class Sea:
"""
Holds the state of the game board.
"""
def __init__(self, ships=[4, 3, 2]):
"""
Initialize the game board with the given ships.
"""
self.board = [[WATER] * 5 for _ in range(5)]
self.populate_board(ships)
def near_ships(self, row, col):
"""
Check if there are ships surrounding the given coordinates.
If the coordinates are out of bounds, return False.
"""
if row > 4 or col > 4:
return False
for i in range(row - 1, row + 2):
for j in range(col - 1, col + 2):
if 0 <= i < 5 and 0 <= j < 5 and (i != row or j != col):
if self.board[i][j] == SHIP:
return False
return True
def possible(self, row, col, size, orientation):
"""
Check if it is possible to place a ship.
The ship must not be near other ships.
"""
for i in range(size):
if orientation == "H":
if not self.near_ships(row, col + i):
return False
elif orientation == "V":
if not self.near_ships(row + i, col):
return False
return True
def place_ship(self, size):
"""
Place a ship of the given size in the board.
The ship will be placed in a random candidate coordinate.
The orientation can be horizontal or vertical.
"""
candidates_coordinates = [
(row, col)
for row in range(5)
for col in range(5)
if self.board[row][col] == WATER
]
while candidates_coordinates:
row, col = random.choice(candidates_coordinates)
candidates_coordinates.remove((row, col))
orientation = random.choice(["H", "V"])
if self.possible(row, col, size, orientation):
break
else:
return False
for i in range(size):
if orientation == "H":
self.board[row][col + i] = SHIP
elif orientation == "V":
self.board[row + i][col] = SHIP
return True
def populate_board(self, ships):
"""
Place all the ships on the board.
If it is not possible to place all the ships, try again.
"""
while True:
placed_ships = [self.place_ship(ship) for ship in ships]
if all(placed_ships):
break
self.board = [[WATER] * 5 for _ in range(5)]
def hit(self, row, col):
"""
Check if there is a ship in the given coordinates.
"""
return self.board[row][col] == SHIP
def show(self):
"""
Show the game board.
"""
display.show(
Image(
":".join(["".join(str(point) for point in line) for line in self.board])
)
)
class Player:
"""
Holds the state of the player board.
"""
def __init__(self):
"""
Initialize the player board.
"""
self.shots = [[0] * 5 for _ in range(5)]
self.row = 2
self.col = 2
def show(self):
"""
Show the player board.
"""
display.show(
Image(
":".join(["".join(str(point) for point in line) for line in self.shots])
)
)
def shoot(self):
"""
Shoot the board.
The player can move the cursor with the accelerometer.
The player can shoot with the A button.
The coordinates are stored for future reference.
"""
while not button_a.is_pressed():
self.show()
if accelerometer.get_x() > THRESHOLD:
self.col = min(self.col + 1, 4)
elif accelerometer.get_x() < -THRESHOLD:
self.col = max(self.col - 1, 0)
if accelerometer.get_y() > THRESHOLD:
self.row = min(self.row + 1, 4)
elif accelerometer.get_y() < -THRESHOLD:
self.row = max(self.row - 1, 0)
self.blink(self.row, self.col)
sleep(100)
def mark(self, row, col, hit):
"""
Mark the player board with the result of the shot.
"""
if hit:
self.shots[row][col] = SHIP
else:
self.shots[row][col] = WATER
def blink(self, row, col):
"""
Blink the given coordinates.
This is used to show the current coordinates of the player.
"""
display.set_pixel(col, row, self.shots[row][col])
sleep(50)
display.set_pixel(col, row, PLAYER)
sleep(50)
class Game:
"""
Holds the state of the game.
"""
def __init__(self):
"""
Initialize the game.
The game board is initialized with the ships.
The player board is initialized.
The player starts at the top-left corner.
"""
self.sea = Sea()
self.player = Player()
def start(self):
"""
Routine to start the game.
"""
display.show(Image.TARGET)
music.play(music.ENTERTAINER)
while not (button_a.is_pressed() and button_b.is_pressed()):
display.clear()
sleep(350)
display.show(Image.TARGET)
sleep(350)
for number in "321":
display.show(number)
music.play(music.BA_DING)
sleep(1000)
music.play(music.JUMP_UP)
display.clear()
def end(self):
"""
Routine to end the game.
"""
display.show(Image.HAPPY)
music.play(music.CHASE)
def win(self):
"""
Check if the player has won the game.
The player wins the game if they hit all the ships.
"""
for i in range(5):
for j in range(5):
if self.sea.board[i][j] == SHIP and self.player.shots[i][j] != SHIP:
return False
return True
def run(self):
"""
Run the logic of the game.
The player can shoot in the board.
If the player hits a ship, the player board is marked with a ship.
If the player misses, the player board is marked with water.
If the player hits all the ships, the game ends.
"""
self.start()
while True:
self.player.shoot()
hit = self.sea.hit(self.player.row, self.player.col)
self.player.mark(self.player.row, self.player.col, hit)
if hit:
display.show(Image.TARGET)
music.play(music.POWER_UP)
if self.win():
self.end()
break
else:
display.show(Image.NO)
music.play(music.POWER_DOWN)
game = Game()
game.run()