贪吃蛇
这是一个简单的贪吃蛇 ,有界面,有颜色,但没有图片,因为它是控制台下的。函数都是先声明,后实现,所以比较方便看。
先看看程序运行图片吧
https://attach.52pojie.cn//forum/202003/06/182203hf8indji8xvs7uwa.png?l
https://attach.52pojie.cn//forum/202003/06/182336m5qqf21gc0019070.png?l
https://attach.52pojie.cn//forum/202003/06/182332f2z55ra75kx5r2ka.png?l
#include<iostream>
#include<windows.h>
#include<time.h>
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
intsudu = 300;
struct snake {
int x;
int y;
snake* next;
snake* fron;
};
intscore = 0;
struct food {
int x;
int y;
};
food f;
struct people {
char name;
intscore;
};
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);
voidspeed();
int main() {
read();
system("color block");
while (1) {
score = 0;
sudu = 300;
system("mode con cols=82 lines=32");
start_meau();
intpause = 0;
int exit = 0;
intflag = 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;
}
voidgotoxy(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 ** ** * ***
*/
voidframe() {
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("#");
}
}
/**
12 3 40 -15
上下左右 空格esc其他
38、40、37和39
*/
intmove() {
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) {
intflag = 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;
}
} 山野村夫-陈墨 发表于 2020-3-9 21:16
终于赶上你,还好我没有放弃
记得是 2005 年写的,当时只会用 VB6 编程语言,虽然是窗口程序,但却绘制一个个的点表示蛇,而并没有使用图片,不过也有些创新的地方。具体来说就是:初始的整个蛇由五个黑色的点是构成,但吃的一个个点(食物)是有颜色的,当吃了三个同样的点之后,即可消除此时才会得分,另外被蛇吃了食物后,不同颜色构成的后面那段蛇尾部分,是可以使用两个特定的按键进行切换的,类似于马里奥医生药丸的切换。之后不断改进版本,终于加入了最多四人本机对战功能,其中两人可以用键盘控制,另外两人是电脑控制,算是人工智能不过是变态难度版本……当然一对一人机对战也是可以的。。。又过了几个月我的第二款游戏诞生,还是 VB6 语言所写,不过这次让游戏可以加载 jpg 的图片功能,显示效果更进一层,具体玩法类似于马里奥医生三消游戏,名为《食物大战》—— 时间推到至今疫情过年前,我终于成为了一名资深的分拣快递的大侠 !!!
;www ;www ;www
直到看了你这个关于贪吃蛇的帖子,已往对小游戏开发而通宵写代码的画面,纷纷浮现在脑海里心情久久不能平静,开发游戏的热情被再次激发。虽然时代变迁,正在从事的行业也与游戏一点不沾边,但平时也在默默关注游戏开发方面的新闻,所以这次就直接使用游戏引擎开发游戏吧,虚幻?Unity3D?不不不,这些太高级了大材小用,开发小游戏太浪费啦,经过一阵搜索找到了一款轻量化的开源游戏引擎 ——【Godot Engine】
{:1_893:}{:1_893:}{:1_893:}
PS:现在才想起来那款用 VB6 写的贪吃蛇源代码,当年好像备份到了某云盘上不知道还能找回不? 网上高人真多,找到个开源的游戏项目:元气骑士 + 贪吃蛇 = 合体的游戏,里面有些很有意思的设计,来源于小众软件发现频道(https://www.appinn.com/dungeonrush/)
加长版介绍: 大一 C 语言课设老师给了一个策划,要求实现一个游戏设定比较多的贪吃蛇。我觉得那份设定不够有趣,想搞点花的。
那一阵子又和室友打元气骑士打得比较多,于是就想,能不能让 英雄 作为贪吃蛇的节,然后做一个 Roughlike 地牢闯关的游戏呢?
于是就有了 DungeonRush 。 元 气 贪 吃 蛇(划掉)
帮你顶一下。:lol我是新来的 666,帮顶。 哈利路亚9527 发表于 2020-3-6 18:30
帮你顶一下。我是新来的
你居然顶我???:$qqq 不愧是你 C++好怀念啊~~~ 顶一下,好东西,正好在学习 好东西,帮忙顶一下 牛啊,纯c写的游戏 哈哈,我以前也写过这个游戏哦
页:
[1]
2