4 Replies - 607 Views - Last Post: 19 August 2013 - 06:46 PM Rate Topic: -----

#1 kugi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 16-August 13

Make game

Posted 16 August 2013 - 06:10 AM

Hello! I start to make my own game train_run. It is doing, but I don't know how to make gameover when train(ghost)touched you.
There is my code:
//Urh Primozic''kugi''.IA producon.16.8.2013
#include <iostream>
#include <Windows.h>
using namespace std;

struct Player 
{
	int x, y;
	Player()
	{
		x = -1;
		y = -1;
	}
};

struct Ghost
{
	int x, y, direction;
	Ghost()
	{
		x = -1;
		y = -1;
		direction = 1;
	}
};

const char SYMBOL_EMPTY = ' ';
const char SYMBOL_PLAYER = 'O';
const char SYMBOL_GHOST = '';
const char SYMBOL_WALL = 'U';
const int MapDx = 10;
const int MapDy = 40;
const int GameSpeed = 100;
const int LEFT = 1;
const int RIGHT = 2;
const int UP = 3;
const int DOWN = 4;
int direction = RIGHT;

char map[10][40] = 
{
	"UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU",
	"U                               U     U",
	"U                               U     U",
	"U                                     U",
	"U      U                              U",
	"U      U              UUU             U",
	"U      U                              U",
	"U                   U                 U",
	"UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU",
};

bool isValidPos(int x, int y) 
{ 
	return (x >= 0 && x < MapDx && y >= 0 && y < MapDy);
}

bool movePlayer(Player &player, int x, int y)
{
	if (!isValidPos(x, y)) 
	{ 
		return false;
	}
	
	char ch = map[x][y];
	
	if(ch != SYMBOL_EMPTY) 
	{
		return false;
	}
	
	if (isValidPos(player.x, player.y)) 
	{ 
		map[player.x][player.y] = SYMBOL_EMPTY;
	}
	player.x = x; player.y = y;
	map[player.x][player.y] = SYMBOL_PLAYER;
	return true;
}

bool moveGhost(Ghost &ghost, int x, int y)
{
	if (!isValidPos(x, y))
	{
		return false;
	}

	char ch = map[x][y];

	if (ch != SYMBOL_EMPTY)
	{
		return false;
	}

	if (isValidPos(ghost.x, ghost.y))
	{
		map[ghost.x][ghost.y] = SYMBOL_EMPTY;
	}
	ghost.x = x; ghost.y = y;
	map[ghost.x][ghost.y] = SYMBOL_GHOST;
	return true;
}

void GhostAI(Ghost &ghost, Player &player)
{
	double a = sqrt((pow((double) (ghost.x - 1) - player.x, 2)) + pow((double) ghost.y - player.y, 2)); //UP
	double b = sqrt((pow((double) (ghost.x + 1) - player.x, 2)) + pow((double) ghost.y - player.y, 2)); //DOWN
	double c = sqrt((pow((double) (ghost.y - 1) - player.x, 2)) + pow((double) ghost.x - player.y, 2)); //RIGHT
	double d = sqrt((pow((double) (ghost.y + 1) - player.x, 2)) + pow((double) ghost.x - player.y, 2)); //LEFT
	if(a < b && a <= c && a <= d && ghost.direction != DOWN) ghost.direction = UP;
	else if(b <= c && b <= d && ghost.direction != UP) ghost.direction = DOWN;
	else if(c < d && ghost.direction != LEFT) ghost.direction = RIGHT;
	else if(ghost.direction != RIGHT) ghost.direction = LEFT;
}

void showMap() 
{
	for (int x = 0; x < MapDx; x++) 
	{
		cout << map[x] << endl;
	}
}

void showPlayer(Player &player)
{
	cout << "\nPlayerX: " << player.x << endl;
	cout << "PlayerY: " << player.y << endl;
}

