เขียนเกมส์ pacman ด้วย c/c++

3 Jun 2017

Share to:

สวัสดีครับ เมื่อไม่กี่วันมานี้มีน้องคนนึงมาให้สอนเขียนเกมส์ pacman ด้วยภาษาซี ผมจึงนํา source code มาแบ่งปัน เผื่อใครสนใจ ตัวเกมส์ก็ไม่มีอะไรมากครับเป็น console application มีตัว pacman และตัว bot โง่ๆ 4 ตัว เขียนบน visual studio 2013 ส่วนวิธีการเล่น คือใช้ w, s, a, d เป็นปุ่มบังคับทิศทาง

  • w: ขึ้น
  • s: ลง
  • a: ซ้าย
  • d: ขวา

VDO

Source code pacman

#include "stdafx.h"
#include "vector"
#include "windows.h"
#include "stdio.h"
#include "conio.h"
#include "thread"
#include "mutex"

#define TRUE 1;
#define FALSE 0;
#define PACMAN_CHAR 2
#define BOT_CHAR 1
#define WALL_CHAR 219
#define DOT_CHAR 46
#define BANK_CHAR 0
#define DIRECTION_UP 'w'
#define DIRECTION_DOWN 's'
#define DIRECTION_LEFT 'a'
#define DIRECTION_RIGHT 'd'
#define MAXBOT 1
#define MAXROWS 32
#define MAXCOLUMNS 28
#define INCREASE_SCORE 10

struct charactor
{
	char direction = 0;
	int x = 0;
	int y = 15;
};

using namespace std;
int score = 0;
HANDLE  hConsole;
COORD coord;
std::mutex mtx;
charactor pacman;
bool gameOver = false;
bool gameWin = false;

char arrDirection[] = { (char)DIRECTION_UP, (char)DIRECTION_DOWN, (char)DIRECTION_LEFT, (char)DIRECTION_RIGHT };

char levelMatrix[MAXROWS][MAXCOLUMNS] = {
	{ WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR },
	{ WALL_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, WALL_CHAR },
	{ WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR },
	{ WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR },
	{ WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR },
	{ WALL_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, WALL_CHAR },
	{ WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR },
	{ WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR },
	{ WALL_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, WALL_CHAR },
	{ WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, BANK_CHAR, WALL_CHAR, WALL_CHAR, BANK_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR },
	{ WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, BANK_CHAR, WALL_CHAR, WALL_CHAR, BANK_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR },
	{ WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, BANK_CHAR, BANK_CHAR, BANK_CHAR, BANK_CHAR, BANK_CHAR, BANK_CHAR, BANK_CHAR, BANK_CHAR, BANK_CHAR, BANK_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR },
	{ WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, BANK_CHAR, WALL_CHAR, BANK_CHAR, BANK_CHAR, BANK_CHAR, BANK_CHAR, BANK_CHAR, BANK_CHAR, WALL_CHAR, BANK_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR },
	{ WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, BANK_CHAR, WALL_CHAR, BANK_CHAR, BANK_CHAR, BANK_CHAR, BANK_CHAR, BANK_CHAR, BANK_CHAR, WALL_CHAR, BANK_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR },
	{ WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, BANK_CHAR, WALL_CHAR, BANK_CHAR, BANK_CHAR, BANK_CHAR, BANK_CHAR, BANK_CHAR, BANK_CHAR, WALL_CHAR, BANK_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR },
	{ 074, BANK_CHAR, BANK_CHAR, BANK_CHAR, BANK_CHAR, BANK_CHAR, DOT_CHAR, BANK_CHAR, BANK_CHAR, BANK_CHAR, WALL_CHAR, BANK_CHAR, BANK_CHAR, BANK_CHAR, BANK_CHAR, BANK_CHAR, BANK_CHAR, WALL_CHAR, BANK_CHAR, BANK_CHAR, BANK_CHAR, DOT_CHAR, BANK_CHAR, BANK_CHAR, BANK_CHAR, BANK_CHAR, BANK_CHAR, BANK_CHAR },
	{ WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, BANK_CHAR, WALL_CHAR, BANK_CHAR, BANK_CHAR, BANK_CHAR, BANK_CHAR, BANK_CHAR, BANK_CHAR, WALL_CHAR, BANK_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR },
	{ WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, BANK_CHAR, WALL_CHAR, BANK_CHAR, BANK_CHAR, BANK_CHAR, BANK_CHAR, BANK_CHAR, BANK_CHAR, WALL_CHAR, BANK_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR },
	{ WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, BANK_CHAR, WALL_CHAR, BANK_CHAR, BANK_CHAR, BANK_CHAR, BANK_CHAR, BANK_CHAR, BANK_CHAR, WALL_CHAR, BANK_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR },
	{ WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, BANK_CHAR, BANK_CHAR, BANK_CHAR, BANK_CHAR, BANK_CHAR, BANK_CHAR, BANK_CHAR, BANK_CHAR, BANK_CHAR, BANK_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR },
	{ WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, BANK_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, BANK_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR },
	{ WALL_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, WALL_CHAR },
	{ WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR },
	{ WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR },
	{ WALL_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, WALL_CHAR },
	{ WALL_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR },
	{ WALL_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR },
	{ WALL_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, WALL_CHAR },
	{ WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR },
	{ WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, DOT_CHAR, WALL_CHAR },
	{ WALL_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, DOT_CHAR, WALL_CHAR },
	{ WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR, WALL_CHAR }
};



