• 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
Kotorez

Snake game help

6 posts in this topic

Hello, I am trying to do a snake game and I am stuck on the step where I have to make a game loop. I want my snake to move automatically until it hits the wall or user steers it into a different direction. I tried to make a function "playerAutomove" that records the user's last move so the game loop knows where the snake turned (up,down,left,right) but it seems like no matter what I do the program will stop and wait for user's key press, similar to "cin". I just need a suggestion, you dont have to give me the exact code. :) This is what I have so far. The code should compile under c++11 if you just copy/paste. I used only default libraries that c++ comes with. http://pastebin.com/g05vYALt Thanks
0

Share this post


Link to post
Share on other sites

You would want to make your loop look something like this:

 

bool run = true;

int main()
{
   // init stuff
   Init();
   
   while(run)
   {
      HandleInput();
      UpdateGameLogic();
      Render();
   {
}

 

Right now, everything before the while loop is only called once and while run remains true, you will keep looping and call these 3 functions that should handle how your game is working.

0

Share this post


Link to post
Share on other sites

Here is my problem in a small code. It appears that getch() stops it from looping. In this small example I want "Hello world" to be printed infinite amout of times and not care about if user entered something or not. 

 

 

#include <stdio.h>
#include <conio.h>
#include <iostream>

using namespace std;

int main ()
{
    bool run = true;
    int ch;

    while (run){
        printf("hello world");
        ch = getch();  

            if (ch == 0 || ch == 224)
                cout << getch();
            printf("\n");
            if (ch == 27)
                run = false;
    }
    printf("ESC %d\n", ch);

    return 0;
}
 

 

Do I have to create a separate thread for input handling or there is a simpler solution for this?

Edited by KotoR`eZ
0

Share this post


Link to post
Share on other sites

Use the console-specific functions in wincon.h (which is included in windows.h) rather than conio and the system functions.  Edit: conio is not standard C++ anyway and you should not use system() (read here).   You can find documentation on the functions specific to the Windows console here, and a bit more general information about the console provided by Microsoft here.

 

To answer your question in that light, check the console's input buffer to see if there are any input events using GetNumberOfConsoleInputEvents.  If there are, use ReadConsoleInput to grab them and handle them appropriately (you will need to obtain the necessary information from the event structure first), then move on to the rest of your loop.  If there are no events in the buffer when you check, then you just move on to updating the snake's position.  You should use the functions specific to the Windows console to control the output as well.  For example, do not clear the console screen with system("cls").  Write your own function to clear the screen making use of FillConsoleOutputCharacter (which will unfortunately require you to obtain some other info about the console as well).  There's a nice breakdown of the ways (good and bad) to clear the console window here.

 

And I know it seems like I just upped the complexity of Snake by a factor of a hundred.  biggrin.png  If you're going to spend any amount of time with the Windows console for anything other than plain text games, spending an afternoon learning how to work with it properly is worthwhile IMO.  Ultimately it will feel very simple, I promise.

 

Anyway, to keep the snake moving when there is no user input, you should keep track of the direction the head is moving in addition to its location.   User input should change this direction when appropriate.  When you update the location of the snake, it's new location is based on its current location and the direction.  You might have other things change the direction of the snake as well, such as a collision with the wall of your "garden".

Edited by Crowseye
2

Share this post


Link to post
Share on other sites

If you want to stick with conio, you may be able to use kbhit(), which will return 0 if no keyboard key has been hit.  Thus in your HandleInput() funciton you would 1st check:

if (kbhit()) {
  input = getch();
  ...
}
1

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