貪吃蛇c原始碼

qq_41617031發表於2020-12-15

貪吃蛇c原始碼

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<graphics.h>
#include<conio.h>
#include<time.h>
#include<mmsystem.h>
#pragma comment(lib,"winmm.lib")
//定義座標結構體
 struct Point       
{
	int x,y;
	

};
//定義蛇的結構體
 struct Snake {
	 Point xy[100];//座標
	int position;//方向
	int num;   //長度
};
 struct food {
	 Point fdxy;//座標
	int flag;//標記
	int grade;//分數
};
//列舉:方向
enum position { up, down, left, right };
/*****************模組化設計**********************/
//初始化蛇
Snake snake{};
//初始化食物
food _food{};
Snake initsnake()
{
	

	//開始有三個身子,第三個矩形的x,y在原點,一個矩形是10*10的正方形
	snake.xy[2].x = 0;
	snake.xy[2].y = 0;
	snake.xy[1].x = 10;
	snake.xy[1].y = 0;
	snake.xy[0].x = 20;
	snake.xy[0].y = 0;
	snake.position = right;
	snake.num = 3;
	

	
	return snake;


}
//畫蛇

	//繪製填充矩形
void drawSnake()
{

	for (int i = 0; i < snake.num; i++) {
		setlinecolor(BLACK);//設定矩形邊框顏色
		setfillcolor(RGB(rand() % 255, rand() % 255, rand() % 255));
		fillrectangle(snake.xy[i].x, snake.xy[i].y, snake.xy[i].x + 10, snake.xy[i].y + 10);

	}
}
	//動起來
void Snakemove() {

	for (int i = snake.num - 1; i > 0; i--) {
		snake.xy[i].x = snake.xy[i - 1].x;
		snake.xy[i].y = snake.xy[i - 1].y;

	}
	//蛇頭的移動
	switch (snake.position) {
	case up:
		snake.xy[0].y -= 10;
		break;
	case down:
		snake.xy[0].y += 10;
		break;
	case left:
		snake.xy[0].x -= 10;
		break;
	case right:
		snake.xy[0].x += 10;
		break;




	}

}
//按鍵操作
void keydown() {
	char userkey = _getch();
	switch (userkey) {
		case'w':
		case'W':
		case 72:
			if (snake.position != down) {
				snake.position = up;
			}
			break;
		case's':
		case'S':
		case 80:
			if (snake.position != up) {
				snake.position = down;
			}
			break;
		case'a':
		case'A':
		case 75:
			if (snake.position != right) {
				snake.position = left;
			}
			break;
		case'd':
		case'D':
		case 77:
			if (snake.position != left) {
				snake.position = right;
			}
			break;

	}
}
//食物
//初始化食物
void initfood() {
	_food.fdxy.x = rand() % 80 * 10;
	_food.fdxy.y = rand() % 60 * 10;
	_food.flag = 1;//1代表食物還存在
	//食物不能出現在蛇的身上
	for (int i = 0; i < snake.num; i++) 
	{
		if ((snake.xy[i].x == _food.fdxy.x) && (snake.xy[i].y == _food.fdxy.y) )
		{
			_food.fdxy.x = rand() % 80 * 10;
			_food.fdxy.y = rand() % 60 * 10;
		}
	}
}
//drawfood
void drawfood() {
	
		setlinecolor(BLACK);//設定矩形邊框顏色
		setfillcolor(RGB(rand() % 255, rand() % 255, rand() % 255));
		fillrectangle(_food.fdxy.x, _food.fdxy.y, _food.fdxy.x + 10, _food.fdxy.y + 10);

	



}
//吃食物
void eatfood() {
	if ((snake.xy[0].x == _food.fdxy.x) && (snake.xy[0].y == _food.fdxy.y)) {
		_food.flag = 0;
		snake.num++;
		_food.grade += 10;
	}


}
//顯示分數
void showgrade() {
	//格式化列印
	char grade[20] = "";
	sprintf(grade, "得分:%d", _food.grade);
	settextcolor(LIGHTGREEN);
	settextstyle(25, 0, "楷體");
	outtextxy(650, 50,grade);
}
//蛇死亡
HWND hwnd = NULL;
int snakedie() {
	//撞牆
	if (snake.xy[0].x > 800 || snake.xy[0].x < 0 || snake.xy[0].y>600 || snake.xy[0].y < 0) {
		mciSendString("close 1.mp3 ", 0, 0, 0);
		MessageBox(hwnd, "撞牆了,遊戲結束", "GameOver", MB_OK);
		return 1;
	}
	//撞到自己
	for (int i = 1; i < snake.num; i++) {
		if (snake.xy[0].x == snake.xy[i].x && snake.xy[0].y == snake.xy[i].y) {
			mciSendString("close 1.mp3 ", 0, 0, 0);
			MessageBox(hwnd, "自殺了,遊戲結束", "GameOver", MB_OK);
			return 1;
		}
	}
	return 0;

}
int main()
{
	
	srand((unsigned int)time(NULL));
	//建立視窗:初始化影像環境
	initgraph(800,600);
	setbkcolor(RGB(248, 219, 86));//設定背景顏色
	
	cleardevice();//清屏
	mciSendString("open 1.mp3 ", 0, 0, 0);
	mciSendString("play 1.mp3 ", 0, 0, 0);
	initsnake();
	drawSnake();
	while (1) {
		cleardevice();
		Snakemove();
		drawSnake();
		if (_food.flag == 0) {
			initfood();
		}
		if (_kbhit()) {
			keydown();
		}
		if (snakedie()) {
			break;
		}
		drawfood();
		eatfood();
		showgrade();
		Sleep(60);//Sleep()函式首字母要大寫
	}

	int a=_getch();//防止閃屏:等待使用者輸入一個字元
	void closegraph();
	
	return 0;
}

相關文章