def aim(now_card, user, aimer): if (now_card == 0): return 0 tot = len(pl.player_in_game) if (now_card.id == 10 or now_card.id == 20): # print(user.pos,aimer.pos) # now_card.show() return pl.distance(user, aimer) <= 1 and user.pos != aimer.pos elif (now_card.id <= 3): defence = aimer.equip[1] if (user.equip[0].id != 25): if (defence.id == 33 and now_card.type & 1 == 0): # 仁王盾 # print("%s使用了[%s]" % (aimer.name, card_nm[aimer.equip[1].id])) return 0 elif (defence.id == 34 and now_card.id == 1): # 藤甲 # print("%s使用了[%s]" % (aimer.name, card_nm[aimer.equip[1].id])) return 0 return pl.distance(user, aimer) <= user.equip[0].range and user.pos != aimer.pos elif (now_card.id in [5, 6, 15] or now_card.id > 20): return user.pos == aimer.pos elif (now_card.id in [4, 18, 11, 12, 13, 14]): return 0 elif (now_card.id in [7, 9, 10, 16, 19]): return user.pos != aimer.pos return 1
def target_tile(max_range=None): """Selects a tile either from a mouse click or selection""" x = player.x y = player.y mx = t.state(t.TK_MOUSE_X) my = t.state(t.TK_MOUSE_Y) render.draw_cursor(x, y) render.draw_max_range(player, max_range) message( "Select using the mouse or movements keys. Use M1, Enter, 5 to confirm and ESC/SPACE to cancel", colours.light_cyan) render_all() key = t.read() while key not in (t.TK_MOUSE_LEFT, t.TK_ENTER, t.TK_KP_ENTER, t.TK_KP_5, t.TK_ESCAPE, t.TK_SPACE): dx = x dy = y if key in DIR: (dir_x, dir_y) = DIR.get(key) dx += dir_x dy += dir_y zx = t.state(t.TK_MOUSE_X) zy = t.state(t.TK_MOUSE_Y) if zx != mx or zy != my: mx = zx my = zy dx = mx dy = my #Check for Out of Bounds/Range if 0 < dx and dx < MAP_WIDTH and 0 < dy and dy < MAP_HEIGHT: #Check for within range if player.distance(dx, dy) <= max_range and not gmap.is_blocked(dx, dy, None, tilesOnly=True) \ and gmap.is_explored(dx, dy): x = dx y = dy render.draw_cursor(x, y) if t.has_input(): key = t.read() else: key = None #Check if it was a mouse confirm or a key confirm/cancel render.draw_cursor(None, None) render.draw_max_range(None, None) if key == t.TK_MOUSE_LEFT: x = t.state(t.TK_MOUSE_X) y = t.state(t.TK_MOUSE_Y) #Cancel selection process elif key in (t.TK_ESCAPE, t.TK_SPACE): return (None, None) #Return coordinates selected return (x, y)
def generate_treasure(fitness, level, done, closest_dist): (x, y) = get_treasure_pos() #Check if the new treasure position is not occupied by any other obstacles. while ((x, y) == player.current_pos or level[y][x] == 1): (x, y) = get_treasure_pos() level[y][x] = 3 #Add 10 points to the genomes fitness (Rewarded) fitness += 10 #The function is used to update the map with the new treasure position update_maze(level, True) wn.update() #Get the distance between the player and the new treasure closest_dist = player.distance(treasure.current_pos) return (fitness, level, done, closest_dist)
#The configuration file contains values which detemines the structure of the neural network and #how it will train itself. config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, './config-feedforward') level = np.array(levels[level_select]) #Initilize the neural network with only one genome net = neat.nn.recurrent.RecurrentNetwork.create(genome, config) update_maze(level, True) wn.update() #Get the distance between the player and the treasure closest_dist = player.distance(treasure.current_pos) #Turn off screen updates wn.tracer(0) #Used to count amount of fitness in a game run fitness_current = 0 #Used to check amount of steps the player has used counter = 0 max_counter = counters done = False while not done:
screen.tracer(0) screen.listen() player = player.Player() car = car.Car() screen.onkey(player.go_up, 'Up') screen.onkey(player.go_down, 'Down') while True: time.sleep(car.car_speed) screen.update() if player.ycor() > 280: player.starting_point() car.starting_point() for i in car.cars: if player.distance(i) < 23: player.gameover() for i in car.cars: if i.xcor() > 290: r = random.randint(-200, 200) i.goto(-290, r) if i.xcor() < -290: r = random.randint(-200, 200) i.goto(290, r) car.move_cars()
def eval_genomes(genomes, config): global level_select #Let every genome play the game for genome_id, genome in genomes: level = np.array(levels[level_select]) net = neat.nn.recurrent.RecurrentNetwork.create(genome, config) update_maze(level, True) wn.update() #Get the distance between the player and the treasure closest_dist = player.distance(treasure.current_pos) #Turn off screen updates wn.tracer(0) #Used to count amount of fitness in a game run fitness_current = 0 #Used to check amount of steps the player has used counter = 0 max_counter = counters done = False while not done: (px, py) = player.current_pos (tx, ty) = treasure.current_pos #Get 8 inputs to feed to the neural network #Every input contains a 1 or 0 #1 = Yes #0 = No #Input 1 - is it clear to the left? left = whatIsNext(level[py][px - 1]) #Input 2 - is it clear to the right? right = whatIsNext(level[py][px + 1]) #Input 3 - is it clear up? up = whatIsNext(level[py + 1][px]) #Input 4 - is it clear down? down = whatIsNext(level[py - 1][px]) #Input 5 - is the treasure to the left? leftG = sign(px - tx) #Input 6 - is the treasure to the right? rightG = sign(tx - px) #Input 7 - is the treasure up? upG = sign(py - ty) #Input 8 - is the treasure down? downG = sign(ty - py) #Add the inputs into the neural network nnOutput = net.activate( [left, up, right, down, leftG, upG, rightG, downG]) #Get the output back from the neural network #The output decides is the next move which the play will take done = player.next_move(nnOutput.index(max(nnOutput)), level) if done: break #Reload the map with new values update_maze(level) wn.update() current_dist = player.distance(treasure.current_pos) #Check if the player has come closer to the treasure #If true then add 1 point to the the genome fitness and the reset the counter if closest_dist > current_dist: closest_dist = current_dist fitness_current += 1 counter = 0 counter += 1 #If the pplayer reached the treasure, spawn a new treasure if current_dist == 0: (fitness_current, level, done, closest_dist) = generate_treasure(fitness_current, level, done, closest_dist) counter = 0 genome.fitness = fitness_current #If the player has reached the maximum amount of moves then end the game for the current genome #Or if the genome has exceeded it's fintess goal then end the training if done or counter == max_counter or fitness_current > 10000: done = True genome.fitness = fitness_current