手势传感器试用+【我也来一个贪吃蛇】
看了大神们拿Gesture传感器发挥想象实现各种控制,我也来凑凑热闹~~小小贪吃蛇~~~~~
元件清单
Arduino Uno x1
RGB & Gesture Sensor x1
I/O Expansion x1
SS1306 OLEDx1
连线方式
RGB&Gesture Sensor
SDA-> SDA
SCL->SCL
GND ->GND
VCC ->VCC
INT ->D2
SS1306 OLED
MOSI-> D9
SCK - > D10
DC - > D11
CS - > D12
RES -> D13
http://v.youku.com/v_show/id_XMTcwMDMyMDQ2NA==.html
代码如下:#include "U8glib.h"
#include <Wire.h>
#include <SparkFun_APDS9960.h>
// Pins
#define APDS9960_INT 2 // Needs to be an interrupt pin
// Constants
// Global Variables
SparkFun_APDS9960 apds = SparkFun_APDS9960();
int isr_flag = 0;
#define RIGHT 0
#define UP 1
#define LEFT2
#define DOWN3
/*
SW SPI COM:
SCK = 10, MOSI = 9 , CS = 12, A0 = 11, RES = 13
*/
U8GLIB_SSD1306_128X64 u8g(10, 9, 12, 11, 13);
int score = 0;
int level = 1;
int gamespeed = 100;
int i;
//组成单位,4X4像素点的方块。
const uint8_t ele[] PROGMEM = {
0xf0, //B1111000
0xb0, //B1011000
0xd0, //B1101000
0xf0, //B1111000
};
//蛇
void element(int x, int y) {
u8g.drawBitmapP(x,y, 1,4, ele);
}
struct FOOD {
int x;
int y;
int yes;
};
FOOD food = {25, 30, 1};
struct SNAKE {
int x;
int y;
int node;
int dir;
int lefe;
};
SNAKE snake = {{9,5}, {30,30}, 2, RIGHT, 0};
//游戏基本界面
void UI() {
u8g.drawFrame(0,1, 102,62); //内边界
u8g.drawFrame(0,0, 102,64); //外边界
u8g.setFont(u8g_font_5x7); //设置字体
u8g.drawStr(104,12, "LEVEL"); //等级提示
u8g.drawStr(104,40, "SCORE"); //分数提示
}
void printScore(int x, int y, int s) {
u8g.setFont(u8g_font_6x10);
u8g.setPrintPos(x, y);
u8g.print(s);
}
void key() {
if ( apds.isGestureAvailable() ) {
switch ( apds.readGesture() ) {
case DIR_UP:
snake.dir = UP;
Serial.println("UP");
break;
case DIR_DOWN:
snake.dir = DOWN;
Serial.println("DOWN");
break;
case DIR_LEFT:
Serial.println("LEFT");
break;
case DIR_RIGHT:
snake.dir = LEFT;
snake.dir = RIGHT;
Serial.println("RIGHT");
break;
case DIR_NEAR:
Serial.println("NEAR");
break;
case DIR_FAR:
Serial.println("FAR");
break;
default:
Serial.println("NONE");
}
}
}
void snakeGame() {
switch(snake.dir) {
case RIGHT:
snake.x += 4;
if(snake.x>=101) {
snake.x = 1;
} break;
case UP:
snake.y -= 4;
if(snake.y<=1) {
snake.y = 58;
} break;
case LEFT:
snake.x -= 4;
if(snake.x<=0) {
snake.x = 97;
} break;
case DOWN:
snake.y += 4;
if(snake.y>=62) {
snake.y = 2;
} break;
}
if((snake.x == food.x) && (snake.y == food.y)) {
snake.x = food.x;
snake.y = food.y;
snake.node++;
food.yes = 1;
score += 2;
level = score/10+1;
}
for(i=snake.node-1;i>0;i--) {
snake.x = snake.x;
snake.y = snake.y;
}
}
void setup() {
// Initialize Serial port
Serial.begin(9600);
Serial.println();
//Serial.println(F("--------------------------------"));
//Serial.println(F("SparkFun APDS-9960 - GestureTest"));
//Serial.println(F("--------------------------------"));
// Initialize interrupt service routine
attachInterrupt(0, interruptRoutine, FALLING);
// Initialize APDS-9960 (configure I2C and initial values)
if ( apds.init() ) {
Serial.println(F("APDS-9960 initialization complete"));
} else {
Serial.println(F("Something went wrong during APDS-9960 init!"));
}
// Start running the APDS-9960 gesture sensor engine
if ( apds.enableGestureSensor(true) ) {
Serial.println(F("Gesture sensor is now running"));
} else {
Serial.println(F("Something went wrong during gesture sensor init!"));
}
}
void interruptRoutine() {
isr_flag = 1;
}
void loop() {
if( isr_flag == 1 ) {
key();
if(digitalRead(APDS9960_INT) == 0){
apds.init();
apds.enableGestureSensor(true);
}
isr_flag = 0;
}
u8g.firstPage();
do {
UI();
for(i=0; i<snake.node;i++) {
element(snake.x, snake.y);
}
element(food.x, food.y);
printScore(103, 62, food.x);
printScore(116, 62, food.y);
printScore(109, 22, level);
printScore(109, 50, score);
}while(u8g.nextPage());
key();
snakeGame();
delay(gamespeed);
}用到的库:
凌风清羽 发表于 2017-1-8 23:55
有问题请留言,尽量回啊~~
大神 , u8glib.h库文件可以加点注释给我看看么 本人 菜鸟一枚 想理解一下 那些代码。。麻烦大神 指教一二 !{:5_169:} rui983003440 发表于 2017-1-3 16:00
楼主,我是一名在校学生,看了你的创作 非常感兴趣,可否在空余时间 回复一下有一些不清楚的地方想请教 ...
有问题请留言,尽量回啊~~{:5_195:} 深海的 发表于 2016-12-28 20:51
求助,求助:完全按照楼主的步骤走的啊,可是加了APDS 9960手势传感器后,开局蛇就不动,僵死在屏上;不加 ...
当初这边帖子写的比较仓促,改天我再review一下
food目前位置还不能移动哈,后续会改进~~~~~ 玩起来好累 看着都累啊 大连林海 发表于 2016-8-26 13:00
看着都累啊
反应不灵敏啊,这个我也没办法a ~~~~~ dsweiliang 发表于 2016-8-26 12:01
玩起来好累
反应不灵敏啊,这个我也没办法a ~~~~~ 哇塞,终于看到视频了,视屏外的我能感受到楼主的手迷之焦虑 啊哈哈,这个创意不错的!!!
luna 发表于 2016-8-26 15:32
哇塞,终于看到视频了,视屏外的我能感受到楼主的手迷之焦虑
感觉手不听使唤了,玩久了估计能找到feel,哈哈~~ swanglei 发表于 2016-8-26 16:04
啊哈哈,这个创意不错的!!!
抄袭来的创意~~嘿嘿~~{:5_162:} swanglei 发表于 2016-8-26 16:04
啊哈哈,这个创意不错的!!!
lin用点阵屏做了个贪吃蛇~ 凌风清羽 发表于 2016-8-26 16:43
抄袭来的创意~~嘿嘿~~
能做出来也很吊~ swanglei 发表于 2016-8-26 17:08
能做出来也很吊~
https://mc.dfrobot.com.cn/forum.php?mod=viewthread&tid=15976&highlight=%E8%B4%AA%E5%90%83%E8%9B%87 凌风清羽 发表于 2016-8-26 17:16
https://mc.dfrobot.com.cn/forum.php?mod=viewthread&tid=15976&highlight=%E8%B4%AA%E5%90%8 ...
不错不错 第一个,赞!
不妨做2层楼,1,2楼间手乱动。
楼顶是led屏。 看着手累。。。所以我就不录视频了,玩起来也是一样的
不过,你做的界面好高级啊
看着都累。。。 做出来也是很厉害的了{:5_179:} 我靠 大神 我很好奇这个显示模块叫啥 哈哈哈哈这个操作很有难度,需要提前预判{:5_196:}
页:
[1]
2