本源代码是基于STM32F4xx硬件平台设计的贪吃蛇小游戏,主要难点在:随机点产生、贪吃蛇转向、贪吃蛇贪吃点;本部分主要接收产生随机点,产生随机点需要注意两个方面:1、随机点在有效的范围内;2、贪吃点与贪吃蛇不重合。产生随机点主要有两个函数,分别如下://随机数产生任务void rng_chansheng(void *p_arg){OS_ERR err;while(1){OSSemPend(&RNG_SEM,0,OS_OPT_PEND_BLOCKING,0,&err);zou.x = RNG_Get_RandomRange(0,50)*8 + 40;zou.y = RNG_Get_RandomRange(0,50)*8 + 260;lcd_fangkuan(zou.x,zou.y,zou.x+SHE_FAANGKUAN_SIZE,zou.y+SHE_FAANGKUAN_SIZE);OSTimeDlyHMSM(0,0,0,500,OS_OPT_TIME_HMSM_STRICT,&err); //延时500ms}}//往下方向画一个实心的正方形,代表贪食蛇的一段void lcd_fangkuan(u16 x1,u16 y1,u16 x2 ,u16 y2){u16 i,j;u16 xx,yy;if(((x2 - x1) != SHE_FAANGKUAN_SIZE)||((y2 - y1) != SHE_FAANGKUAN_SIZE))return ;if(x1 > x2) {xx = x1;x1 = x2;x2 = xx;}if(y1 > y2){yy = y1;y1 = y2;y2 = yy;}if((y1 < 260)|| (y2 > 660)||(x1 < 40)||(x2 > 448)){game_yun_error = 1;LCD_ShowString(150,300,500,24,24,"GAME OVER!!");return ;}for(i=x1; i<x2; i++){for(j=y1; j<y2; j++){LCD_DrawPoint(i,j);}}}
标签:
stm32
ucosiii
贪吃蛇游戏
上传时间:
2022-08-10
上传用户: