#include <stdio.h>
#include <graphics.h>
#include <windows.h>
#include <mmsystem.h>
#include <time.h>
#include <conio.h>
#include <stdlib.h>
#pragma comment (lib,"winmm.lib")
constexpr auto WINDOWSLENGTH = 640;
constexpr auto WINDOWSHIGHT = 480+20;
IMAGE background, apple1, apple2;
//字母结构体
typedef struct Letter
{
//数据域
int x, y;//字母坐标
char ch;//字母ASCII码
//指针域;
struct Letter* pnext = NULL;
}LETTER;
typedef LETTER* Pt_letter;
//初始化函数
char str[10];
void GameInit()
{
srand((unsigned int)time(NULL));//随机数种子
//初始化窗口
initgraph(WINDOWSLENGTH, WINDOWSHIGHT);
//初始化背景音乐
mciSendString(L"open ./AppleGame/3.mp3", 0, 0, 0);
mciSendString(L"play ./AppleGame/3.mp3", 0, 0, 0);
//加载图片
loadimage(&background,L"./AppleGame/0.png", WINDOWSLENGTH, WINDOWSHIGHT);
loadimage(&apple1, L"./AppleGame/1.png",50,50);
loadimage(&apple2, L"./AppleGame/2.png",50,50);
settextstyle(25, 20, L"宋体");
setbkmode(0);
}
//创建链表头结点
Pt_letter Listinit()
{
Pt_letter p = (Pt_letter)malloc(sizeof(LETTER));
if (p == NULL)
{
return 0;
}
p->pnext = NULL;
return p;
}
//输出图片
void GamePoint(Pt_letter pletter)
{
BeginBatchDraw();//批量绘制
//背景图片
putimage(0, 0, &background);
//苹果
putimage(0, 0, &background);
//字母
pletter = pletter->pnext;
while (pletter != NULL)
{
putimage(pletter->x - 14, pletter->y - 17, &apple2,SRCPAINT);//三光栅,把apple2变透明,这个是win
putimage(pletter->x-14, pletter->y-17, &apple1,SRCAND);
outtextxy(pletter->x, pletter->y, pletter->ch);
pletter = pletter->pnext;
}
//putimage(100, 100, &apple1);//为了测试苹果和字母的关系
//outtextxy(100+14, 100+17,L"B");
EndBatchDraw();
}
//添加链表结点
void AddListNode(Pt_letter pletter)
{
//为了释放方便,用尾插法
while (pletter->pnext != NULL)
{
pletter = pletter->pnext;
}
Pt_letter pnode = (Pt_letter)malloc(sizeof(LETTER));
pnode->x = rand() % 591 + 14;
pnode->y = 17;
pnode->ch = rand() % 26 + 'A';
pnode->pnext = pletter->pnext; //为了让这个结点的pnext指向NULL
pletter->pnext = pnode; //把头结点和下一个结点相连,从第二次开始,作用就是连接各个结点。
}
void AppleMove(Pt_letter pletter , int speedy)
{
int speedx = 1;
Pt_letter pfront = pletter;
pletter = pletter->pnext;
while (pletter != NULL)
{
pletter->y += speedy;
if (rand() % 2 == 0)
pletter->x += speedx;
else
pletter->x -= speedx;
if (pletter->y >= WINDOWSHIGHT)
{
pfront->pnext = pletter->pnext;
free(pletter);
pletter = pfront;
}
pletter = pletter->pnext;
}
}
//删除结点
void FreePnode(Pt_letter pletter, char key)
{
int score = 0;
Pt_letter pfront = pletter;//再pletter的前面的结点
pletter = pletter->pnext;
while (pletter != NULL )
{
if (pletter->ch == key || pletter ->ch == key +'A')
{
pfront ->pnext = pletter->pnext;
free(pletter);
score = 10;
break;
}
else
{
pfront = pfront -> pnext;
pletter = pletter->pnext;
}
}
}
int main()
{
GameInit();
Pt_letter pletter = Listinit();
DWORD t1, t2 ,tt1 ,tt2;
int speedy = 2;
char key;
int score = 0;
tt1 = GetTickCount();
t1 = GetTickCount();
while(1)
{
t2 = GetTickCount();
if (t2 - t1 >= 1000)
{
AddListNode(pletter);
t1 = t2;
}
tt2 = GetTickCount();
if (tt2 - tt1 >= 20)
{
AppleMove(pletter, speedy);
tt1 = tt2;
}
GamePoint(pletter);
//删除链表
if (_kbhit())
{
key = _getch();
//调节速度 六个档,这里可以随着速度来变换歌曲。。。。。。。。。。。。。暂时还不会
if (key == '1')
speedy = 1;
if (key == '2')
speedy = 6;
if (key == '3')
speedy = 10;
FreePnode(pletter, key);
}
}
system("pause");
return 0;
}