#include <graphics.h>
#include <conio.h>
#include <time.h>
#include <stdio.h>
#include<iostream>
#include<string.h>
#include <tchar.h>
#include <mmsystem.h>//导入声音头文件
#pragma comment(lib,"Winmm.lib")
#define MAP_HEIGHT 30
#define MAP_WIDE 30
#define SIZE 16
#define SPEED 200
//定义蛇的结点
typedef struct Snakes
{
int x;//蛇的结点的位置坐标
int y;
struct Snakes* next;
}snake;
snake* head, * tail;
//定义食品的结构
struct Food
{
int x;
int y;
}food;
int grow = 0;
char ch1 = 1;
int a1 = 0, a2 = 1, a3 = 0;;//grow =0 表现没吃到食品,grow = 1表现吃到食品
TCHAR str[100];
TCHAR str1[100];
TCHAR str2[100];
TCHAR str3[100];
int score = 0;
int count=1;
char ch = 'a';//初始方向向左
void BGM()
{
PlaySound(TEXT("平凡之路.wav"), NULL, SND_FILENAME | SND_ASYNC | SND_LOOP);
}
void login() //登录界面//
{
char name[100];
initgraph(1200, 900);
setbkcolor(BLUE);
cleardevice();
settextcolor(WHITE);
setfont(100, 0, _T("华文行楷"));
outtextxy(300, 150, _T("WELCOME"));
wchar_t username[1000];
errno_t err;
//FILE* fb;
InputBox(username, 100, _T("请输入你的昵称"));
FILE* fb = fopen("username.txt", "a");
fprintf(fb, "%S ", username);
fprintf(fb, "%c", '\n');
fclose(fb);
// err = fopen_s(&fp, "username.txt", "a");
//fprintf(fp, "%d", username);
//fclose(fp);
}
void DrawMap()//打印舆图
{
setbkcolor(YELLOW);
cleardevice(); //调用清屏cleardevice用配景色革新配景
for (int i = 0; i < MAP_WIDE; i++)
{
setfillcolor(BLUE);
//上边框
fillrectangle(i * SIZE, 0, (i + 1) * SIZE, SIZE);
//下边框
fillrectangle(i * SIZE, (MAP_HEIGHT - 1) * SIZE, (i + 1) * SIZE, MAP_HEIGHT * SIZE);
}
for (int i = 0; i < MAP_HEIGHT; i++)
{
setfillcolor(BLUE);
//左边框
fillrectangle(0, i * SIZE, SIZE, (i + 1) * SIZE);
//右边框
fillrectangle((MAP_WIDE - 1) * SIZE, i * SIZE, MAP_WIDE * SIZE, (i + 1) * SIZE);
}
settextcolor(GREEN);
_stprintf_s(str1, _T("%d"), score);
outtextxy(500, 20, str1);
}
//打印结点
void PrintNode(int x, int y)
{
setfillcolor(RED);
fillcircle(x * SIZE, y * SIZE, SIZE / 2);
setcolor(WHITE);
circle(x * SIZE, y * SIZE, SIZE / 2);
}
//删除结点
void DeleteNode(int x, int y)
{
setfillcolor(YELLOW);
fillcircle(x * SIZE, y * SIZE, SIZE / 2);
setcolor(YELLOW);
circle(x * SIZE, y * SIZE, SIZE / 2);
}
void InitMap()
{
//初始化蛇
head = (snake*)malloc(sizeof(snake));
head->x = (MAP_WIDE) / 2;
head->y = (MAP_HEIGHT) / 2;
snake* p = (snake*)malloc(sizeof(snake));
snake* q = (snake*)malloc(sizeof(snake));
p->x = head->x + 1;
p->y = head->y;
q->x = head->x + 2;
q->y = head->y;
head->next = p;
p->next = q;
tail = q;
tail->next = NULL;
snake* temp = head;
while (temp != NULL)//打印出所有结点
{
PrintNode(temp->x, temp->y);
temp = temp->next;
}
//初始化食品
srand((int)time(NULL));
food.x = rand() % (MAP_WIDE - 10) + 2;
food.y = rand() % (MAP_HEIGHT - 10) + 2;
if (food.x > 28 || food.y > 28)
{
food.x = food.x - 3;
food.y = food.y - 3;
}
PrintNode(food.x, food.y);
}
void UpdataFood()
{
//如果蛇的结点与食品的坐标相称了,则必要创造新的食品
snake* judge = head;
while (judge->next != NULL)//遍历所有结点
{
if (judge->x == food.x && judge->y == food.y)
{
food.x = rand() % (MAP_WIDE - 10) + 2; //生成2~MAP_WIDE - 2的随机数
food.y = rand() % (MAP_HEIGHT - 10) + 2; //生成2~MAP_HEIGHT - 2的随机数
if (food.x >28 || food.y >28)
continue;
PrintNode(food.x, food.y);
score=score+10;
grow = 1;
settextcolor(GREEN);
_stprintf_s(str1, _T("%d"), score);
outtextxy(500, 20, str1);
//outtextxy(400,20,L"123");
break;
}
judge = judge->next;
}
}
void ChangeBody()
{
snake* p = head;
int midx, midy, _midx, _midy;
midx = p->x;
midy = p->y;
while (p->next != NULL)
{
_midx = p->next->x;
_midy = p->next->y;
p->next->x = midx;
p->next->y = midy;
midx = _midx;
midy = _midy;
p = p->next;
}
}
void MoveSnake()
{
char oldch = ch;//记载原来的方向
if (_kbhit())
{
ch = _getch();//新的方向
if (ch ==' ')
{
a1=1;
ch = oldch;
}
}
//控制方向:向左的时间不能向右转,向上的时间不能向下,向右不能向左,向下不能向上
if ((ch == 'd' || ch == 'D') && (oldch == 'a' || oldch == 'A'))
//保持原来的方向不变
ch = oldch;
if ((oldch == 'w' || oldch == 'W') && (ch == 'S' || ch == 's'))
ch = oldch;
if ((oldch == 'd' || oldch == 'D') && (ch == 'A' || ch == 'a'))
ch = oldch;
if ((oldch == 'S' || oldch == 's' && ch == 'w' || ch == 'W'))
ch = oldch;
if (ch != 's' && ch != 'S' && ch != 'W' && ch != 'w' && ch != 'a' && ch != 'A' && ch != 'd' && ch != 'D')
ch = oldch;
//先清空所有结点,再打印,实现动态效果
snake* p = head;
while (p != NULL)
{
DeleteNode(p->x, p->y);
p = p->next;
}
//记载下尾结点的坐标,为吃到食品生成新结点做准备
int a = tail->x, b = tail->y;
//除头结点外 ,剩余结点前面结点的坐标赋值给后面的结点
ChangeBody();
//更新头结点的坐标
switch (ch)
{ //向右边转
case 'd':
case 'D':
head->x += 1;
break;
//左转
case 'a':
case 'A':
head->x -= 1;
break;
//向上
case 'w':
case 'W':
head->y--;
break;
//向下
case 's':
case 'S':
head->y++;
break;
default:
break;
}
//如果吃到食品,就用尾插法插入一个结点
if (grow)
{
snake* newnode;
newnode = (snake*)malloc(sizeof(snake));
newnode->x = a;
newnode->y = b;
tail->next = newnode;
tail = newnode;//更新尾结点
tail->next = NULL;
grow = 0;//更新grow的值
}
//重新打印所有结点
p = head;
while (p != NULL)
{
PrintNode(p->x, p->y);
p = p->next;
}
Sleep(SPEED);//控制速度
}
bool Finish()//判断是否竣事
{
//蛇撞墙,或者蛇头撞到身上,则游戏竣事
if (head->x <= 1 || head->x >= (MAP_WIDE - 1) || head->y <= 1 || head->y >= (MAP_HEIGHT - 1))
return 0;
snake* p = head->next;
while (p != NULL)
{
if (head->x == p->x && head->y == p->y)
return 0;
p = p->next;
}
return 1;
}
void BUTTON1()
{
int x = 100, y = 100;
int w = 300, h = 100;
TCHAR text[20] = L"start";
setbkmode(TRANSPARENT);
setfillcolor(GREEN);
fillroundrect(x, y, x + w, y + h, 10, 10);
// TCHAR s1[] = L"Arial BLACK";
settextstyle(100, 0, text);
settextcolor(WHITE);
int tx = x + (w - textwidth(text)) / 2;
int ty = y + (h - textheight(text)) / 2;
outtextxy(tx, ty, text);
}
void BUTTON2()
{
int x = 100, y = 300;
int w = 300, h = 100;
TCHAR text[20] = L"RANK";
setbkmode(TRANSPARENT);
setfillcolor(GREEN);
fillroundrect(x, y, x + w, y + h, 10, 10);
// TCHAR s1[] = L"Arial BLACK";
settextstyle(100, 0, text);
settextcolor(WHITE);
int tx = x + (w - textwidth(text)) / 2;
int ty = y + (h - textheight(text)) / 2;
outtextxy(tx, ty, text);
}
void PLAY()
{
//login();
initgraph(640, 480);
DrawMap();
InitMap();
//FILE* fb = fopen("排名.txt", "r");
//for (i = 0; i<10;i++)
//{
// fgets(a, 200, fb);
//outtextxy(20, 30 * j, a);
// for (j = 0; j < strlen(a)-1; j++)
// {
// num = num * 10 + int(a[j] - '0');
//
// }
// if (num >= max)
// {
// max = num;
//
//}
//printf("%d", max);
//num = 0;
//memset(a, 0, sizeof(a));
//}
// while (1)
//_stprintf_s(str2, _T("%d"), max);
//settextcolor(GREEN);
//outtextxy(300, 20, str2);
//while (1);
//fclose(fb);
while (Finish())
{
MoveSnake();
while (a1)
{
ch1 = _getch();
if (ch1 == ' ')
{
a1 = 0;
}
};
UpdataFood();
}
//int num = 999;
FILE* fa = fopen("排名.txt", "a");
fprintf(fa, "%d", score);
fprintf(fa, "%c", '\n');
fclose(fa);
//printf("游戏竣事!\n您的得分为:%d", score * 10);
/* FILE* fb = fopen("排名.txt", "r");
for (i = 0; i++; i < 100)
{
fgets(a, 10, fb);
for (j = 0; j < strlen(a); j++)
{
num = num * 10 + int(a - '0');
}
if (num > max)
{
max = num;
}
//printf("%d", max);
_stprintf_s(str2, _T("%d"),max);
outtextxy(400, 20, str2);
}
//while (1);
fclose(fb);*/
setbkcolor(BLACK);
cleardevice();
_stprintf_s(str, _T("%d"), score);
outtextxy(100, 200, str);
_getch();
}
void RANK()
{
int i;
initgraph(640, 480);
setbkcolor(WHITE);
cleardevice();
settextcolor(GREEN);
outtextxy(100, 20, L"玩家");
outtextxy(200, 20, L"得分");
outtextxy(50, 20, L"名次");
outtextxy(50, 50, '1');
outtextxy(50, 50+100, '2');
outtextxy(50, 50+200, '3');
outtextxy(50, 50+300, '4');
outtextxy(50, 50+400, '5');
int a[100],b[5],c[5], max = 0, temp = 0,p=0;
wchar_t name[100],q[100];
int j,k,b1=0,count=0;
FILE* fp = fopen("排名.txt", "r");
for (i = 0; i < 10; i++)
{
fscanf(fp, "%d", &a);
fscanf(fp, "%*[^\n]%*c");
}
fclose(fp);
for (i = 0; i < 10; i++)
{
for (j = i + 1; j < 9; j++)
{
if (a[j] > a)
{
temp = a;
a = a[j];
a[j] = temp;
}
}
}for (i = 0; i < 5; i++)
{
b = a;
}
FILE* fp1 = fopen("排名.txt", "r");
for (i = 0; i < 5; i++)
{
for (j = 0; j < 10; j++)
{
fscanf(fp, "%d", &b1);
if (b==b1)
{
c = j;
}
}
rewind(fp1);
}
fclose(fp1);
FILE* fp3 = fopen("排名.txt", "r");
FILE* fp2 = fopen("username.txt", "r");
for (i = 0; i < 5; i++)
{
for (j = 0; j < c;j++)
{
fscanf(fp2, "%*[^\n]%*c");
fscanf(fp3, "%*[^\n]%*c");
}
fscanf(fp2, "%S",&name );
fscanf(fp3, "%S", &q);
settextcolor(BLACK);
outtextxy(100, 50+100*i, name);
//_stprintf_s(str3, _T("%d"), );
//outtextxy(500, 20, p);
//settextcolor(BLACK);
outtextxy(200, 50+100*i, q);
rewind(fp2);
rewind(fp3);
}
fclose(fp2);
fclose(fp3);
//printf("%s", name);
//getchar();
}
int main()
{
//RANK();
BGM();
login();
char a[1000];
int i, j,num=0,max=0;
initgraph(640, 480);
setbkcolor(WHITE);
cleardevice();
BUTTON1();
BUTTON2();
ExMessage msg; //声明一个消息指针
while (true) {
if (peekmessage(&msg, EM_MOUSE)) {
switch (msg.message)
{
case WM_LBUTTONDOWN:
if (msg.x >= 100 && msg.x <= 400 && msg.y >=100&& msg.y <200)
{
PLAY();
} //break;
if (msg.x >= 100 && msg.x <= 400 && msg.y >= 300 && msg.y < 400)
{
RANK();
} break;
//case WM_MOUSEMOVE:
//if (msg.x >= 240 && msg.x <= 240 + 50 && msg.y >= 50 && msg.y <= 50 + 40)
// {
//}break;
//tipInfo(tipInfoRect, title[0]);
//if (msg.x >= 220 && msg.x <= 220 + 170 && msg.y >= 140 && msg.y <= 140 + 50)
//tipInfo(tipInfoRect, title[1]);
// if (msg.x >= 220 && msg.x <= 220 + 170 && msg.y >= 230 && msg.y <= 230 + 50)
//tipInfo(tipInfoRect, title[2]);
// if (msg.x >= 220 && msg.x <= 220 + 170 && msg.y >= 320 && msg.y <= 320 + 50)
// tipInfo(tipInfoRect, title[3]);
default:break;
}
}
}
system("pause");
}
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |