[C++] 纯文本查看 复制代码
#include <iostream>
#include <windows.h>
#include <time.h>
#include <conio.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int sudu = 300;
struct snake {
int x;
int y;
snake* next;
snake* fron;
};
int score = 0;
struct food {
int x;
int y;
};
food f;
struct people {
char name[20];
int score;
};
people p;
int direction = 3;
//不是看不起你的专业 ,而是看不懂,嗯,是这样!
//"看不懂",划重点,
//考试要考!
void gotoxy(int x, int y);//光标移动
void color(int c ); //输出字体颜色
void frame(); //框架
void create(snake* &head);//连接蛇节点
void pri(snake* head,int ok); //打印蛇
snake* init(); // 初始化蛇
int move(); // 上下左右 空格 退出 数字化
int food_judge(int x,int y,snake *head);//判断位置食物是否为空
bool start_meau(); //开始菜单
void location(snake* head); //产生食物位置
void write(); //记录最高分
void read(); //读取最高分
void priscore();//打印分数,记录分数
bool snake_judge(snake* head);//是否吃到食物
bool judge(snake* head); //是否被撞
void pri_food();
void over(snake* head);
// x = 111; y= 31;
void run(snake* &head,snake* &End,int &pause, int &exit);
void speed();
int main() {
read();
system("color block");
while (1) {
score = 0;
sudu = 300;
system("mode con cols=82 lines=32");
start_meau();
int pause = 0;
int exit = 0;
int flag = move();
Sleep(300);
if (flag == 0) {
snake* head = init();
snake* End = init();
head->next = End;
End->fron = head ;
End->x = head->x;
End->y = head->y + 1;
system("mode con cols=112 lines=32");
location(head);
while (!exit) {
while (!pause && !exit) {
speed();
Sleep(sudu);
priscore();
run(head, End, pause, exit);
}
if (pause == 1) {
if (move() == 0)
pause = 0;
}
if (exit == 1) {
write();
getchar();
}
}
}
else if (flag == -1)
break;
}
return 0;
}
void gotoxy(int x,int y) {
COORD c;
c.X = x;
c.Y = y;
SetConsoleCursorPosition(GetStdHandle( STD_OUTPUT_HANDLE),c);
}
/**
1=蓝色 2=绿色 3=湖蓝色
4=红色 7=白色 9=淡蓝色
*/
void color(int c) {
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE), c);
}
/**x\y
0 * * * * * 81
* *
* *
31 * * * * * ***
*/
void frame() {
for (int i = 0; i < 32; i++) {
color(1);
gotoxy(0,i);
printf("#");
gotoxy(81, i);
printf("#");
}
for (int i = 0; i < 82; i++) {
color(1);
gotoxy(i,0);
printf("#");
gotoxy(i,31);
printf("#");
}
}
/**
1 2 3 4 0 -1 5
上 下 左 右 空格 esc 其他
38、40、37和39
*/
int move() {
if (GetAsyncKeyState(VK_UP))
return 1;
else if (GetAsyncKeyState(VK_DOWN))
return 2;
else if (GetAsyncKeyState(VK_LEFT))
return 3;
else if (GetAsyncKeyState(VK_RIGHT))
return 4;
else if (GetAsyncKeyState(VK_SPACE))
return 0;
else if (GetAsyncKeyState(VK_ESCAPE))
return -1;
else
return 5;
}
bool start_meau(){
frame();
color(4);
gotoxy(30,4);
printf(" 贪 吃 蛇 ");
color(2);
gotoxy(28,3);
printf("=============================");
gotoxy(28,5);
printf("=============================");
gotoxy(28,4);
printf("||");
gotoxy(55,4);
printf("||");
color(9);
gotoxy(35,10);
printf(" 开 始 ");
gotoxy(30,14);
printf(" 最 高 分");
gotoxy(33,16);
color(7);
printf("玩家:%s ",p.name);
gotoxy(33, 17);
printf("分数: %d",p.score);
color(3);
for (int i = 10; i < 70; i++) {
gotoxy(i,20);
printf("*");
for (int i = 20; i < 31; i++) {
gotoxy(10, i);
printf("*");
gotoxy(70,i);
printf("*");
}
}
color(9);
gotoxy(16,22);
printf("提示: 空格开始或者暂停; ↑↓← → 进行移动; ESC 退出");
color(7);
gotoxy(25,24);
printf("游戏快乐大家,");
gotoxy(40,25);
printf("玩家嘻嘻哈哈.");
gotoxy(25,26);
printf("你若乐得自在,");
gotoxy(40,27);
printf("小哥打个赏吧!");
gotoxy(60,28);
printf("---陈墨");
color(0);
for (; ; ) {
int tmp = move();
if ( tmp == 0)
return true;
if (tmp == -1)
return false;
}
color(0);
}
/**
在空闲处输出食物
*/
void location(snake* head) {
for (; ; ) {
f.x = rand() % 80 + 1;
f.y = rand() % 30 + 1;
if (food_judge(f.x, f.y,head))
break;
}
}
int food_judge(int x, int y, snake *head) {
while (head) {
if (head->x == x && head->y == y)
return 0;
head = head->next;
}
return 1;
}
snake* init() {
snake* s = (snake*)malloc( sizeof( snake));
s->next = NULL;
s->fron = NULL;
s->x = rand() % 80 + 1;
s->y = rand() % 30 + 1;
return s;
}
void pri(snake* head, int ok) {
system("cls");
priscore();
color(2);
frame();
pri_food();
color(7);
while (head->fron) {
head->x = (head->fron)->x;
head->y = (head->fron)->y;
head = head->fron;
gotoxy(head->x, head->y);
printf("+");
}
if(ok != 5)
direction = ok ;
if (ok == 1)
head->y -= 1;
else if (ok == 2)
head->y += 1;
else if (ok == 3)
head->x -= 1;
else if (ok == 4)
head->x += 1;
else {
ok = direction;
if (ok == 1)
head->y -= 1;
else if (ok == 2)
head->y += 1;
else if (ok == 3)
head->x -= 1;
else if (ok == 4)
head->x += 1;
}
gotoxy(head->x, head->y);
printf("+");
}
void create(snake* &End) {
snake* tmp = init();
tmp->x = f.x;
tmp->y = f.y;
tmp->fron = End;
End->next = tmp;
End = tmp;
}
bool judge(snake* head) {
if (head->x == 0 || head->x == 81 || head->y == 0 || head->y == 31)
return false;
int x = head->x;
int y = head->y;
if (!head->next)
return true;
head = head->next;
while (head) {
if (head->x == x && head->y == y)
return false;
head = head->next;
}
return true;
}
void write() {
system("cls");
frame();
color(7);
if (score > p.score) {
gotoxy(20, 15);
printf("最高分: %d ",score);
gotoxy(20,17);
printf("请输入昵称:");
gotoxy(32,17);
scanf("%s", p.name);
p.score = score;
FILE* pf;
pf = fopen("s.txt", "w");
fwrite(&p, sizeof(people),1,pf );
fclose(pf);
}
else{
gotoxy(20,15);
printf("游 戏 结 束 !");
gotoxy(20,17);
printf("最终得分 : %d",score);
}
}
void read() {
FILE* pf;
if ((pf = fopen("s.txt", "a+")) == NULL) {
strcpy(p.name, "no one");
std::cout << p.name;
p.score = 0;
write();
}
else {
fread(&p, sizeof(people), 1, pf);
fclose(pf);
}
}
void priscore() {
color(7);
gotoxy(90, 4);
printf("贪吃蛇大作战");
gotoxy(90,8);
printf("分数: %d",score);
gotoxy(90,12);
printf("最高分: %d",p.score);
gotoxy(90,14);
printf("获得者: %s",p.name);
gotoxy(90, 20);
printf("游戏是好玩,");
gotoxy(90,22);
printf("维修要花钱。");
gotoxy(90,24);
printf("大哥若富裕,");
gotoxy(90,26);
printf("谢谢你助力!");
gotoxy(94, 28);
printf("---(^-^)你懂得");
color(1);
for (int i = 81; i < 112; i++) {
gotoxy(i,0);
printf("#");
gotoxy(i, 31);
printf("#");
}
for (int i = 0; i < 32; i++) {
gotoxy(111, i);
printf("#");
}
}
bool snake_judge(snake* head) {
if (head->x == f.x && head->y == f.y)
return true;
return false;
}
void pri_food() {
gotoxy(f.x, f.y);
color(2);
printf("*");
}
void over(snake* head) {
snake* tmp = init();
while(head) {
tmp = head->next;
free(head);
head = tmp;
}
free(tmp);
}
void run(snake* &head, snake* &End,int &pause,int &exit) {
int flag = move();
switch (flag) {
case 1:
case 2:
case 3:
case 4:
case 5:pri(End,flag); break;
case 0:pause = 1; break;
case -1:exit = 1; break;
}
if (judge(head)) {
if (snake_judge(head)) {
snake* tmp = init();
tmp->x = End->x + 1;
tmp->y = End->y;
tmp->fron = End;
End->next = tmp;
End = tmp;
location(head);
score += 10;
}
}
else
exit = 1;
}
void speed() {
switch (score) {
case 100: sudu = 250; break;
case 200:sudu = 200; break;
case 250:sudu = 150; break;
case 300:sudu = 100; break;
case 350:sudu = 50; break;
}
}