C语言-贪吃蛇

 main.c

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include "func.h"

int main(int argc, char* argv[])
{
	srand((unsigned)time(NULL));

	while (1) {
		//新的一局
		HideCursor();
		initInformation();
		playGame();
	}

	return EXIT_SUCCESS;
}

func.h

#pragma once
#include <conio.h>
#include <Windows.h>
#include <stdio.h>
#include "information.h"
void HideCursor();
void gotoxy(int x, int y);
void initInformation();
void initSnake();
void initFood();
void initWall();
void initOther();
void playGame();
void printUI();
void printWall();
void printSnake();
void printFood();
void printOther();
void setSnakeDirection(char ch);
void moveSnake();
void clearSnakeTail();
int adjudge();

 func.c

#include "func.h"

void gotoxy(int x, int y) {
	COORD coord;
	coord.X = x;
	coord.Y = y;

	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void HideCursor()
{
	CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };  // 第二个值为0表示隐藏光标
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);//函数和结构体都在windows.h中定义。
}

void initInformation() {
	initSnake();
	initFood();
	initWall();
	initOther();
}

void initSnake() {
	snake.size = 3;
	snake.direction = 'd';
	snake.headCh = '@';
	snake.bodyCh = '*';

	for (int i = 0; i < sizeof(snake.body) / sizeof(snake.body[0]); ++i) {
		snake.body[i].X = -1;
		snake.body[i].Y = -1;
	}

	Point head,body1,body2;
	head.X = (int)(WIDTH * (0.5));
	head.Y = (int)(HEIGHT * (0.5));
	body1.X = head.X - 1;
	body1.Y = head.Y;
	body2.X = head.X - 2;
	body2.Y = head.Y;

	snake.body[0] = head;
	snake.body[1] = body1;
	snake.body[2] = body2;
}

void initFood() {
	food.foodCh = '#';
	int pos = (HEIGHT - 2)* (WIDTH - 2)-snake.size;
	pos = rand() % pos;

	for (int i = 0; i < pos; i++) {
		int w = i % (WIDTH - 2)+1;
		int h = i / (WIDTH - 2)+1;
		for (int i = 0; i < snake.size; ++i) {
			if (w == snake.body[i].X && h == snake.body[i].Y) {
				pos++;
			}
		}
	}
	int w = pos % (WIDTH - 2) + 1;
	int h = pos / (WIDTH - 2) + 1;
	food.point.X = w;
	food.point.Y = h;
}
void initWall() {
	wall.wallCh = '%';
}

void initOther() {
	other.speed = 100;
	other.score = 0;
}
void playGame() {

	char key = ' ';
	int adj = 0;
	printUI();
	while (1) {
		if (_kbhit()) {
			key=(char)_getch();
		}
		setSnakeDirection(key);
		moveSnake();
		
		adj = adjudge();
		if ( adj == -1) {
			system("cls");
			printf("game over!!!!!!!!");
			system("pause");
			return;
		}
		else if (adj == 1) {
			--other.speed;
			++other.score;
		}
		clearSnakeTail();

		printUI();
		Sleep(other.speed);
	}
}

void printUI() {

	printWall();
	printSnake();
	printFood();
	printOther();
}

void printWall() {
	for (int i = 0; i < WIDTH; i++) {
		gotoxy(i, 0);
		putchar(wall.wallCh);
		gotoxy(i, HEIGHT - 1);
		putchar(wall.wallCh);
	}
	for (int j = 0; j < HEIGHT; j++) {
		gotoxy(0, j);
		putchar(wall.wallCh);
		gotoxy(WIDTH - 1, j);
		putchar(wall.wallCh);
	}
}

void printSnake() {

	for (int i = 0; i < snake.size; ++i) {
		gotoxy(snake.body[i].X, snake.body[i].Y);
		if (i == 0) putchar(snake.headCh);
		else  putchar(snake.bodyCh);
	}

}

void printFood() {
	gotoxy(food.point.X, food.point.Y);
	putchar(food.foodCh);
}
void printOther() {
	gotoxy(WIDTH + 20, 10);
	printf("score: %d", other.score);

}

void setSnakeDirection(char ch) {
	if (ch == 'w' && snake.direction == 's')return;
	if (ch == 'a' && snake.direction == 'd')return;
	if (ch == 's' && snake.direction == 'w')return;
	if (ch == 'd' && snake.direction == 'a')return;

	if(ch=='w' || ch=='a' || ch=='d'||ch=='s')
		snake.direction = ch;
}

void moveSnake() {
	for (int i = snake.size-1; i >=0 ; --i)
		snake.body[i + 1] = snake.body[i];
	if (snake.direction == 'w')snake.body[0].Y--;
	if (snake.direction == 's')snake.body[0].Y++;
	if (snake.direction == 'a')snake.body[0].X--;
	if (snake.direction == 'd')snake.body[0].X++;
}


int adjudge() {
	if (snake.body[0].X == food.point.X && snake.body[0].Y == food.point.Y)
	{
		snake.size++;
		initFood();
		return 1;
	}

	if (snake.body[0].X <= 0 || snake.body[0].X >= WIDTH - 1 || snake.body[0].Y <= 0 || snake.body[0].Y >= HEIGHT - 1)
	{
		return -1;
	}

	return 0;
}

void clearSnakeTail() {
	if (snake.body[snake.size].X != -1 && snake.body[snake.size].Y != -1) {
		gotoxy(snake.body[snake.size].X, snake.body[snake.size].Y);
		putchar(' ');
	}
}

 information.h

#pragma once

#define WIDTH 80
#define HEIGHT 30

typedef struct{
	int X;
	int Y;
}Point;

struct Snake {
	int size;
	char direction;
	char headCh;
	char bodyCh;		
	Point body[WIDTH*HEIGHT];

}snake;

struct Food {
	char foodCh;
	Point point;
}food;

struct Wall
{
	char wallCh;
}wall;
struct Other {
	int speed;
	int score;
}other;

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值