Jump to content
Column

(C++) Creare joc in consola

Recommended Posts

Posted

Stiu ca sunt tineri programatori ce isi doresc sa-si faca propriul joc si de aceea m-am gandit sa le ofer o sursa de inspiratie.


#include <iostream>
#include <windows.h>
#include <ctime>
using namespace std;
char GameOver[50][50] = {"-------------------------",
"- -",
"- GAME OVER -",
"- -",
"-1)Apasati ESC(ExitGame)-",
"-2)Apasati F1(Repetare) -",
"-------------------------"};

char GameWinner[50][50] = {"@@@@@@@@@@@@@@@@@@@@@@@",
"@ @",
"@ @",
"@ Win this Game @",
"@ @",
"@ F1 - Repeat @",
"@@@@@@@@@@@@@@@@@@@@@@@"};

char map[50][50] = {"################",
"# #",
"# #",
"# @ #",
"# #",
"#* #",
"################"};
int game_speed = 90;
bool pause = false;

//----------PLAYER------------//
int score = 0;

bool IsDead = false;
int playerCount = 0;

int inc_score = 5;
int HP = 100;
int SP = 100;
//----------OTHER-------------//
int over = 0;
int enemy = 0;
int totalenemy = 0;
//----------------------------//

//---------DIRECTION----------//
bool LimitUpDown = false;
//----------------------------//
void MyGame();
void WinGame()
{
while(pause==true)
{
system("cls");
for(int x = 0; x < 20; x++)
{
cout<<GameWinner[x]<<endl;
}
if(GetAsyncKeyState(VK_F1)!=0)
{
pause = false;
if(enemy==0)
{
map[2][2] = '*';
map[2][3] = '@';
}
MyGame();
}
if(GetAsyncKeyState(VK_ESCAPE)!=0)
{
pause = false;
}
}
}
void MyGame()
{
while((pause==false)&&(IsDead == false))
{
system("cls");

for(int x = 0; x < 7; x++)
{
cout<<map[x]<<endl;
}
for(int x = 0; x < 17; x ++ )
{
for(int y = 0 ; y < 17 ; y ++ )
{
switch(map[y][x])
{
case '*':
{
if(LimitUpDown==false)
{
int y2 = y - 1; //up
switch(map[y2][x])
{
case ' ':
{
map[y][x] = ' ';
y -= 1;
map[y2][x] = '*';
}break;

case '@':
{
map[y][x] = ' ';
y -= 1;
map[y2][x] = '*';
}break;

case '#':
{
LimitUpDown = true;
}break;
}
}
else
{
//down
int y2 = y + 1;
switch(map[y2][x])
{
case ' ':
{
map[y][x] = ' ';
y += 1;
map[y2][x] = '*';
}break;

case '@':
{
map[y][x] = ' ';
y += 1;
map[y2][x] = '*';
}break;

case '#':
{
LimitUpDown = false;
}break;
}
}
}break;
case '@':
{
if(GetAsyncKeyState(VK_UP)!=0)
{
int y2 = y - 1;
switch(map[y2][x])
{
case ' ':
{
map[y][x] = ' ';
y -= 1;
map[y2][x] = '@';
}break;

case '*':
{
score = score+inc_score;
map[y][x] = ' ';
y -= 1;
map[y2][x] = '@';
}break;
}
}

if(GetAsyncKeyState(VK_DOWN)!=0)
{
int y2 = y + 1;
switch(map[y2][x])
{
case ' ':
{
map[y][x] = ' ';
y += 1;
map[y2][x] = '@';
}break;
case '*':
{
score = score+inc_score;
map[y][x] = ' ';
y += 1;
map[y2][x] = '@';
}break;
}
}

if(GetAsyncKeyState(VK_RIGHT)!=0)
{
int x2 = x + 1;
switch(map[y][x2])
{
case ' ':
{
map[y][x] = ' ';
x += 1;
map[y][x2] = '@';
}break;

case '*':
{
score = score+inc_score;
map[y][x] = ' ';
x += 1;
map[y][x2] = '@';
}break;
}
}

if(GetAsyncKeyState(VK_LEFT)!=0)
{
int x2 = x - 1;
switch(map[y][x2])
{
case ' ':
{
map[y][x] = ' ';
x -= 1;
map[y][x2] = '@';
}break;

case '*':
{
score = score+inc_score;
map[y][x] = ' ';
x -= 1;
map[y][x2] = '@';
}break;
}
}

}break;
}

if(GetAsyncKeyState(VK_ESCAPE)!=0)
{
pause = true;
}
}
}
for(int x = 0 ; x < 17; x ++ )
{
for(int y = 0; y < 17; y ++ )
{
if(map[y][x]=='@')
{
playerCount ++;
}
}
}
if(playerCount==0)
{
IsDead = true;
over += 1;
}
for(int x = 0 ; x < 17; x ++ )
{
for(int y = 0; y < 17 ; y++)
{
if(map[y][x]=='*')
{
enemy ++;
}
}
}
if(totalenemy<=enemy)
{
totalenemy = enemy;
}
if(enemy==0)
{
pause = true;
WinGame();
}
cout<<"----------------------------------"<<endl;
cout<<"ENEMY: "<<enemy<<endl;
cout<<"SCORE: "<<score<<endl;
cout<<"GAME SPEED: "<<game_speed<<endl;
cout<<"----------------------------------"<<endl;
cout<<"Health: "<<HP<<endl;
cout<<"----------------------------------"<<endl;
Sleep(game_speed);
enemy = 0;
playerCount = 0;
}
while(IsDead == true)
{
system("cls");
for(int x = 0; x < 15; x ++ )
{
cout<<GameOver[x]<<endl;
}
if(GetAsyncKeyState(VK_F1)!=0)
{
IsDead = false;
pause = false;
if(enemy == 0)
{
map[2][2] = '*';
}
map[2][3] = '@';
MyGame();
}
if(GetAsyncKeyState(VK_ESCAPE)!=0)
{
IsDead = false;
}
}
}
int main()
{
MyGame();
}

