一只穿云箭,千万基佬来相见...(其实我想知道妹纸们哪去了)
好吧,话不多说,小狼这次给大家带来的是贪吃蛇源码,这是小狼尝试的第一个游戏,虽然这次的代码是从网上摘抄的,不过自己完完全全敲出来还是蛮有感觉的,所以建议小伙伴们都可以试试哦。
/*****
*文件名称:贪吃蛇游戏
*内容摘要:无
*其他说明:无
*当前版本:1.0
*版本信息:无
*作者:小狼
*完成日期:2017.5.10
*****/
#include<stdio.h>
#include<windows.h>
#include<conio.h>
#include<time.h>
#define FrameX 4//游戏窗口左上角的X轴坐标
#define FrameY 4//游戏窗口右上角的Y轴坐标
#define Frame_height 20//游戏窗口的高度
#define Frame_width 20//游戏窗口的宽度
int i,j;
int a[2];//用于记住蛇尾坐标,其中a[0],a[1]分别表示横,纵坐标
struct Snake
{
int x[100];//蛇的横坐标,其中x[0]表示蛇尾的横坐标,x[N-1]表示蛇头的横坐标
int y[100];//蛇的纵坐标,其中y[0]表示蛇尾的纵坐标,y[N-1]表示蛇头的纵坐标
int count;//蛇吃食物的个数
int length;//蛇的长度
int speed;//蛇的速度
};//不要忘记最后的";"
struct Food
{
int x;//食物的横坐标
int y;//食物的纵坐标
};
void make_frame();
void gotoxy(HANDLE hOut,int x,int y);
void print_information(HANDLE hOut,struct Snake *snake,struct Food *food);
void get_food(HANDLE hOut,struct Snake *snake,struct Food *food);
void init_snake(struct Snake *snake);
void Set_Txtcolor_Green();
void print_snake(HANDLE hOut,struct Snake *snake);
void move_snake(HANDLE hOut,struct Snake *snake);
void eat_food(HANDLE hOut,struct Snake *snake,struct Food *food);
void through_wall(HANDLE hOut,struct Snake *snake,char ch);
int if_die(struct Snake *snake);
void start_game();
void over_game();
void main()
{
system("color 0D");//设置文本为粉红色,注意那个是1234的0,不是Opq的O
start_game();
over_game();
}
/*制作游戏窗口*/
void make_frame()
{
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);//定义显示器句柄变量
gotoxy(hOut,FrameX + 2 * Frame_width + 3,FrameY + 13);//打印选择菜单
printf("Esc退出游戏");
gotoxy(hOut,FrameX + 2 * Frame_width + 3,FrameY + 15);
printf("长按方向键:加速");
gotoxy(hOut,FrameX,FrameY);//打印框角
printf("╔");
gotoxy(hOut,FrameX + 2 * Frame_width - 2,FrameY);
printf("╗");
gotoxy(hOut,FrameX,FrameY + Frame_height);
printf("╚");
gotoxy(hOut,FrameX + 2 * Frame_width - 2,FrameY + Frame_height);
printf("╝");
for(i = 2;i < 2 * Frame_width - 2;i += 2)
{
gotoxy(hOut,FrameX + i,FrameY);
printf("═");//打印上边框
}
for(i = 2;i < 2 * Frame_width - 2;i += 2)
{
gotoxy(hOut,FrameX + i,FrameY + Frame_height);
printf("═");//打印下边框
}
for(i = 1;i < Frame_height;i ++)
{
gotoxy(hOut,FrameX,FrameY + i);
printf("║");//打印左边框
}
for(i = 1;i < Frame_height;i ++)
{
gotoxy(hOut,FrameX + 2 * Frame_width - 2,FrameY + i);
printf("║");//打印右边框
}
gotoxy(hOut,FrameX + Frame_width - 5,FrameY - 2);//打印游戏名称
Set_Txtcolor_Green();//设置蛇为绿色
printf("贪吃蛇游戏");
}
/*光标移到指定位置*/
void gotoxy(HANDLE hOut,int x,int y)
{
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(hOut,pos);
}
/*设置文本为绿色*/
void Set_Txtcolor_Green(void)
{
HANDLE Handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(Handle, FOREGROUND_INTENSITY | FOREGROUND_GREEN);
}
/*菜单信息*/
void print_information(HANDLE hOut,struct Snake *snake,struct Food *food)
{
gotoxy(hOut,FrameX + 2 * Frame_width + 3,FrameY + 1);
printf("level : %d",snake->count / 5 + 1);//打印游戏等级
gotoxy(hOut,FrameX + 2 * Frame_width + 3,FrameY + 3);
printf("score : %d",snake->count);//打印游戏得分
gotoxy(hOut,FrameX + 2 * Frame_width + 3,FrameY + 5);
printf("eat food : %d",snake->count);//打印产生食物个数
gotoxy(hOut,FrameX + 2 * Frame_width + 3,FrameY + 7);
printf("speed : %d",snake->speed);//打印游戏速度
gotoxy(hOut,FrameX + 2 * Frame_width + 3,FrameY + 9);
printf("foodX : %d",food->x);//打印食物的横坐标
gotoxy(hOut,FrameX + 2 * Frame_width + 3,FrameY + 11);
printf("foodY : %d",food->y);//打印食物的竖坐标
}
/*随机产生食物*/
void get_food(HANDLE hOut,struct Snake *snake,struct Food *food)
{
srand((unsigned)time(NULL));//初始化随机数
while(1)//产生食物条件:1.在游戏窗口内 2.不在蛇身上
{
food->x = rand() % (Frame_width - 1);
food->y = rand() % Frame_height;
if(food->x == 0||food->y == 0)
{
continue;
}
food->x = 2*food->x + FrameX;//得到食物横坐标
food->y += FrameY;//得到食物纵坐标
for(i = 0;i < snake->length;i ++)
{
if(food->x == snake->x[i] && food->y == snake->y[i])
break;
}
if(i == snake->length)
{
gotoxy(hOut,food->x,food->y);
printf("⊙");
break;
}
}
}
/*初始化蛇*/
void init_snake(struct Snake *snake)
{
snake->x[0] = FrameX + 2;
snake->y[0] = FrameY + Frame_height/2;
snake->speed = 300;
snake->length = 3;
snake->count = 0;
for(i = 1;i < snake->length;i++)
{
snake->x[i] = snake->x[i-1] + 2;
snake->y[i] = snake->y[i-1];
}
}
/*打印蛇*/
void print_snake(HANDLE hOut,struct Snake *snake)
{
for(i = 0;i < snake->length;i++)
{
gotoxy(hOut,snake->x[i],snake->y[i]);
if(i == 0)
{
printf("○"); //打印蛇尾
}
else if(i == snake->length-1)
{
printf("¤"); //打印蛇头
}
else
{
printf("⊙"); //打印蛇身
}
}
}
/*移动蛇*/
void move_snake(HANDLE hOut,struct Snake *snake)
{
gotoxy(hOut,snake->x[0],snake->y[0]);
printf(" ");
for(i = 1;i < snake->length;i ++)
{
snake->x[i - 1] = snake->x[i];
snake->y[i - 1] = snake->y[i];
}
}
/*吃食物*/
void eat_food(HANDLE hOut,struct Snake *snake,struct Food *food)
{
if(snake->x[snake->length-1] == food->x && snake->y[snake->length-1] == food->y)
{//如果蛇头位置与食物位置相同,吃食物
snake->length++;
for(i = snake->length - 1;i >= 1;i --)
{
snake->x[i] = snake->x[i-1];
snake->y[i] = snake->y[i-1];
}
snake->x[0] = a[0];
snake->y[0] = a[1];
get_food(hOut,snake,food);//重新产生食物
snake->count++;
if(snake->count%5 ==0)//升级加速
{
snake->speed -= 50;
}
}
}
/*穿墙*/
void through_wall(HANDLE hOut,struct Snake *snake,char ch)
{
if(ch == 72 && snake->y[snake->length-1] == FrameY)
{
snake->y[snake->length-1] = FrameY + Frame_height - 1;
}
if(ch == 80 && snake->y[snake->length-1] == FrameY + Frame_height)
{
snake->y[snake->length-1] = FrameY + 1;
}
if(ch == 75 && snake->x[snake->length-1] == FrameX)
{
snake->x[snake->length-1] = FrameX + 2*Frame_width - 4;
}
if(ch == 77 && snake->x[snake->length-1] == FrameX + 2*Frame_width - 2)
{
snake->x[snake->length-1] = FrameX + 2;
}
}
/*判断蛇是否死*/
int if_die(struct Snake *snake)
{
for(i = 0;i < snake->length-1;i++)
{
if(snake->x[snake->length-1] == snake->x[i] && snake->y[snake->length-1] == snake->y[i])
{
return 0;
}
}
return 1;
}
/*开始游戏*/
void start_game()
{
unsigned char ch = 77;//定义用于接受键盘输入的字符变量
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);//定义显示器句柄变量
struct Snake s,*snake = &s;//定义蛇的结构体指针并指向
struct Food f,*food = &f;//定义食物的结构体指针并指向
make_frame();
init_snake(snake);
get_food(hOut,snake,food);
while(1)
{
print_information(hOut,snake,food);
a[0] = snake->x[0];//记住蛇尾的横坐标
a[1] = snake->y[0];//记住蛇尾的纵坐标
j = 0;
if(kbhit())//判断是否按下键盘,如果按下,ch接收
{
ch=getch();
if(kbhit())//如果长按键盘,加速
{
Sleep(20);
j = 1;
}
}
switch(ch)
{
case 72:
{/* 向上移动 */
move_snake(hOut,snake);
snake->y[snake->length-1]-=1; //蛇头的竖坐标向上移,即减1
break;
}
case 80:
{/* 向下移动 */
move_snake(hOut,snake);
snake->y[snake->length-1]+=1; //蛇头的竖坐标向下移,即加1
break;
}
case 75:
{/* 向左移动 */
move_snake(hOut,snake);
snake->x[snake->length-1]-=2; //蛇头的横坐标向左移,即减2
break;
}
case 77:
{/* 向右移动 */
move_snake(hOut,snake);
snake->x[snake->length-1]+=2; //蛇头的横坐标向右移,即加2
break;
}
}
through_wall(hOut,snake,ch);
eat_food(hOut,snake,food);
print_snake(hOut,snake);
if(if_die(snake) == 0 || ch == 27 || snake->speed == 50)
{//死亡条件:1.碰到自身 2.按Esc键 3.速度达到50
gotoxy(hOut,FrameX + Frame_width - 2,FrameY + Frame_height/2 - 1);
printf("Game Over");
Sleep(2000);
break;
}
if(j == 0)
{
Sleep(snake->speed);//延迟时间
}
else
{
Sleep(10);
}
}
}
/*结束游戏*/
void over_game()
{
system("cls");
printf("\n\n\n\n\n\n\n\n\t\t\t\t游戏结束\n\n\n");
Sleep(2000);
getch();
}
基本内容就在代码里了,小狼会继续努力,下次会给大家带来更优化的代码的
皮皮虾,我们走,得儿,驾...