【C++ 程式】 貪吃蛇遊戲(EasyX 圖形介面)

2020-09-21 11:00:47

這個程式解決了我之前 【C++ 程式】 貪吃蛇遊戲 中螢幕一直在閃的問題。具體介紹詳見我的部落格 【C++ 程式】 井字棋遊戲(人 VS Lv3電腦)(戰績統計版)(EasyX 圖形介面)

程式

// This is a simple snake game

#include <iostream>
#include <string>
#include <vector>
#include <cstddef>
#include <Windows.h>
#include <conio.h>
#include <random>
#include <ctime>
#include <deque>
#include <graphics.h>
using namespace std;

IMAGE iat/* @ */, iO/* O */, istar/* * */, ihash/* # */, iw, game_over;

struct location {
	unsigned x, y;
}loc1, loc2;

int state = 0;
int ret = 4; // head right by default
unsigned cnt = 0;
unsigned score = 0;
char p[25][30]; // the board
vector<location> vec;
deque<location> snake;

void p_def() // define p
{
	for (int i = 0; i != 25; i++)
	{
		if (i != 0 && i != 24)
		{
			p[i][0] = p[i][29] = '*';
			for (int j = 1; j != 29; j++)
				p[i][j] = ' ';
		}
		else
		{
			for (int j = 0; j != 30; j++)
				p[i][j] = '*';
		}
	}
	p[13][5] = 'O'; // body
	p[13][6] = '@'; // head
	loc1.x = 13;
	loc1.y = 6; // the initial location of the head of the snake
	loc2.x = 13;
	loc2.y = 5; // the initial location of the tail of the snake
	snake.push_front(loc2); // store the first location of the snake tail
}

void input(char c1, char c2)
{
	c1 = _getch();
	c2 = _getch();
	switch (c2)
	{
	case 72: // up
		if (ret != 2)
			ret = 1;
		break;
	case 80: // down
		if (ret != 1)
			ret = 2;
		break;
	case 75: // left
		if (ret != 4)
			ret = 3;
		break;
	case 77: // right
		if (ret != 3)
			ret = 4;
		break;
	default:
		break;
	}
}

void print1(char c, int i, int j)
{
	switch (c)
	{
	case '*':
		loadimage(&istar, _T("star.jpg"), 22, 22);
		putimage(50 + 22 * j, 180 + 22 * i, &istar);
		break;
	case '#':
		loadimage(&ihash, _T("hash.jpg"), 22, 22);
		putimage(50 + 22 * j, 180 + 22 * i, &ihash);
		break;
	case '@':
		loadimage(&iat, _T("at.jpg"), 22, 22);
		putimage(50 + 22 * j, 180 + 22 * i, &iat);
		break;
	case 'O':
		loadimage(&iO, _T("O.jpg"), 22, 22);
		putimage(50 + 22 * j, 180 + 22 * i, &iO);
		break;
	case ' ':
		loadimage(&iw, _T("whitespace.jpg"), 22, 22);
		putimage(50 + 22 * j, 180 + 22 * i, &iw);
	default:
		break;
	}
}

void print()
{
	for (int i = 0; i != 25; i++)
	{
		for (int j = 0; j != 30; j++)
			print1(p[i][j], i, j);
	}
}

void ran() // generate a random food for the snake to eat
{
	default_random_engine random1, random2;
	for (int i = 0; i != 644; i++)
	{
		unsigned ran1 = random1() % 23 + 1; // ranging from 1 to 23
		unsigned ran2 = random2() % 28 + 1; // ranging from 1 to 28
		location ran_l;
		ran_l.x = ran1;
		ran_l.y = ran2;
		vec.push_back(ran_l);
	}
}

void move()
{
	switch (ret)
	{
	case 1: // up
		if (p[loc1.x - 1][loc1.y] == 'O' || p[loc1.x - 1][loc1.y] == '*')
			state = 1; // indicate the end of the game
		else
		{
			if (p[loc1.x - 1][loc1.y] == '#')
			{
				score++;
				snake.push_front(loc1);
			}
			else
			{
				p[snake.back().x][snake.back().y] = ' '; // delete the end of the tail
				snake.pop_back();
				snake.push_front(loc1); // former head -> front of the tail
			}
		}
		--loc1.x;
		break;
	case 2: // down
		if (p[loc1.x + 1][loc1.y] == 'O' || p[loc1.x + 1][loc1.y] == '*')
			state = 1; // indicate the end of the game
		else
		{
			if (p[loc1.x + 1][loc1.y] == '#')
			{
				score++;
				snake.push_front(loc1);
			}
			else
			{
				p[snake.back().x][snake.back().y] = ' '; // delete the end of the tail
				snake.pop_back();
				snake.push_front(loc1); // former head -> front of the tail
			}
		}
		++loc1.x;
		break;
	case 3: // left
		if (p[loc1.x][loc1.y - 1] == 'O' || p[loc1.x][loc1.y - 1] == '*')
			state = 1; // indicate the end of the game
		else
		{
			if (p[loc1.x][loc1.y - 1] == '#')
			{
				score++;
				snake.push_front(loc1);
			}
			else
			{
				p[snake.back().x][snake.back().y] = ' '; // delete the end of the tail
				snake.pop_back();
				snake.push_front(loc1); // former head -> front of the tail
			}
		}
		--loc1.y;
		break;
	case 4: // right
		if (p[loc1.x][loc1.y + 1] == 'O' || p[loc1.x][loc1.y + 1] == '*')
			state = 1; // indicate the end of the game
		else
		{
			if (p[loc1.x][loc1.y + 1] == '#')
			{
				score++;
				snake.push_front(loc1);
			}
			else
			{
				p[snake.back().x][snake.back().y] = ' '; // delete the end of the tail
				snake.pop_back();
				snake.push_front(loc1); // former head -> front of the tail
			}
		}
		++loc1.y;
		break;
	default: //others
		break;
	}
	for (auto c : snake)
		p[c.x][c.y] = 'O'; // print the tail
	p[loc1.x][loc1.y] = '@'; // print the head
}