Jocul este facut in CodeBlocks, nu l-am testat in Visual sau Dev, in orice caz sper ca nu apar erori.

  • Upvote 2
Posted (edited)

Si despre ce este jocul? Cum se joaca?

EDIT: nvm. Doar ce am compilat jocul. Are cateva defectiuni cand trebuie sa apara meniul principal. In rest presupun ca este ok. Putin cam greu de jucat totusi.

EDIT2:

char GameOver[50][50] = ...
char GameWinner[50][50] = ...
char map[50][50] = ...

[50][50] ? ai doar 7 randuri :-/

Edited by S.L.C
Posted

Frumos da cam greu de jucat. Ar fi bine sa inveti o implementare mai buna. Poate un OOP.

Intre timp postez si eu opera mea de acum cativa ani, neterminata.

Console Game:


#include <iostream>
#include <string>
#include <ctype.h>
#include <stdio.h>

using namespace std;

class Item
{
public:
string name, roomLoc, description;
int use;
};
class Room
{
public:
int nr;
string name;
Room* vecini[4];
string description;
Item* contain[5];
};

class User
{
public:
Room* location;
string name;
Item* inventory[5];
};

int action(string w)
{
if(w=="analyze") return 0;
if(w=="look") return 0;
if(w=="go") return 1;
if(w=="enter") return 1;
if(w=="help") return 2;
if(w=="location") return 3;
if(w=="take") return 4;
if(w=="steal") return 4;
if(w=="inventory") return 5;
return 666;

}
int object(string w)
{
if(w=="bathroom") return 0;
if(w=="hall") return 1;
if(w=="living room") return 2;
if(w=="key") return 3;
if(w=="biscuit") return 4;
if(w=="letter") return 5;
return 666;
}

string readt()
{
string input;
getline(cin, input);
for(int i=0;i<input.length();i++)
{
input[i]=tolower(input[i]);
}
return input;
}
void initial();
void decide();
void analyze(int c);
void enter(int c);
void help(int c);
int checkNeigh(Room &loc);
void take(int c);
int takeItem(Item &tk);
void inventory();
//Global declare space
Room hall, bathroom, livingRoom;
User User1;
Item biscuit, key, letter;
int inv;
int main()
{
cout<<"--Begin--"<<'\n';
initial();
do{
cout<<"\nInsert command: ";
decide();
}while(1);

return 0;
}

