文章目录
- 一、主要功能
- 二、软件设计
- 三、实验现象
- 联系作者
一、主要功能
本项目使用STM32F103/F407单片机控制器,TFTLCD触摸屏、按键等。
主要功能:
系统运行后,TFTLCD显示游戏界面,可按下KEY_UP键进入游戏;
系统内置3关游戏地图,默认从第1关开始,通过KEY_UP、KEY1、KEY2和KEY0键控制游戏上下左右方向;当玩家到达目的地,游戏通关,可按下KEY_UP键进入下一关
二、软件设计
/*
作者:嗨小易(技术QQ群:570487280)
*/
/*
作者:嗨小易(QQVIP群2:570487280)
B站:https://space.bilibili.com/444388619/video
版权:本教程版权归嗨小易所有,未经授权不得转载、摘编或用于其他商业用途!!!
一经发现,后果自负!!!
*/
#include "appdemo.h"
#include "SysTick.h"
#include "led.h"
#include "beep.h"
#include "key.h"
#include "usart.h"
#include "tftlcd.h"
#include "picture.h"
#include "wall25X25.h"
#include "wanjia.h"
//定义结构体变量
_sys_ctrl sys_ctrl;
//关卡数
#define LEVEL 3
//迷宫长宽定义
#define ROW 10 //行
#define COL 10 //列
//游戏元素大小
#define X_SIZE 25
#define Y_SIZE 25
//游戏开始界面位置
#define START_X (tftlcd_data.width-X_SIZE*ROW)/2
#define START_Y (tftlcd_data.height-Y_SIZE*COL)/2
//迷宫数组 墙壁=0 路=1 角色=2,目的地3
int map[LEVEL][ROW][COL] =
{
//第1关
{
{0,0,0,0,0,0,0,0,0,0},
{0,1,0,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,0,1,0},
{0,2,0,0,0,0,0,0,1,3},
{0,1,0,0,0,0,0,0,1,0},
{0,1,1,1,1,1,1,0,1,0},
{0,1,1,0,0,0,1,0,1,0},
{0,1,1,0,0,0,1,1,1,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0}
},
//第2关
{
{0,0,0,0,0,0,0,0,0,0},
{0,1,1,1,0,1,1,1,1,0},
{0,1,0,1,0,1,0,0,1,0},
{2,1,0,1,0,1,0,0,1,0},
{0,1,0,0,0,1,0,0,1,0},
{0,1,0,1,1,1,1,0,1,0},
{0,1,1,1,0,0,1,0,0,0},
{0,0,0,1,0,0,1,1,1,0},
{0,1,1,1,0,0,0,0,1,3},
{0,0,0,0,0,0,0,0,0,0}
},
//第3关
{
{0,0,0,0,0,3,0,0,0,0},
{0,1,1,1,0,1,1,1,1,0},
{0,1,0,1,0,0,1,0,1,0},
{0,1,0,1,1,1,0,1,1,0},
{0,1,0,0,0,1,1,1,0,0},
{0,1,1,1,0,1,0,1,1,0},
{0,1,0,1,1,1,1,0,1,0},
{2,1,1,0,1,0,1,1,1,0},
{0,0,1,1,1,1,0,0,1,0},
{0,0,0,0,0,0,0,0,0,0}
},
};
//定义游戏元素
enum element
{
WALL, //墙
SPACE, //空地
PLAYER, //玩家
DEST //目的地
}ele;
u8 game_over=0;
u8 game_level=1;
//系统参数初始化
void sys_parm_init(void)
{
sys_ctrl.cnt=0;
sys_ctrl.dir=0;
}
//系统开机界面显示
void sys_open_show(void)
{
FRONT_COLOR=BLACK;
LCD_ShowFontHZ_Ex((tftlcd_data.width-4*32)/2,10,"迷宫游戏",22);
LCD_ShowPicture((tftlcd_data.width-240)/2,(tftlcd_data.height-240)/2,240,240,(u8 *)gImage_picture);
while(1)
{
if(KEY_Scan(0)==KEY_UP_PRESS)
{
LCD_Clear(BACK_COLOR);
break;
}
}
}
//游戏界面显示
void game_show(void)
{
u8 i=0,k=0;
FRONT_COLOR=BLACK;
LCD_ShowFontHZ_Ex((tftlcd_data.width-4*32)/2,10,"迷宫游戏",22);
LCD_ShowString(100,40,100,100,24,"Level:");
LCD_ShowNum(100+6*12,40,game_level,1,24);
//绘制界面,墙、路、玩家
for(i=0;i<ROW;i++)
{
for(k=0;k<COL;k++)
{
if(map[game_level-1][i][k]==WALL)//绘制墙
{
LCD_ShowPicture(START_X+k*X_SIZE,START_Y+i*Y_SIZE,X_SIZE,Y_SIZE,(u8 *)gImage_wall25X25);
}
else if(map[game_level-1][i][k]==PLAYER)//绘制玩家
{
LCD_ShowPicture(START_X+k*X_SIZE,START_Y+(i)*Y_SIZE,X_SIZE,Y_SIZE,(u8 *)gImage_wanjia);
}
}
}
}
//应用程序
void app_demo(void)
{
BEEP_Init();
KEY_Init();
LED_Init();
TFTLCD_Init();
sys_parm_init();//系统参数初始化
sys_open_show();//系统开机界面显示
game_show();//游戏界面显示
while(1)
{
game_ctrl();//游戏控制
}
}
三、实验现象
B站演示视频:https://space.bilibili.com/444388619
联系作者
B站演示视频:https://space.bilibili.com/444388619
专注于51单片机、STM32、国产32、DSP、Proteus、ardunio、ESP32、物联网软件开发,PCB设计,视频分享,技术交流。