void gameLoop() 
{
	Player player;
	Ghost ghosts[3];
	movePlayer(player, 1, 2);
	moveGhost(ghosts[0], 5, 2);
	moveGhost(ghosts[1], 5, 5);
	moveGhost(ghosts[2], 5, 8);
	while (true) 
	{
		system("cls");
		showMap();
		showPlayer(player);
		if (GetAsyncKeyState(VK_UP)) 
		{
			direction = UP;
		} 
		else if (GetAsyncKeyState(VK_DOWN)) 
		{
			direction = DOWN;
		} 
		else if (GetAsyncKeyState(VK_LEFT)) 
		{
			direction = LEFT;
		} 
		else if (GetAsyncKeyState(VK_RIGHT)) 
		{
			direction = RIGHT;
		}
		switch (direction)
		{
		case UP:
			movePlayer(player, player.x-1, player.y);
			break;
		case DOWN:
			movePlayer(player, player.x+1, player.y);
			break;
		case LEFT:
			movePlayer(player, player.x, player.y-1);
			break;
		case RIGHT:
			movePlayer(player, player.x, player.y+1);
			break;
		}
		for (int ghost = 0; ghost < 3; ghost++)
		{
			GhostAI(ghosts[ghost], player);
			switch (ghosts[ghost].direction)
			{
			case UP:
				moveGhost(ghosts[ghost], ghosts[ghost].x-1, ghosts[ghost].y);
				break;
			case DOWN:
				moveGhost(ghosts[ghost], ghosts[ghost].x+1, ghosts[ghost].y);
				break;
			case LEFT:
				moveGhost(ghosts[ghost], ghosts[ghost].x, ghosts[ghost].y-1);
				break;
			case RIGHT:
				moveGhost(ghosts[ghost], ghosts[ghost].x, ghosts[ghost].y+1);
				break;
			}
		}
		Sleep(GameSpeed);
	}
}


int main() 
{
	gameLoop();
	return 0;
}





Is This A Good Question/Topic? 0
  • +

Replies To: Make game

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8374
  • View blog
  • Posts: 31,122
  • Joined: 12-June 08

Re: Make game

Posted 16 August 2013 - 06:56 AM

That's called collision detection. Somewhere (and this is generally for this layout of a game) - in the game engine update or an agent's move you need to check if they run on top of coordinates similar to another item. If so then kick off an event or do something..
Was This Post Helpful? 1
  • +
  • -

#3 kugi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 16-August 13

Re: Make game

Posted 17 August 2013 - 12:00 AM

I make my code like this:
//Urh Primozic''kugi''.IA producon.17.8.2013, popravljena verzija
#include <iostream>
#include <Windows.h>
using namespace std;

struct Player 
{
	int x, y;
	Player()
	{
		x = -1;
		y = -1;
	}
};

struct Ghost
{
	int x, y, direction;
	Ghost()
	{
		x = -1;
		y = -1;
		direction = 1;
	}
};
	
const char SYMBOL_EMPTY = ' ';
const char SYMBOL_PLAYER = 'O';
const char SYMBOL_GHOST = '=';
const char SYMBOL_WALL = 'U';
const int MapDx = 10;
const int MapDy = 40;
const int GameSpeed = 100;
const int LEFT = 1;
const int RIGHT = 2;
const int UP = 3;
const int DOWN = 4;
int direction = RIGHT;

char map[10][40] = 
{
	"UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU",
	"U          -----   ---          U     U",
	"U          train - run          U     U",
	"U          -----   ---                U",
	"U      U                              U",
	"U      U              UUU             U",
	"U      U                              U",
	"U                   U                 U",
	"UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU",
};

bool isValidPos(int x, int y) 
{ 
	return (x >= 0 && x < MapDx && y >= 0 && y < MapDy);
}

bool movePlayer(Player &player, int x, int y)
{
	if (!isValidPos(x, y)) 
	{ 
		return false;
	}
	
	char ch = map[x][y];
	
	if(ch != SYMBOL_EMPTY) 
	{
		return false;
	}
	
	if (isValidPos(player.x, player.y)) 
	{ 
		map[player.x][player.y] = SYMBOL_EMPTY;
	}
	player.x = x; player.y = y;
	map[player.x][player.y] = SYMBOL_PLAYER;
	return true;
}

bool moveGhost(Ghost &ghost, int x, int y)
{
	if (!isValidPos(x, y))
	{
		return false;
	}

	char ch = map[x][y];

	if (ch != SYMBOL_EMPTY)
	{
		return false;
	}

	if (isValidPos(ghost.x, ghost.y))
	{
		map[ghost.x][ghost.y] = SYMBOL_EMPTY;
	}
	ghost.x = x; ghost.y = y;
	map[ghost.x][ghost.y] = SYMBOL_GHOST;
	return true;
}

