def test_fireballexit(self): starttime = time.time() P = Player("Hello",1,ROWS - 2, COLS, 1, 0) newb = Board() mainarr = [] while True: pathexists,numofcoins,testofthree = newb.init(COLS,ROWS,mainarr,P) if pathexists == True: break numofdonkeys,numofball,upint = levelVar(P) numofball = 1 #One ball is enough for testing dindexarr = [0,0,0] dlenarr = [0,0,0] #global dobjectarr dobjectarr = [0,0,0] #global fbarr fbarr = [0,0,0,0,0,0,0,0] #global currball currball = 0 #global counter counter = 0 dindexarr,dlenarr,counter = createDonkeys(dindexarr,dlenarr,counter,numofdonkeys,newb,mainarr,COLS,dobjectarr,upint) ball1time = time.time() tempcount = 0 P.changeposn(ROWS-2,7) mainarr[ROWS-2][7] = 4 running = True while running: #Main while loop for i in dobjectarr: if i != 0: i.donkeyWalk(mainarr) ball2time = time.time() dobjectarr,ball1time,currball,fbarr = createBalls(ball1time, ball2time, currball, numofball, dobjectarr, COLS, fbarr,upint, mainarr) for i in fbarr: if i != 0: x,y = i.getPosition() if y == 1 and x == ROWS-2: updateBalls(mainarr,fbarr,ROWS,COLS,dobjectarr) assert mainarr[ROWS-2][1] != 6 running = False mainarr,fbarr = updateBalls(mainarr,fbarr,ROWS,COLS,dobjectarr)
def test_directionchange(self): starttime = time.time() P = Player("Hello",1,ROWS - 2, COLS, 1, 0) newb = Board() mainarr = [] while True: pathexists,numofcoins,testofthree = newb.init(COLS,ROWS,mainarr,P) if pathexists == True: break numofdonkeys,numofball,upint = levelVar(P) numofball = 1 dindexarr = [0,0,0] dlenarr = [0,0,0] #global dobjectarr dobjectarr = [0,0,0] #global fbarr fbarr = [0,0,0,0,0,0,0,0] #global currball currball = 0 #global counter counter = 0 dindexarr,dlenarr,counter = createDonkeys(dindexarr,dlenarr,counter,numofdonkeys,newb,mainarr,COLS,dobjectarr,upint) ball1time = time.time() P.changeposn(ROWS-2,7) mainarr[ROWS-2][7] = 4 running = True while running: #Main while loop for i in dobjectarr: if i != 0: i.donkeyWalk(mainarr) ball2time = time.time() dobjectarr,ball1time,currball,fbarr = createBalls(ball1time, ball2time, currball, numofball, dobjectarr, COLS, fbarr,upint, mainarr) tempcount = 0 for i in fbarr: if i != 0: olddirection = i.getDirection() if i.getY()+1 == COLS-1 or i.getY() - 1 == 0: mainarr,fbarr = updateBalls(mainarr,fbarr,ROWS,COLS,dobjectarr) assert i.getDirection() != olddirection running = False mainarr,fbarr = updateBalls(mainarr,fbarr,ROWS,COLS,dobjectarr)
def test_playerspawndetails(self): P = Player("Hello",1,ROWS - 2, COLS, 1, 0) newb = Board() array = [] pathexists = False testofthree = False numofcoins = 0 while True: pathexists,numofcoins,testofthree = newb.init(COLS,ROWS,array,P) if pathexists == True: break assert ( P.getScore() == 0 and P.getLife() == 3 and P.getLevel() == 1 ) #Tests if player score,lives and level is initialized correctly
def test_playerspawnposn(self): P = Player("Hello",1,ROWS - 2, COLS, 1, 0) newb = Board() array = [] pathexists = False testofthree = False numofcoins = 0 while True: pathexists,numofcoins,testofthree = newb.init(COLS,ROWS,array,P) if pathexists == True: break assert ( P.getY() == 1 and P.getX() == (ROWS - 2) ) #Tests if player spawn position is correct
def test_numoffireballs(self): starttime = time.time() P = Player("Hello",1,ROWS - 2, COLS, 2, 0) newb = Board() mainarr = [] while True: pathexists,numofcoins,testofthree = newb.init(COLS,ROWS,mainarr,P) if pathexists == True: break numofdonkeys,numofball,upint = levelVar(P) dindexarr = [0,0,0] dlenarr = [0,0,0] #global dobjectarr dobjectarr = [0,0,0] #global fbarr fbarr = [0,0,0,0,0,0,0,0] #global currball currball = 0 #global counter counter = 0 dindexarr,dlenarr,counter = createDonkeys(dindexarr,dlenarr,counter,numofdonkeys,newb,mainarr,COLS,dobjectarr,upint) ball1time = time.time() P.changeposn(ROWS-2,7) mainarr[ROWS-2][7] = 4 running = True while running: #Main while loop for i in dobjectarr: if i != 0: i.donkeyWalk(mainarr) ball2time = time.time() dobjectarr,ball1time,currball,fbarr = createBalls(ball1time, ball2time, currball, numofball, dobjectarr, COLS, fbarr,upint, mainarr) tempcount = 0 for i in fbarr: if i != 0: tempcount += 1 if tempcount == numofball: running = False assert tempcount == numofball
def test_princessrescue(self): P = Player("Hello",1,ROWS - 2, COLS, 1, 0) newb = Board() array = [] pathexists = False testofthree = False numofcoins = 0 while True: pathexists,numofcoins,testofthree = newb.init(COLS,ROWS,array,P) if pathexists == True: break oldscore = P.getScore() if array[1][newb.princess] == 1: temprow = 1 tempcol = newb.princess P.changeposn(1,newb.princess) array[1][newb.princess] = 4 if P.getX() == 1 and P.getY() == newb.princess: P.score += 50 if P.level == 3: os.system("clear") assert P.getScore() == oldscore + 50 running = False
def test_donkeyandplayer(self): starttime = time.time() P = Player("Hello", 1, ROWS - 2, COLS, 1, 0) newb = Board() mainarr = [] while True: pathexists, numofcoins, testofthree = newb.init(COLS, ROWS, mainarr, P) if pathexists == True: break numofdonkeys, numofball, upint = levelVar(P) numofball = 1 # One ball is enough for testing dindexarr = [0, 0, 0] dlenarr = [0, 0, 0] # global dobjectarr dobjectarr = [0, 0, 0] # global fbarr fbarr = [0, 0, 0, 0, 0, 0, 0, 0] # global currball currball = 0 # global counter counter = 0 dindexarr, dlenarr, counter = createDonkeys( dindexarr, dlenarr, counter, numofdonkeys, newb, mainarr, COLS, dobjectarr, upint ) ball1time = time.time() tempcount = 0 running = True while running: # Main while loop for i in dobjectarr: if i != 0: i.donkeyWalk(mainarr) ball2time = time.time() dobjectarr, ball1time, currball, fbarr = createBalls( ball1time, ball2time, currball, numofball, dobjectarr, COLS, fbarr, upint, mainarr ) note = -1 tempindex = 0 for i in dobjectarr: if i != 0: if i.getdir() == 1: P.changeposn(i.getX(), i.getY() + 1) mainarr[i.getX()][i.getY() + 1] = 4 else: P.changeposn(i.getX(), i.getY() - 1) mainarr[i.getX()][i.getY() - 1] = 4 assert i.checkCollision(mainarr) == True if i.checkCollision(mainarr) == True: i.killDonkey(mainarr, P) note = 1 running = False break else: i.donkeyWalk(mainarr) tempindex += 1 if note != -1: dobjectarr[tempindex] = 0 P.score += 25 note = -1
def test_playerleftwallcheck(self): P = Player("Hello",1,ROWS - 2, COLS, 1, 0) newb = Board() array = [] pathexists = False testofthree = False numofcoins = 0 while True: pathexists,numofcoins,testofthree = newb.init(COLS,ROWS,array,P) if pathexists == True: break P.changeposn(P.getX(),1) array[P.getX()][P.getY()] = 4 if array[P.getX()][P.getY()-1] == -1: P.changeposn(P.getX(),P.getY()) array[P.getX()][P.getY()] = 4 else: P.changeposn(P.getX(),P.getY()-1) array[P.getX()][P.getY()-1] = 4 assert P.getY() != (0) P.changeposn(P.getX(),COLS-7) array[P.getX()][P.getY()] = 4 if array[P.getX()][P.getY()-1] == -1: P.changeposn(P.getX(),P.getY()) array[P.getX()][P.getY()] = 4 else: P.changeposn(P.getX(),P.getY()-1) array[P.getX()][P.getY()-1] = 4 assert P.getY() != (0)
def test_playercollectcoin(self): P = Player("Hello",1,ROWS - 2, COLS, 1, 0) newb = Board() array = [] pathexists = False testofthree = False numofcoins = 0 while True: pathexists,numofcoins,testofthree = newb.init(COLS,ROWS,array,P) if pathexists == True: break oldscore = P.getScore() temprow = ROWS-2 tempcol = 1 while True: if array[temprow][tempcol+1] == 3: break elif tempcol == COLS-4: temprow += 4 tempcol = P.getY() else: tempcol += 1 P.changeposn(temprow,tempcol) array[temprow][tempcol] = 4 if array[P.getX()][P.getY() + 1] == 3: P.changeposn(temprow,tempcol) array[temprow][tempcol] = 4 P.collectCoin() assert P.getScore() == oldscore + 5 running = False
def mainfunc(level, pname, score): # Tertiary main function. Contains the main while loop # Create hookmanager hookman = pyxhook.HookManager() # Define our callback to fire when a key is pressed down hookman.KeyDown = kbevent # Hook the keyboard hookman.HookKeyboard() # Start our listener hookman.start() global ball1time, ball2time ball1time = time.time() # These ensure that the balls are produced after a time gap and not all together global P P = Player(pname, ROWS - 2, 1, COLUMNS, level, score) # Instantiating Player # Create new layout global newboard, mainarr while True: mainarr = [] newboard = Board() if ( newboard.init(COLUMNS, ROWS, mainarr, P) == True ): # Creating board and ensuring that there exists a path from player spawn position to the princess break global numofdonkeys, numofball, upint numofdonkeys, numofball, upint = levelVar(P) dindexarr = [0, 0, 0] dlenarr = [0, 0, 0] global dobjectarr dobjectarr = [0, 0, 0] global fbarr fbarr = [0, 0, 0, 0, 0, 0, 0, 0] global currball currball = 0 global counter counter = 0 dindexarr, dlenarr, counter = createDonkeys( dindexarr, dlenarr, counter, numofdonkeys, newboard, mainarr, COLUMNS ) # Create donkeys oldtime = time.time() # To judge jump direction global keyarr global running running = True while running: # Main while loop os.system("clear") newtime = time.time() # To judge jump direction when the spacebar is pressed if (newtime - oldtime) > 1: keyarr.pop(0) keyarr.append(115) oldtime = newtime # Choose what to do depending on current variable values if P.life == 0: os.system("clear") hookman.cancel() return (-1), P.score if P.getX() == 1 and P.getY() == newboard.princess: P.score += 50 if P.level == 3: os.system("clear") func(3, P.score) hookman.cancel() return (-1), P.score hookman.cancel() return P.level + 1, P.score if mainarr[P.getX()][P.getY() + 1] == 6 or mainarr[P.getX()][P.getY() - 1] == 6: mainarr[P.getX()][P.getY()] = 0 P.life -= 1 P.changeposn(ROWS - 2, 1) mainarr[P.getX()][P.getY()] = 4 P.score -= 25 continue tempr, tempcol = P.getPosition() if mainarr[tempr + 1][tempcol] != -1 and mainarr[tempr + 1][tempcol] != 2 and P.updown != 1: mainarr[tempr][tempcol] = 0 mainarr[tempr + 1][tempcol] = 4 P.changeposn(tempr + 1, tempcol) global note note = -1 tempindex = 0 for i in dobjectarr: if i != 0: if i.checkCollision(mainarr) == True: i.killDonkey(mainarr, P) note = 1 break else: i.donkeyWalk(mainarr) tempindex += 1 if note != -1: dobjectarr[tempindex] = 0 P.score += 25 note = -1 # ball section ball2time = time.time() dobjectarr, ball1time, currball, fbarr = createBalls( ball1time, ball2time, currball, numofball, dobjectarr, COLUMNS, fbarr ) mainarr, fbarr = updateBalls(mainarr, fbarr, ROWS, COLUMNS, dobjectarr) printboard(COLUMNS, ROWS, mainarr, P) # Print the board time.sleep(0.12) # Close the listener when we are done hookman.cancel() sys.exit()
def mainfunc(level,pname,score): #Tertiary main function. Contains the main while loop #Create hookmanager hookman = pyxhook.HookManager() #Define our callback to fire when a key is pressed down hookman.KeyDown = kbevent #Hook the keyboard hookman.HookKeyboard() #Start our listener hookman.start() global ball1time,ball2time ball1time = time.time() #These ensure that the balls are produced after a time gap and not all together global P P = Player(pname, ROWS-2, 1,COLUMNS, level,score) #Instantiating Player #Create new layout global newboard,mainarr while True: mainarr = [] newboard = Board() if newboard.init(COLUMNS,ROWS,mainarr,P) == True: #Creating board and ensuring that there exists a path from player spawn position to the princess break global numofdonkeys,numofball,upint numofdonkeys,numofball,upint = levelVar(P) dindexarr = [0,0,0] dlenarr = [0,0,0] global dobjectarr dobjectarr = [0,0,0] global fbarr fbarr = [0,0,0,0,0,0,0,0] global currball currball = 0 global counter counter = 0 dindexarr,dlenarr,counter = createDonkeys(dindexarr,dlenarr,counter,numofdonkeys,newboard,mainarr,COLUMNS) #Create donkeys oldtime=time.time() #To judge jump direction global keyarr global running running = True while running: #Main while loop os.system("clear") newtime=time.time() #To judge jump direction when the spacebar is pressed if (newtime-oldtime) > 1: keyarr.pop(0) keyarr.append(115) oldtime=newtime #Choose what to do depending on current variable values if P.life == 0: os.system("clear") hookman.cancel() return (-1),P.score if P.getX() == 1 and P.getY() == newboard.princess: P.score += 50 if P.level == 3: os.system("clear") func(3, P.score) hookman.cancel() return (-1),P.score hookman.cancel() return P.level+1,P.score if mainarr[P.getX()][P.getY()+1] == 6 or mainarr[P.getX()][P.getY()-1] == 6: mainarr[P.getX()][P.getY()] = 0 P.life -= 1 P.changeposn(ROWS - 2, 1) mainarr[P.getX()][P.getY()] = 4 P.score -= 25 continue tempr,tempcol = P.getPosition() if mainarr[tempr+1][tempcol] != -1 and mainarr[tempr+1][tempcol] != 2 and P.updown != 1: mainarr[tempr][tempcol] = 0 mainarr[tempr+1][tempcol] = 4 P.changeposn(tempr+1,tempcol) global note note = -1 tempindex = 0 for i in dobjectarr: if i != 0: if i.checkCollision(mainarr) == True: i.killDonkey(mainarr,P) note = 1 break else: i.donkeyWalk(mainarr) tempindex += 1 if note != -1: dobjectarr[tempindex] = 0 P.score += 25 note = -1 #ball section ball2time = time.time() dobjectarr,ball1time,currball,fbarr = createBalls(ball1time, ball2time, currball, numofball, dobjectarr, COLUMNS, fbarr) mainarr,fbarr = updateBalls(mainarr,fbarr,ROWS,COLUMNS,dobjectarr) printboard(COLUMNS, ROWS, mainarr, P) #Print the board time.sleep(0.12) #Close the listener when we are done hookman.cancel() sys.exit()
def test_fireballandplayer(self): starttime = time.time() P = Player("Hello",1,ROWS - 2, COLS, 1, 0) newb = Board() mainarr = [] while True: pathexists,numofcoins,testofthree = newb.init(COLS,ROWS,mainarr,P) if pathexists == True: break numofdonkeys,numofball,upint = levelVar(P) numofball = 1 #One ball is enough for testing dindexarr = [0,0,0] dlenarr = [0,0,0] #global dobjectarr dobjectarr = [0,0,0] #global fbarr fbarr = [0,0,0,0,0,0,0,0] #global currball currball = 0 #global counter counter = 0 dindexarr,dlenarr,counter = createDonkeys(dindexarr,dlenarr,counter,numofdonkeys,newb,mainarr,COLS,dobjectarr,upint) ball1time = time.time() tempcount = 0 running = True while running: #Main while loop for i in dobjectarr: if i != 0: i.donkeyWalk(mainarr) ball2time = time.time() dobjectarr,ball1time,currball,fbarr = createBalls(ball1time, ball2time, currball, numofball, dobjectarr, COLS, fbarr,upint, mainarr) for i in fbarr: if i != 0: x,y = i.getPosition() if i.getDirection() == 1: P.changeposn(x,y+1) else: P.changeposn(x,y-1) break if mainarr[P.getX()][P.getY()+1] == 6 or mainarr[P.getX()][P.getY()-1] == 6: mainarr[P.getX()][P.getY()] = 0 P.life -= 1 P.changeposn(ROWS - 2, 1) mainarr[P.getX()][P.getY()] = 4 assert P.getX() == ROWS-2 and P.getY() == 1 P.score -= 25 running = False