Skip to content

Instantly share code, notes, and snippets.

@sanchitgangwar
Created March 29, 2012 06:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sanchitgangwar/2234330 to your computer and use it in GitHub Desktop.
Save sanchitgangwar/2234330 to your computer and use it in GitHub Desktop.
Bomb Defusing Robot Advanced
import curses
from curses import KEY_RIGHT, KEY_LEFT, KEY_UP, KEY_DOWN
import random
from random import randrange, randint
class Box:
pass
def Game(pos_x, pos_y, size):
B = Box()
B.pos_x = pos_x
B.pos_y = pos_y
B.size = size
B.score = 0
def printRobot(win):
for i in range(B.size):
for j in range(B.size):
win.addch(B.pos_y + i, B.pos_x + j, 'X')
def printDefuses(win, defuses):
for i in defuses:
win.addch(i[0], i[1], '#')
def printBomb(win, bombPos):
win.addch(bombPos[0], bombPos[1], '*')
def checkDefuse(defuses):
for d in defuses:
if d[0] < B.pos_y + B.size and d[0] >= B.pos_y and d[1] < B.pos_x + B.size and d[1] >= B.pos_x:
defuses.remove(d)
B.score += 1
return defuses
def checkBomb(bombPos):
if bombPos[0] < B.pos_y + B.size and bombPos[0] >= B.pos_y and bombPos[1] < B.pos_x + B.size and bombPos[1] >= B.pos_x:
if B.score == 5:
endGame(0)
else:
endGame(1)
def checkBoundary(height, width, start_x, start_y):
if B.pos_x <= start_x or B.pos_y <= start_y or B.pos_x + B.size >= start_x + width or B.pos_y + B.size >= start_y + height:
endGame(1)
def pauseGame(win):
key = -1
while key != ord('p'):
try:
key = win.getch()
except KeyboardInterrupt:
curses.endwin()
exit(1)
def incrementX():
B.pos_x += 1
def decrementX():
B.pos_x -= 1
def incrementY():
B.pos_y += 1
def decrementY():
B.pos_y -= 1
def endGame(status):
curses.endwin()
print("\nScore - " + str(B.score))
if status == 0:
print("Congratulations!! You won the game!!")
else:
print("You lost the game!!")
print("\nThanks for Playing! (http://bitemelater.in).\n")
exit(0)
A = Box()
A.printRobot = printRobot
A.printDefuses = printDefuses
A.printBomb = printBomb
A.checkDefuse = checkDefuse
A.checkBomb = checkBomb
A.incrementX = incrementX
A.incrementY = incrementY
A.decrementX = decrementX
A.decrementY = decrementY
A.pauseGame = pauseGame
A.getposX = lambda : B.pos_x
A.getposY = lambda : B.pos_y
A.getSize = lambda : B.size
A.getScore = lambda : B.score
A.checkBoundary = checkBoundary
A.endGame = endGame
return A
print("\nNOTE: You might not be able to play the game if you enter weird inputs.")
print(" Recommended height, width and robot size are 15, 50 and 3 respectively.\n")
print(" If you skip a value, recommended value will be used for that parameter.\n")
try:
h = input("Enter height (<50): ")
except:
h = 15
try:
w = input("Enter width (<50): ")
except:
w = 50
height = 15 if h <= 5 else h
width = 50 if w <= 5 else w
try:
s = input("Enter size of robot (<5): ")
except:
s = 3
size = 1 if s < 1 else s
curses.initscr()
curses.curs_set(0)
win = curses.newwin(height, width, 0, 0)
win.keypad(1)
win.nodelay(1)
win.border(0)
curses.noecho()
key = KEY_RIGHT
defaultKey = KEY_RIGHT
score = 0
pos_x = 2
pos_y = 2
G = Game(pos_x, pos_y, size)
defuses = []
nBombs = 5
while len(defuses) < nBombs + 1:
defuses.extend([n for n in [[randint(1, height-2), randint(1, width-2)] for x in range(10)] if (n[0] < pos_y or n[0] > pos_y + size) and (n[1] < pos_x or n[1] > pos_x + size)])
for n in defuses:
if defuses.count(n) > 1:
while defuses.count(n) > 1:
defuses.remove(n)
bombPos = defuses[len(defuses) - 1]
defuses = defuses[:nBombs]
while key != 27:
win.clear()
win.border(0)
pos_x = G.getposX()
pos_y = G.getposY()
size = G.getSize()
b = G.checkBoundary(height, width, 0, 0)
if b == 1:
break
G.printRobot(win)
prev = len(defuses)
defuses = G.checkDefuse(defuses)
G.checkBomb(bombPos)
G.printDefuses(win, defuses)
G.printBomb(win, bombPos)
win.addstr(0, 2, 'Score: ' + str(G.getScore()))
try:
win.timeout(150)
key = win.getch()
except KeyboardInterrupt:
break
if key == -1:
key = defaultKey
if key == KEY_RIGHT:
G.incrementX()
defaultKey = key
elif key == KEY_LEFT:
G.decrementX()
defaultKey = key
elif key == KEY_DOWN:
G.incrementY()
defaultKey = key
elif key == KEY_UP:
G.decrementY()
defaultKey = key
elif key == ord('p'):
G.pauseGame(win)
curses.endwin()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment