【交作业】手势贪吃蛇

2016-08-182.4万
AI 快速预览详细 收起
本文介绍了一个基于Arduino的手势贪吃蛇项目。所需硬件包括LED点阵(含MAX7219控制芯片)和红外手势传感器。通过连接这些模块到Arduino板子,并安装必要的库文件,可以实现一个通过手势控制的贪吃蛇游戏。项目难度适中,适合初学者和STEM教育场景。


想不想自己做个贪吃蛇玩玩?
顺便锻炼一下整天被鼠标控制着的手腕?
那我们开始吧

首先,我们需要:
LED点阵(含MAX7219控制芯片);
红外手势传感器;
随便一块Arduino板子;
【交作业】手势贪吃蛇图1


接下来,这些模块与arduino的连接:
MAX7219控制的8x8点阵
  Din-D12
  CLK-D11
  CS -D10
红外手势传感器
  SDA -A4
  SCL -A5
  INT -D2


上程序
等等,还需要库文件呢
下载附件LedControl.zip
下载附件SparkFun_APDS9960.zip
嗯。
下载附件LedControl.zip

  1. /*
  2.   name:Snake game made with the Gesture Sensor
  3.   version:  1.0
  4.   Author:  Lin <824676271@qq.com>
  5.   Date:    2016-08-18
  6.   Description:  红外手势贪吃蛇
  7.   note: 首先,把 LedControl 和 SparkFun_APDS9960 的库通通丢进libraries里
  8. */
  9. /*
  10.   接线:
  11.   MAX7219控制的8x8点阵
  12.   Din-D12
  13.   CLK-D11
  14.   CS -D10
  15.   红外手势传感器
  16.   SDA -A4
  17.   SCL -A5
  18.   INT -D2
  19. */
  20. #include <LedControl.h>
  21. #include <Wire.h>
  22. #include <SparkFun_APDS9960.h>
  23. LedControl lc = LedControl(12, 11, 10, 1);
  24. #define APDS9960_INT    2
  25. SparkFun_APDS9960 apds = SparkFun_APDS9960();
  26. int isr_flag = 0;
  27. String direction;
  28. int snakeX[36];
  29. int snakeY[36];
  30. int speed = 1000; //延迟,数值越大贪吃蛇速度会越慢
  31. int snakeSize;
  32. int foodX;
  33. int foodY;
  34. boolean inGame = false;
  35. void setup() {
  36.   attachInterrupt(0, interruptRoutine, FALLING);
  37.   if ( apds.init() ) {
  38.     Serial.println(F("APDS-9960 initialization complete"));
  39.   } else {
  40.     Serial.println(F("Something went wrong during APDS-9960 init!"));
  41.   }
  42.   if ( apds.enableGestureSensor(true) ) {
  43.     Serial.println(F("Gesture sensor is now running"));
  44.   } else {
  45.     Serial.println(F("Something went wrong during gesture sensor init!"));
  46.   }
  47.   lc.shutdown(0, false);
  48.   lc.setIntensity(0, 2); //亮度(0-15),太亮了会伤眼睛哦
  49.   lc.clearDisplay(0); //清屏
  50.   Serial.begin(9600);
  51.   newGame(); //开始新游戏
  52. }
  53. void loop() {
  54.   if (inGame) { //检查是否已经在游戏中
  55.     lc.clearDisplay(0); //清屏
  56.     switch ( apds.readGesture() ) {
  57.       //
  58.       case DIR_UP:
  59.         Serial.println("UP");
  60.         direction = "right";//为什么?因为我想要把屏幕转90度
  61.         break;
  62.       case DIR_DOWN:
  63.         Serial.println("DOWN");
  64.         direction = "left";
  65.         break;
  66.       case DIR_LEFT:
  67.         Serial.println("LEFT");
  68.         direction = "up";
  69.         break;
  70.       case DIR_RIGHT:
  71.         Serial.println("RIGHT");
  72.         direction = "down";
  73.         break;
  74.       default:
  75.         Serial.println("NONE");
  76.     }
  77.     move(direction); //朝挥手的方向移动蛇
  78.     checkIfHitFood(); //检测是否吃到食物
  79.     checkIfHitSelf(); //检查是否吃到自己
  80.     drawSnake(); //在点阵平上画蛇
  81.     drawFood(); //在点阵平上画食物
  82.     if (digitalRead(APDS9960_INT) == 0) {
  83.       apds.init();
  84.       apds.enableGestureSensor(true);
  85.     }
  86.     isr_flag = 0;
  87.     delay(speed); //稍稍延时
  88.   }
  89. }
  90. int simple(int num) {
  91.   return (num * 9 / 1024);
  92. }
  93. void move(String dir) {
  94.   for (int i = snakeSize - 1; i > 0; i--) {
  95.     snakeX = snakeX[i - 1];
  96.     snakeY = snakeY[i - 1];
  97.   }
  98.   if (dir == "up") {
  99.     if (snakeY[0] == 0) {   
  100.       snakeY[0] = 7;
  101.     } else {
  102.       snakeY[0]--;
  103.     }
  104.   } else if (dir == "down") {
  105.     if (snakeY[0] == 7) {      
  106.       snakeY[0] = 0;
  107.     } else {
  108.       snakeY[0]++;
  109.     }
  110.   } else if (dir == "left") {
  111.     if (snakeX[0] == 0) {
  112.       snakeX[0] = 7;
  113.     } else {
  114.       snakeX[0]--;
  115.     }
  116.   } else if (dir == "right") {
  117.     if (snakeX[0] == 7) {
  118.       snakeX[0] = 0;
  119.     } else {
  120.       snakeX[0]++;
  121.     }
  122.   }
  123. }
  124. void drawSnake() {
  125.   for (int i = 0; i < snakeSize; i++) {
  126.     lc.setLed(0, snakeY, snakeX, true);
  127.   }
  128. }
  129. void drawFood() {
  130.   lc.setLed(0, foodY, foodX, true);
  131.   delay(50); //一点小延时让食物区别于蛇身
  132.   lc.setLed(0, foodY, foodX, false);
  133. }
  134. //This method sets a new location of the food randomly.
  135. void newFood() {
  136.   int newFoodX = random(0, 8);
  137.   int newFoodY = random(0, 8);
  138.   while (isSnake(newFoodX, newFoodY)) {
  139.     newFoodX = random(0, 8);
  140.     newFoodY = random(0, 8);
  141.   }
  142.   foodX = newFoodX;
  143.   foodY = newFoodY;
  144. }
  145. void checkIfHitFood() {
  146.   if (snakeX[0] == foodX && snakeY[0] == foodY) {
  147.     snakeSize++;
  148.     newFood();
  149.   }
  150. }
  151. void checkIfHitSelf() {
  152.   for (int i = 1; i < snakeSize - 1; i++) {
  153.     if (snakeX[0] == snakeX && snakeY[0] == snakeY) {
  154.       gameOver(); //Call the gameOver() method.
  155.     }
  156.   }
  157. }
  158. boolean isSnake(int x, int y) {
  159.   for (int i = 0; i < snakeSize - 1; i++) {
  160.     if ((x == snakeX) && (y == snakeY)) {
  161.       return true;
  162.     }
  163.   }
  164.   return false;
  165. }
  166. void newGame() {
  167.   for (int i = 0; i < 36; i++) {
  168.     snakeX = -1;
  169.     snakeY = -1;
  170.   }
  171.   snakeX[0] = 4;
  172.   snakeY[0] = 8;
  173.   direction = "up";
  174.   snakeSize = 1;
  175.   newFood();
  176.   inGame = true;
  177.   }
  178. void interruptRoutine() {
  179.   isr_flag = 1;
  180. }
  181. void gameOver() {
  182.   inGame = false;
  183.   for (int x = 0; x < 8; x++) {
  184.     for (int y = 0; y < 8; y++) {
  185.       lc.setLed(0, y, x, true);
  186.       delay(20);
  187.       lc.setLed(0, y, x, false);
  188.     }
  189.   }
  190.   newGame();
  191. }
