1. 程式人生 > >貪吃蛇(移動游標實現)

貪吃蛇(移動游標實現)

我將其寫成了一個貪吃蛇類

主程式

#include<stdio.h>
#include"SnakeGame.h"
int main()
{
	SnakeGame a;//建立一個貪吃蛇遊戲
	system("pause");//系統暫停
	return 0;
}

SnakeGame.h標頭檔案
#pragma once
#include<iostream>
#include<list>
#include<conio.h>
#include<Windows.h>
#include<time.h>
#include<string>
using namespace std;
class SnakeGame
{
public:
	SnakeGame();
	virtual ~SnakeGame();
private:
	int dir;//方向
	string q[4] = { "你真厲害!        ",
		            "666 (⊙o⊙)    ",
		            "吃到50個有獎勵QWQ", 
		            "加油 (*>.<*)   " };
	int grades;//分數
	COORD coord;//儲存下一次前進的點
	list<COORD>Snake;//儲存蛇身
	int mp[26][26];//儲存地圖
public:
	void CreateMp();//初始化地圖
	void SetCOORD(int x, int y);//移動游標
	void Move();//蛇移動
	bool GameOver(int x, int y);//判斷蛇是否死亡
	void CreateFood();//建立一個食物
	int ChangeDir(char ch);//將字元轉換成數字w:1 a:2 s:3 d:4
	void Mood();//打印表情QWQ
	void SetColor();
};



SnakeGame.cpp檔案

#include "SnakeGame.h"

SnakeGame::SnakeGame()
{
	dir = 0;
	grades = 0;
	unsigned int t = clock();
	srand(t);
	CreateMp();
	CreateFood();
	dir = 1;//先向上走
	coord.X = 3;
	coord.Y = 7;
	mp[7][3] = 3;
	SetCOORD(coord.X*2,coord.Y);
	printf("●");
	//system("pause");
	Snake.push_front(coord);
	while(!_kbhit())
	{
	}
	SetCOORD(6 * 2, 10);
	cout << "                  ";
	Move();
		
}


SnakeGame::~SnakeGame()
{
}


void SnakeGame::CreateMp()
{
	memset(mp, 0, sizeof(mp));
	SetColor();
	for (int i = 0; i < 21; i++)
	{
		mp[i][0] = 1;
		mp[0][i] = 1;
		SetCOORD(0, i);
		cout << "■";
		Sleep(20);
		SetCOORD(i * 2, 0);
		cout << "■";
		Sleep(20);
	}
	for (int i = 0; i < 21; i++)
	{
		mp[20][i] = 1;
		mp[i][20] = 1;
		SetCOORD(20*2, i);
		cout << "■";
		Sleep(20);
		SetCOORD(i * 2, 20);
		cout << "■";
		Sleep(20);
	}
	SetCOORD(6 * 2, 10);
	cout << "請按任意鍵開始遊戲";
	SetCOORD(2, 23);
	cout << "方向控制:";
	SetCOORD(2, 24);
	cout << "上:W 下:S 左:A 右:D";
	SetCOORD(24*2, 2);
	cout << "你的得分為:" << grades;
	SetCOORD(2, 25);
	cout << "製作:ZZK" << endl;
}


void SnakeGame::SetCOORD(int x, int y)//設定游標
{
	COORD xx;//windows.h裡的一個結構體,有兩個引數 X 和 Y 分別代表列和行
	xx.X = x;
	xx.Y = y;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), xx);//windows.h裡的函式--把游標移到xx處
}


void SnakeGame::Move()//移動
{
	char ch = 'w';
	while (true)
	{
		ch = _getch();//立即從鍵盤的到一個字元
		while (!_kbhit())//_kbhit()函式沒有字元時輸入返回0
		{
			int kk = ChangeDir(ch);
			if (kk != dir&&abs(kk - dir) != 2)//判斷是否能改變方向
			{
				dir = kk;
			}
			coord = Snake.front();
			switch (dir)//1上2左3下4右
			{
			case 1:coord.Y--; break;
			case 2:coord.X--; break;
			case 3:coord.Y++; break;
			case 4:coord.X++; break;
			}
			if (GameOver(coord.X, coord.Y))//判讀蛇是否死掉
			{
				system("cls");
				COORD s;
				s.X = 10 * 2;
				s.Y = 10;
				SetCOORD(s.X, s.Y);
				cout << "遊戲結束,你掛了QWQ";
				SetCOORD(s.X, s.Y + 1);
				cout << "你的分數為:" << grades;
				SetCOORD(s.X, s.Y + 2);
				cout << "要重新開始咩?";
				SetCOORD(s.X, s.Y + 3);
				cout << "1:重開就重開 2:算了吧";
				SetCOORD(s.X, s.Y + 4);
				cout << "你的選擇:";
				int kkk;
				cin >> kkk;
				if (kkk == 1)
				{
					system("cls");
					SnakeGame();
					return;
				}
				return;
			}
			if (mp[coord.Y][coord.X] == 2)//吃到食物
			{
				SetColor();
				grades++;
				SetCOORD(24 * 2, 2);
				cout << "你的得分為:" << grades;
				SetCOORD(coord.X * 2, coord.Y);
				cout << "●";
				mp[coord.Y][coord.X] = 3;
				Snake.push_front(coord);
				CreateFood();
				COORD ss;
				ss = Snake.front();
				SetCOORD(ss.X * 2, ss.Y);
				cout << "●";
				Mood();

			}
			else//沒吃到食物
			{
				SetColor();
				SetCOORD(coord.X * 2, coord.Y);
				cout << "●";
				mp[coord.Y][coord.X] = 3;
				Snake.push_front(coord);
				COORD ss;
				if (Snake.size() != 1)
				{
					ss = Snake.front();
					SetCOORD(ss.X * 2, ss.Y);
					cout << "●";
				}
				ss = Snake.back();
				Snake.pop_back();
				mp[ss.Y][ss.X] = 0;
				SetCOORD(ss.X * 2, ss.Y);
				cout << "  ";
			}
			if (grades * 10 == 250)
			{
				Sleep(250);
			}
			else
			Sleep(500-grades*10);
		}
	}
}


bool SnakeGame::GameOver(int x, int y)
{
	if (mp[y][x] == 1||mp[y][x]==3)
		return true;
	else
		return false;
}


void SnakeGame::CreateFood()
{
	int x, y;
	do
	{
		x = rand() % 20;
		y = rand() % 20;
		if (mp[y][x] == 0)
		{
			break;
		}
	} while (1);
		mp[y][x] = 2;//食物標記為2
		SetCOORD(x*2 , y);
		SetColor();
		cout << "★"; 
}


int SnakeGame::ChangeDir(char ch)
{
	switch (ch)
	{
	case 'w':
	case 'W':return 1;
	case 'a':
	case 'A':return 2;
	case 's':
	case 'S':return 3;
	case 'd':
	case 'D':return 4;
	}
	return 0;
}


void SnakeGame::Mood()
{
	int x = 25, y = 6;
	SetCOORD(x * 2, y);
	int k;
	k = rand() % 4;
	cout << q[k];

}


void SnakeGame::SetColor()
{
	HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
	unsigned short x;
	x = rand() % 15+1;
	SetConsoleTextAttribute(hCon, (x) | (0));
}