7952浏览
查看: 7952|回复: 0

[项目] 贪吃蛇——游戏控制器

[复制链接]
本帖最后由 iooops 于 2016-1-31 13:09 编辑

啊这个月快到头了 要发原创项目贴 - -

于是趁着正好还有点时间,做了一个略显简陋的- -小项目。

不多说了,直接上软硬件清单。

【硬件】
1. arduino UNO
2. 数字大按钮模块 黄色(Arduino兼容)  X 4
3. 扩展板
【软件】
1. arduino
2. Processing

【项目构思】
把按键做成键盘上的上下左右做控制器,直接控制贪吃蛇。

制作方式嘛~
首先是根据上次写的这篇帖子来的:

【processing系列I】processing与arduino通信参见【方案I】


然后呢,在Arduino上接上按键,我这次用的是数字大按钮模块,当然也可以用其他奇奇怪怪的开关,比如触摸传感器、红外运动传感器、光敏传感器等等等等……

最后接完大概是这样的:
贪吃蛇——游戏控制器图1分别对应上下左右。


下面直接上代码
  1. import processing.serial.*;
  2. import cc.arduino.*;
  3. Arduino arduino;
  4. int button1 = 5;
  5. int button2 = 2;
  6. int button3 = 3;
  7. int button4 = 4;
  8. int B1,B2,B3,B4 = 0;
  9. // arduino
  10. int w = 10;
  11. int snakeLength = 2;
  12. int snakeHeadX;
  13. int snakeHeadY;
  14. char snakeDirection = 'R';
  15. int maxSnakeLength = 500;
  16. int[] x = new int[maxSnakeLength];
  17. int[] y = new int[maxSnakeLength];
  18. boolean foodKey = true;
  19. int foodX;
  20. int foodY;
  21. int backgroundColor = 244;
  22. int bestScore = snakeLength - 2;
  23. boolean gameOverKey = false;
  24. void setup() {
  25.   arduino = new Arduino(this, Arduino.list()[2], 57600);
  26.   arduino.pinMode(button1, Arduino.INPUT);
  27.   arduino.pinMode(button2, Arduino.INPUT);
  28.   arduino.pinMode(button3, Arduino.INPUT);
  29.   arduino.pinMode(button4, Arduino.INPUT);
  30.   
  31.   size(500, 500);
  32.   frameRate(15);
  33.   noStroke();
  34. }
  35. void draw() {
  36.   B1 = arduino.digitalRead(button1);
  37.   B2 = arduino.digitalRead(button2);
  38.   B3 = arduino.digitalRead(button3);
  39.   B4 = arduino.digitalRead(button4);
  40.   println("B1:" + B1);
  41.   println("B2:" + B2);
  42.   println("B3:" + B3);
  43.   println("B4:" + B4);
  44.   
  45.   if( isGameOver() ){
  46.     return;
  47.   }
  48.   background(backgroundColor);
  49.   
  50.   if (B1 == 1) {
  51.     snakeDirection = 'L';
  52.   } else if (B2 == 1) {
  53.     snakeDirection = 'R';
  54.   } else if (B3 == 1) {
  55.     snakeDirection = 'D';
  56.   } else if (B4 == 1) {
  57.     snakeDirection = 'U';
  58.   }
  59.   
  60.   switch(snakeDirection){
  61.     case 'L':
  62.       snakeHeadX -= w;
  63.       break;
  64.     case 'R':
  65.       snakeHeadX += w;
  66.       break;
  67.     case 'D':
  68.       snakeHeadY += w;
  69.       break;
  70.     case 'U':
  71.       snakeHeadY -= w;
  72.       break;
  73.   }
  74.   
  75.   if( isSnakeDie() ){
  76.     return;
  77.   }
  78.   
  79.   //add another food
  80.   drawFood(width, height);
  81.   
  82.   //eat it
  83.   if( snakeHeadX == foodX && snakeHeadY == foodY ){
  84.     snakeLength++;
  85.     foodKey = true;
  86.   }
  87.   
  88.   //store snake body
  89.   for (int i=snakeLength-1; i>0; i--) {
  90.     x[i] = x[i-1];
  91.     y[i] = y[i-1];
  92.   }
  93.   
  94.   //store snake new head
  95.   y[0] = snakeHeadY;
  96.   x[0] = snakeHeadX;
  97.   
  98.   fill(#7B6DB7);
  99.   
  100.   //draw snake
  101.   for (int i=0; i<snakeLength; i++) {
  102.     rect(x[i], y[i], w, w);
  103.   }
  104. }//end draw
  105. void snakeInit(){
  106.   background(backgroundColor);
  107.   snakeLength = 2;
  108.   gameOverKey = false;
  109.   snakeHeadX = 0;
  110.   snakeHeadY = 0;
  111.   snakeDirection = 'R';
  112. }
  113. void drawFood(int maxWidth, int maxHeight) {
  114.   fill(#ED1818);
  115.   
  116.   if ( foodKey ) {
  117.     foodX = int( random(0, maxWidth)/w  ) * w;
  118.     foodY = int( random(0, maxHeight)/w ) * w;
  119.   }
  120.   
  121.   rect(foodX, foodY, w, w);
  122.   foodKey = false;
  123. }
  124. boolean isGameOver(){
  125.   if( gameOverKey && keyPressed && (key == 'r' || key == 'R') ){
  126.     snakeInit();
  127.   }
  128.   
  129.   return gameOverKey;
  130. }
  131. void showGameOver(){
  132.   pushMatrix();
  133.    
  134.   gameOverKey = true;
  135.   bestScore = bestScore > snakeLength ? bestScore : snakeLength;
  136.   
  137.   background(0);
  138.   translate(width/2, height/2 - 50);
  139.   fill(255);
  140.   textAlign(CENTER);
  141.   textSize(84);
  142.   text(snakeLength + " / " + bestScore, 0, 0);
  143.   
  144.   fill(120);
  145.   textSize(18);
  146.   text("score / best", 0, 230);
  147.   text("Game over, press 'R' to restart.", 0, 260);
  148.   popMatrix();
  149. }
  150.   boolean isSnakeDie(){
  151.   //hitting the wall
  152.   if( snakeHeadX < 0 || snakeHeadX >= width || snakeHeadY < 0 || snakeHeadY >= height){
  153.     showGameOver();
  154.     return true;
  155.   }
  156.   
  157.   //eat itself
  158.   if( snakeLength > 2 ){
  159.     for( int i=1; i<snakeLength; i++ ){
  160.       if( snakeHeadX == x[i] && snakeHeadY == y[i] ){
  161.         showGameOver();
  162.         return true;
  163.       }
  164.     }
  165.   }
  166.   
  167.   return false;
  168. }
复制代码

啊对了贪吃蛇的代码是找的这哥们:https://github.com/paper/processing-snake

我就改了改其中控制的代码,把键盘换成了按键 - -
然后再给它加个壳估计手感会不错。


视频由于条件有限- - 就不拍了




啊顺手再来分享一下最近玩的东西。。

老夫前几个月买了个makeymakey,就下面这货:


这货能替代键盘按键,总共有6个开关,默认情况下是对应键盘上的上下左右、空格和Enter,不过可以通过arduino软件来进行修改,可以接任何能导电的东西做控制器。就是必须把人体给接通- - 手上还得再接根线 - - 个人感觉有点麻烦。。
但这货被玩得创意满满- -
详情参阅官网:http://www.makeymakey.com/









您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail