#include
#include
#include
#include
#include
#include
#include
#define OK 1
#define ERROR 0
#define STACK_INIT_SIZE 10// 存储空间初始分配量
#define STACKINCREMENT 1// 存储空间分配增量
typedef char SElemType; // 定义栈元素类型
typedef int Status; // Status是函数的类型,其值是函数结果状态代码,如OK等
IMAGE Background;
IMAGE Foreground;
IMAGE Restart;
IMAGE Rest;
time_t start, end;
int loss;
const int Width = 556;
const int Height = 720;
char Alice[30] = { "ALICE SYNTHESIS THIRTY" };
char word[10][10] = { "include","main","return","void","switch","case","struct","typedef","fopen" };
struct TARGET
{
float x;
float y;
char *word;
}Target[3];
void image();
void site();
void show();
void run();
void get();
void Reset();
void AliceError();
void BackSpace();
void ClearText();
void InitWord(int n);
char text[10] = { '\0' };
struct SqStack
{
SElemType *base; // 在栈构造以前和销毁以后,base的值为NULL
SElemType *top; // 栈顶指针
int stacksize; // 当前已分配的存储空间,以元素为单位
}S; // 顺序栈
Status InitStack()
{
// 构造一个空栈S,该栈预约义大小为STACK_INIT_SIZE
S.base = (SElemType*)malloc(STACK_INIT_SIZE * sizeof(SElemType));
if (!S.base) return ERROR;
S.top = S.base;
S.stacksize = STACK_INIT_SIZE;
return OK;
}
Status Push(SElemType e)
{
// 在栈S中插入元素e为新的栈顶元素
if (S.top - S.base >= S.stacksize)
{
S.base = (SElemType*)realloc(S.base, (S.stacksize + STACKINCREMENT) * sizeof(SElemType));
if (!S.base) return ERROR;
S.top = S.base + S.stacksize;
S.stacksize += STACKINCREMENT;
}
*S.top++ = e;
return OK;
}
Status Pop(SElemType &e)
{
// 若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;不然返回ERROR
if (S.top == S.base) return ERROR;
e = *--S.top;
return OK;
}
Status ClearStack()
{
// 把S置为空栈
S.top = S.base;
return OK;
}
int StackLength()
{
// 返回栈S的元素个数
int i;
i = S.top - S.base;
return i;
}
void LineEdit()
{
int n;
SElemType e;
SElemType *p = (SElemType *)malloc(sizeof(SElemType));
if (_kbhit())
{
e = _getch();
if (e == 32)
{
putimage(0, 0, &Rest);
FlushBatchDraw();
char stop = _getch();
if (stop == 27)
exit(NULL);
}
else if (e == 8)
{
Pop(e);
BackSpace();
}
else if (e >= 97 && e <= 122 && StackLength() < 10)
{
Push(e);
}
}
p = S.base;
for (n = 0; n < StackLength(); n++)
{
text[n] = *p++;
}
for (n = 0; n < 3; n++)
{
if (strcmp(text, Target[n].word) == 0)
{
ClearStack();
ClearText();
InitWord(n);
}
}
}
int main() // 主函数
{
initgraph(Width, Height);
image();
BeginBatchDraw();
/*开始界面*/
site();
while (true)
{
while (difftime(end, start) < 60000)
{
show();
run();
get();
/*开始界面*/
FlushBatchDraw();
end = clock();
}
Reset();
}
EndBatchDraw();
closegraph();
}
void image()
{
loadimage(&Background, _T("D:\\暂存Ⅱ\\C project demo Ⅱ\\Type_Defend\\Texture\\Background.png"));
loadimage(&Foreground, _T("D:\\暂存Ⅱ\\C project demo Ⅱ\\Type_Defend\\Texture\\Foreground.png"));
loadimage(&Restart, _T("D:\\暂存Ⅱ\\C project demo Ⅱ\\Type_Defend\\Texture\\Restart.png"));
loadimage(&Rest, _T("D:\\暂存Ⅱ\\C project demo Ⅱ\\Type_Defend\\Texture\\Rest.png"));
}
void site()
{
start = clock();
InitStack();
loss = 0;
int n;
for (n = 0; n < 3; n++)
{
InitWord(n);
Target[n].y -= n * 30;
}
}
void show()
{
putimage(0, 0, &Background);
int num;
for (num = 0; num < 3; num++)
{
setbkmode(TRANSPARENT);
settextcolor(RGB(200, 150, 150));
settextstyle(30, 0, _T("Verdana"));
outtextxy((int)Target[num].x, (int)Target[num].y, Target[num].word);
}
putimage(0, 0, &Foreground);
setbkmode(TRANSPARENT);
settextcolor(RGB(200, 150, 150));
settextstyle(30, 0, _T("Verdana"));
outtextxy(100, 240, Alice);
setlinecolor(RGB(200, 150, 150));
setfillcolor(RGB(200, 150, 150));
fillroundrect(30, 270, 530, 275, 5, 5);
setbkmode(TRANSPARENT);
settextcolor(RGB(200, 150, 150));
settextstyle(21, 0, _T("Verdana"));
outtextxy(60, 670, text);
TCHAR TIME[10];
TCHAR LOSS[10];
_stprintf_s(LOSS, _T("%2d"), loss);
_stprintf_s(TIME, _T("%2lld"), (60000 - (end - start)) / 1000);
outtextxy(420, 600, "TIME");
outtextxy(420, 635, "LOSS");
outtextxy(480, 600, TIME);
outtextxy(480, 635, LOSS);
static int fps = 0;
if (fps < 30)
{
fillrectangle(65 + textwidth(text), 670, 70 + textwidth(text), 690);
}
else if (fps > 60)
{
fps = 0;
}
fps += 1;
}
void run()
{
AliceError();
int n;
for (n = 0; n < 3; n++)
{
Target[n].y += (float)1.4;
if (Target[n].y > 590)
{
InitWord(n);
loss += 1;
}
}
}
void get()
{
LineEdit();
}
void Reset()
{
putimage(0, 0, &Restart);
FlushBatchDraw();
char input;
do
{
input = _getch();
} while (input != 32 && input != 27);
if (input == 32)
{
site();
}
else if (input == 27)
{
exit(NULL);
}
}
void AliceError()
{
static time_t Alice_start = clock(); time_t Alice_end = clock();
if ((difftime(Alice_end, Alice_start)) >= 30)
{
int n = rand() % 15 + 6;
while (n == 15)
n = rand() % 15 + 6;
Alice[n] = rand() % 26 + 65;
Alice_start = clock();
}
}
void BackSpace()
{
int n;
for (n = 0; text[n + 1] != '\0'; n++)
continue;
text[n] = '\0';
}
void ClearText()
{
int n;
for (n = 0; text[n] != '\0'; n++)
text[n] = '\0';
}
void InitWord(int n)
{
srand((unsigned int)time(NULL));
Target[n].word = word[rand() % 10];
while (Target[n].word == Target[(n + 1) % 3].word || Target[n].word == Target[(n + 2) % 3].word)
Target[n].word = word[rand() % 10];
Target[n].x = (float)(rand() % 300 + 100);
Target[n].y = (float)(-n * 30 + 240);
}