C语言小游戏 :贪吃蛇 代码

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <conio.h>
#include <Windows.h>
#define WIDE 60
#define HIGH 20
#define _CRT_SECURE_NO_WARNINGS




int score = 0;	

int qx = 0;			
int qy = 0;

int lX = 0;		
int lY = 0;
int sleepSecond = 400;



     struct BODY 
	 {
	    int x;
	    int y;
	 };



struct FOOD 
	{
	int x;
	int y;
	}food;



      
    struct SNAKE 
	{
	struct BODY body[WIDE*HIGH];
	int size;
	}snake;			



void initWall(void)
{
	int i=0;
	int j=0;
	for (i = 0; i <= HIGH; i++)	
	{
		for (j = 0; j <= WIDE; j++)			
		{
			if (j == WIDE)
			{
				printf("|");
			}
			else if (i == HIGH)
			{
				printf("_");
			}
			else
			{
				printf(" ");
			}
		}
		printf("\n");
	}
}




void pos(int x,int y)
{
COORD coord;
HANDLE houtput;
coord.X=x;
coord.Y=y;
houtput=GetStdHandle(STD_OUTPUT_HANDLE);

SetConsoleCursorPosition(houtput, coord);
}

void initSnake(void)
{
	snake.size = 2;

	snake.body[0].x = WIDE / 2;		
	snake.body[0].y = HIGH / 2;

	snake.body[1].x = WIDE / 2 - 1;	
	snake.body[1].y = HIGH / 2;

	return;
}




void initFood(void)
{   srand(time(NULL));
	food.x = rand() % WIDE;  
	food.y = rand() % HIGH;  
	return;
}



void initUI(void)
{
	COORD coord = {0};
	int i=0;

	
	for ( i = 0; i < snake.size; i++)
	{
		pos(snake.body[i].x,snake.body[i].y);
		if (i == 0)
			putchar('@');
		else
			putchar('*');
	}
	
      pos(lX,lY);
	putchar(' ');

	
	 pos( food.x, food.y);
	putchar('#');
}


void playGame(void)
{
	char key = 'd';
	int i=0;

	
	while (snake.body[0].x >= 0 && snake.body[0].x < WIDE
		&& snake.body[0].y >= 0 && snake.body[0].y < HIGH)
	{
	
		initUI();

		if (_kbhit()) {				
			key = _getch();
		}
		switch (key)
		{
		case 'w': qx = 0; qy = -1; break;
		case 's': qx = 0; qy = +1; break;
		case 'd': qx = +1; qy = 0; break;
		case 'a': qx = -1; qy = 0; break;
		default:
			break;
		}

		
		for (i = 1; i < snake.size; i++)
		{
			if (snake.body[0].x == snake.body[i].x
				&& snake.body[0].y == snake.body[i].y)
			{
				return;		
			}
		}

		
		if (snake.body[0].x == food.x && snake.body[0].y == food.y)
		{
			initFood();		
			snake.size++;	
			score += 10;	

			sleepSecond -= 5;	
		}

		
		lX = snake.body[snake.size - 1].x;
		lY = snake.body[snake.size - 1].y;

		
		for (i = snake.size - 1; i > 0; i--)
		{
			snake.body[i].x = snake.body[i - 1].x;
			snake.body[i].y = snake.body[i - 1].y;
		}
		snake.body[0].x += qx;		
		snake.body[0].y += qy;

		Sleep(sleepSecond);
	
		//system("cls");	
	}

	return;
}

void showScore(void)
{
	
	COORD coord;

	coord.X = 0;
	coord.Y = HIGH + 2;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);

	printf("Game Over!!!\n");
	printf("成绩为:%d\n", score);
}
int main()
{


	CONSOLE_CURSOR_INFO cci;
	cci.dwSize = sizeof(cci);
	cci.bVisible = FALSE;  
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cci);

	srand(time(NULL));  

	initSnake();	
	initFood();		

	initWall();		
	initUI();		

	playGame();		
	showScore();	


return 0;
}

代码有许多不足,请大家多多指正

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值