This repository has been archived on 2024-01-06. You can view files and clone it, but cannot push or open issues or pull requests.
justhomework/MCU/MCU_REAL _FINAL/menu.c
2022-06-06 17:40:49 +08:00

159 lines
2.5 KiB
C

#include "menu.h"
#include "display.h"
#include "delay.h"
#include "draw.h"
#include <stdio.h>
#include <stdlib.h>
#include "block.h"
//便于调试的预编译命令
#define DEBUG_MODE
#ifdef DEBUG_MODE
#define M_DEBUG M_BLOCK
#endif
Menu M_MAINMENU;//主菜单
Menu M_BLOCK; //俄罗斯方块
Menu M_BALL; //弹球
Menu M_STARTUP;
Menu* NOW; //当前菜单指针
Menu* LAST = NULL; //上一个状态的菜单
enum OPR opr = idle;
static unsigned char local[10] = {0};
extern unsigned char score=0;
//初始化显示
void m_startup(struct _menu* this)
{
if(LAST != &M_STARTUP)
{
LAST = &M_STARTUP;
drawNAME(); //绘制名字
delay(1000);
drawNUMBER(); //绘制学号
}
if(opr == right)
{
opr = idle;
NOW = this->n; //转向下一菜单
clear();
}
}
//主菜单
void m_mainmenu(struct _menu* this)
{
if(LAST != &M_MAINMENU)
{
LAST = &M_MAINMENU;
drawMAINMENU(local[0]);
}
if(opr == right)
{
local[0]++;
if(local[0] > 3)
local[0] = 3;
opr = idle;
drawMAINMENU(local[0]);
}
if(opr == left)
{
if(local[0] != 0)
local[0]--;
opr = idle;
drawMAINMENU(local[0]);
}
if(opr == confirm)
{
switch(local[0])
{
case 0:
return;
case 1:
NOW = &M_BLOCK; //转向下一菜单
break;
case 2:
NOW = &M_BALL; //转向下一菜单
break;
case 3:
NOW = &M_STARTUP; //转向下一菜单
break;
}
local[0] = 0;
clear();
opr = idle;
}
}
//方块游戏
void m_block(struct _menu* this)
{
if(LAST != &M_BLOCK)
{
LAST = &M_BLOCK;
drawVerticalDottedLine(64);
drawBLOCKSCORE();
blockInit();
}
//重置
if(opr == confirm)
{
opr = idle;
blockDestroy();
drawBlock();
return;
}
if(opr == left)
{
opr = idle;
moveLeftPiece();
dropPiece();
drawBlock();
return;
}
if(opr == right)
{
opr = idle;
moveRightPiece();
dropPiece();
drawBlock();
return;
}
drawSUPNUM(110, 0, score);
genPiece(); //若有需要,生成新块
dropPiece(); //若有需要,块下落
judgeBlock(); //若有需要,清除一行
drawBlock(); //绘制界面
delay(200);
}
void menuInit()
{
M_STARTUP.n = &M_MAINMENU;
M_STARTUP.f = m_startup;
M_MAINMENU.f = m_mainmenu;
M_BLOCK.f = m_block;
#ifdef DEBUG_MODE
NOW = &M_DEBUG;
#else
NOW = &M_STARTUP;
#endif
}