编译运行环境 vs2019
我创建了一个链表:
typedef struct student{char data[20];}student;
typedef struct Node{student Sdata;Node* next;}Node;
利用outtextxy(x ,y , list->Sdata.data);输出乱码
#include<MyList.h>
#include<graphics.h>
#include<conio.h>
#include<windows.h>
#include<mmsystem.h>
#pragma comment(lib,"winmm.lib")
/*
课程内容:贪吃蛇
知识点:结构体,循环,函数,easyx,数组
*/
//做界面 1.创建一个窗口,图形窗口
#define SNAKE_NUM 500
Node* list = createList();
enum DIR //表示蛇的方向
{
UP,
DOWN,
LEFT,
RIGHT,
};
//蛇的结构
struct Snake
{
int size;//蛇的节数
int dir;//蛇的方向
int speed;//移动速度
POINT coor[SNAKE_NUM];//座标
}snake;
//食物结构
struct Food
{
int x;
int y;
int r; //食物大小
bool flag; //食物是否被吃
DWORD color; //食物颜色
}Food;
//数据的初始化
void GameInit()
{
//innit 初始化 graph 图像窗口 SHOWCONSOLE显示控制台
initgraph(1280, 960,SHOWCONSOLE);
//设置一个随机数种子,GetTickCount();获取系统开机,到现在所经过的毫秒数
srand(GetTickCount());
snake.size = 3;
snake.speed = 30;
snake.dir = RIGHT;
snake.coor[0].x = 10;
snake.coor[0].y = 10;
for (int i = 0; i < snake.size; i++)
{
snake.coor[i].x = 40-10*i;
snake.coor[i].y = 10;
//printf("%d %d\t", snake.coor[i].x, snake.coor[i].y);
}
//初始化食物 rand()随机产生一个整数,如果没有设置随机数种子,每次产生的都是固定的整数
Food.x = rand() % 1280;
Food.y = rand() % 960;
Food.color = RGB(rand() % 256, rand() % 256, rand() % 256);
//Food.r = rand() % 10 + 20;
Food.r = 20;
Food.flag = true;
}
void GameDraw()
{
//双缓冲绘图
BeginBatchDraw();
//设置背景颜色
setbkcolor(RGB(28, 115, 119));
cleardevice();
//绘制蛇
setbkmode(TRANSPARENT);
settextcolor(RGB(0, 122, 204));
setfillcolor(GREEN);
for (int i = 0; i < snake.size; i++)
{
solidcircle(snake.coor[i].x, snake.coor[i].y, 20);
}
//绘制食物
if (Food.flag)
{
solidcircle(Food.x, Food.y, Food.r);
outtextxy(Food.x - 15, Food.y - 10, list->Sdata.data);
}
else {
list = list->next;
}
EndBatchDraw();
}
//移动蛇
void snakeMove()
{
//让身体跟着头移动
for (int i = snake.size-1; i>0; i-- )
{
snake.coor[i] = snake.coor[i - 1];
}
//移动是什么发生移动
switch (snake.dir)
{
case UP:
snake.coor[0].y-=snake.speed;
if (snake.coor[0].y+10 <= 0)
{
snake.coor[0].y = 960;
}
break;
case DOWN:
snake.coor[0].y+=snake.speed;
if (snake.coor[0].y - 10 >= 960)
{
snake.coor[0].y = 0;
}
break;
case LEFT:
snake.coor[0].x-=snake.speed;
if (snake.coor[0].x + 10 <= 0)
{
snake.coor[0].x = 1280;
}
break;
case RIGHT:
snake.coor[0].x+=snake.speed;
if (snake.coor[0].x - 10 >= 1280)
{
snake.coor[0].x = 0;
}
break;
}
}
//通过按键改变蛇的移动方向
void keyControl()
{
//判断有没有按键
if (_kbhit())
{
// 72 80 75 77 上下左右键值
switch (_getch())
{
case 'w':
case 'W':
case 72:
if (snake.dir != DOWN)
{
snake.dir = UP;
}
break;
case 's':
case 'S':
case 80:
if (snake.dir != UP)
{
snake.dir = DOWN;
}
break;
case 'a':
case 'A':
case 75:
if (snake.dir != RIGHT)
{
snake.dir = LEFT;
}
break;
case 'd':
case 'D':
case 77:
if (snake.dir != LEFT)
{
snake.dir = RIGHT;
}
break;
case ' ': //游戏暂停
while (1)
{
if (_getch() == ' ')
break;
}
}
}
}
void EatFood()
{
if (Food.flag&&snake.coor[0].x >= Food.x - Food.r-5 && snake.coor[0].x <= Food.x + Food.r+5 &&
snake.coor[0].y >= Food.y - Food.r-5 && snake.coor[0].y <= Food.y + Food.r+5)
{
Food.flag = false;
snake.size++;
}
//如果食物消失,重新生成食物
if (!Food.flag)
{
Food.x = rand() % 1280;
Food.y = rand() % 960;
Food.color = RGB(rand() % 256, rand() % 256, rand() % 256);
//Food.r = rand() % 10 + 5;
Food.r = 20;
Food.flag = true;
}
}
//添加音乐
void BGM() {
//播放背景音乐
PlaySound(TEXT("B.wav"), NULL, SND_FILENAME | SND_ASYNC | SND_LOOP);
}
//创建一个保存字符串的链表,并保存。
void creatListAndSave() {
readInfoFromFile(list, "1.txt");
for (int i = 0; i < 10; i++)
{
printf("请输入data\n");
scanf("%s", Sdata.data);
insertNodeByHead(list, Sdata);
writeInfoToFile(list, "1.txt");
}
}
int main()
{
//BGM();
/*creatListAndSave();*/
readInfoFromFile(list, "1.txt");
//printfList(list);
GameInit();
while (1)
{
GameDraw();
keyControl();
snakeMove();
EatFood();
Sleep(120);
}
return 0;
}
/*
可以加一个分数,吃一个食物,加n分
*/