本帖最后由 iooops 于 2016-1-31 13:09 编辑
啊这个月快到头了 要发原创项目贴 - -
于是趁着正好还有点时间,做了一个略显简陋的- -小项目。
不多说了,直接上软硬件清单。
【硬件】
1. arduino UNO
2. 数字大按钮模块 黄色(Arduino兼容) X 4
3. 扩展板
【软件】
1. arduino
2. Processing
【项目构思】
把按键做成键盘上的上下左右做控制器,直接控制贪吃蛇。
制作方式嘛~
首先是根据上次写的这篇帖子来的:
【processing系列I】processing与arduino通信参见【方案I】
然后呢,在Arduino上接上按键,我这次用的是数字大按钮模块,当然也可以用其他奇奇怪怪的开关,比如触摸传感器、红外运动传感器、光敏传感器等等等等……
最后接完大概是这样的:
分别对应上下左右。
下面直接上代码
-
- import processing.serial.*;
- import cc.arduino.*;
-
- Arduino arduino;
- int button1 = 5;
- int button2 = 2;
- int button3 = 3;
- int button4 = 4;
-
- int B1,B2,B3,B4 = 0;
- // arduino
-
- int w = 10;
- int snakeLength = 2;
- int snakeHeadX;
- int snakeHeadY;
- char snakeDirection = 'R';
-
- int maxSnakeLength = 500;
- int[] x = new int[maxSnakeLength];
- int[] y = new int[maxSnakeLength];
-
- boolean foodKey = true;
- int foodX;
- int foodY;
-
- int backgroundColor = 244;
- int bestScore = snakeLength - 2;
-
- boolean gameOverKey = false;
-
- void setup() {
- arduino = new Arduino(this, Arduino.list()[2], 57600);
- arduino.pinMode(button1, Arduino.INPUT);
- arduino.pinMode(button2, Arduino.INPUT);
- arduino.pinMode(button3, Arduino.INPUT);
- arduino.pinMode(button4, Arduino.INPUT);
-
- size(500, 500);
- frameRate(15);
- noStroke();
- }
-
- void draw() {
- B1 = arduino.digitalRead(button1);
- B2 = arduino.digitalRead(button2);
- B3 = arduino.digitalRead(button3);
- B4 = arduino.digitalRead(button4);
- println("B1:" + B1);
- println("B2:" + B2);
- println("B3:" + B3);
- println("B4:" + B4);
-
- if( isGameOver() ){
- return;
- }
-
- background(backgroundColor);
-
- if (B1 == 1) {
- snakeDirection = 'L';
- } else if (B2 == 1) {
- snakeDirection = 'R';
- } else if (B3 == 1) {
- snakeDirection = 'D';
- } else if (B4 == 1) {
- snakeDirection = 'U';
- }
-
- switch(snakeDirection){
- case 'L':
- snakeHeadX -= w;
- break;
- case 'R':
- snakeHeadX += w;
- break;
- case 'D':
- snakeHeadY += w;
- break;
- case 'U':
- snakeHeadY -= w;
- break;
- }
-
-
- if( isSnakeDie() ){
- return;
- }
-
- //add another food
- drawFood(width, height);
-
- //eat it
- if( snakeHeadX == foodX && snakeHeadY == foodY ){
- snakeLength++;
- foodKey = true;
- }
-
- //store snake body
- for (int i=snakeLength-1; i>0; i--) {
- x[i] = x[i-1];
- y[i] = y[i-1];
- }
-
- //store snake new head
- y[0] = snakeHeadY;
- x[0] = snakeHeadX;
-
- fill(#7B6DB7);
-
- //draw snake
- for (int i=0; i<snakeLength; i++) {
- rect(x[i], y[i], w, w);
- }
- }//end draw
-
- void snakeInit(){
- background(backgroundColor);
- snakeLength = 2;
- gameOverKey = false;
- snakeHeadX = 0;
- snakeHeadY = 0;
- snakeDirection = 'R';
- }
-
- void drawFood(int maxWidth, int maxHeight) {
- fill(#ED1818);
-
- if ( foodKey ) {
- foodX = int( random(0, maxWidth)/w ) * w;
- foodY = int( random(0, maxHeight)/w ) * w;
- }
-
- rect(foodX, foodY, w, w);
- foodKey = false;
- }
-
- boolean isGameOver(){
- if( gameOverKey && keyPressed && (key == 'r' || key == 'R') ){
- snakeInit();
- }
-
- return gameOverKey;
- }
-
- void showGameOver(){
- pushMatrix();
-
- gameOverKey = true;
- bestScore = bestScore > snakeLength ? bestScore : snakeLength;
-
- background(0);
- translate(width/2, height/2 - 50);
- fill(255);
- textAlign(CENTER);
-
- textSize(84);
- text(snakeLength + " / " + bestScore, 0, 0);
-
- fill(120);
- textSize(18);
- text("score / best", 0, 230);
- text("Game over, press 'R' to restart.", 0, 260);
-
- popMatrix();
- }
-
- boolean isSnakeDie(){
- //hitting the wall
- if( snakeHeadX < 0 || snakeHeadX >= width || snakeHeadY < 0 || snakeHeadY >= height){
- showGameOver();
- return true;
- }
-
- //eat itself
- if( snakeLength > 2 ){
- for( int i=1; i<snakeLength; i++ ){
- if( snakeHeadX == x[i] && snakeHeadY == y[i] ){
- showGameOver();
- return true;
- }
- }
- }
-
- return false;
- }
复制代码
啊对了贪吃蛇的代码是找的这哥们:https://github.com/paper/processing-snake
我就改了改其中控制的代码,把键盘换成了按键 - -
然后再给它加个壳估计手感会不错。
视频由于条件有限- - 就不拍了
啊顺手再来分享一下最近玩的东西。。
老夫前几个月买了个makeymakey,就下面这货:
这货能替代键盘按键,总共有6个开关,默认情况下是对应键盘上的上下左右、空格和Enter,不过可以通过arduino软件来进行修改,可以接任何能导电的东西做控制器。就是必须把人体给接通- - 手上还得再接根线 - - 个人感觉有点麻烦。。
但这货被玩得创意满满- -
详情参阅官网:http://www.makeymakey.com/
|
|
|
|
|
|