void init()
{
	initgraph(1200, 800);
	loadimage(0, _T("Welcome.jpg"), 1200, 800);
	Sleep(3000);
	initgraph(1200, 800);
	loadimage(0, _T("Instruction.jpg"), 1200, 800);
	Sleep(3000);
	loadimage(0, _T("Choice.jpg"), 1200, 800);// the board
}

char speed_input()
{
	char ret = '0';
	if (MouseHit())
	{
		MOUSEMSG mouse = GetMouseMsg();
		if (mouse.uMsg == WM_LBUTTONDOWN)
		{
			/**/ if (mouse.y > 150 && mouse.y < 350 && mouse.x < 600) ret = '1'; // Very Slow
			else if (mouse.y > 150 && mouse.y < 350 && mouse.x > 600) ret = '2'; // Slow
			else if (mouse.y > 350 && mouse.y < 550 && mouse.x < 600) ret = '3'; // Medium
			else if (mouse.y > 350 && mouse.y < 550 && mouse.x > 600) ret = '4'; // Fast
			else if (mouse.y > 550 && mouse.y < 750 && mouse.x < 600) ret = '5'; // Very Fast
			else if (mouse.y > 550 && mouse.y < 750 && mouse.x > 600) ret = '6'; // Very Very Fast
		}
	}
	return ret;
}

void print_score(unsigned n)
{
	setbkcolor(RGB(253, 236, 166)); // set the background colour
	settextstyle(120, 50, _T("Times New Roman")); // set the sytle
	settextcolor(BLUE); // set the colour
	TCHAR s01[5], s02[5], s03[5];
	swprintf_s(s01, _T("%d"), score % 10);
	swprintf_s(s02, _T("%d"), (score / 10) % 10);
	swprintf_s(s03, _T("%d"), (score / 100) % 10); // change into string first
	outtextxy(878, 300, s03);
	outtextxy(942, 300, s02);
	outtextxy(1006, 300, s01); // print
}

int main()
{
	init();
	char c1 = '\0', c2 = '\0';
	int time_set;
	char choice = '0';
	while (choice == '0')
	{
		choice = speed_input();
	}
	switch (choice) // set the speed
	{
	case '1':
		time_set = 1000;
		break;
	case '2':
		time_set = 700;
		break;
	case '3':
		time_set = 500;
		break;
	case '4':
		time_set = 350;
		break;
	case '5':
		time_set = 250;
		break;
	case '6':
		time_set = 150;
		break;
	default:
		time_set = 500;
		break;
	}
	p_def(); // define p
	Sleep(500);
	loadimage(0, _T("Board.jpg"), 1200, 800);// the background
	if (cnt % 5 == 0)ran();
	while (state == 0)
	{
		unsigned long clock_p = clock();
		print();
		print_score(score);
		do
		{
			if (_kbhit()) // test if there is a click on the keyboard
				input(c1, c2);
		} while (clock() - clock_p < time_set);
		move();
		if (p[(vec[cnt]).x][(vec[cnt]).y] == ' ' && cnt % 5 == 0)
			p[(vec[cnt]).x][(vec[cnt]).y] = '#'; // generate food for the snake to eat
		cnt++;
	}
	loadimage(&game_over, _T("over.jpg"), 320, 100);
	putimage(810, 500, &game_over);
	print_score(score);
	getchar();
	return 0;
}

// ALL RIGHTS RESERVED (c) 2020 Teddy van Jerry

輸出範例

Output

素材

(略)

分析

詳見我的部落格 【C++ 程式】 井字棋遊戲(人 VS Lv3電腦)(戰績統計版)(EasyX 圖形介面)

  • 提醒:進行滑鼠操作的函數應該巢狀在一個while中,否則會認為沒有輸入跳一下就沒了。

ALL RIGHTS RESERVED © 2020 Teddy van Jerry
歡迎轉載,轉載請註明出處。


See also

【C++ 程式】 井字棋遊戲(人 VS 人)
【C++ 程式】 井字棋遊戲(人 VS Lv1電腦)
【C++ 程式】 井字棋遊戲(人 VS Lv2電腦)
【C++ 程式】 井字棋遊戲(人 VS Lv3電腦)
【C++ 程式】 井字棋遊戲(人 VS Lv3電腦)(戰績統計版)
【C++ 程式】 五子棋遊戲(人 VS 人)
【C++ 程式】 五子棋遊戲(人 VS Lv1電腦)(思路及框架,內容待填充)
【C++ 程式】 亂數
【C++ 程式】 移動迷宮遊戲
【C++ 程式】 貪吃蛇遊戲
【C++ 程式】 數位推盤遊戲(15-puzzle)
【C++ 程式】 2048遊戲
【C++ 程式】 井字棋遊戲(人 VS 人)(EasyX 圖形介面)
【C++ 程式】 井字棋遊戲(人 VS Lv3電腦)(戰績統計版)(EasyX 圖形介面)
【C++ 程式】 2048遊戲(EasyX 圖形介面)