有一张由多种颜色组成的二维卡片。地图上的红点就是有方向的人。该人必须仅沿着某种颜色移动(在本例中,仅沿着白色路径)。
我不明白如何从照片中获取信息。理论上,算法应该接收图像并沿着路径定向人
我猜这个问题与神经网络有关。
实现目标的方法有多种。下面我创建了将图像过渡到游戏的简单示例。我使用
PIL
lib 来解析图像,并使用 pyxel
作为游戏引擎。
import pyxel
from PIL import Image
BORDER_OFFSET = 5
STEP = 5
COLOR_ROAD = (255, 255, 255, 255)
COLOR_PLAYER = (237, 28, 36, 255)
def parse_image():
road = []
img = Image.open('image.png')
pix = img.load()
width, height = img.size
x = 0
y = 0
player = None
for y in range(BORDER_OFFSET, height-BORDER_OFFSET, STEP):
for x in range(BORDER_OFFSET, width-BORDER_OFFSET, STEP):
color = pix[x, y]
if color == COLOR_ROAD:
road.append((x, y))
elif color == COLOR_PLAYER:
player = (x, y)
return x, y, player, road
class Game:
def __init__(self):
width, height, self.player, self.road = parse_image()
pyxel.init(width, height)
pyxel.run(self.update, self.draw)
def update(self):
pass
def draw(self):
pyxel.cls(pyxel.COLOR_BLACK)
for c in self.road:
pyxel.circ(c[0], c[1], 10, pyxel.COLOR_WHITE)
pyxel.circ(self.player[0], self.player[1], 30, pyxel.COLOR_RED)
Game()