本帖最后由 KARKEY 于 2016-10-23 20:25 编辑
平常都是看程序多...自己倒没动手写过..想练习写个mpu6050 控制方向的贪吃蛇...不过6050 三个角的含意 及数据分析 还不太会...就先用红外写个贪吃蛇把....
然后体会到原子哥说的 还是要多多自己写程序..... 很简单的贪吃蛇程序 ...写了大概5 6个小时 然后编出来虽然bug没几个 但放到板子上跑 就问题不断...然后花了2个小时才调好...基本实现了贪吃蛇...................
LCD 是2.8寸的 基于精英版跑的
算法方面...只考虑实现 所以没多考虑优化的问题...也没用ucos系统跑 觉得麻烦....调程序也是碰到bug 就修补 没考虑整体再优化......
参考了这个
http://www.stmcu.org/module/forum/thread-583680-1-1.html
随机点的生成直接用 捕获红外的TIME4 TIM4->CNT 作为随机点的生成
一直都是从别人分享的东西学习...
虽然水平很差....程序也写的很烂...不过还是拿出来把...一是记录成长..二是总归有需要参考的人,无论正面参考还是负面把.......
#include "snake.h"
#include "key.h"
#include "stdio.h"
#include <stdlib.h>
#include <time.h>
//************************************************
//snkae game code
//code by KARKEY
//date 23/10/2016
//************************************************
#define SnakeUp 2
#define SnakeDown 8
#define SnakeLeft 4
#define SnakeRight 6
#define SnakeOk 5
#define Snakebeginlen 4
u16 snake[500]; //sanke xy high 8bit x low 8bit y
u16 snakelen=Snakebeginlen; //snake len
u16 snakemeat;
int lcd_discolor[9]={ BLACK, BLUE, BRED,
GRED, GBLUE, RED,
GREEN, YELLOW,BRRED};
//LCD block 240*320=30*40* (8*8)
// Draw 8*8 point
void snake_DrawPoint (u16 x,u16 y)
{
u16 x1,x2,y1,y2;
x=x>>8;
x1=8*x;y1=8*y;
x2=x1+7;y2=y1+7;
LCD_Fill(x1,y1,x2,y2,POINT_COLOR);
}
//get drc
u8 snake_getdrc(void)
{
u8 snakedrc=0xff;
snakedrc=Remote_Scan();
switch(snakedrc)
{
case 152: snakedrc=SnakeUp;break;
case 48: snakedrc=SnakeLeft;break;
case 122: snakedrc=SnakeRight;break;
case 56: snakedrc=SnakeDown;break;
//case 24: snakedrc=SnakeOk;break;
default: snakedrc=0; break;
}
return(snakedrc);
}
void snake_init(void)
{ u16 i,x,y;
snakelen=Snakebeginlen; //初始化 蛇长4
LCD_ShowNum(5,20,snakelen,3,16);
for (i=0;i<500;i++) {snake
=0;}
snake[0]=0x0f16;
snake[1]=0x0f15;
snake[2]=0x0f14;
snake[3]=0x0f13;
for(i=0;i<Snakebeginlen;i++)
{ //x=snake&0xff00
snake_DrawPoint(snake&0xff00,snake&0x00ff);
} //显示初始化蛇
snakemeat=0x0718;
snake_DrawPoint(snakemeat&0xff00,snakemeat&0x00ff); //显示蛇肉
// srand(16515);
}
// set rand meat xy
//肉被吃 会被调用 擦除之前的肉 并产生新肉
void snake_randmeat(void)
{ s32 randt;u32 t;
u16 x,y,temp;
x=snakemeat&0xff00;y=snakemeat&0x00ff;
temp=POINT_COLOR;
POINT_COLOR=WHITE;
snake_DrawPoint(x,y);
POINT_COLOR=temp;
t= TIM4->CNT; // 蛇肉被吃 清楚被吃的点 RAND产生新的蛇肉并显示
snakemeat=(u16)(t%30);
snakemeat=(u16)(t%40)+(snakemeat<<8); //TIM4->CNT rand
x=snakemeat&0xff00;y=snakemeat&0x00ff;
snake_DrawPoint(x,y);
}
//update snake[]
u8 drc=SnakeDown; //4 drc now drc 默认方向下
u16 pointclear;
u8 snake_move(void)
{
u8 temp;
u16 i=0;
temp=snake_getdrc();
if(temp) {if((drc!=temp)&&(drc*temp!=24)&&(drc*temp!=16)) drc=temp; } //update drc
pointclear=snake[snakelen-1]; // this point will clear
for(i=snakelen-1;i>0;i--)
{ snake=snake[i-1]; } //update snake body
switch(drc){ case SnakeUp:{i=snake[0]&0x00ff;if(i==0x0000) return 0;else snake[0]--;}break;
case SnakeDown:{i=snake[0]&0x00ff;if(i==0x0027) return 0;else snake[0]++;}break;
case SnakeLeft:{i=snake[0]&0xff00;if(i==0x0000) return 0;else snake[0]-=0x0100;}break;
case SnakeRight:{i=snake[0]&0xff00;if(i==0x1d00) return 0; else snake[0]+=0x0100;}break;
default: snake[0]++;break;
} //update snake head && cheak area
if(snake[0]==snakemeat) {snakelen++;snake[snakelen-1]=snake[snakelen-2];
POINT_COLOR=lcd_discolor[snakelen%9];
snake_randmeat(); } //eat meat
else {
for(i=1;i<snakelen;i++)
{if(snake[0]==snake) break;}
if (i<snakelen) return 0; //eat body
}
return 1;
//else update done game go on
}
void snake_show(void)
{ u16 i=0,x,y;
x=pointclear&0xff00;y=pointclear&0x00ff;
i=POINT_COLOR;
POINT_COLOR=WHITE; //clear point
snake_DrawPoint(x,y);
POINT_COLOR=i;
for(i=0;i<snakelen;i++)
{ x=snake&0xff00;y=snake&0x00ff; //show snake
snake_DrawPoint(x,y);
}
i=POINT_COLOROINT_COLOR=BLACK;
LCD_ShowNum(5,20,snakelen,3,16); // show len
POINT_COLOR=i;
}
void snake_startgame(void)
{ u8 i=0xff,snakespeed=20,key,j=0;
LCD_Clear(WHITE);
POINT_COLOR=RED; //设置字体为红 {MOD}
snake_init();
while(1)
{
delay_ms(10);
j++;
key=KEY_Scan(0);
if(key==KEY0_PRES) snakespeed+=10;
else if(key==KEY1_PRES) snakespeed-=10;
if(j>snakespeed) //every speed*10ms update moren 500ms K0 up 100ms k1 down 100ms
{
i=snake_move() ; if(i==0) snake_gameover();
snake_show();
j=0;
}
}
}
void snake_gameover(void)
{ u8 i=0,sta=1;
LCD_Clear(WHITE);
POINT_COLOR=RED;
//LCD_ShowString(70,140,200,16,24,"GAMEOVER");
LCD_ShowString(100,140,200,16,16,"SCORE:");
POINT_COLOR=BLACK;
LCD_ShowNum(160,140,snakelen,3,16);
while(sta)
{i=KEY_Scan(0); //scan inculde 10MS
if(i==WKUP_PRES) sta=0;
} //deng dai kaishi
snake_startgame();
}
一周热门 更多>