兰顿蚂蚁html版

1024快乐!

 用html+js模拟兰顿蚂蚁(Langton‘s Ant)。地图和蚂蚁是分离的,地图自动扩充,地图能拖动缩放,可以添加干扰,,bug未知

兰顿蚂蚁,是于1986年,由克里斯·兰顿提出来的,属于细胞自动机的一种。

平面上的正方形格子被填上黑色或白色。在其中一格正方形内有一只“蚂蚁”。
蚂蚁的头部朝向为:上下左右其中一方。

蚂蚁的移动规则十分简单:
若蚂蚁在黑格,右转90度,将该格改为白格,并向前移一格;
若蚂蚁在白格,左转90度,将该格改为黑格,并向前移一格。

规则虽然简单,蚂蚁的行为却十分复杂。刚刚开始时留下的路线都会有接近对称,像是会重复,但不论起始状态如何,蚂蚁经过漫长的混乱活动后,会开辟出一条规则的“高速公路”。

<!-- ######################################################################### -->
<button onclick="nextSteps(1)">下一步</button>
<button onclick="nextSteps(10)">下10步</button>
<button onclick="nextSteps(100)">下100步</button>
<button onclick="nextSteps(1000)">下1000步</button>
<button onclick="nextSteps()">下N步</button>
<input type='number' min=1 max=10000 value=100 id="stepsCount">

<button onclick="randomDots()">干扰点</button>

<button onclick="nextStepsStart()">开始</button>
<button onclick="nextStepsStop()">停止</button>
步数:<span id='stepsTotal'>0</span>

<br>
<canvas id="out" width="800px" height="600px" style="border:2px solid #777;"></canvas>
<script>
	
	
	
	function CellMap(canvasId = 'out', initMapWidth = 50, initMapHeight = 50, mapWidthMax = 100000, mapHeightMax = 100000) {
		
		this.canvasId = canvasId;
		
		this.map = null;
		this.mapWidth = initMapWidth;
		this.mapHeight = initMapHeight;
		this.mapWidthMax = mapWidthMax;
		this.mapHeightMax = mapHeightMax;
		
		this.canvas = null;
		this.cctx = null;
		
		this.mapImageZoomMin = 0.5;
		this.mapImageZoomMax = 10;
		
		this.cellBaseWidth = 8;
		this.mapImageZoom = 1;
		this.mapImageOffsetX = 0;
		this.mapImageOffsetY = 0;
		this.mapImageWidth = null;
		this.mapImageHeight = null;
		
		// ------------------------------------
		this.init = function() {
			this.initMap();
			this.initCanvas();
			this.initVMap();
			
		}
		this.initMap = function() {
			this.map = Array(this.mapHeight);
			for (var i = 0, len = this.mapHeight; i < len; i++) {
				this.map[i] = Array(this.mapWidth);
			}
		}
		this.initCanvas = function() {
			this.canvas = document.getElementById(this.canvasId);
			this.canvasWidth = this.canvas.width;
			this.canvasHeight = this.canvas.height;
			//console.log(this.canvas, this.canvasWidth, this.canvasHeight);
			this.cctx = this.canvas.getContext("2d");
			this.cctx.strokeStyle = '#aaa';
			this.cctx.fillStyle = '#888';
			this.cctx.lineWidth = 1;
			this.cctx.translate(0.5,0.5);
		}
		// 支持缩放和拖动地图
		this.bindCanvasEvent = function() {
			var that = this;
			var down = false;
			var x1, x2, y1, y2;
			this.canvas.onmousedown = function(evt) { // 触发鼠标按下
				down = true;
				x1 = evt.offsetX;
				y1 = evt.offsetY;
			}
			this.canvas.onmouseup = function(evt) { // 触发鼠标抬起
				down = false;
				x2 = evt.offsetX;
				y2 = evt.offsetY;
				that.moveMapImageDisplayArea(x2 - x1, y2 - y1);
			}
			this.canvas.onmousemove = function(evt) { // 触发鼠标拖动
				if (!down) {
					return;
				}
				var x =
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
使用pygame可以很容易地实现兰顿蚂蚁的模拟。下面是一个简单的示例代码: ```python import pygame import numpy as np # 初始化pygame pygame.init() # 设置窗口尺寸和标题 width, height = 800, 600 window = pygame.display.set_mode((width, height)) pygame.display.set_caption("Langton's Ant") # 定义颜色 BLACK = (0, 0, 0) WHITE = (255, 255, 255) # 定义兰顿蚂蚁的初始位置和方向 ant_pos = np.array([width // 2, height // 2]) ant_dir = np.array([0, -1]) # 初始方向向上 # 定义兰顿蚂蚁的移动规则 turn_right = np.array([[0, -1], [1, 0]]) turn_left = np.array([[0, 1], [-1, 0]]) # 创建一个二维数组来表示画布上每个像素的状态 grid_size = 5 grid = np.zeros((width // grid_size, height // grid_size)) # 游戏循环 running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # 更新兰顿蚂蚁的位置和方向 ant_pos[0] = (ant_pos[0] + ant_dir[0]) % (width // grid_size) ant_pos[1] = (ant_pos[1] + ant_dir[1]) % (height // grid_size) ant_dir = np.dot(ant_dir, turn_right if grid[ant_pos[0], ant_pos[1]] == 0 else turn_left) # 更新当前像素的状态 grid[ant_pos[0], ant_pos[1]] = 1 - grid[ant_pos[0], ant_pos[1]] # 绘制画布 window.fill(BLACK) for x in range(width // grid_size): for y in range(height // grid_size): if grid[x, y] == 1: pygame.draw.rect(window, WHITE, (x * grid_size, y * grid_size, grid_size, grid_size)) pygame.display.flip() # 退出pygame pygame.quit() ``` 这段代码使用numpy库来处理数组,使用pygame库来进行图形绘制。在游戏循环中,首先更新兰顿蚂蚁的位置和方向,然后根据当前像素的状态更新该像素的状态,最后绘制画布。 运行以上代码,你将看到一个兰顿蚂蚁在窗口中移动并改变像素的状态。你可以尝试修改代码来调整窗口大小、格子的大小、兰顿蚂蚁的初始位置和方向等参数,以实现你想要的效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值