用c语言写的贪吃蛇项目
花里胡哨的贪吃蛇你值得拥有,话多不多说直接上图!
代码在这里喔,代码不多上手敲就完事,如果这个项目帮助到你,希望可以点亮小心心给点鼓励!
#include <stdio.h>
#include <Windows.h>
#include <time.h>
#include <conio.h>
struct point_pos //此结构体保存一个点的坐标
{
int x;
int y;
};
struct point_pos bean; //豆子
int score = 0;
int level = 1;
int speed = 10; //值越小,速度越快
int block[20][20] = {
0 }; //背景,1 墙, 0 空格
int color[7] = {
31, 32, 33, 34, 35, 36, 37};
int snake_head_color = 0; //color[0]
int snake_body_color = 1; //color[1]
int bean_color = 2; //color[2]
void gotoxy(int x, int y)
{
printf("\033[%d;%dH", y + 1, x + 1);
}
struct point_pos snake[100] = {
0 }; //蛇身
int snake_len = 3; //蛇的长度,最长100个点
void new_snake() //出一个蛇, 有两个点, 一个蛇头,一个蛇身
{
snake[0].x = 9;
snake[0].y = 9;
snake[1].x = 9;
snake[1].y = 10;
snake[2].x = 9;
snake[2].y = 11;
}
void draw_snake() //画一个蛇
{
int i;
gotoxy(snake[0].x * 2, snake[0].y); //
printf("\033[1;%dm★\033[0m", color[snake_head_color]); //先画蛇头
for(i = 1; i < snake_len; i++)
{
gotoxy(snake[i].x * 2, snake[i].y); //
printf("\033[1;%dm◆\033[0m", color[snake_body_color]); //再画蛇头
}
}
int bean_on_snake()
{
int i;
for(i = 0; i < snake_len; i++)
{
if(bean.x == snake[i].x && bean.y == snake[i].y)
{
return 1;
}
}
return 0;
}
void new_bean() //出一个新豆子, 只要将x, y 赋值即可
{
do
{
bean.x = rand() % 16 + 2; // 2 - 17
bean.y = rand() % 16 + 2;
}while(bean_on_snake() == 1);
}
void draw_bean() //画一个豆子
{
gotoxy(bean.x * 2, bean.y); //跳转到坐标点
printf("\033[1;%dm●\033[0m", color[bean_color]); //打印出一个豆子
}
void init_block() //初始化墙,四边是1
{
int i;
for(i = 0; i < 20; i++)
{
block[0][i] = 1; //让数组中相应位置是墙,后面会描画
block[19][i] = 1;
block[i][0] = 1;
block[i][19] = 1;
}
}
void draw_block() //将墙画出来
{
int i, j;
gotoxy(0, 0);
for(i = 0; i < 20; i++)
{
for(j = 0; j < 20; j++)
{
if(block[i][j] == 1)
printf("\033[1;34m□\033[0m");
else
printf(" "); //能实现擦除功能
}
printf("\r\n");
}
}
#define UP 0
#define DOWN 1
#define LEFT 2
#define RIGHT 3
int direction = UP; //0----> up
void add_bean_to_snake() //豆子变蛇头 , 蛇身+1
{
int i;
snake_len++;
for(i = snake_len - 1; i > 0; i--)
{
snake[i] = snake[i - 1];
}
snake[0].x = bean.x;
snake[0].y = bean.y;
new_bean();
score += 100;
if(score % 500 == 0 && score != 0)
{
level++;
speed--;
if(speed < 1)
speed = 1;
}
snake_body_color++;
snake_body_color = snake_body_color % 7;
snake_head_color++;
snake_head_color = snake_head_color % 7;
bean_color++;
bean_color = bean_color % 7;
}
void snake_move_right() //蛇向右走
{
//蛇头 x+1, y不变
int i;
if(snake[0].x + 1 == bean.x && snake[0].y == bean.y)
{
//将豆子添加到蛇中
add_bean_to_snake();
return;
}
for(i = snake_len - 1; i > 0; i--) //除了蛇头,每个节点前移
{
snake[i] = snake[i - 1];
}
snake[0].x++; //y不变
}
void snake_move_left() //蛇向左走
{
//蛇头 x+1, y不变
int i;
if(snake[0].x - 1 == bean.x && snake[0].y == bean.y)
{
//将豆子添加到蛇中
add_bean_to_snake();
}
for(i = snake_len - 1; i > 0; i--) //除了蛇头,每个节点前移
{
snake[i] = snake[i - 1];
}
snake[0].x--; //y不变
}
void snake_move_up() //蛇向上走
{
//蛇头 x+1, y不变
int i;
if(snake[0].x == bean.x && snake[0].y - 1 == bean.y)
{
//将豆子添加到蛇中
add_bean_to_snake();
}
for(i = snake_len - 1; i > 0; i--) //除了蛇头,每个节点前移
{
snake[i] = snake[i - 1];
}
snake[0].y--; //y不变
}
void snake_move_down() //蛇向下走
{
//蛇头 x+1, y不变
int i;
if(snake[0].x == bean.x && snake[0].y + 1 == bean.y)
{
//将豆子添加到蛇中
add_bean_to_snake();
}
for(i = snake_len - 1; i > 0; i--) //除了蛇头,每个节点前移
{
snake[i] = snake[i - 1];
}
snake[0].y++; //y不变
}
void snake_move() //因为如果没按按键,蛇也应该自己走
{
if(direction == RIGHT)
{
snake_move_right();
}
else if(direction == LEFT)
{
snake_move_left();
}
else if(direction == UP)
{
snake_move_up();
}
else if(direction == DOWN)
{
snake_move_down();
}
}
int judge_game_over() //蛇撞墙, 蛇撞自身
{
int i;
if(snake[0].x > 18 || snake[0].x < 1 || snake[0].y > 18 || snake[0].y < 1)
return 1; //游戏结束
for(i = 1; i < snake_len; i++)
{
if(snake[0].x == snake[i].x && snake[0].y == snake[i].y)
{
return 1; //游戏结束
}
}
return 0;
}
int main()
{
int i;
char ch;
system("cls");
HANDLE fd = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cinfo;
cinfo.bVisible = 0;
cinfo.dwSize = 1;
SetConsoleCursorInfo(fd,&cinfo); //不显示光标
srand(time(0)); //产生随机种子
init_block();
new_bean();
new_snake();
while(1)
{
draw_block(); //清除整个屏幕
gotoxy(46, 1);
printf("score:%d", score);
gotoxy(46, 2);
printf("level:%d", level);
draw_bean();
draw_snake();
for(i = 0; i < speed; i++)
{
if(_kbhit()) //开一个新线程,专门用来等待按键
{
ch = _getch(); //返回值cmd 是按键值, 75:left 77:right 80:down
}
if(ch == 77 && direction != LEFT)
{
direction = RIGHT;
ch = 0;
break;
}
else if(ch == 75 && direction != RIGHT)
{
direction = LEFT;
ch = 0;
break;
}
else if(ch == 72 && direction != DOWN)
{
direction = UP;
ch = 0;
break;
}
else if(ch == 80 && direction != UP)
{
direction = DOWN;
ch = 0;
break;
}
Sleep(50); //50ms
}
snake_move();
if(judge_game_over() == 1)
{
gotoxy(20, 10);
printf("game over\n");
return 0;
}
}
while(1);
}