赞
踩
今天无意间翻到了大一用C语言写的贪吃蛇,竟然还标注着BUG满天飞,留存一下做个纪念,可能以后就找不到了
/*
此程序 -->
贪吃蛇3.0 Sur_流沐
当前版本: Bug满天飞
*/
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<conio.h>
#include<time.h>
#define Wide 88//边框宽
#define High 44//边框高
#define snack_HP 3//初始生命
#define snack_Length 3 //蛇的初始长度
#define snack_Sleep 500 //蛇的移动速度
#define Bomb_snack_Length 6 //出现炸弹时的蛇长
#define Bomb_num 0//初始炸弹个数
#define GoTo gotoxy(Wide+2,High+1)//将光标移至边框外
struct food{//食物
int x;
int y;
}food;
struct snack{//蛇
int x[100];
int y[100];
int sleep;//蛇移动速度
int length;//蛇长
int HP;//蛇的命数
int sum_score;//分数
}snack;
struct bomb{
int x[500];
int y[500];
int num;//炸弹个数
}bomb;
int snack_x,snack_y; //保存蛇头位置的变量 // 用于蛇头位置的改变
int i , e , k , m ; // ‘i’多用于循环 // ‘e’ 通常传递判断条件是否成立 // 'k 'm 用法类似
char ch = 'a';//初始化移动方向为向 左
void gotoxy(int x,int y);//坐标函数
void come();//初始化游戏
void menu();//封面
void rim();//边框
void DarwSnack();//画蛇
void DarwFood();//画食物
void move();//移动蛇身坐标
void DarwSnack_move();//蛇移动
void eat();//吃食物
void score();//评分系统
void DarwBomb();//产生炸弹
void Bomb_eat();//检查是否吃到炸弹
int main(){
come();//初始化
return 0;
}
void gotoxy(int x,int y){ //坐标函数
HANDLE handle = GetStdHandle (STD_OUTPUT_HANDLE);
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition (handle,coord);
}
void come(){//初始化 蛇 及 炸弹 信息
k = 500 , snack.sum_score = 0 , snack.HP = snack_HP;
snack.length = snack_Length;//初始蛇长
snack.sleep = snack_Sleep;//初始蛇速度
snack.x[0] = Wide/2,snack.y[0] = High/2;//初始蛇头位置
for (i = 1;i<snack.length;i++)//使用循环初始化蛇身
{
snack.x[i] = snack.x[i-1]+2;//初始蛇身坐标
snack.y[i] = snack.y[i-1];
}
bomb.num = Bomb_num ;//初始化炸弹个数
menu();
}
void menu(){//封面
system("color 3E");
system("mode con cols=140 lines=50");//改变窗口宽高 // cols 宽 lines 高
gotoxy(6,3);
printf("贪吃蛇3.0终极版! ——Sur流沐");
gotoxy(12,6);
system("pause");
system("cls");
rim();
DarwSnack();//第一次调用‘画蛇函数’目的是初始化蛇
DarwFood();//第一次调用‘画食物函数’母的初始化食物
DarwSnack_move();//游戏开始
}
void rim(){//边框
for (i = 0;i<=Wide;i+=2)
{
gotoxy(i,0);
printf("■");
gotoxy(i,High);
printf("■");
}
for (i = 0;i<=High;i++)
{
gotoxy(0,i);
printf("■");
gotoxy(Wide,i);
printf("■");
}
gotoxy(Wide+4,40);
printf("按任意键暂停!!");
gotoxy(Wide+4,30);
printf("tip:当蛇长超过5时随机产生炸弹!");
gotoxy(Wide+4,32);
printf("吃到炸弹会扣除蛇长及分数哦!!");
gotoxy(Wide+4,34);
printf("如果蛇长大于12,将无视炸弹带来的生命伤害");
gotoxy(Wide+4,44);
printf("BUG太多,不想修复了!!");
gotoxy(Wide+30,2);
printf("W-上");
gotoxy(Wide+25,4);
printf("A-左 D-右");
gotoxy(Wide+30,6);
printf("S-下");
GoTo;//将光标移到框外 。见宏定义
}
void DarwFood(){//画食物
srand((int)time(NULL));
for(i = 1;i!=0;)
{
food.x = rand()%10*4 + rand()%10*4 ;
food.y = rand()%10*2 + rand()%10*2 ;
if((food.x != 0 && food.y != 0) && (food.x!=snack.x[0] && food.y !=snack.y[0]) && (food.x < Wide && food.y < High))
{
i=0;
}
}
gotoxy(food.x,food.y);
printf("◆");
}
void DarwSnack(){//画蛇
gotoxy(snack.x[0],snack.y[0]);//光标移至蛇头位置
printf("◎");//打印蛇头
for (i = 1;i<snack.length;i++)//使用循环打印蛇身
{
gotoxy(snack.x[i],snack.y[i]);//光标移至某段蛇身
printf("○");//打印蛇身
}
}
void move(){//移动蛇身坐标
gotoxy(snack.x[snack.length-1],snack.y[snack.length-1]);//光标移至蛇尾,
printf(" ");//在蛇尾打印空格,使蛇有向前移动的状态之一
for (i = snack.length-1;i>0;i--)//依次改变蛇身的坐标
{
snack.x[i] = snack.x[i-1];
snack.y[i] = snack.y[i-1];
}
snack.x[0] = snack_x , snack.y[0] = snack_y;//将上次位置改变后的蛇头位置给蛇头/// 即 变换后的蛇头的位置
DarwSnack();//画出蛇
score();
Sleep(snack.sleep);
if(snack.sleep!=snack_Sleep-450)
snack.sleep-=5;
}
void DarwSnack_move(){//移动蛇
while(1)
{
snack_x = snack.x[0] , snack_y = snack.y[0];
if(kbhit()){ //kbhit 检测键盘是否有输入,//有返回
//用ch接收输入
ch=getch();
}
switch(ch)
{
case 'A':
case 'a':
snack_x-=2;
eat();
if(e)
{
snack.x[snack.length-1] = snack.x[snack.length-2] + 2;
snack.y[snack.length-1] = snack.y[snack.length-2];
}
move();
break;
case 'W':
case 'w':
snack_y--;
eat();
if(e)
{
snack.x[snack.length-1] = snack.x[snack.length-2];
snack.y[snack.length-1] = snack.y[snack.length-2] + 1;
}
move();
break;
case 'S':
case 's':
snack_y++;
eat();
if(e)
{
snack.x[snack.length-1] = snack.x[snack.length-2];
snack.y[snack.length-1] = snack.y[snack.length-2] - 1;
}
move();
break;
case 'D':
case 'd':
snack_x+=2;
eat();
if(e)
{
snack.x[snack.length-1] = snack.x[snack.length-2] - 2;
snack.y[snack.length-1] = snack.y[snack.length-2];
}
move();
break;
}
}
}
void eat(){//吃食物
Bomb_eat();//检查是否吃到了炸弹
e = 0;
if(snack.x[0] == food.x && snack.y[0] == food.y)//如果吃到食物
{
snack.length++;//蛇长增1
e = 1;
snack.sum_score += 40 ;//分数增加
DarwFood();
if(snack.length >= Bomb_snack_Length)//如果蛇长满足产生炸弹的条件
{
DarwBomb();//产生炸弹
}
}
if(snack.x[0] == 0 || snack.y[0] == 0 || snack.x[0] == Wide || snack.y[0] == High || snack.HP == 0)//游戏结束条件
{
system("cls");
score();
gotoxy(Wide/2,High/2);
printf("你over了。。。。。。。\n\n\n\t\t\t");
system("pause");
system("cls");
menu();
}
}
void score(){ //计分版及公告
gotoxy(Wide+4,2);
printf("Sur_流沐__制作!");
gotoxy(Wide+4,4);
printf("贪吃蛇3.0终极版!");
gotoxy(Wide+4,6);
printf("速度范围0~%d",snack_Sleep-50);
gotoxy(Wide+4,10);
printf("当前分数:%d",snack.sum_score);
gotoxy(Wide+4,12);
printf("当前速度:%d",snack_Sleep-snack.sleep) ;
gotoxy(Wide+4,14);
printf("当前蛇长:%d",snack.length);
gotoxy(Wide+4,16);
printf("剩余炸弹:%d",500 - bomb.num);
gotoxy(Wide+4,18);
printf("你还有%d条生命!",snack.HP);
}
void DarwBomb(){//产生炸弹
bomb.num ++ ;//炸弹个数增加
m = 1;
srand((int)time(NULL));
for(i = bomb.num -1; m!=0 ; )//随机产生炸弹
{
for(m = 1;m!=0;)
{
bomb.x[i] = rand()%10*4 + rand()%10*4 + rand()%10*2 ;
bomb.y[i] = rand()%10*2 + rand()%10*2 ;
if((bomb.x[i] != 0 && bomb.y[i] != 0) && (bomb.x[i] != snack.x[0] && bomb.y[i] !=snack.y[0]) && (bomb.x[i] < Wide && bomb.y[i] < High) && (bomb.x[i] != food.x)&&(bomb.y[i] != food.y))
{
m = 0;
}
}
gotoxy(bomb.x[i],bomb.y[i]);
printf("※");
}
}
void Bomb_eat(){//吃到炸弹??????
for(i = 0 ; i < bomb.num ; i++)
{
if(snack.x[0] == bomb.x[i] && snack.y[0] == bomb.y[i])//如果吃到炸弹
{
snack.sum_score-=100 ;//分数减去100
if(snack.length <= 11)//如果蛇长过短
{
snack.HP--;//扣除生命值
for( m = 0 ; m < bomb.num ; m++ )//扣除生命值的同时清除现存所有炸弹
{
gotoxy(bomb.x[m],bomb.y[m]);
printf(" ");
bomb.x[m] = Wide + 1 ;
bomb.x[m] = High + 1 ;//将炸弹坐标清除
}
}
//适于当蛇长大于11 //如果吃到炸弹
bomb.x[i] = Wide + 1 ;
bomb.x[i] = High + 1 ;//将被吃到的炸弹坐标清除
k = snack.length - 1;
snack.length-=5; //蛇长减5
if(snack.length >= 1)//清除蛇尾
{
for(m = k ; m > k - 5 ; m-- )
{
gotoxy(snack.x[m] , snack.y[m]);
printf(" ");
}
}
if(snack.length <= 1)//保持蛇长大于1
{
snack.length = 1 ;
snack.sleep += 100;
if(snack.sleep <= 50)//控制蛇速范围
{
snack.sleep = 20 ;
}
}
}
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。