def move(self, dx, dy): #The move function is special in that it accepts the map and objectlist on which you are moving (along with the change in x and y). It follows the following basic structure: #1) Determine if anything exists at the destination, tile or object. #2) If something exists, perform the associated action (whether denying movement or attacking a monster) #3) If nothing exists, move to that tile and set the player coordinates to the target. tiletarget = None target = None newx = self.xcoordinate + dx newy = self.ycoordinate + dy mapincrement = 0 map = levelhandler.activelevel().map objectlist = levelhandler.activelevel().objectlist for row in map: for tile in row:#Check to see if the tile exists at new location, if so, assign the tile to tiletarget if tile.xcoordinate == newx and tile.ycoordinate == newy: tiletarget = tile break for object in objectlist: #Check to see if anything exists at target location, if so, assign the object to target if object.xcoordinate == newx and object.ycoordinate == newy: target = object break # print target.__class__.__name__ if tiletarget is None: #Does the tile exist? log.addEvent((1,"A monster is attempting to move outside the map!")) elif tiletarget.isPassable != 1: #Is the tile passable? log.addEvent((1,"A monster is trying to move into an impassable tile!")) elif target is not None: #If there is a monster, attack it if target.__class__.__name__=='Player': log.addEvent((1,"A monster attacks %s, dealing %s damage." % (target.name,self.damage))) target.current_health=target.current_health-self.damage #if target.current_health<=0: # self.battleWin(target) # objectlist.remove(target) #else: # log.addEvent((1,"%s attacks you back, dealing %s damage." % (target.name,target.damage))) # self.current_health=self.current_health-target.damage # if self.current_health<=0: # self.battleLose() #elif target.__class__.__name__=='NPC': # target.engage_dialogue(self) elif target is None: #Otherwise move #print("A monster moves") self.xcoordinate += dx self.ycoordinate += dy
def playersTurn(): #Pauses the game and allows the player to take a turn playersturn = True while playersturn == True: for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() elif event.type == KEYDOWN: if (event.key == K_o): levelhandler.move_floor(-1) levelhandler.activelevel().objectlist.append(player) redrawScreen() if (event.key == K_p): levelhandler.move_floor(1) levelhandler.activelevel().objectlist.append(player) redrawScreen() if (event.key == K_d): player.move(32,0) #Modify the game state when some action is entered. The move function takes target coordinates, the map[] list of a Dungeon object (see Dungeon.py), and the object list. playersturn = False if (event.key == K_a): player.move(-32,0) playersturn = False if (event.key == K_w): player.move(0,-32) playersturn = False if (event.key == K_s): player.move(0,32) playersturn = False if (event.key == K_h): print ('Some Bullshit') if (event.key == K_i): #inventory paused = 1 while (paused == 1): drawMap(boardWidth, boardHeight) drawObjects() boolean = False invmenu=player.inventorymenu invmenu.Update() invscreen=player.inventorysheet DISPLAYSURF.blit(invscreen, (invmenu.xloc,invmenu.yloc )) pygame.display.update() for event in pygame.event.get(): if event.type==MOUSEBUTTONDOWN and event.button == 1 : cursx,cursy=event.pos if cursx>invmenu.xloc and cursx<invmenu.xloc+200 and cursy >invmenu.yloc and cursy < invmenu.yloc+10: boolean=True while(boolean): for event in pygame.event.get(): if event.type==MOUSEBUTTONUP and event.button == 1 : boolean=False relx, rely = event.pos invmenu.xloc=invmenu.xloc+(relx-cursx) invmenu.yloc=invmenu.yloc+(rely-cursy) elif event.type==MOUSEBUTTONDOWN and event.button == 3: cursx,cursy=event.pos if(cursx>invmenu.xloc+10 and cursx<invmenu.xloc+42 and cursy>invmenu.yloc+20 and cursy<invmenu.yloc+52): player.equip_item(0) if(cursx>invmenu.xloc+60 and cursx<invmenu.xloc+92 and cursy>invmenu.yloc+20 and cursy<invmenu.yloc+52): player.equip_item(1) if(cursx>invmenu.xloc+110 and cursx<invmenu.xloc+142 and cursy>invmenu.yloc+20 and cursy<invmenu.yloc+52): player.equip_item(2) if(cursx>invmenu.xloc+10 and cursx<invmenu.xloc+42 and cursy>invmenu.yloc+60 and cursy<invmenu.yloc+92): player.equip_item(3) if(cursx>invmenu.xloc+60 and cursx<invmenu.xloc+92 and cursy>invmenu.yloc+60 and cursy<invmenu.yloc+92): player.equip_item(4) if(cursx>invmenu.xloc+110 and cursx<invmenu.xloc+142 and cursy>invmenu.yloc+60 and cursy<invmenu.yloc+92): player.equip_item(5) if event.type == KEYDOWN: if event.key == K_p: redrawScreen() paused = 0 if (event.key == K_e): #equipment paused = 1 while (paused == 1): drawMap(boardWidth, boardHeight) drawObjects() equipmenu=player.charactermenu equipscreen=player.charactersheet print player.charactermenu.xloc,player.charactermenu.yloc DISPLAYSURF.blit(equipscreen, (player.charactermenu.xloc,player.charactermenu.yloc)) pygame.display.update() for event in pygame.event.get(): if event.type==MOUSEBUTTONDOWN and event.button == 1 : cursx,cursy=event.pos if cursx>player.charactermenu.xloc and cursx<player.charactermenu.xloc+200 and cursy >player.charactermenu.yloc and cursy < player.charactermenu.yloc+10: boolean=True while(boolean): for event in pygame.event.get(): if event.type==MOUSEBUTTONUP and event.button == 1 : boolean=False relx, rely = event.pos player.charactermenu.xloc=player.charactermenu.xloc+(relx-cursx) player.charactermenu.yloc=player.charactermenu.yloc+(rely-cursy) elif event.type==MOUSEBUTTONDOWN and event.button == 3: cursx,cursy=event.pos if(cursx>player.charactermenu.xloc+10 and cursx<player.charactermenu.xloc+42 and cursy>player.charactermenu.yloc+10 and cursy<player.charactermenu.yloc+42 and player.equipment.check_slot('head') == False): index = 0 for item in player.equipment.items: if item.type=='head': break else: index=index+1 player.unequip_item(index) if(cursx>player.charactermenu.xloc+150 and cursx<player.charactermenu.xloc+182 and cursy>player.charactermenu.yloc+10 and cursy<player.charactermenu.yloc+42 and player.equipment.check_slot('shoulders') == False): index = 0 for item in player.equipment.items: if item.type=='shoulders': break else: index=index+1 player.unequip_item(index) if(cursx>player.charactermenu.xloc+10 and cursx<player.charactermenu.xloc+42 and cursy>player.charactermenu.yloc+60 and cursy<player.charactermenu.yloc+92 and player.equipment.check_slot('chest') == False): index = 0 for item in player.equipment.items: if item.type=='chest': break else: index=index+1 player.unequip_item(index) if(cursx>player.charactermenu.xloc+150 and cursx<player.charactermenu.xloc+182 and cursy>player.charactermenu.yloc+60 and cursy<player.charactermenu.yloc+92 and player.equipment.check_slot('hands') == False): index = 0 for item in player.equipment.items: if item.type=='hands': break else: index=index+1 player.unequip_item(index) if(cursx>player.charactermenu.xloc+10 and cursx<player.charactermenu.xloc+42 and cursy>player.charactermenu.yloc+110 and cursy<player.charactermenu.yloc+142 and player.equipment.check_slot('legs') == False): index = 0 for item in player.equipment.items: if item.type=='legs': break else: index=index+1 player.unequip_item(index) if(cursx>player.charactermenu.xloc+150 and cursx<player.charactermenu.xloc+182 and cursy>player.charactermenu.yloc+110 and cursy<player.charactermenu.yloc+142 and player.equipment.check_slot('feet') == False): index = 0 for item in player.equipment.items: if item.type=='feet': break else: index=index+1 player.unequip_item(index) if(cursx>player.charactermenu.xloc+10 and cursx<player.charactermenu.xloc+42 and cursy>player.charactermenu.yloc+160 and cursy<player.charactermenu.yloc+192 and (player.equipment.check_slot('1h') == False or player.equipment.check_slot('2h') == False)): index = 0 for item in player.equipment.items: if item.type=='1h' or item.type=='2h': break else: index=index+1 player.unequip_item(index) if(cursx>player.charactermenu.xloc+150 and cursx<player.charactermenu.xloc+182 and cursy>player.charactermenu.yloc+160 and cursy<player.charactermenu.yloc+192 and player.equipment.check_slot('shield') == False): index = 0 for item in player.equipment.items: if item.type=='shield': break else: index=index+1 player.unequip_item(index) if event.type == KEYDOWN: if event.key == K_p: redrawScreen() paused = 0
def drawObjects(): for object in levelhandler.activelevel().objectlist: DISPLAYSURF.blit(object.Img, (object.xcoordinate, object.ycoordinate))
def drawMap(boardWidth, boardHeight): for y in xrange(boardHeight): for x in xrange(boardWidth): DISPLAYSURF.blit(levelhandler.activelevel().map[x][y].Img, (levelhandler.activelevel().map[x][y].xcoordinate,levelhandler.activelevel().map[x][y].ycoordinate)) #Surface.Blit takes an image and a coordinate and then draws to that location.
def speedAdjust(): objectlist = sorted(levelhandler.activelevel().objectlist, key=lambda object:object.speed, reverse = True) return objectlist
def redrawStats(): stats.Update(player) DISPLAYSURF.blit(stats.menusurface, (stats.xcoord, stats.ycoord)) def redrawLog(): DISPLAYSURF.blit(log.menusurface, (log.xcoord, log.ycoord)) #The game structure is as follows: #1) Draw the map and allow the player to take his first turn. #2) After the player takes his first turn, enter the game loop. #3a) Re order the list of actors depending on speed. Higher speed = earlier turn order #3b) Iterate through the list of actors, allowing each to take his respective turn. #3c) When you get to the player, pause the game and allow him to take his turn. #3d) After everyone has taken their turns, refresh the screen with the new game state. levelhandler.activelevel().objectlist = speedAdjust() #Adjust the objects according to speed redrawScreen() #First draw the screen redrawLog() #Updates the log after each player turn playersTurn() #Player ALWAYS gets first turn. This is to avoid a monster attacking you when you enter the level before you have a chance to respond or receiving an attack before the screen is drawn. redrawScreen() #Update the screen after the player takes the first turn, and then begin game loop. redrawLog() #Updates the log after each player turn while True: levelhandler.activelevel().objectlist = speedAdjust() for object in levelhandler.activelevel().objectlist: if object == player: playersTurn() redrawStats() #Updates the current player stats at the end of each turn redrawLog() #Updates the log after each player turn
def look(self): sensedistance = 4 #Sight range is this value - 1 target = None tiletarget = None dungeon = levelhandler.activelevel() ##THIS IS THE UGLIEST CODE I HAVE EVER WRITTEN, WARNING ##Look up for x in range(1,sensedistance): x+1 newx = self.tilecoordinatex newy = self.tilecoordinatey - x try: if target == None: tiletarget = dungeon.map[newx][newy] ##Does the tile exist? for object in dungeon.objectlist: ##If so, is there an object on it? if object.xcoordinate == newx*32 and object.ycoordinate == newy*32: target = object print "I see: %s, at %s, %s" %(target, newx, newy) break else: break except IndexError: tiletarget = None print "I generate index errors" break ##Look down for x in range(1,sensedistance): x+1 newx = self.tilecoordinatex newy = self.tilecoordinatey + x try: if target == None: tiletarget = dungeon.map[newx][newy] ##Does the tile exist? for object in dungeon.objectlist: ##If so, is there an object on it? if object.xcoordinate == newx*32 and object.ycoordinate == newy*32: target = object print "I see: %s, at %s, %s" %(target, newx, newy) break else: break except IndexError: tiletarget = None print "I generate index errors" break ##Look east for x in range(1,sensedistance): x+1 newx = self.tilecoordinatex + x newy = self.tilecoordinatey try: if target == None: tiletarget = dungeon.map[newx][newy] ##Does the tile exist? for object in dungeon.objectlist: ##If so, is there an object on it? if object.xcoordinate == newx*32 and object.ycoordinate == newy*32: target = object print "I see: %s, at %s, %s" %(target, newx, newy) break else: break except IndexError: tiletarget = None print "I generate index errors" break #Look West for x in range(1,sensedistance): x+1 newx = self.tilecoordinatex - x newy = self.tilecoordinatey try: if target == None: tiletarget = dungeon.map[newx][newy] ##Does the tile exist? for object in dungeon.objectlist: ##If so, is there an object on it? if object.xcoordinate == newx*32 and object.ycoordinate == newy*32: target = object print "I see: %s, at %s, %s" %(target, newx, newy) break else: break except IndexError: tiletarget = None print "I generate index errors" break #Look NorthEast for x in range(1,sensedistance): x+1 newx = self.tilecoordinatex + x newy = self.tilecoordinatey + x try: if target == None: tiletarget = dungeon.map[newx][newy] ##Does the tile exist? for object in dungeon.objectlist: ##If so, is there an object on it? if object.xcoordinate == newx*32 and object.ycoordinate == newy*32: target = object print "I see: %s, at %s, %s" %(target, newx, newy) break else: break except IndexError: tiletarget = None print "I generate index errors" break #Look NorthWest for x in range(1,sensedistance): x+1 newx = self.tilecoordinatex - x newy = self.tilecoordinatey + x try: if target == None: tiletarget = dungeon.map[newx][newy] ##Does the tile exist? for object in dungeon.objectlist: ##If so, is there an object on it? if object.xcoordinate == newx*32 and object.ycoordinate == newy*32: target = object print "I see: %s, at %s, %s" %(target, newx, newy) break else: break except IndexError: tiletarget = None print "I generate index errors" break #Look SouthEast for x in range(1,sensedistance): x+1 newx = self.tilecoordinatex + x newy = self.tilecoordinatey - x try: if target == None: tiletarget = dungeon.map[newx][newy] ##Does the tile exist? for object in dungeon.objectlist: ##If so, is there an object on it? if object.xcoordinate == newx*32 and object.ycoordinate == newy*32: target = object print "I see: %s, at %s, %s" %(target, newx, newy) break else: break except IndexError: tiletarget = None print "I generate index errors" break #Look SouthWest for x in range(1,sensedistance): x+1 newx = self.tilecoordinatex - x newy = self.tilecoordinatey - x try: if target == None: tiletarget = dungeon.map[newx][newy] ##Does the tile exist? for object in dungeon.objectlist: ##If so, is there an object on it? if object.xcoordinate == newx*32 and object.ycoordinate == newy*32: target = object print "I see: %s, at %s, %s" %(target, newx, newy) break else: break except IndexError: tiletarget = None print "I generate index errors" break if target != None and target not in self.canSee: self.canSee[target.type] = (target.xcoordinate,target.ycoordinate) print self.canSee if len(self.canSee) > 0: print self.canSee