void gotoxy(int x, int y) {
	hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
	coord.X = x;
	coord.Y = y;
	SetConsoleCursorPosition(hConsole, coord);
}

void printxy(int x, int y, char ch) {
	gotoxy(x, y + 1);
	printf("%c", ch);
	gotoxy(0, 0);
}

void show_map() {
	printf("Score : %d\n", score);
	for (int y = 0; y < 32; y++) {
		for (int x = 0; x < 28; x++) {
			printf("%c", levelMatrix[y][x]);
		}
		printf("\n");
	}
}

void addScore(int x, int y) {
	if (levelMatrix[y][x] == DOT_CHAR) {
		score += INCREASE_SCORE;
	}
}

void pacmanMove(int old_x, int old_y, int new_x, int new_y){
	if (levelMatrix[new_y][new_x] != (char)WALL_CHAR){
		addScore(new_x, new_y);
		levelMatrix[old_y][old_x] = BANK_CHAR;
		levelMatrix[new_y][new_x] = PACMAN_CHAR;
		printxy(old_x, old_y, BANK_CHAR);
		printxy(new_x, new_y, PACMAN_CHAR);
		pacman.x = new_x;
		pacman.y = new_y;
	}
}

bool pacmanWalk(){
	int tmpX = pacman.x;
	int tmpY = pacman.y;
	switch (pacman.direction)
	{
	case DIRECTION_UP:
		tmpY = tmpY - 1 < 0 ? MAXROWS - 1 : tmpY - 1;
		break;
	case DIRECTION_DOWN:
		tmpY = (tmpY +1) % MAXROWS;
		break;
	case DIRECTION_LEFT:
		tmpX = tmpX - 1 < 0 ? MAXCOLUMNS - 1 : tmpX - 1;
		break;
	case DIRECTION_RIGHT:
		tmpX = (tmpX + 1) % MAXROWS;
		break;
	default:
		return false;
	}
	pacmanMove(pacman.x, pacman.y, tmpX, tmpY);
	if (score>=(248*INCREASE_SCORE))
	{
		return true;
	}
	else{
		return false;
	}
}

void botMove(charactor &botIn, int new_x, int new_y){
	printxy(botIn.x, botIn.y, levelMatrix[botIn.y][botIn.x]);
	printxy(new_x, new_y, BOT_CHAR);
	botIn.x = new_x;
	botIn.y = new_y;
}

bool navigat(charactor &botIn, int &new_x, int &new_y){
	int tmpX = botIn.x;
	int tmpY = botIn.y;
	switch (botIn.direction)
	{
	case DIRECTION_UP:
		tmpY = tmpY - 1 < 0 ? MAXROWS - 1 : tmpY - 1;
		break;
	case DIRECTION_DOWN:
		tmpY = (tmpY + 1) % MAXROWS;
		break;
	case DIRECTION_LEFT:
		tmpX = tmpX - 1 < 0 ? MAXCOLUMNS - 1 : tmpX - 1;
		break;
	case DIRECTION_RIGHT:
		tmpX = (tmpX + 1) % MAXROWS;
		break;
	default:
		break;
	}
	if (levelMatrix[tmpY][tmpX] != (char)WALL_CHAR){
		new_x = tmpX;
		new_y = tmpY;
		return true;
	}
	else{
		return false;
	}
}

bool botWalk(charactor &botIn){
	int tmpX = 0;
	int tmpY = 0;

	if (navigat(botIn, tmpX, tmpY) == false){
		while (true){
			int ran = rand() % 4;
			botIn.direction = arrDirection[ran] != botIn.direction ? arrDirection[ran] : arrDirection[(ran + 1) % 4];
			if (navigat(botIn, tmpX, tmpY) == true){
				break;
			}
		}
	}

	if (levelMatrix[botIn.y][botIn.x] == (char)PACMAN_CHAR){
		return true;
	}
	else if (levelMatrix[tmpY][tmpX] == (char)PACMAN_CHAR){
		botMove(botIn, tmpX, tmpY);
		return true;
	}
	else{
		botMove(botIn, tmpX, tmpY);
		return false;
	}

}

