vs2013环境下贪吃蛇 欢迎尝试
注意:要把控制台窗口默认值从“简体中文”改成“美国”代码放这里,应用程序在下面
希望高手多多指正
接下来是代码了
思路都是自己的
//global.h
#ifndef global_h
#define global_h
wchar_t a;
int hl = 0;
int hr = 0;
int fl = 22;
int fr = 42;
int bl;
int br;
wchar_t c;
int ju;
int b=0;
int k = 1;//food
FILE *fp;
int kill = 0;
#endif
//menu.h
#ifndef menu_h
#define menu_h
void out();
void model();
void start();
void food();
void control(int j);
void over();
int initial();
void file();
#endif
//main.cpp
#include<stdio.h>
#include"menu.h"
#include<Windows.h>
#include<conio.h>
#include<time.h>
extern wchar_t a;
extern int hl;
extern int hr;
extern int bl;
extern int br;
extern wchar_t c;
extern int ju;
extern int fl;
extern int fr;
extern int kill;
int main()
{
srand((unsigned)time(NULL));
model();
start();
int j = initial();
while(1)
{
kill++;
control(j);
a = 0;
file();
//printf("j=%d\n", j);
//getchar();
j = ju;
}
return 0;
//menu.cpp
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include"global.h"
#include<Windows.h>
#include<conio.h>
extern void food();
void out()
{
system("cls");
for (int i = 0; i < 22; i++)
{
for (int j = 0; j < 42; j++)
printf("%c", a);
printf("\n");
}
Sleep(100);
}
void model()
{
for (int i = 0; i < 22; i++)
{
for (int j = 0; j < 42; j++)
{
if (i == 0 || i == 21 || j == 0 || j == 41)
a = 219;
else
a = 0;
}
}
a = 0;
for (int i = 0; i < 100; i++)
{
bl = 22;
br = 42;
}
}
void start()
{
hl = rand() % 10 + 5;
hr = rand() % 20 + 10;
a = 4;
bl = hl;
br = hr+1;
a]] = 15;
food();
out();
/*while (1)
{
a]] = 0;
br--;
hr--;
a]] = 15;
a = 4;
out();
if (_kbhit())
{
c = _getch();
if (c == 'w'||c=='a'||c=='s'||c=='d')
{
break;
}
}
}*/
}
int initial()
{
int judge = 0;//1u 2d 3l 4r
if (hl == (bl + 1) && hr == br)
judge = 1;
else if (hl == (bl - 1) && hr == br)
judge = 2;
else if (hl == bl && hr == (br - 1))
judge = 3;
else if (hl == bl && hr == (br + 1))
judge = 4;
else
printf("wrong\n");
return judge;
}
void control(int j)
{
switch (j)
{
case 1:
{
while (1)
{
int i = 0;
for (i = 0; i < 100; i++)
{
if (a]] == 0) break;
}
a]] = 0;
for (int j = i - 1; j>0; j--)
{
bl = bl;
br = br;
a]] = 15;
}
bl = hl;
br = hr;
a]] = 15;
--hl;//most important
a = 4;
food();//before out
out();
if (_kbhit())
{
c = _getch();
if (c == 'a')
{
ju = 3;
while (1)
{
int i = 0;
for (i = 0; i < 100; i++)
{
if (a]] == 0) break;
}
a]] = 0;
for (int j = i - 1; j>0; j--)
{
bl = bl;
br = br;
a]] = 15;
}
bl = hl;
br = hr;
a]] = 15;
--hr;//most important
a = 4;
food();
out();
b = 1;
break;
}
}
else if (c == 'd')
{
ju = 4;
while (1)
{
int i = 0;
for (i = 0; i < 100; i++)
{
if (a]] == 0) break;
}
a]] = 0;
for (int j = i - 1; j>0; j--)
{
bl = bl;
br = br;
a]] = 15;
}
bl = hl;
br = hr;
a]] = 15;
++hr;//most important
a = 4;
food();
out();
b = 1;
break;
}
}
}
if (b == 1)
break;
}
break;
}
case 2:
{
while (1)
{
int i = 0;
for (i = 0; i < 100; i++)
{
if (a]] == 0) break;
}
a]] = 0;
for (int j = i - 1; j>0; j--)
{
bl = bl;
br = br;
a]] = 15;
}
bl = hl;
br = hr;
a]] = 15;
++hl;//most important
a = 4;
food();
out();
if (_kbhit())
{
c = _getch();
if (c == 'a')
{
ju = 3;
while (1)
{
int i = 0;
for (i = 0; i < 100; i++)
{
if (a]] == 0) break;
}
a]] = 0;
for (int j = i - 1; j>0; j--)
{
bl = bl;
br = br;
a]] = 15;
}
bl = hl;
br = hr;
a]] = 15;
--hr;//most important
a = 4;
food();
out();
b = 1;
break;
}
}
else if (c == 'd')
{
ju = 4;
while (1)
{
int i = 0;
for (i = 0; i < 100; i++)
{
if (a]] == 0) break;
}
a]] = 0;
for (int j = i - 1; j>0; j--)
{
bl = bl;
br = br;
a]] = 15;
}
bl = hl;
br = hr;
a]] = 15;
++hr;//most important
a = 4;
food();
out();
b = 1;
break;
}
}
}
if (b == 1)
break;
}
break;
}
case 3:
{
while (1)
{
int i = 0;
for (i = 0; i < 100; i++)
{
if (a]] == 0) break;
}
a]] = 0;
for (int j = i-1;j>0; j--)
{
bl = bl;
br = br;
a]] = 15;
}
bl = hl;
br = hr;
a]] = 15;
--hr;//most important
a = 4;
food();
out();
if (_kbhit())
{
c = _getch();
if (c == 'w')
{
ju = 1;
while (1)
{
int i = 0;
for (i = 0; i < 100; i++)
{
if (a]] == 0) break;
}
a]] = 0;
for (int j = i - 1; j>0; j--)
{
bl = bl;
br = br;
a]] = 15;
}
bl = hl;
br = hr;
a]] = 15;
--hl;//most important
a = 4;
food();
out();
b = 1;
break;
}
}
else if (c == 's')
{
ju = 2;
while (1)
{
int i = 0;
for (i = 0; i < 100; i++)
{
if (a]] == 0) break;
}
a]] = 0;
for (int j = i - 1; j>0; j--)
{
bl = bl;
br = br;
a]] = 15;
}
bl = hl;
br = hr;
a]] = 15;
++hl;//most important
a = 4;
food();
out();
b = 1;
break;
}
}
}
if (b == 1)
break;
}
break;
}
case 4:
{
while (1)
{
int i = 0;
for (i = 0; i < 100; i++)
{
if (a]] == 0) break;
}
a]] = 0;
for (int j = i - 1; j>0; j--)
{
bl = bl;
br = br;
a]] = 15;
}
bl = hl;
br = hr;
a]] = 15;
++hr;//most important
a = 4;
food();
out();
if (_kbhit())
{
c = _getch();
if (c == 'w')
{
ju = 1;
while (1)
{
int i = 0;
for (i = 0; i < 100; i++)
{
if (a]] == 0) break;
}
a]] = 0;
for (int j = i - 1; j>0; j--)
{
bl = bl;
br = br;
a]] = 15;
}
bl = hl;
br = hr;
a]] = 15;
--hl;//most important
a = 4;
food();
out();
b = 1;
break;
}
}
else if (c == 's')
{
ju = 2;
while (1)
{
int i = 0;
for (i = 0; i < 100; i++)
{
if (a]] == 0) break;
}
a]] = 0;
for (int j = i - 1; j>0; j--)
{
bl = bl;
br = br;
a]] = 15;
}
bl = hl;
br = hr;
a]] = 15;
++hl;//most important
a = 4;
food();
out();
b = 1;
break;
}
}
}
if (b == 1)
break;
}
break;
}
}
}
void food()
{
int x = 1;
if (a == 0)
{
do
{
x = 1;
fl = rand() % 18 + 2;
fr = rand() % 38 + 2;
for (int i = 0; i < 100; i++)
{
if (fl == bl && fr == br)
x++;
}
} while (x != 1);
a = 3;
}
else if (a == 4)
{
int i = 0;
for (i = 0; i < 100; i++)
{
if (a]] == 0)
break;
}
if (i == 1)
{
if (hl == (bl + 1) && hr == br)
{
bl = bl - 1;
br = br;
}
else if (hl == (bl - 1) && hr == br)
{
bl = bl + 1;
br = br;
}
else if (hl == bl && hr == (br - 1))
{
bl = bl;
br = br + 1;
}
else if (hl == bl && hr == (br + 1))
{
bl = bl;
br = br - 1;
}
else
printf("wrong\n");
}
else if (i > 1)
{
if (bl == (bl + 1) && br == br)
{
bl = bl + 1;
br = br;
}
else if (bl == (bl - 1) && br == br)
{
bl = bl - 1;
br = br;
}
else if (bl == bl && br == (br - 1))
{
bl = bl;
br = br - 1;
}
else if (bl == bl && br == (br + 1))
{
bl = bl;
br = br + 1;
}
else
printf("wrong\n");
}
a]] = 15;
do
{
x = 1;
fl = rand() % 18 + 2;
fr = rand() % 38 + 2;
for (int i = 0; i < 100; i++)
{
if (fl == bl && fr == br)
x++;
}
} while (x != 1);
a = 3;
}
}
void file()
{
char *fname;
fname = "text.txt";
if (fopen_s(&fp, fname, "a+") != 0)
if (fopen_s(&fp, fname, "w+") != 0)
printf("file open wrong\n");
printf("the %d control\n",kill);
for (int i = 0; i < 20; i++)
{
fprintf(fp, "%-4d %-4d %d %d\n", bl, br,kill, a]]);
}
fclose(fp);
}
{:1_906:}一个DB啊楼主 活着只为输出 发表于 2015-5-31 22:12
一个DB啊楼主
这个吾爱币是谁收了
我点了一下下载 我自己吾爱币也减一了。。。。 崽崽呦 发表于 2015-5-31 22:30
2012可以不
exe文件可能可以运行
或者你把代码复制进去 X雷廷X 发表于 2015-5-31 22:37
谢谢楼主分享!
不客气 你觉得开心就好 lz多加些注释就好了。。。 wisology 发表于 2015-6-1 20:33
lz多加些注释就好了。。。
嗯 你的建议很好 下次我会多加注释的
页:
[1]