#include <stdio.h>
#include <graphics.h>
#include <windows.h>
#include <mmsystem.h>
#include <time.h>
#include <stdlib.h>
#pragma comment (lib,"winmm.lib")
constexpr auto WINDOWSLENGTH = 640;
constexpr auto WINDOWSHIGHT = 480;
IMAGE background, apple1, apple2;
//字母结构体
typedef struct Letter
{
//数据域
int x, y;//字母坐标
char ch;//字母ASCII码
//指针域;
struct Letter *pnext = NULL;
}LETTER;
typedef LETTER *Pt_letter;
//初始化函数
void GameInit()
{
srand((unsigned int)time(NULL));//随机数种子
//初始化窗口
initgraph(WINDOWSLENGTH, WINDOWSHIGHT);
//初始化背景音乐
mciSendString("open ./AppleGame/3.mp3", 0, 0, 0);
mciSendString("play ./AppleGame/3.mp3", 0, 0, 0);
//加载图片
loadimage(&background, "./AppleGame/0.png", WINDOWSLENGTH, WINDOWSHIGHT);
loadimage(&apple1, "./AppleGame/1.png", 50, 50);
loadimage(&apple2, "./AppleGame/2.png", 50, 50);
settextstyle(25, 20, "宋体");
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)
{
//背景图片
putimage(0, 0, &background);
//苹果
putimage(0, 0, &background);
//字母
pletter = pletter->pnext;
while(pletter != NULL)
{
putimage(pletter->x - 14, pletter->y - 17, &apple1);
outtextxy(pletter->x, pletter->y, pletter->ch);
pletter = pletter->pnext;
}
//putimage(100, 100, &apple1);//为了测试苹果和字母的关系
//outtextxy(100+14, 100+17,L"B");
}
//添加链表结点
void AddListNode(Pt_letter pletter)
{
//为了释放方便,用尾插法
while(pletter->pnext != NULL)
{
pletter = pletter->pnext;
}
//Pt_letter pnode = (Pt_letter)malloc(sizeof(pnode));
Pt_letter pnode = (Pt_letter)malloc(sizeof(*pnode));
pnode->x = rand() % 591 + 14;
pnode->y = 17;
pnode->ch = rand() % 27 + 'A';
pnode->pnext = pletter->pnext; //为了让这个结点的pnext指向NULL
pletter->pnext = pnode; //把头结点和下一个结点相连,从第二次开始,作用就是连接各个结点。
}
int main()
{
GameInit();
Pt_letter pletter = Listinit();
for(int i = 0; i < 10; i++)
{
AddListNode(pletter);
}
GamePoint(pletter);
system("pause");
return 0;
}
|