复制代码




效果:
【交作业】手势贪吃蛇图5
诶,确实能玩但是拍不出效果啊


后续我还会3D打印一个盒子,做成便携式的贪吃蛇~






创作许可协议

本项目采用 None(不开放任何权利,保留所有权利) 进行许可。

评论(7)
0kn_wm3_
0kn_wm3_
这是怎么回事
rui983003440
rui983003440
楼主,我是一名在校学生,看了你的创作 非常感兴趣 ,可否在空余时间 回复一下 有一些不清楚的地方想请教一下。 不甚感激!!(其他懂得 人 也可以帮一下忙多谢 多谢)
吴人奔越
吴人奔越
之前也做过一个红外线操作的贪吃蛇游戏,但是发现每次拍视频的时候,红外线都会实效。。
凌风清羽
凌风清羽
录制个视频呗,哈哈~~~~~
hnyzcj
hnyzcj
估计操作有难度
hnyzcj
hnyzcj回复hnyzcj
这游戏对鼠标手有治愈系作用
luna
luna
拍个简单的视频看看效果,好奇怎么用手势玩滴
hnyzcj
hnyzcj回复luna
因为我猜的,哈哈
lin
lin
作者
@luna 已完成第一个项目
luna
luna回复lin
吃瓜群众来围观~~你是不是要3天完成3个idea啊
- 没有更多了 -