博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDLBits 系列(35)Lemmings Game
阅读量:2027 次
发布时间:2019-04-28

本文共 2516 字,大约阅读时间需要 8 分钟。

目录


背景

这是一个Lemmings的简单游戏,我们通过状态机可以设计这个游戏,从简单到复杂,一步一步实现这个游戏。


Lemmings1

前言

Lemmings1想要往左走,可是遇到左侧的障碍后,便向右转向;在向右走的过程中,如果遇到右侧的障碍,同理左转向。头脑简单的Lemmings要么左走,要么右走,两种状态。

原题复现

The game involves critters with fairly simple brains. So simple that we are going to model it using a finite state machine.

In the Lemmings' 2D world, Lemmings can be in one of two states: walking left or walking right. It will switch directions if it hits an obstacle. In particular, if a Lemming is bumped on the left, it will walk right. If it's bumped on the right, it will walk left. If it's bumped on both sides at the same time, it will still switch directions.

Implement a Moore state machine with two states, two inputs, and one output that models this behaviour.

module top_module(    input clk,    input areset,    // Freshly brainwashed Lemmings walk left.    input bump_left,    input bump_right,    output walk_left,    output walk_right);

我的设计

设计我的Lemmings:

module top_module(    input clk,    input areset,    // Freshly brainwashed Lemmings walk left.    input bump_left,    input bump_right,    output walk_left,    output walk_right); //       parameter LEFT=0, RIGHT=1;    reg state, next_state;     always @(*) begin        // State transition logic        case(state)            LEFT: begin                if(bump_left) next_state = RIGHT;                else next_state = LEFT;            end            RIGHT: begin                if(bump_right) next_state = LEFT;                else next_state = RIGHT;            end            default: begin               next_state = LEFT;             end        endcase    end     always @(posedge clk, posedge areset) begin        // State flip-flops with asynchronous reset        if(areset) begin            state <= LEFT;        end        else begin           state <= next_state;         end    end        always@(*) begin        case(state)            LEFT: begin                walk_left = 1;                 walk_right = 0;            end            RIGHT: begin                walk_right = 1;                walk_left = 0;            end            default: begin // almost impossible                walk_left = 0;                walk_right = 0;            end        endcase    end     // Output logic    // assign walk_left = (state == ...);    // assign walk_right = (state == ...); endmodule

我设计的Lemmings,就是两个状态,处于LEFT状态,则向左走,处于RIGHT状态,也就向右走,复位则向左走。

输出仅仅和当前处于那个状态有关,而状态的切换,也是看当前状态下有没有当前方向的阻碍,例如在LEFT状态下,存在bump_left,则下一个时钟状态切换到RIGHT,固然,Lemmings也就向右走。

测试成功。


Lemmings2

 

 

 

 

 


Lemmings3

 

 

 

 

 

 

 

转载地址:http://sdcaf.baihongyu.com/

你可能感兴趣的文章
CMake学习笔记(二)——CMake语法
查看>>
CMake学习笔记(三)——以笔者的Robosub竞赛为例
查看>>
Ubuntu环境如何上传项目到GitHub网站?
查看>>
程序生成之编译、链接、加载浅析
查看>>
交叉编译学习笔记(一)——交叉编译和交叉工具链
查看>>
交叉编译学习笔记(二)——交叉编译器的命名规则
查看>>
ARM交叉编译OpenCV错误总结
查看>>
Retinex图像增强算法(SSR, MSR, MSRCR)详解及其OpenCV源码
查看>>
图像纹理特征总体简述
查看>>
纹理特征提取方法:LBP, 灰度共生矩阵
查看>>
Opencv中数据结构Mat的相关属性
查看>>
OpenCV像素点邻域遍历效率比较,以及访问像素点的几种方法
查看>>
背景提取算法——帧间差分法、背景差分法、ViBe算法、ViBe+算法
查看>>
论文翻译:ViBe+算法(ViBe算法的改进版本)
查看>>
学习July博文总结——支持向量机(SVM)的深入理解(上)
查看>>
学习July博文总结——支持向量机(SVM)的深入理解(下)
查看>>
OpenCV 2.4.9 支持向量机(SVM)说明
查看>>
OpenROV Cockpit说明
查看>>
HTML 表单 (form) 的作用解释
查看>>
Node.js 异步编程基础理解
查看>>