From 88f825ae5e8d6fefa19c64770e117d06db364e2e Mon Sep 17 00:00:00 2001 From: Tan-ishaa <67544074+Tan-ishaa@users.noreply.github.com> Date: Fri, 16 Oct 2020 17:43:56 +0530 Subject: [PATCH] Create Ocean_game.py This is an interesting ocean game coded in Python --- Ocean_game.py | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 Ocean_game.py diff --git a/Ocean_game.py b/Ocean_game.py new file mode 100644 index 0000000..2dbdc1e --- /dev/null +++ b/Ocean_game.py @@ -0,0 +1,66 @@ +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()