• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Meerul264

Annoying flickering of screen and holding the control key problem.

12 posts in this topic

Hello.

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;
}
0

Share this post


Link to post
Share on other sites

Thanks! I'm going to have fun with these. BTW, I have problem printing out the character 2A. It produce an error: invalid suffix 'A' on integer constant.

 

[CODE]t_image = 2A;[/CODE]

 

t_image has type char.

0

Share this post


Link to post
Share on other sites

The numbers are in hexadecimal, so the correct format is 0x2A or 0x2a. All hexadecimal numbers are prefixed with 0x in C++, or # in some other languages.

This lets the compiler know the difference between a decimal '20' which means is 20 (we use decimal normally in english), and a hexadecimal '20' (0x20) which means 32 in decimal.

Edited by Servant of the Lord
1

Share this post


Link to post
Share on other sites

another noob question; the console.h and console.cpp you gave me.

 

To use it, do I have to create a new class and copy paste each file to the class's header file and class's cpp file respectively?

0

Share this post


Link to post
Share on other sites
They aren't a class, just a collection of functions. You can copy the code into a new empty header file and a new empty source file.
You need to make sure the files are "added" to your "project" in whatever way your IDE (Visual Studio?) requires that.
 
Then, any source file that wants to use any of the functions in Console.h must #include "Console.h".
 
Your project needs to then call "ConsoleStartup()" when you enter "main()", and "ConsoleShutdown()" before you exit main(). I think. smile.png
 
Something like this:
 
int main()
{ 
    if(ConsoleStartup() == -1)
        return -1;
    
    ResizeConsole(CONSOLE_WIDTH, CONSOLE_HEIGHT);
 
    //setup anything else here.
 
    while(playingGame)
    { 
        //Handle any events.
        //Do any time-based updates.
        //Do any drawing.

        Sleep(20);
    }
    
    ConsoleShutdown();
    
    return 0;
}

Silly enough, it appears that I hard-code the console's caption text in the ConsoleStartup() function, so you'll want to edit that as well. Edited by Servant of the Lord
0

Share this post


Link to post
Share on other sites

Hmm I already put the console.cpp into a new empty cpp file and the console.h into a new header file.

 

I also put the #include "console.h" into the main.cpp and console.cpp. But I get this error:

 

C:\Program Files (x86)\CodeBlocks\Projects\TexORPG\src\Console.cpp|1|error: Console.h: No such file or directory|

C:\Program Files (x86)\CodeBlocks\Projects\TexORPG\src\Console.cpp|2|error: main.h: No such file or directory|

 

C:\Program Files (x86)\CodeBlocks\Projects\TexORPG\src\Console.cpp|2|error: main.h: No such file or directory| <- more of this same error over and over,

0

Share this post


Link to post
Share on other sites

In my console.cpp, I had a "main.h", which you don't. You can remove that #include from console.cpp.

 

Are you sure you named "console.h" correctly? Maybe you typo'd the file name as consle.h or something by mistake. Check if you saved it to the right location.

0

Share this post


Link to post
Share on other sites
Okay, figured how to fix that error. Sorry the "more of this error over and over" was a not the correct error. Actually, I got this error over and over: include\Console.h|2|error: stray '\240' in program| BTW, may I ask why you don't use this code anymore? Is it because you "leveled-up"?
0

Share this post


Link to post
Share on other sites

No, it's because I canceled the project and started something else. laugh.png

Back then, I canceled and started a new project probably every other month. It was horrible, since I never finished anything.

 

Now, being much wiser, I've been working on the same project for two and a half years. And I haven't finished anything. dry.png

 

The stray \240 is probably a non-visible character that was accidentally copied when you copied the code from the forum post. Try going to the very very first character of the file, and hitting 'backspace' several times to get rid of any non-visible characters at the front, then go to the very end of the file and hit 'delete' several times to remove any from the end.

 

Or, here's the entire project, I don't know if it compiles or not.

The executable appears to work, and wow, does it looks pretty cool. I half want to return to this project now! happy.png

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0