I just made a simple game - where you just go around evading the bombs and stuff.
The game works fine, except 2 things:
1. When I hold down the arrow keys that control the character's movement, the character (printed as '@') disappear, and then reappear upon releasing the key.
So in the end I just have to tap the arrow keys furiously just move the character around. I have no idea how to fix this.
2. The game flickering (is this what you call screen refresh rate) is annoying. How do I make it less flicker, or don't appear to flicker at all? Does this
have something to do with the Sleep() or System("cls") function?
If it may help, here's the source code for the game.
#include <iostream>
#include <windows.h>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <time.h>
using namespace std;
//=========================WORD MASTER CLASS===========================================
class word_master
{
public:
int w_horizontal;
int w_vertical;
char w_image;
word_master();
};
word_master::word_master()
{
w_horizontal = 1;
w_vertical = 9;
w_image = '@';
}
//================================SHOW MAP FUNCTION=======================================
void show_map(char map[][20])
{
for (int row=0;row<20;row++)
{
for(int col=0;col<20;col++)
{
std::cout << map[row][col] << " ";
}
std::cout << endl;
}
}
//=====================================BOMB CLASS============================================
class bomb
{
public:
int b_horizontal;
int b_vertical;
char b_image;
bomb();
int setHorizontal();
int setVertical();
};
int bomb::setHorizontal()
{
int tempHorizontal = (rand()%17);
return tempHorizontal;
}
int bomb::setVertical()
{
int tempVertical = (rand()%17);
return tempVertical;
}
bomb::bomb()
{
b_horizontal = setHorizontal();
b_vertical = setVertical();
b_image = '!';
}
void move_bomb(bomb &bomb_move, char map[][20])
{
int newHorizontal = (rand()%2);
int newVertical = (rand()%2);
map[bomb_move.b_vertical][bomb_move.b_horizontal] = ' ';
bomb_move.b_horizontal += newHorizontal;
bomb_move.b_vertical += newVertical;
if(bomb_move.b_vertical > 17 || bomb_move.b_horizontal > 17 || bomb_move.b_vertical < 3 || bomb_move.b_horizontal < 3 ) //prevent bomb from escaping out of field
{
bomb_move.b_vertical = newVertical+1;
bomb_move.b_horizontal = newHorizontal+1;
}
map[bomb_move.b_vertical][bomb_move.b_horizontal] = bomb_move.b_image;
}
//=================================================ROCKET CLASS=============================================
class Rocket
{
public:
int r_horizontal;
int r_vertical;
int r_randomFallPosition;
char r_image;
Rocket();
};
Rocket::Rocket()
{
r_horizontal = 7;
r_vertical = 1;
r_image = 'V';
}
void move_rocket(Rocket &launch, char map[][20])
{
launch.r_randomFallPosition = ((rand()%16)+2);
map[launch.r_vertical][launch.r_horizontal] = ' ';
launch.r_vertical +=1;
map[launch.r_vertical][launch.r_horizontal] = launch.r_image;
if(launch.r_vertical > 17)
{
map[launch.r_vertical][launch.r_horizontal] = ' ';
launch.r_vertical = 1;
launch.r_horizontal = launch.r_randomFallPosition; //random potential fall position
}
}
//=========================================FOOD CLASS============================================
class treasure
{
public:
int t_horizontal;
int t_vertical;
char t_image;
treasure();
int setHorizontal();
int setVertical();
};
int treasure::setHorizontal()
{
int tempHorizontal = (rand()%17)+1;
return tempHorizontal;
}
int treasure::setVertical()
{
int tempVertical = (rand()%17)+1;
return tempVertical;
}
treasure::treasure()
{
t_horizontal = setHorizontal();
t_vertical = setVertical();
t_image = 'X';
}
void generate_treasure(int &collected,word_master hero,treasure &treasure_location, char map[][20])//apply this function during start game and after the treasure is taken
{
int newHorizontal = (rand()%17)+1;
int newVertical = (rand()%17)+1;
map[treasure_location.t_vertical][treasure_location.t_horizontal] = treasure_location.t_image;
if(hero.w_horizontal == treasure_location.t_horizontal && hero.w_vertical == treasure_location.t_vertical) //if hero take treasure, add new treasure at random location
{
map[treasure_location.t_vertical][treasure_location.t_horizontal] = ' ';
treasure_location.t_vertical = newVertical;
treasure_location.t_horizontal = newHorizontal;
collected++;
}
}
//==========================================BOOL CHECK IF HIT FUNCTION=======================================================
bool checkHit(bomb bomb_move,word_master hero, Rocket launch)
{
if(hero.w_vertical == bomb_move.b_vertical && hero.w_horizontal == bomb_move.b_horizontal ||
hero.w_vertical == launch.r_vertical && hero.w_horizontal == launch.r_horizontal)
return true;
return false;
}
//========================================INT MAIN=================================
int main()
{
srand(time(NULL));
char playagain;
char userContinue;
int time = 0;
int collected = 0; //count how many treasure collected.
bool gameover(false);
bool end_instruction(false);
char map[20][20] {"###################",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"###################"};
word_master hero;
bomb bomb_move;
Rocket launch;
treasure treasure_location;
//===================tell lore==============================
std::cout << "=============LORE============\n";
std::cout << endl;
std::cout << "You, a cat burglar, infiltrated an island that is owned by the most\n";
std::cout << "notorious pirate in Southern Mosby, known as the Bomber Pirate. You are caught\n";
std::cout << "infiltrating their base and now all their crews are alerted about your presence.\n";
std::cout << "Their rocket launchers and bombs are all trained at you, so watch out!\n\n";
std::cout << "PRESS 'ENTER' TO CONTINUE\n";
cin.get();
cin.clear();
system("cls");
//===========================instruction=====================
std::cout << "===========HOW=TO=PLAY=========\n";
std::cout << "You control @.\n";
std::cout << "Press the arrow keys to move in their corresponding direction.\n";
std::cout << "Evade the bombs, and steal as many treasures as you can!\n\n";
std::cout << "PRESS 'ENTER' KEY YO PLAY!\n";
cin.get();
cin.clear();
while(!gameover)
{
system("cls");
show_map(map);
map[hero.w_vertical][hero.w_horizontal] = hero.w_image;
move_bomb(bomb_move,map);
move_rocket(launch,map);
generate_treasure(collected, hero,treasure_location,map);
time++;
std::cout << "Time spent playing: ";
std::cout << setprecision(2);
std:cout << time/38.00;
std::cout << " seconds.";
std::cout << endl;
std::cout << "Treasure stolen: ";
std::cout << collected;
std::cout << endl;
if(checkHit(bomb_move,hero,launch))
{
system("cls");
for(int i=0;i<10;i++){ std::cout << endl;}
std::cout << " ===YOU=GOT=HIT=BY=THE=BOMB!!===\n";
std::cout << " Would you like to play again? y/n \n";
std::cin >> playagain;
if (playagain == 'y')
{
time = 0; //set time played back to 0
collected = 0; //set treasure collected back to 0
//set hero to starting point--
hero.w_horizontal = 1;
hero.w_vertical = 9;
//set treasure location to random--
map[treasure_location.t_vertical][treasure_location.t_horizontal] = ' ';
treasure_location.t_horizontal = (rand()%17)+1;
treasure_location.t_vertical = (rand()%17)+1;
}
else
gameover = true;
}
if(GetAsyncKeyState(VK_UP))
{
map[hero.w_vertical][hero.w_horizontal] = ' ';
hero.w_vertical -=1;
if(hero.w_vertical < 1)
{
hero.w_vertical = 1;
} // prevent hero from going through the wall
}
else if(GetAsyncKeyState(VK_DOWN))
{
map[hero.w_vertical][hero.w_horizontal] = ' ';
hero.w_vertical +=1;
if(hero.w_vertical > 17)
{
hero.w_vertical = 17;
}
}
else if(GetAsyncKeyState(VK_RIGHT))
{
map[hero.w_vertical][hero.w_horizontal] = ' ';
hero.w_horizontal +=1;
if(hero.w_horizontal > 17 )
{
hero.w_horizontal = 17;
}
}
else if(GetAsyncKeyState(VK_LEFT))
{
map[hero.w_vertical][hero.w_horizontal] = ' ';
hero.w_horizontal -=1;
if(hero.w_horizontal < 1 )
{
hero.w_horizontal = 1;
}
}
}
Sleep(300);
return 0;
}