Exemple #1
0
def rule_action(frame):
	"""
	function: according to the relative location of closest enemy and player return the action based rule
	:param frame: the input frame
	:return: the action based rule
	"""
	objects = imageProcessing.extract_objects(frame)
	player_coordinate = []
	enemy_coordinate = []
	for ob in objects:
		if PLAYER_RADIUS - THRESH < ob[1] < PLAYER_RADIUS + THRESH:
			player_coordinate.append(ob)
		if ENEMY_RADIUS - THRESH < ob[1] < ENEMY_RADIUS + THRESH:
			enemy_coordinate.append(ob)
	# if there are more than one player, raise a error
	# if len(player_coordinate) > 1:
	# 	raise Exception("More than one player!")

	# action options
	options = ["left", "right"]

	# if no enemy or more than one player, return random choice
	if len(enemy_coordinate) == 0 or len(player_coordinate) > 1:
		return choice(options)
	else:
		# find the enemy that closest to player, record it at a tuple
		closest_enemy = min(enemy_coordinate, key=lambda x: abs(x[0][0] - player_coordinate[0][0][0]))

		# compare the X coordinate of player and closest enemy, choose action
		if closest_enemy[0][0] > player_coordinate[0][0][0]:
			return "right"
		else:
			return "left"
Exemple #2
0
def rule_action(frame):
	"""
	function: according to the relative location of enemy and player return the action based on rules
	:param frame: the input frame
	:return: the action based on rule
	"""
	objects = imageProcessing.extract_objects(frame)
	player_coordinate = []
	enemy_coordinate = []
	for ob in objects:
		if PLAYER_RADIUS - THRESH < ob[1] < PLAYER_RADIUS + THRESH:
			player_coordinate.append(ob)
		if ENEMY_RADIUS - THRESH < ob[1] < ENEMY_RADIUS + THRESH:
			enemy_coordinate.append(ob)

	# action options
	options = ["left", "right"]

	unsafe_zone = []
	for enemy in enemy_coordinate:
		if SAFE_HIGH < enemy[1] < PLAYER_HIGH and abs(enemy[0] - player_coordinate[0][0]) < SAFE_WIDTH:
			unsafe_zone.append(enemy)
	# if no enemy or more than one player, return random choice
	if len(enemy_coordinate) == 0 or len(player_coordinate) > 1:
		ruleAction = choice(options)
		safeAction = "safe"
	elif len(unsafe_zone) == 0:  # there are enemy in the screen, but no enemy in the unsafe zone.
		# find the enemy that closest to player, record it at a tuple
		closest_enemy = min(enemy_coordinate, key=lambda x: abs(x[0][0] - player_coordinate[0][0][0]))
		# compare the X coordinate of player and closest enemy, choose action
		if closest_enemy[0][0] > player_coordinate[0][0][0]:
			ruleAction = "right"
			safeAction = "safe"
		else:
			ruleAction = "left"
			safeAction = "safe"
	elif len(unsafe_zone) == 1:  # there is one enemy in the unsafe zone
		ruleAction = choice(options)
		if unsafe_zone[0][0][0] > player_coordinate[0][0][0]:
			safeAction = "left"
		else:
			safeAction = "right"
	else:
		ruleAction = choice(options)
		most_dangerous_enemy = max(unsafe_zone, key=lambda x: x[0][1])
		if most_dangerous_enemy[0][0] > player_coordinate[0][0][0]:
			safeAction = "left"
		else:
			safeAction = "right"
	return ruleAction, safeAction
Exemple #3
0
def rule_action(frame):
	"""
	function: according to the relative location of closest enemy and player return the action based rule
	:param frame: the input frame
	:return: the action based rule
	"""
	player_coordinate = imageProcessing.extract_objects(frame, PLAYER_COLOR, THRESH)
	flag_coordinate = imageProcessing.extract_objects(frame, FLAG_COLOR, THRESH)

	# action options
	options = ["left", "right"]

	# if no enemy or more than one player, return random choice
	if len(flag_coordinate) == 0 or len(player_coordinate) > 1:
		return choice(options)
	else:
		# find the enemy that closest to player, record it at a tuple
		closest_flag = min(flag_coordinate, key=lambda x: x[1])

		# compare the X coordinate of player and closest enemy, choose action
		if closest_flag[0] > player_coordinate[0][0]:
			return "right"
		else:
			return "left"
Exemple #4
0
def rule_action(frame):
	"""
	function: according to the incline orientation of the pole return the action based rule
	:param frame: the input frame
	:return: the action based rule
	"""
	box = imageProcessing.extract_objects(frame, POLE_COLOR, THRESH)

	# numpy array to list
	box_list = box.tolist()
	# sorted according to Y coordinate
	box_list = sorted(box_list, key=lambda x: x[1])
	if box_list[0][0] >= box_list[2][0]:
		return "right"
	else:
		return "left"
Exemple #5
0
import cv2
from random import choice
import imageProcessing
import numpy as np

PLAYER_COLOR = [1, 240, 255]
FLAG_COLOR = [1, 1, 255]
TREE_COLOR = [1, 255, 1]
THRESH = 40

frame = cv2.imread(r"E:\Reserch\GameAI\skier\image_processing_lab\sample1.png")

player_coordinate = imageProcessing.extract_objects(frame, PLAYER_COLOR, THRESH)
tree_coordinate = imageProcessing.extract_objects(frame, TREE_COLOR, THRESH)

distance = [((player_coordinate[0][0] - tree[0])**2 + (player_coordinate[0][1] - tree[1])**2)**0.5 for tree in tree_coordinate]

print(tree_coordinate)
print(player_coordinate)
min_distance_index = np.argmin(distance)
print(tree_coordinate[min_distance_index])