void readKey() {
	while (true) {
		char directionInput = _getch();
		if (gameWin == true || gameOver == true) break;
		mtx.lock();
		int tmpX = pacman.x;
		int tmpY = pacman.y;
		switch (directionInput)
		{
		case DIRECTION_UP:
			tmpY = tmpY - 1 < 0 ? MAXROWS - 1 : tmpY - 1;
			break;
		case DIRECTION_DOWN:
			tmpY = (tmpY + 1) % MAXROWS;
			break;
		case DIRECTION_LEFT:
			tmpX = tmpX - 1 < 0 ? MAXCOLUMNS - 1 : tmpX - 1;
			break;
		case DIRECTION_RIGHT:
			tmpX = (tmpX + 1) % MAXROWS;
			break;
		default:
			return;
		}
		pacman.direction = levelMatrix[tmpY][tmpX] != (char)WALL_CHAR ? directionInput : pacman.direction;
		mtx.unlock();
	}

}

void main() {
	printf(":::PACMAN:::\n");
	printf("Please enter to start game.");
	_getch();

	system("cls");
	std::thread first(readKey);
	show_map();
	gotoxy(0,33);
	printf("w : Up     s: Down\n");
	printf("a : Left   d: Right\n");
	pacman.direction = (char)DIRECTION_RIGHT;
	int maxBot = 4;
	charactor bot[4];
	for (int i = 0; i < maxBot;i++){
		bot[i].direction = arrDirection[i%4];
		bot[i].x = 13;
		bot[i].y = 15;
	}

	while (true) {
		Sleep(500);
		gameWin = pacmanWalk();
		gotoxy(0, 0);
		printf("                                           ");
		gotoxy(0, 0);
		printf("Score: %d", score);

		for (int i = 0; i < maxBot; i++){
			gameOver = botWalk(bot[i]);
			if (gameOver == true)
			{
				break;
			}
		}

		if (gameOver == true || gameWin == true) break;
	}

	if (gameOver == true)
	{
		gotoxy(0, 0);
		printf("GAMEOVER!! Score: %d", score);
	}
	else if (gameWin == true){
		printf("You Win!! Score: %d", score);
	}

	first.join();
	while (true){
		_getch();
	}
}

download source code ได้ที่นี่ครับ github.com/mrthiti/pacman-c

รูปภาพจาก www.todayifoundout.com

Suggestion blogs

C++ OOP การสร้าง Pointer Object ของ Class

จากบทความ C++ OOP การสร้าง Class และการใช้งาน Class ผมอธิบายถึงการสร้าง Object แบบ Stack แต่ในบทความนี้ผมจะอธิบายเรื่องการสร้าง Object โดยใช้ Pointer ข้อแตกต่างกันคือ การสร้าง Object แบบ Stack จะคล้ายๆกับเราสร้างตัวแปรขึ้นมาตัวนึงเมื่อใช้งานเสร็จหรือจบโปรแกรม ระบบจะคืน memory หรือทําลาย Object นั้นทิ้งให้โดยอัตโนมัติ แต่ถ้าสร้าง Object แบบ Pointer จะไม่ทําลาย Object ให้เราเมื่อจบโปรแกรม เราจะต้องเป็นคนทําลาย Object เอง ทําสั่งที่ใช้ ทําลาย Object คือ "delete" ตามด้วย Object ที่ต้องการลบ _ถ้าเราไม่ลบ Object ที่เราสร้างขึ้น ระบบจะไม่สามารถเข้าใช้งาน memory ในส่วนนั้นได้ ทําให้เกิด memory leak _

อินเวอร์เตอร์ (inverter)

อินเวอร์เตอร์ คือ อุปกรณ์ทําทําหน้าที่แปลงกระแสไฟฟ้าจากไฟฟ้าระแสตรง (DC) ไปเป็นไฟฟ้ากระแสสลับ (AC) ซึ่งจะใช้สําหรับแปลงไฟฟ้าจากแบตเตอรี่ไปเป็นไฟฟ้ากระแสสลับ 220V 50Hz (สําหรับประเทศไทย) ที่ใช้กับตามบ้านทั่วไป

php header content type and charset utf8

วิธีใส่ header charset utf-8 ในภาษา PHP


Copyright © 2019 - 2024 thiti.dev |  v1.34.0 |  Privacy policy | 

Build with ❤️ and Astro.

Github profile   Linkedin profile   Instagram   X profile   Youtube channel   Telegram   Email contact   วงแหวนเว็บ