#include <U8g2lib.h>
#include <Wire.h>
//U8G2_SSD1306_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE);
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
int fangxiang =3;
int shetoux =0;
int shetouy =8;
//128/4=32 64/4=16 512
byte shentix[100];
byte shentiy[100];
int changdu=18;
void setup(){
u8g2.setI2CAddress(0x3C*2);
u8g2.begin();
Serial.begin(9600);
u8g2.clearBuffer();
pinMode(5, INPUT);
pinMode(4, INPUT);
pinMode(3, INPUT);
pinMode(2, INPUT);
pinMode(13, OUTPUT);
chushihua();//初始化 未完成
}
void loop(){
//page1();
for(int a=2;a<=5;a++){
if(digitalRead(a)==1){
Serial.print(a);
fangxiang=a;
Serial.print(" ");
Serial.println(fangxiang);
}
}
yidongfx();//移动方向 未完成
}
void yidongfx() { //移动方向 未完成
for (int a=changdu-1;a>0;a--){
//身体跟前进
shentix[a]=shentix[a-1];
shentiy[a]=shentiy[a-1];
}
shentix[0]=shetoux;
shentiy[0]=shetouy;
switch(fangxiang){
//头移动
case 5 :
shetoux--;
//Serial.print("5 ok");
break;
case 4 :
shetouy--;
break;
case 3 :
shetoux++;
break;
case 2 :
shetouy++;
break;
}
if(shetoux>=32){shetoux=0;}
if(shetoux<0){shetoux=31;}
if(shetouy>=16){shetouy=0;}
if(shetouy<0){shetouy=15;}
panding();//碰撞判断
xianshi();//显示
}
void panding(){//判定 未完成
for(int a=0;a<changdu;a++){
if(shetoux==shentix[a]){
if(shetouy==shentiy[a]){
Serial.print(shetoux);
Serial.print(shetouy);
Serial.print(shentix[a]);
Serial.print(shentiy[a]);
jieshu();
}
}
}
}
void yidong() {//移动 未完成
}
void xianshi() { //显示 未完成
u8g2.clearBuffer();//清屏
for(int a=0;a<changdu;a++) {
u8g2.drawBox(shentix[a]*4,shentiy[a]*4,4,4);
}
u8g2.drawBox(shetoux*4,shetouy*4,4,4);//方块蛇头
u8g2.sendBuffer();//显示
delay(100);
}
void chushihua(){//初始化
shentix[0]=2;
shentiy[0]=8;
shentix[1]=1;
shentiy[1]=8;
shentix[2]=0;
shentiy[2]=8;
shetoux=3;
shetouy=8;
}
void jieshu() {
Serial.println("游戏结束");
fangxiang=0;
}
void page1() { //绘图参考
u8g2.clearBuffer();//清屏
u8g2.drawPixel(10,10);//画点
u8g2.drawLine(0,10,10,10);//画直线
u8g2.drawFrame(32,0,5,5);//空心方块
u8g2.drawBox(0,0,5,5);//实心方块
u8g2.drawCircle(32,32,5);//空心圆
u8g2.drawDisc(50,32,5);//实心圆
u8g2.setFont(u8g2_font_ncenB10_tr);
u8g2.drawStr(0,40,"test");
u8g2.sendBuffer();//显示
delay(1000);
}