void initial()
{
User1.location = &hall;
cout<<"Insert your username:";
User1.name=readt();
cout<<'\n'<<"Welcome agent "<<User1.name<<"!\nYou just woke up with no memories. You find yourself in a big white hall.\nTry to gain more information about your location.\nHow? Try some help!\ntype: help\n";
//room instantiate
hall.name = "Hall";
hall.description = "You in are in a big long hall with white walls. In front of you is a door leading to living room.";
hall.vecini[0]=&livingRoom;
livingRoom.name= "Living Room";
livingRoom.description = "You are in a big living room. On the walls you can see a bear head. In the center of the room is a big table which have around 4 chairs. On the table you find a letter and a biscuit. There is another door with a bathroom sign on it.";
livingRoom.vecini[0]=&hall;
livingRoom.vecini[1]=&bathroom;
bathroom.name="Men's Bathroom";
bathroom.description = "It is a small bathroom. A small sink sits in the corner next to the shower cabin. The toilet seams to be broken.";
bathroom.vecini[0]=&livingRoom;
letter.name = "old letter";
letter.use = 0;
key.name = "golden key";
key.roomLoc = "Under the sink a key shines.";
letter.roomLoc = "A letter sits on the table.";
biscuit.roomLoc = "You find a biscuit on the floor.";
key.use = 1;
biscuit.name = "biscuit";
biscuit.use = 2;
bathroom.contain[0]=&key;
livingRoom.contain[0]=&biscuit;
livingRoom.contain[1]=&letter;
hall.nr=0;
livingRoom.nr=2;
bathroom.nr=1;
}

void decide()
{
string input,w1,w2;
int p1;
input=readt();
p1=input.find(' ');
w1=input.substr(0,p1);
w2=input.substr(p1+1,input.size());

switch(action(w1))
{
case 0: analyze(object(w2)); break;
case 1: enter(object(w2)); break;
case 2: help(0); break;
case 3: help(1); break;
case 4: take(object(w2)); break;
case 5: inventory(); break;
default: cout<<"Wrong action!"<<'\n'; break;
}
}
void analyze(int c)
{
cout<<User1.location->description;
for(int i=0;i<User1.location->nr;i++)
cout<<" "<<User1.location->contain[i]->roomLoc;
cout<<'\n';
}
void enter(int c)
{
switch(c)
{
case 0: if(checkNeigh(bathroom)){ User1.location = &bathroom; cout<<"You went to bathroom."<<'\n';}else cout<<"No bathroom around.\n"; break;
case 1: if(checkNeigh(hall)){User1.location = &hall; cout<<"You went to hall."<<'\n';}else cout<<"No hall around."; break;
case 2: if(checkNeigh(livingRoom)){User1.location = &livingRoom; cout<<"You went to living room."<<'\n';}else cout<<"No living room around"; break;
default: cout<<"Wrong action!"<<'\n'; break;
}
}
void inventory()
{
cout<<"You have: ";
for(int i=0;i<inv;i++)
cout<<User1.inventory[i]->name<<", ";
cout<<'\n';
}

void help(int c)
{
if(c==0) cout<<"Type 2 words commands: action + object\nLike:analyze, look, go, enter, location, take, steal, help, inventory\nTry: analyze hall or simply: look\n";
else if(c==1) cout<<"You are in the: "<<User1.location->name<<'\n';
}
void take(int c)
{
switch(c)
{
case 3: takeItem(key); break;
case 4: takeItem(biscuit); break;
case 5: takeItem(letter); break;
default: cout<<"You can't take that!\n";
}

}
int takeItem(Item &tk)
{
for(int i=0;i<=5;i++)
{
if(&tk == (User1.location->contain[i]))
{
User1.inventory[inv]=&tk;
User1.location->contain[i]=NULL;
inv++;
User1.location->nr--;
cout<<"You took: "<<tk.name<<'\n';
return 1;
}
}
cout<<"Can't find that!\n";
return 0;
}

int checkNeigh(Room &loc)
{
for(int i=0;i<=4;i++)
{
if(&loc == (User1.location->vecini[i]))
return 1;
}
return 0;
}

  • Upvote 2

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...