void GhostAI(Ghost &ghost, Player &player)
{
	double a = sqrt((pow((double) (ghost.x - 1) - player.x, 2)) + pow((double) ghost.y - player.y, 2)); //UP
	double b = sqrt((pow((double) (ghost.x + 1) - player.x, 2)) + pow((double) ghost.y - player.y, 2)); //DOWN
	double c = sqrt((pow((double) (ghost.y - 1) - player.x, 2)) + pow((double) ghost.x - player.y, 2)); //RIGHT
	double d = sqrt((pow((double) (ghost.y + 1) - player.x, 2)) + pow((double) ghost.x - player.y, 2)); //LEFT
	if(a < b && a <= c && a <= d && ghost.direction != DOWN) ghost.direction = UP;
	else if(b <= c && b <= d && ghost.direction != UP) ghost.direction = DOWN;
	else if(c < d && ghost.direction != LEFT) ghost.direction = RIGHT;
	else if(ghost.direction != RIGHT) ghost.direction = LEFT;
}

void showMap() 
{
	for (int x = 0; x < MapDx; x++) 
	{
		cout << map[x] << endl;
	}
}

void showPlayer(Player &player)
{
	cout << "\nPlayerX: " << player.x << endl;
	cout << "PlayerY: " << player.y << endl;
}

void gameLoop() 
{
	Player player;
	Ghost ghosts[3];
	movePlayer(player, 1, 2);
	moveGhost(ghosts[0], 5, 2);
	moveGhost(ghosts[1], 5, 5);
	moveGhost(ghosts[2], 5, 8);
	while (true) 
	{
		system("cls");
		showMap();
		showPlayer(player);
		if (GetAsyncKeyState(VK_UP)) 
		{
			direction = UP;
		} 
		else if (GetAsyncKeyState(VK_DOWN)) 
		{
			direction = DOWN;
		} 
		else if (GetAsyncKeyState(VK_LEFT)) 
		{
			direction = LEFT;
		} 
		else if (GetAsyncKeyState(VK_RIGHT)) 
		{
			direction = RIGHT;
		}
		switch (direction)
		{
		case UP:
			movePlayer(player, player.x-1, player.y);
			break;
		case DOWN:
			movePlayer(player, player.x+1, player.y);
			break;
		case LEFT:
			movePlayer(player, player.x, player.y-1);
			break;
		case RIGHT:
			movePlayer(player, player.x, player.y+1);
			break;
		}
		for (int ghost = 0; ghost < 3; ghost++)
		{
			GhostAI(ghosts[ghost], player);
			switch (ghosts[ghost].direction)
			{
			case UP:
				moveGhost(ghosts[ghost], ghosts[ghost].x-1, ghosts[ghost].y);
				break;
			case DOWN:
				moveGhost(ghosts[ghost], ghosts[ghost].x+1, ghosts[ghost].y);
				break;
			case LEFT:
				moveGhost(ghosts[ghost], ghosts[ghost].x, ghosts[ghost].y-1);
				break;
			case RIGHT:
				moveGhost(ghosts[ghost], ghosts[ghost].x, ghosts[ghost].y+1);
				break;
			}
		}
		Sleep(GameSpeed);
	}
}






void gameover()
{
	 if map[ghost.x][ghost.y] = SYMBOL_PLAYER;
}
	
int main()
	{
	    GameOver();
	    std::cout << "gameover";
}


But there are 3 errors:
--------------------Configuration: trainrun - Debug--------------------
Compiling source file(s)...
trainrun_main.cpp
trainrun_main.cpp: In function `void gameover()':
trainrun_main.cpp:204: error: syntax error before `[' token
trainrun_main.cpp: In function `int main()':
trainrun_main.cpp:209: error: `GameOver' undeclared (first use this function)
trainrun_main.cpp:209: error: (Each undeclared identifier is reported only once

for each function it appears in.)

trainrun.exe - 3 error(s), 0 warning(s)
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,749
  • Joined: 25-December 09

Re: Make game

Posted 17 August 2013 - 05:43 AM

Check the spelling of your function call. Remember C/C++ are case sensitive. This means that Game is not the same as game.

Jim
Was This Post Helpful? 0
  • +
  • -

#5 slehmann101  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 146
  • Joined: 30-November 11

Re: Make game

Posted 19 August 2013 - 06:46 PM

on line 204 your syntax for an if statement is incorrect, where are the brackets?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1