forked from codehouseindia/Python-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOcean_game.py
More file actions
67 lines (56 loc) · 1.96 KB
/
Ocean_game.py
File metadata and controls
67 lines (56 loc) · 1.96 KB
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
'''
Simple python game:Ocean treasures
Simple game in which you have to find treasure chests lost in the
bottom of the ocean. The ocean is represented by a grid of 60 columns
by 15 rows and the chests can be in any one cell. To find a chest you
need to drop sonar devices at given locations.
To play, click buttons to drop sonar device.
The sonar reports the distance to the treasure.
'''
import math
import random
import tkinter
def odd(n):
return n & 1
def color(a):
return 'green' if odd(a) else 'blue'
class Map:
def __init__(self, master, rows = 15, columns = 60):
self.master = master
self.row = random.randrange(rows)
self.col = random.randrange(columns)
self.cost = 0
self.found = False
Button = tkinter.Button
self.buttons = []
options = dict(text = '??', font = 'Courier 14')
for r in range(rows):
br = [] # row of buttons
self.buttons.append(br)
for c in range(columns):
b = Button(
master,
command = lambda r=r, c=c: self(r, c),
fg = color(r+c),
**options
)
b.grid(row = r, column = c)
br.append(b)
master.mainloop()
def __bool__(self):
return self.found
def __call__(self, row, col):
if self:
self.master.quit()
distance = int(round(math.hypot(row-self.row, col-self.col)))
self.buttons[row][col].configure(text = '{}'.format(distance), bg = 'yellow', fg = 'red')
self.cost += 1
if not distance:
print('You win! At the cost of {} sonar devices.'.format(self.cost))
self.found = True
def main():
root = tkinter.Tk()
map = Map(root)
root.destroy()
if __name__ == '__main__':
main()