def move_player(world, player, m_grid): px = player['x'] py = player['y'] # move player new_pos = calc_move(world[py][px], {'x': px, 'y': py}) player['x'] = new_pos['x'] player['y'] = new_pos['y'] died = False # see if player lands on monster if m_grid[player['y']] is not None and m_grid[player['y']][player['x']] is not None: died = True return {'player':player, 'died':died}
def move_monsters(world, monsters, player, m_grid, active_monsters, p_grid, powerups): changes = { 'world': [], 'monsters': [], 'is_dead': False } px = player['x'] py = player['y'] heartrate = player['heartrate'] for a in active_monsters: m = monsters[a] mx = m['x'] my = m['y'] mcode = world[my][mx] new_pos = calc_move(mcode, {'x': mx, 'y': my}) nx = new_pos['x'] ny = new_pos['y'] ncode = world[ny][nx] #Check if new location points back here if abs(ncode - mcode) == 2: ncode = (ncode + [-1,1][randint(0,1)]) % 4 world[ny][nx] = ncode changes['world'].append({ 'x': nx, 'y': ny, 'direction': directions[ncode] }) # check if it can move there if m_grid[ny][nx] == None: # moving allowed! (i.e. nothing in the way) m_grid[my][mx] = None # off of old spot m_grid[ny][nx] = m # onto new spot # save new position m['x'] = nx m['y'] = ny # remove any powerups stepped on p_grid[ny][nx] = None new_powerups = [] for pu in powerups: if not (pu['x'] == nx and pu['y'] == ny): new_powerups.append(pu) powerups = new_powerups else: mcode = (mcode + [-1,1][randint(0,1)]) % 4 world[my][mx] = mcode changes['world'].append({ 'x': mx, 'y': my, 'direction': directions[mcode] }) mx = m['x'] my = m['y'] # calculate proximity if abs(mx - px) <= 2 and abs(my - py) <= 2: if mx == px and my == py: # on the same square heartrate += 1 changes['is_dead'] = True elif abs(mx - px) <= 1 and abs(my - py) <= 1: # superclose heartrate += 5 else: # nearby heartrate += 2 if heartrate == player['heartrate']: heartrate -= 10 player['heartrate'] = heartrate if heartrate > 50 else 50 player['heartbeats'] -= player['heartrate'] changes['player'] = player return { 'm_grid': m_grid, 'changes': changes, 'world': world, 'monsters': monsters, 'p_grid': p_grid, 'powerups': powerups }