d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > [c++] Snake Project > Ascii Art Game
123Next
Add Reply New Topic New Poll
Member
Posts: 31,680
Joined: Nov 10 2007
Gold: 1.00
Jun 19 2009 11:08pm
Hello, I started on this project approx. half a year ago. I work on it once in a while when I am bored. It's currently trashed, very unorganized. The last thing I can remember doing was in February, I was working on the AI.
As such, I've patched it together right before making this post, with full intent of posting it, so people can learn, study, and most of all enjoy! :)

Any and all constructive criticism is welcome, along with any biased personal opinions about the game itself, and what I should add (cheat-codes have not been added in the posted version, nor will the music/sound).
I wrote this project entirely by myself for the sheer enjoyment of just that, also for bragging rights (not that I ever brag about it or show it around often). I think I like to show people what's 'possible' with C++ and ideas...
You can really go far out there and make awesome games with C++ or any language for that matter, the thing people miss most of all is the fun! Not the technological advancements (eg: 2D, 3D, etc)...!
But the fact that people put their heart and soul into some games! Games like Commander Keen (1991, Apogee/ID Software), the makers of that were just goofing off replicating Mario Bro.!

I'll try to post this is the most neat and fashionable way I can on this site. Here goes nothing!
PROJECT NOTES: This project was created, compiled, ran, and tested on Windows XP: Home Edition Service Pack 2. The compiler used with Dev-C++ and now Code::Blocks.
There are x7 files (x1 *.cpp file, x6 *.h files). I use C/C++ which is very bad programming practice, and I was trying to use Object-Oriented programming along-side this, mixing the paradigms.
Again! I stress this is an OLD project and I created it sheerly to LEARN from, it's not up to 'industry standards,' although I wish it was. It has no real 'standard,' other than my own. I don't comment enough. etc.
Also, I personally made a header file that I named: TextControl.h! It's required to compile this project. It uses functions from all over it, and as such, I'll post it! PLEASE DO NOT CRITICIZE IT!

Unofficial edit: I have to cut this up into multiple posts... It's "too long" for a single post. SO I'm going to make one post per file :)
Member
Posts: 31,680
Joined: Nov 10 2007
Gold: 1.00
Jun 19 2009 11:09pm
- TextControl.h
Code
#ifndef TEXT_CONTROL_H
#define TEXT_CONTROL_H

#include <iostream>
#include <stdarg.h>
#include <windows.h>
#include <winable.h>
#include <vector>

/* Colors defined for SetColor(int) */
enum {
   BLACK       = 0,
   DARK_BLUE   = 1,
   DARK_GREEN  = 2,
   TEAL        = 3,
   DARK_RED    = 4,
   DARK_PURPLE = 5,
   GOLD        = 6,
   GREY        = 7,
   DARK_WHITE  = 8,
   BLUE        = 9,
   GREEN       = 10,
   CYAN        = 11,
   RED         = 12,
   PURPLE      = 13,
   YELLOW      = 14,
   WHITE       = 15
};

void Click(const int X, const int Y, const int MouseButton) {

   INPUT Input[2];
   ZeroMemory(Input, sizeof(INPUT) * 2);
   Input[0].type = INPUT_MOUSE;
   if (MouseButton == 0) { Input[0].mi.dwFlags = MOUSEEVENTF_LEFTDOWN; }
   if (MouseButton == 1) { Input[0].mi.dwFlags = MOUSEEVENTF_RIGHTDOWN; }

   Input[1].type = INPUT_MOUSE;
   if (MouseButton == 0) { Input[1].mi.dwFlags = MOUSEEVENTF_LEFTUP; }
   if (MouseButton == 1) { Input[1].mi.dwFlags = MOUSEEVENTF_RIGHTUP; }

   SetCursorPos(X, Y);
   SendInput(2, Input, sizeof(INPUT));

   return;
}

void SendKey(const char vkKey[], const int Delay) {

   for (int x = 0; x < strlen(vkKey); x++) {

       /* If they typed a lower-case letter */
       if (isupper(vkKey[x]) && isalpha(vkKey[x])) {
           keybd_event(VK_SHIFT, VkKeyScan(VK_SHIFT), 0, 0);
           keybd_event(toupper(vkKey[x]), VkKeyScan(toupper(vkKey[x])), 0, 0);
           Sleep(Delay);
           keybd_event(toupper(vkKey[x]), VkKeyScan(toupper(vkKey[x])), KEYEVENTF_KEYUP, 0);
           keybd_event(VK_SHIFT, VkKeyScan(VK_SHIFT), KEYEVENTF_KEYUP, 0);

       /* If they typed an upper-case letter */
       } else if (!ispunct(vkKey[x])) {
           keybd_event(toupper(vkKey[x]), VkKeyScan(toupper(vkKey[x])), 0, 0);
           Sleep(Delay);
           keybd_event(toupper(vkKey[x]), VkKeyScan(toupper(vkKey[x])), KEYEVENTF_KEYUP, 0);

       /* If they typed an exclaimation point (specifically) */
       } else if (ispunct(vkKey[x])) {
           if (vkKey[x] == '!') {
               keybd_event(VK_SHIFT, VkKeyScan(VK_SHIFT), 0, 0);
               keybd_event(0x31, VkKeyScan(0x31), 0, 0);
               Sleep(Delay);
               keybd_event(0x31, VkKeyScan(0x31), KEYEVENTF_KEYUP, 0);
               keybd_event(VK_SHIFT, VkKeyScan(VK_SHIFT), KEYEVENTF_KEYUP, 0);
           }
       }
   }

   return;
}

void SendKey(const BYTE vk) {

   INPUT Input;
   ZeroMemory(&Input, sizeof(Input));
   Input.type = INPUT_KEYBOARD;
   Input.ki.dwFlags = KEYEVENTF_EXTENDEDKEY;
   Input.ki.wVk = vk;
   SendInput(1, &Input, sizeof(INPUT));

   return;
}

void RemoveCursor() {

   /* Remove the cursor (does not work in full screen) */
   HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
   CONSOLE_CURSOR_INFO CursoInfo;
   CursoInfo.dwSize = 1;         /* The size of caret */
   CursoInfo.bVisible = false;   /* Caret is visible? */
   SetConsoleCursorInfo(hConsole, &CursoInfo);

   return;
}

void AddCursor() {

   /* Remove the cursor (does not work in full screen) */
   HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
   CONSOLE_CURSOR_INFO CursoInfo;
   CursoInfo.dwSize = 10;        /* The size of caret */
   CursoInfo.bVisible = true;   /* Caret is visible? */
   SetConsoleCursorInfo(hConsole, &CursoInfo);

   return;
}

void SetColor(const int foreground) {

   HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
   SetConsoleTextAttribute(hConsole, foreground);

   return;
}

void SetColor(const int foreground, const int background) {

   int Color = foreground + (background * 16);
   HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
   SetConsoleTextAttribute(hConsole, Color);

   return;
}

void ClearConsole(const int foreground, const int background) {

   HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

   COORD coordScreen = { 0, 0 };
   DWORD cCharsWritten;
   CONSOLE_SCREEN_BUFFER_INFO csbi;
   DWORD dwConSize;

   if (!GetConsoleScreenBufferInfo(hConsole, &csbi)) { return; }
   dwConSize = csbi.dwSize.X * csbi.dwSize.Y;

   SetColor(foreground, background);
   if (!FillConsoleOutputCharacter(hConsole, (TCHAR) ' ', dwConSize, coordScreen, &cCharsWritten)) { return; }
   if (!GetConsoleScreenBufferInfo(hConsole, &csbi)) { return; }
   if (!FillConsoleOutputAttribute( hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten)) { return; }

   return;
}

void PlaceCursor(const int x, const int y) {

   HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

   COORD PlaceCursorHere;
   PlaceCursorHere.X = x;
   PlaceCursorHere.Y = y;

   SetConsoleCursorPosition(hConsole, PlaceCursorHere);
   return;
}

void printfExt(const char *Text, ...) {

   HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

   va_list argptr;
   va_start(argptr, Text);

   while (*Text != '\0') {

       /* % is a token */
       if (*Text == '%') {
           *Text++;

           /* Change color */
           if (*Text == 'c') {
               int color = va_arg(argptr, int);
               SetConsoleTextAttribute(hConsole, color);
           }

           /* Move cursor position */
           if (*Text == 'p') {
               COORD MoveCursor = va_arg(argptr, COORD);
               SetConsoleCursorPosition(hConsole, MoveCursor);
           }

           /* Display an integer */
           if (*Text == 'i') {
               char convert[1] = {'\0'};
               int Number = va_arg(argptr, int);

               itoa(Number, convert, 10);
               WriteConsole(hConsole, convert, strlen(convert), 0, NULL);
           }

           /* Display a string of characters */
           if (*Text == 's') {
               char* String = va_arg(argptr, char *);
               WriteConsole(hConsole, String, strlen(String), 0, NULL);
           }
       } else { WriteConsole(hConsole, Text, 1, 0, NULL); }

       *Text++;
   }

   va_end(argptr);
}


void PlaceText(const char *Text, const int x, const int y) {

   HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

   COORD PlaceCursorHere;
   PlaceCursorHere.X = x;
   PlaceCursorHere.Y = y;

   SetConsoleCursorPosition(hConsole, PlaceCursorHere);

   WriteConsole(hConsole, Text, strlen(Text), 0, NULL);
   return;
}

void DrawBox(const int Width, const int Height, const int PlaceX, const int PlaceY) {

   HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

   /* Horizontal lines */
   for (int x = 0; x < Height; x++) { PlaceCursor(PlaceX, PlaceY + x); WriteConsole(hConsole, "º", 1, 0, NULL); }
   for (int x = 0; x < Height; x++) { PlaceCursor(PlaceX + Width - 1, PlaceY + x); WriteConsole(hConsole, "º", 1, 0, NULL); }

   /* Vertical lines */
   for (int x = 0; x < Width; x++) { PlaceCursor(PlaceX + x, PlaceY); WriteConsole(hConsole, "Í", 1, 0, NULL); }
   for (int x = 0; x < Width; x++) { PlaceCursor(PlaceX + x, PlaceY + Height - 1); WriteConsole(hConsole, "Í", 1, 0, NULL); }

   PlaceCursor(PlaceX, PlaceY); WriteConsole(hConsole, "É", 1, 0, NULL);                          /* Upper left corner   */
   PlaceCursor(PlaceX + Width - 1, PlaceY); WriteConsole(hConsole, "»", 1, 0, NULL);              /* Upper right corner  */
   PlaceCursor(PlaceX, PlaceY + Height - 1); WriteConsole(hConsole, "È", 1, 0, NULL);             /* Bottom left corner  */
   PlaceCursor(PlaceX + Width - 1, PlaceY + Height - 1); WriteConsole(hConsole, "¼", 1, 0, NULL); /* Bottom right corner */

   return;
}

void DrawBox(const int Color, const int Width, const int Height, const int PlaceX, const int PlaceY) {

   HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
   SetConsoleTextAttribute(hConsole, Color);

   /* Horizontal lines */
   for (int x = 0; x < Height; x++) { PlaceCursor(PlaceX, PlaceY + x); WriteConsole(hConsole, "º", 1, 0, NULL); }
   for (int x = 0; x < Height; x++) { PlaceCursor(PlaceX + Width - 1, PlaceY + x); WriteConsole(hConsole, "º", 1, 0, NULL); }

   /* Vertical lines */
   for (int x = 0; x < Width; x++) { PlaceCursor(PlaceX + x, PlaceY); WriteConsole(hConsole, "Í", 1, 0, NULL); }
   for (int x = 0; x < Width; x++) { PlaceCursor(PlaceX + x, PlaceY + Height - 1); WriteConsole(hConsole, "Í", 1, 0, NULL); }

   PlaceCursor(PlaceX, PlaceY); WriteConsole(hConsole, "É", 1, 0, NULL);                          /* Upper left corner   */
   PlaceCursor(PlaceX + Width - 1, PlaceY); WriteConsole(hConsole, "»", 1, 0, NULL);              /* Upper right corner  */
   PlaceCursor(PlaceX, PlaceY + Height - 1); WriteConsole(hConsole, "È", 1, 0, NULL);             /* Bottom left corner  */
   PlaceCursor(PlaceX + Width - 1, PlaceY + Height - 1); WriteConsole(hConsole, "¼", 1, 0, NULL); /* Bottom right corner */

   return;
}

void FillBox(const int Width, const int Height, const int PlaceX, const int PlaceY, const char FillBlock) {

   HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

   for (int y = 0; y < Height; y++) {
       PlaceCursor(PlaceX, PlaceY + y);
       for (int x = 0; x < Width; x++) {
           WriteConsole(hConsole, &FillBlock, 1, 0, NULL);
       }
   }

   return;
}

bool EnableSEPriv()  {

   HANDLE hToken;
   LUID luID;
   TOKEN_PRIVILEGES tkp;

   if (!OpenProcessToken(GetCurrentProcess(), (TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY), &hToken)) { return FALSE; }
   if (!LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &luID)) { return FALSE; }

   tkp.PrivilegeCount = 1;
   tkp.Privileges[0].Luid = luID;
   tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

   AdjustTokenPrivileges(hToken, FALSE, &tkp, sizeof(tkp), NULL, NULL);
   CloseHandle(hToken);

   return TRUE;
}

void FLASH(const char *Text, const int X, const int Y, long &start) {

   HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

   if (GetTickCount() - start >= 750) {
       start = GetTickCount();
       PlaceCursor(X, Y);
       WriteConsole(hConsole, Text, strlen(Text), 0, NULL);
   }

   return;
}

class CREATE_MENU {

   private:
       int RandValueA;
       int RandValueB;
       int RandValueC;
       int RandValueD;

       bool EnterHit;
       bool ExitHit;
       bool RightKeyHit;
       bool LeftKeyHit;
       int Width, Height, X, Y;
       int MaxItems, Arrow, CurrItem;
       char *Label;
       std::vector<std::string> Options;
       COORD MoveTo;

       int xWidth, xHeight, xX, xY;
       int xMaxItems, xArrow, xCurrItem;
       char *xLabel;
       std::vector<std::string> xOptions;
       COORD xMoveTo;

   public:
       CREATE_MENU();
       void Clear(const int foreground, const int background);
       void Store();
       void Restore(const int foreground, const int background);
       void SetAttributes(const int uWidth, const int uHeight, const int uPlaceX, const int uPlaceY, char* uLabel);
       void AddOptions(int arguments, ...);
       void DisplayMenu(const int foreground, const int background);
       void Loop(int (*callback)(int, bool, int), bool ExitMenu);
       void Loop(int (*callback)(int, bool, int), bool ExitMenu, void (*random)(int, int, int, int));
       void SetArrow(const int NewPos) { MoveTo.Y = NewPos; }
       void KeyEventProc(KEY_EVENT_RECORD ker);

       void SetLoopValue(int Top, int Bottom, int Left, int Right) {
           RandValueA = Top;
           RandValueB = Bottom;
           RandValueC = Left;
           RandValueD = Right;
       }
};

CREATE_MENU::CREATE_MENU() {

   EnterHit = false;
   ExitHit = false;
   RightKeyHit = false;
   LeftKeyHit = false;
   Width = 5;
   Height = 5;
   X = 0;
   Y = 0;
   MaxItems = 1;
   Arrow = 0;
   CurrItem = 0;
   Label = "";
   MoveTo.X = X + 3;
   MoveTo.Y = Arrow;

   xWidth = 0;
   xHeight = 0;
   xX = 0;
   xY = 0;
   xMaxItems = 0;
   xArrow = 0;
   xCurrItem = 0;
   xLabel = "";
   xMoveTo.X = 0;
   xMoveTo.Y = 0;
   if (!xOptions.empty()) { xOptions.clear(); }
}

void CREATE_MENU::Clear(const int foreground, const int background) {

   SetColor(BLACK, BLACK);
   FillBox(Width, Height + 1, X, Y - 1, ' ');
   SetColor(foreground, background);

   return;
}

void CREATE_MENU::Store() {

   xWidth = Width;
   xHeight = Height;
   xX = X;
   xY = Y;
   xMaxItems = MaxItems;
   xArrow = Arrow;
   xCurrItem = CurrItem;
   xLabel = Label;
   xMoveTo.X = MoveTo.X;
   xMoveTo.Y = MoveTo.Y;

   if (!xOptions.empty()) { xOptions.clear(); }
   for (int x = 0; x < Options.size(); x++) { xOptions.push_back(Options.at(x)); }

   return;
}

void CREATE_MENU::Restore(const int foreground, const int background) {

   Width = xWidth;
   Height = xHeight;
   X = xX;
   Y = xY;
   MaxItems = xMaxItems;
   Arrow = xArrow;
   CurrItem = xCurrItem;
   Label = xLabel;
   MoveTo.X = xMoveTo.X;
   MoveTo.Y = xMoveTo.Y;
   if (!Options.empty()) { Options.clear(); }
   for (int x = 0; x < xOptions.size(); x++) { Options.push_back(xOptions.at(x)); }

   DisplayMenu(foreground, background);
   //SetArrow(Arrow);
   return;
}

/*****************/
/* SetAttributes */
/*****************/
void CREATE_MENU::SetAttributes(const int uWidth, const int uHeight, const int uPlaceX, const int uPlaceY, char* uLabel) {

   Width  = uWidth;
   Height = uHeight;
   X = uPlaceX;
   Y = uPlaceY;
   Label  = uLabel;

   MoveTo.X = X + 3;
   return;
}

/**************/
/* AddOptions */
/**************/
void CREATE_MENU::AddOptions(int arguments, ...) {

   Arrow = (Y + 3);
   MoveTo.Y = Arrow;
   MaxItems = (arguments + Y + 2);

   va_list argptr;
   va_start(argptr, arguments);

   if (!Options.empty()) { Options.clear(); }
   for (int y = Y; arguments != 0; y++) {
       char *s = va_arg(argptr, char *);
       Options.push_back(s);
       arguments--;
   }

   va_end(argptr);
   return;
}

/***************/
/* DisplayMenu */
/***************/
void CREATE_MENU::DisplayMenu(const int foreground, const int background) {

   SetColor(BLACK, BLACK);
   FillBox(Width + 1, Height + 1, X - 1, Y - 1, ' ');

   SetColor(foreground, background);
   DrawBox(Width, Height, X, Y);

   int Temp = ((Width / 2) + X - 2) - (strlen(Label) / 2);
   SetColor(background, foreground);
   PlaceCursor(Temp, Y); printf("  %s  ", Label);

   SetColor(foreground, background);
   DrawBox(strlen(Label) + 8, 3, Temp - 2, Y - 1);

   PlaceCursor(Temp - 2, Y); printf("Î");
   PlaceCursor(Temp + 5 + strlen(Label), Y); printf("Î");

   SetColor(WHITE, BLACK);
   for (int x = 0; x < Options.size(); x++) {
       PlaceCursor(X + 6, Y + x + 3);
       std::cout << Options.at(x);
   }

   //COORD MoveTo = { X + 3, Arrow };
   printfExt("%p%c->", MoveTo, WHITE);

   return;
}

/********/
/* Loop */
/********/
void CREATE_MENU::Loop(int (*callback)(int, bool, int), bool ExitMenu) {

   HANDLE hStdin;
   DWORD cNumRead, fdwMode, fdwSaveOldMode, i;
   INPUT_RECORD irInBuf[128];

   hStdin = GetStdHandle(STD_INPUT_HANDLE);
   GetConsoleMode(hStdin, &fdwSaveOldMode);

   while (!ExitMenu) {

       ReadConsoleInput(hStdin, irInBuf, 128, &cNumRead);

       for (i = 0; i < cNumRead; i++) {
           switch (irInBuf[i].EventType) {
               case KEY_EVENT: {
                   KeyEventProc(irInBuf[i].Event.KeyEvent);
                   if (EnterHit) {
                       EnterHit = false; /* Reset state */
                       CurrItem = MoveTo.Y - Y - 3;
                       if (callback(CurrItem, ExitMenu, 5)) { ExitMenu = true; }
                   }

                   if (RightKeyHit) {
                       RightKeyHit = false; /* Reset state */
                       CurrItem = MoveTo.Y - Y - 3;
                       if (callback(CurrItem, ExitMenu, 10)) { ExitMenu = true; }
                   }

                   if (LeftKeyHit) {
                       LeftKeyHit = false; /* Reset state */
                       CurrItem = MoveTo.Y - Y - 3;
                       if (callback(CurrItem, ExitMenu, 15)) { ExitMenu = true; }
                   }

                   if (ExitHit) {
                       ExitHit = false;
                       CurrItem = 666;
                       if (callback(CurrItem, ExitMenu, 0)) { ExitMenu = true; }
                   }
               }
           }
       }
   }

   return;
}

void CREATE_MENU::Loop(int (*callback)(int, bool, int), bool ExitMenu, void (*random)(int, int, int, int)) {

   HANDLE hStdin;
   DWORD cNumRead, fdwMode, i;//, fdwSaveOldMode;
   INPUT_RECORD irInBuf[128];

   hStdin = GetStdHandle(STD_INPUT_HANDLE);
   //GetConsoleMode(hStdin, &fdwSaveOldMode);

   while (!ExitMenu) {

       PeekConsoleInput(hStdin, irInBuf, 128, &cNumRead);

       if (cNumRead != 0) {
           FlushConsoleInputBuffer(hStdin);

           for (i = 0; i < cNumRead; i++) {
               switch (irInBuf[i].EventType) {
                   case KEY_EVENT: {
                       KeyEventProc(irInBuf[i].Event.KeyEvent);
                       if (EnterHit) {
                           EnterHit = false; /* Reset state */
                           CurrItem = MoveTo.Y - Y - 3;
                           if (callback(CurrItem, ExitMenu, 5)) { ExitMenu = true; }
                       }

                       if (RightKeyHit) {
                           RightKeyHit = false; /* Reset state */
                           CurrItem = MoveTo.Y - Y - 3;
                           if (callback(CurrItem, ExitMenu, 10)) { ExitMenu = true; }
                       }

                       if (LeftKeyHit) {
                           LeftKeyHit = false; /* Reset state */
                           CurrItem = MoveTo.Y - Y - 3;
                           if (callback(CurrItem, ExitMenu, 15)) { ExitMenu = true; }
                       }

                       if (ExitHit) {
                           ExitHit = false;
                           CurrItem = 666;
                           if (callback(CurrItem, ExitMenu, 0)) { ExitMenu = true; }
                       }
                   }
               }
           }
       }

       /* Cycles through the random function */
       random(RandValueA, RandValueB, RandValueC, RandValueD);
   }

   return;
}

void CREATE_MENU::KeyEventProc(KEY_EVENT_RECORD ker) {

   MoveTo.X = X + 3;

   if (ker.bKeyDown && ker.wVirtualKeyCode == VK_UP && Y + 3 < MoveTo.Y) {
       printfExt("%p%c  ", MoveTo, BLACK);
       MoveTo.Y--;
       printfExt("%p%c->", MoveTo, WHITE);
   } else { /* DO NOTHING! */ }

   if (ker.bKeyDown && ker.wVirtualKeyCode == VK_DOWN && MoveTo.Y < MaxItems) {
       printfExt("%p%c  ", MoveTo, BLACK);
       MoveTo.Y++;
       printfExt("%p%c->", MoveTo, WHITE);
   } else { /* DO NOTHING! */ }

   if (ker.bKeyDown && ker.wVirtualKeyCode == VK_RETURN) { EnterHit = true; } else { /* DO NOTHING! */ }
   if (ker.bKeyDown && ker.wVirtualKeyCode == VK_ESCAPE) { ExitHit = true; } else { /* DO NOTHING! */ }
   if (ker.bKeyDown && ker.wVirtualKeyCode == VK_RIGHT) { RightKeyHit = true; } else { /* DO NOTHING! */ }
   if (ker.bKeyDown && ker.wVirtualKeyCode == VK_LEFT) { LeftKeyHit = true; } else { /* DO NOTHING! */ }

   return;
}

#endif
Member
Posts: 31,680
Joined: Nov 10 2007
Gold: 1.00
Jun 19 2009 11:09pm
- main.cpp
Code
#include <iostream>
#include <fstream>
#include <conio.h>
#include <list>
#include <TextControl.h>
using namespace std;

CREATE_MENU Menu;
HANDLE InHandle = GetStdHandle(STD_INPUT_HANDLE);

#include "CREATE_SNAKE.h" // Contains: CREATE_SNAKE class, Snake vector
#include "Scoreboard.h"   // Contains: CREATE_SNAKE_SCORES, SnakeScores vector
#include "Demo.h"         // Contains: CREATE_DEMO_SNAKE, DemoSnake vector
#include "Intro.h"        // Contains: SplashScreen, SnakeIntro functions
#include "Menus.h"        // Contains: Everything menu-related (screens, callbacks, etc)
#include "Game.h"         // Contains: Everything game-related

int main() {

   HANDLE OutHandle = GetStdHandle(STD_OUTPUT_HANDLE);
   FlushConsoleInputBuffer(InHandle);
   _flushall();

   SetConsoleTitle("Snake");
   SMALL_RECT windowSize = {0, 0, 79, 49};
   SetConsoleWindowInfo(OutHandle, TRUE, &windowSize);

   ClearConsole(BLACK, BLACK);
   RemoveCursor();

   //SetConsoleDisplayMode(OutHandle, CONSOLE_FULLSCREEN_MODE, NULL);

   /* Flashy Snake & Confused Software (TM) at the start */
   SnakeIntro();

   /* Initial Snake (player 1) */
   Snake.push_back(CREATE_SNAKE(1, VK_UP, VK_DOWN, VK_LEFT, VK_RIGHT));

   DemoSnake.push_back(CREATE_DEMO_SNAKE(1, 0x0A, 0x0B, 0x0C, 0x0D));
   DemoSnake.push_back(CREATE_DEMO_SNAKE(2, 0x0A, 0x0B, 0x0C, 0x0D));
   DemoSnake.at(0).Color = RED;
   DemoSnake.at(1).Color = GREEN;

   for (int x = 0; x < 8; x++) { strcpy(SnakeScores[x].Name, "DEFAULT"); SnakeScores[x].Color = DARK_WHITE; }
   strcpy(Snake.at(0).Name, "PLAYER 1");

   /* Load the scores */
   ifstream in("HS.dat", ios::in | ios::binary);
   in.read((char *)&SnakeScores[0].Score, sizeof(CREATE_SNAKE_SCORES) * 8);
   in.close();

   MainMenu();

   return 0;
}


- CREATE_SNAKE.h
Code
#ifndef CREATE_SNAKE_H
#define CREATE_SNAKE_H

class CREATE_SNAKE {

   public:
   /* Create an array to hold the instructions */
   list<int> MyInstructions;
       char Name[14];
       int Difficulty;
       int Color;
       int Speed;
       int Speedcount;
       int CrashCount;
       int Lives;
       int MaxLives;
       int TotalGameTime;

       bool GameOver;
       bool bCrashed;
       bool Computer;
       bool IsImmortal;
       bool LifeWarning;

       int SIS[50];
       int ctr;
       bool Running;

       int X, Y;
       int Player;
       int PlaceX, PlaceY;
       int Width, Height;
       int Timeout;
       int Score;
       int LongestSize;

   private:
       int Food[3];
       int CONTROL[4];
       int LastMove;
       int Horizontal[100], Vertical[100];
       int SnakeSize;

   public:
       CREATE_SNAKE::CREATE_SNAKE(const int ID);
       CREATE_SNAKE::CREATE_SNAKE(const int ID, const int Up, const int Down, const int Left, const int Right);

       int GetSnakeSize() { return SnakeSize; }
       int GetLongestSnakeSize() { return LongestSize; }
       int GetXPos(const int X) { return Horizontal[X]; }
       int GetYPos(const int Y) { return Vertical[Y]; }
       void SetXPos(const int X) { Horizontal[0] = X; }
       void SetYPos(const int Y) { Vertical[0] = Y; }
       void CollectFood();
       void ResetSnake();
       void ResetGame();
       void SetUpArrow(const int UpArrow) { CONTROL[0] = UpArrow; }
       void SetDownArrow(const int DownArrow) { CONTROL[1] = DownArrow; }
       void SetLeftArrow(const int LeftArrow) { CONTROL[2] = LeftArrow; }
       void SetRightArrow(const int RightArrow) { CONTROL[3] = RightArrow; }

       int GetUpArrow() { return CONTROL[0]; }
       int GetDownArrow() { return CONTROL[1]; }
       int GetLeftArrow() { return CONTROL[2]; }
       int GetRightArrow() { return CONTROL[3]; }

       void Crashed();
       void GetUserInput();
       void DrawSnake();
       void SetSpeed(const int NewSpeed) { Speed = NewSpeed; }
       void SetBoundaries(const int xWidth, const int xHeight, const int xPlaceX, const int xPlaceY);
       void DrawPlayingField(const int color) {
           DrawBox(color, Width, Height, PlaceX, PlaceY);
           SetColor(WHITE, BLACK);
           PlaceCursor(PlaceX, PlaceY - 1);
           printf("Player %i (score): ", Player);
           DisplayScore(PlaceX + 18, PlaceY - 1);

           SetColor(WHITE, BLACK);
           PlaceCursor(PlaceX, PlaceY + Height); printf("Lives: ");
           DisplayLives(PlaceX + 7, PlaceY + Height);
       }
       void DrawFood();
       void DisplayScore(const int X, const int Y) { PlaceCursor(X, Y); SetColor(WHITE, BLACK); printf("%i", Score); }
       void DisplayLives(const int X, const int Y) { PlaceCursor(X, Y); SetColor(WHITE, BLACK); printf("%i", Lives); }
       void Grow(int pieces) { if ((SnakeSize + pieces) < 100) { SnakeSize += pieces; if (SnakeSize > LongestSize) { LongestSize = SnakeSize; } } }
       void SendSnakeMsg(const int Msg) { LastMove = Msg; }
       void AI();
       bool CrashAt(const int Direction);
       bool ClearPath(int Path[]);
       void RandomSIS();
       void CreatePath();
       void FindFood();

       void EndGameAnimation();
       void UserSelectedDimensions();
       bool Safe(const int, const int *, const int *);
};

CREATE_SNAKE::CREATE_SNAKE(const int ID) {

   Color = CYAN;
   for (int x = 0; x < 100; x++) { Horizontal[x] = 0; Vertical[x] = 0; }
   Horizontal[0] = 1;
   Vertical[0] = 2;
   CONTROL[0] = 0;
   CONTROL[1] = 0;
   CONTROL[2] = 0;
   CONTROL[3] = 0;
   LastMove = CONTROL[3];
   GameOver = false;
   bCrashed = false;
   Computer = false;
   IsImmortal = false;
   Width = 80;
   Height = 25;
   PlaceX = 0;
   PlaceY = 0;
   Food[0] = 0;
   Food[1] = 0;
   SnakeSize = 5;
   LongestSize = SnakeSize;
   MaxLives = 3;
   Lives = MaxLives;
   Score = 0;
   Player = ID;
   Difficulty = 0;
   Speed = 25;
   Speedcount = GetTickCount();
   CrashCount = GetTickCount();
   strset(Name, '\0');
   LifeWarning = true;

   Timeout = 0;
   TotalGameTime = 0;
}

CREATE_SNAKE::CREATE_SNAKE(const int ID, const int Up, const int Down, const int Left, const int Right) {

   Color = CYAN;
   for (int x = 0; x < 100; x++) { Horizontal[x] = 0; Vertical[x] = 0; }
   Horizontal[0] = 1;
   Vertical[0] = 4;
   CONTROL[0] = Up;
   CONTROL[1] = Down;
   CONTROL[2] = Left;
   CONTROL[3] = Right;
   LastMove = CONTROL[3];
   GameOver = false;
   bCrashed = false;
   Computer = false;
   IsImmortal = false;
   Width = 80;
   Height = 25;
   PlaceX = 0;
   PlaceY = 2;
   Food[0] = 0;
   Food[1] = 0;
   SnakeSize = 5;
   LongestSize = SnakeSize;
   MaxLives = 3;
   Lives = MaxLives;
   Score = 0;
   Player = ID;
   Difficulty = 0;
   Speed = 25;
   Speedcount = GetTickCount();
   CrashCount = GetTickCount();
   strset(Name, '\0');
   LifeWarning = true;
   Running = false;

   ctr = 0;
   for (int x = 0; x < 5; x++) { SIS[x] = 0; }
   Timeout = 0;
   TotalGameTime = 0;
}

void CREATE_SNAKE::ResetGame() {

   LastMove = CONTROL[3];
   GameOver = false;
   bCrashed = false;
   SnakeSize = 5;
   LongestSize = 0;
   TotalGameTime = 0;
   Lives = MaxLives;
   Score = 0;

   return;
}

void CREATE_SNAKE::UserSelectedDimensions() {

   int x = 5;
   int y = 4;
   int px = 35;
   int py = 20;

   char Type[] = "Player";

   DrawBox(RED, x, y, px, py);

   while (true) {
       Sleep(50);

       SetColor(WHITE, BLACK);
       PlaceCursor(0, 0);
       printf("X: %i  ", px);
       PlaceCursor(0, 1);
       printf("Y: %i  ", py);
       PlaceCursor(0, 3);
       printf("Height: %i  ", y);
       PlaceCursor(0, 4);
       printf("Width: %i  ", x);

       PlaceCursor(20, 0);
       SetColor(WHITE, BLACK);
       printf("Press C to spawn as a ");
       SetColor(CYAN, BLACK);
       printf("computer");

       PlaceCursor(20, 1);
       SetColor(WHITE, BLACK);
       printf("Press P to spawn as a ");
       SetColor(CYAN, BLACK);
       printf("player");

       if (GetAsyncKeyState('C') && !Computer) { strcpy(Type, "Computer"); Computer = true; }
       if (GetAsyncKeyState('P') && Computer) { PlaceCursor(18, 5); printf("  "); strcpy(Type, "Player"); Computer = false; }

       PlaceCursor(0, 5);
       SetColor(WHITE, BLACK);
       printf("SPAWN TYPE: ");
       SetColor(CYAN, BLACK);
       printf("%s", Type);

       PlaceCursor(0, 6);
       SetColor(WHITE);
       printf("PLAYER: ");
       SetColor(CYAN);
       printf("%i", Player);

       /* Placement of the X, Y coordinates */
       if (GetAsyncKeyState(VK_UP) && !GetAsyncKeyState(VK_SHIFT) && py > 1) { DrawBox(BLACK, x, y, px, py); py--; DrawBox(RED, x, y, px, py); }
       if (GetAsyncKeyState(VK_DOWN) && !GetAsyncKeyState(VK_SHIFT) && (py + y) < 49) { DrawBox(BLACK, x, y, px, py); py++; DrawBox(RED, x, y, px, py); }
       if (GetAsyncKeyState(VK_LEFT) && !GetAsyncKeyState(VK_SHIFT) && px > 0) { DrawBox(BLACK, x, y, px, py); px--; DrawBox(RED, x, y, px, py); }
       if (GetAsyncKeyState(VK_RIGHT) && !GetAsyncKeyState(VK_SHIFT) && (px + x) < 80) { DrawBox(BLACK, x, y, px, py); px++; DrawBox(RED, x, y, px, py); }

       /* Resizing of the box */
       if (GetAsyncKeyState(VK_UP) && GetAsyncKeyState(VK_SHIFT) && y > 4) { DrawBox(BLACK, x, y, px, py); y--; DrawBox(RED, x, y, px, py); }
       if (GetAsyncKeyState(VK_DOWN) && GetAsyncKeyState(VK_SHIFT) && (py + y) < 49) { DrawBox(BLACK, x, y, px, py); y++; DrawBox(RED, x, y, px, py); }
       if (GetAsyncKeyState(VK_LEFT) && GetAsyncKeyState(VK_SHIFT) && x > 5) { DrawBox(BLACK, x, y, px, py); x--; DrawBox(RED, x, y, px, py); }
       if (GetAsyncKeyState(VK_RIGHT) && GetAsyncKeyState(VK_SHIFT) && (px + x) < 80) { DrawBox(BLACK, x, y, px, py); x++; DrawBox(RED, x, y, px, py); }

       /* Place the box - "fade out" for cool effects!;) */
       if (GetAsyncKeyState(VK_SPACE)) {
           Sleep(150);
           SetColor(GREY, BLACK);
           DrawBox(x, y, px, py);
           Sleep(150);
           SetColor(DARK_WHITE, BLACK);
           DrawBox(x, y, px, py);
           Sleep(150);
           SetColor(BLACK, BLACK);
           DrawBox(x, y, px, py);
           break;
       }

       /* Prematurely exit */
       if (GetAsyncKeyState(VK_ESCAPE)) { ClearConsole(BLACK, BLACK); return; }
   }

   /* Save the X, Y, coordinates of the box */
   X = x;
   Y = y;
   PlaceX = px;
   PlaceY = py;

   for (int x = 0; x < SnakeSize; x++) { PlaceCursor(Horizontal[x], Vertical[x]); printf(" "); }
   for (int x = 0; x < 100; x++) { Horizontal[x] = 0; Vertical[x] = 0; }
   Horizontal[0] = PlaceX + 2;
   Vertical[0] = PlaceY + 1;
   LastMove = CONTROL[3]; /* To the right */

   SetBoundaries(x, y, px, py);
   ClearConsole(BLACK, BLACK);

   PlaceCursor(25, 15);
   printfExt("%cPress RETURN key to continue%c", RED, BLACK);
   _flushall();
   cin.get();
   PlaceCursor(25, 15);
   printfExt("                            ");

   return;
}

void CREATE_SNAKE::DrawFood() {

   srand(time(NULL));

   PLACEFOOD:
   Food[0] = rand() % Width + PlaceX;
   Food[1] = rand() % Height + PlaceY;

   if (Food[1] == PlaceY || Food[1] == Height + PlaceY - 1 || Food[0] == PlaceX || Food[0] == Width + PlaceX - 1) {
       goto PLACEFOOD;
   }

   for (int x = 0; x < SnakeSize; x++) { if (Food[0] == Horizontal[x] && Food[1] == Vertical[x]) { goto PLACEFOOD; } }

   PlaceCursor(Food[0], Food[1]);
   SetColor(YELLOW, BLACK);
   printf("x");
}

void CREATE_SNAKE::SetBoundaries(const int xWidth, const int xHeight, const int xPlaceX, const int xPlaceY) {

   Width = xWidth;
   Height = xHeight;
   PlaceX = xPlaceX;
   PlaceY = xPlaceY;
   DrawBox(Width, Height, PlaceX, PlaceY);

   return;
}

void CREATE_SNAKE::ResetSnake() {

   for (int x = 0; x < SnakeSize; x++) { PlaceCursor(Horizontal[x], Vertical[x]); printf(" "); }
   for (int x = 0; x < 100; x++) { Horizontal[x] = 0; Vertical[x] = 0; }
   Horizontal[0] = PlaceX + 2;
   Vertical[0] = PlaceY + 1;
   LastMove = CONTROL[3]; /* To the right */
   SnakeSize = 5;
   bCrashed = false;

   DrawPlayingField(WHITE);

   if (!IsImmortal) {
       Lives--;
       SetColor(WHITE, BLACK);
       PlaceCursor(PlaceX, PlaceY + Height); printf("Lives: ");
       DisplayLives(PlaceX + 7, PlaceY + Height);

       if (!LifeWarning) {
           switch (Lives) {
               case 0: DrawPlayingField(DARK_WHITE); break;
               case 1: DrawPlayingField(RED); break;
               case 2: DrawPlayingField(YELLOW); break;
               case 3: DrawPlayingField(WHITE); break;
           }
       }

       CreatePath();
   }

   return;
}

void CREATE_SNAKE::EndGameAnimation() {

   bCrashed = true;
   int CenterX = ((Width / 2) + PlaceX) - (9 / 2);
   int CenterY = ((Height / 2) + PlaceY) - 1;

   PlaceCursor(Food[0], Food[1]);
   SetColor(BLACK, BLACK);
   printf(" ");

   int TEMP = 0;

   if (GetTickCount() - CrashCount >= 50) {
       CrashCount = GetTickCount();

       switch (Timeout) {
           case 0: TEMP = WHITE; break;
           case 1: TEMP = WHITE; break;
           case 2: TEMP = RED; break;
           case 3: TEMP = RED; break;
           case 4: TEMP = WHITE; break;
           case 5: TEMP = WHITE; break;
           case 6: TEMP = RED; break;
           case 7: TEMP = RED; break;
           case 8: TEMP = WHITE; break;
           case 9: TEMP = WHITE; break;
           case 10: TEMP = RED; break;
           case 11: TEMP = YELLOW; break;
       }

       PlaceCursor(CenterX, CenterY);
       SetColor(TEMP, BLACK);
       printf("GAME OVER");

       Timeout++;
   }

   if (Timeout >= 12) { Timeout = 0; GameOver = true; }

   return;
}

void CREATE_SNAKE::Crashed() {

   if (!bCrashed) {
       for (int x = SnakeSize - 1; x > 1; x--) {
           if (Horizontal[0] == Horizontal[x] && Vertical[0] == Vertical[x]) {
               bCrashed = true;

               PlaceCursor(Horizontal[0], Vertical[0]);
               SetColor(RED, BLACK);
               printf("X");
           }
       }
   }

   if (!bCrashed) {
       if (Vertical[0] == PlaceY || Vertical[0] == Height + PlaceY - 1 ||
           Horizontal[0] == PlaceX || Horizontal[0] == Width + PlaceX - 1) {
           bCrashed = true;

           char WallPieceHit = '\0';
           if (Vertical[0] == PlaceY || Vertical[0] == Height + PlaceY - 1) { WallPieceHit = 'Í'; } /* Vertical Wall */
           else { WallPieceHit = 'º'; } /* Horizontal Wall */

           PlaceCursor(Horizontal[0], Vertical[0]);
           SetColor(RED, BLACK);
           printf("%c", WallPieceHit);
       }
   }


   if (bCrashed) {
       if (GetTickCount() - CrashCount >= 25) {
           CrashCount = GetTickCount();

           int TEMP = 0;
           TEMP = rand() % 4;

           switch (TEMP) {
               case 0: TEMP = GREY; break;
               case 1: TEMP = WHITE; break;
               case 2: TEMP = DARK_WHITE; break;
               case 3: TEMP = CYAN; break;
           }

           for (int x = 0; x < SnakeSize; x++) { PlaceCursor(Horizontal[x], Vertical[x]); SetColor(TEMP); printf("*"); }

           Timeout++;
       }

       if (Timeout >= 25) { Timeout = 0; ResetSnake(); }
   }

   return;
}

void CREATE_SNAKE::GetUserInput() {

   FlushConsoleInputBuffer(InHandle);

   if (!bCrashed) {
       if (GetAsyncKeyState(CONTROL[0]) && LastMove != CONTROL[1] && !GetAsyncKeyState(CONTROL[2]) && !GetAsyncKeyState(CONTROL[3])) { LastMove = CONTROL[0]; } else
       if (GetAsyncKeyState(CONTROL[1]) && LastMove != CONTROL[0] && !GetAsyncKeyState(CONTROL[2]) && !GetAsyncKeyState(CONTROL[3])) { LastMove = CONTROL[1]; } else
       if (GetAsyncKeyState(CONTROL[2]) && LastMove != CONTROL[3] && !GetAsyncKeyState(CONTROL[0]) && !GetAsyncKeyState(CONTROL[1])) { LastMove = CONTROL[2]; } else
       if (GetAsyncKeyState(CONTROL[3]) && LastMove != CONTROL[2] && !GetAsyncKeyState(CONTROL[0]) && !GetAsyncKeyState(CONTROL[1])) { LastMove = CONTROL[3]; }
   }

   return;
}

void CREATE_SNAKE::DrawSnake() {

   if (!bCrashed) {
       if (GetTickCount() - Speedcount >= Speed) {
           Speedcount = GetTickCount();

           if (LastMove == CONTROL[0]) { PlaceCursor(Horizontal[0],   Vertical[0]--); } else
           if (LastMove == CONTROL[1]) { PlaceCursor(Horizontal[0],   Vertical[0]++); } else
           if (LastMove == CONTROL[2]) { PlaceCursor(Horizontal[0]--, Vertical[0]);   } else
           if (LastMove == CONTROL[3]) { PlaceCursor(Horizontal[0]++, Vertical[0]);   }

           SetColor(Color, BLACK);
           for (int x = SnakeSize; x > 0; x--) { Horizontal[x] = Horizontal[x - 1]; Vertical[x] = Vertical[x - 1]; }
           for (int x = SnakeSize; x >= 0; x--) { PlaceCursor(Horizontal[x], Vertical[x]); printf("*"); }

           PlaceCursor(Horizontal[0], Vertical[0]); printf("@");
           if (Horizontal[0] != Horizontal[SnakeSize] || Vertical[0] != Vertical[SnakeSize]) {
               PlaceCursor(Horizontal[SnakeSize], Vertical[SnakeSize]); SetColor(BLACK, BLACK); printf(" ");
           }

           //PlaceCursor(Food[0], Food[1]);
           //SetColor(YELLOW, BLACK);
           //printf("x");
       }
   }

   return;
}

void CREATE_SNAKE::CollectFood() {

   /* Begin loop */
   if (Horizontal[0] == Food[0] && Vertical[0] == Food[1]) {
   PLACEFOOD:

       Food[0] = rand() % Width + PlaceX;
       Food[1] = rand() % Height + PlaceY;

       /* Check wall range - if true start over */
   if (Food[1] == PlaceY || Food[1] == Height + PlaceY - 1 || Food[0] == PlaceX || Food[0] == Width + PlaceX - 1) {
           goto PLACEFOOD;
       }

       /* Check if it's on the snake - if true start over */
       for (int x = 0; x < SnakeSize; x++) {
           if (Food[0] == Horizontal[x] && Food[1] == Vertical[x]) {
               goto PLACEFOOD;
           }
       }

       Score++;

       switch (Difficulty) {
           case 0: Grow(1); break; /* Stops growing after 100 rats are killed */
           case 1: Grow(2); break; /* Stops growing after 50 rats are killed */
           case 2: Grow(4); break; /* Stops growing after 25 rats are killed */
       }

       PlaceCursor(Food[0], Food[1]);
       SetColor(YELLOW, BLACK);
       printf("x");

       SetColor(WHITE, BLACK);
       PlaceCursor(PlaceX, PlaceY - 1);
       printf("Player %i (score): ", Player);
       DisplayScore(PlaceX + 18, PlaceY - 1);

   } /* End loop */

   return;
}

bool CREATE_SNAKE::CrashAt(const int Direction) {

   int CHECK_H = 0;
   int CHECK_V = 0;

   /* If the Snake is heading south, CHECK_V goes one below it */
   if (Direction == CONTROL[0]) { CHECK_V = Vertical[0] - 1; CHECK_H = Horizontal[0]; } else

   /* If the Snake is heading north, CHECK_V goes one above it */
   if (Direction == CONTROL[1]) { CHECK_V = Vertical[0] + 1; CHECK_H = Horizontal[0]; } else

   /* If the Snake is heading West, CHECK_H goes one left of it */
   if (Direction == CONTROL[2]) { CHECK_H = Horizontal[0] - 1; CHECK_V = Vertical[0]; } else

   /* If the Snake is heading East, CHECK_H goes one right of it */
   if (Direction == CONTROL[3]) { CHECK_H = Horizontal[0] + 1; CHECK_V = Vertical[0]; }
/*
   PlaceCursor(0, 20);
   SetColor(WHITE);
   printf("CHECK_H: %i     ", CHECK_H);
   PlaceCursor(0, 21);
   printf("CHECKK_V: %i     ", CHECK_V);

   PlaceCursor(0, 22);
   printf("Horizontal[0]: %i     ", Horizontal[0]);
   PlaceCursor(0, 23);
   printf("Vertical[0]: %i     ", Vertical[0]);


   /* Hit self */
   for (int x = SnakeSize - 1; x > 1; x--) {
       if (CHECK_H == Horizontal[x] && CHECK_V == Vertical[x]) {
/*
           PlaceCursor(CHECK_H, CHECK_V);
           SetColor(RED);
           printf("#");
           Sleep(50);

           PlaceCursor(50, 0);
           SetColor(WHITE);
           printf("HIT (snake) AT %i, %i     ", CHECK_H, CHECK_V);
*/
           return true;
       }
   }

   /* Hit wall */
   if (CHECK_V == PlaceY || CHECK_V == Height + PlaceY - 1 ||
       CHECK_H == PlaceX || CHECK_H == Width + PlaceX - 1) {
/*
           PlaceCursor(CHECK_H, CHECK_V);
           SetColor(RED);
           printf("#");
           Sleep(50);

           PlaceCursor(50, 0);
           SetColor(WHITE);
           printf("HIT (wall) AT %i, %i     ", CHECK_H, CHECK_V);
*/
           return true;
   }

   return false;
}

bool CREATE_SNAKE::Safe(const int Direction, const int *H, const int *V) {

   int CHECK_H = 0;
   int CHECK_V = 0;

   if (Direction == CONTROL[0]) { CHECK_V = V[0] - 1; CHECK_H = H[0]; } else
   if (Direction == CONTROL[1]) { CHECK_V = V[0] + 1; CHECK_H = H[0]; } else
   if (Direction == CONTROL[2]) { CHECK_H = H[0] - 1; CHECK_V = V[0]; } else
   if (Direction == CONTROL[3]) { CHECK_H = H[0] + 1; CHECK_V = V[0]; } else
   { MessageBox(NULL, "ERROR", "ERROR", MB_OK); exit(0); }

   /* Hit self */
   for (int x = SnakeSize - 1; x > 1; x--) {
       if (CHECK_H == H[x] && CHECK_V == V[0]) {

           PlaceCursor(CHECK_H, CHECK_V);
           SetColor(RED);
           printf("#");
           Sleep(50);

           PlaceCursor(50, 0);
           SetColor(WHITE);
           printf("HIT (snake) AT %i, %i     ", CHECK_H, CHECK_V);

           return false;
       }
   }

   /* Hit wall */
   if (CHECK_V == PlaceY || CHECK_V == Height + PlaceY - 1 ||
       CHECK_H == PlaceX || CHECK_H == Width + PlaceX - 1) {

           PlaceCursor(CHECK_H, CHECK_V);
           SetColor(RED);
           printf("#");
           Sleep(50);

           PlaceCursor(50, 0);
           SetColor(WHITE);
           printf("HIT (wall) AT %i, %i     ", CHECK_H, CHECK_V);

           return false;
   }

   /* Safe move */
   return true;
}

void CREATE_SNAKE::CreatePath() {

   /* Erase the instructions */
   MyInstructions.clear();

   /* Find a path to the food by:
       Creating a duplicate of the current X, Y position
       of the Snake, and the objective (Snake's food)
   */

   /* Duplicate Snake positions */
   int CHECK_H[100] = {'\0'};
   int CHECK_V[100] = {'\0'};

   for (int x = 0; x < 100; x++) {
       CHECK_H[x] = Horizontal[x];
       CHECK_V[x] = Vertical[x];
   }

   /* Generate the shortest path to the food */
   while (CHECK_H[0] != Food[0] || CHECK_V[0] != Food[1]) {

       /* Move to the food */
       if (Food[0] > CHECK_H[0]) { CHECK_H[0]++; MyInstructions.push_front(3); }
       if (Food[0] < CHECK_H[0]) { CHECK_H[0]--; MyInstructions.push_front(2); }
       if (Food[1] > CHECK_V[0]) { CHECK_V[0]++; MyInstructions.push_front(1); }
       if (Food[1] < CHECK_V[0]) { CHECK_V[0]--; MyInstructions.push_front(0); }
   }

   int WTF = 0;
   if (Food[1] > Vertical[0]) { WTF = 1; } else  /* The food is below */
   if (Food[1] < Vertical[0]) { WTF = 0; } else  /* The food is above */
   if (Food[0] > Horizontal[0]) { WTF = 3; } else/* The food is right */
   if (Food[0] < Horizontal[0]) { WTF = 2; } /* The food is left  */

   if (CrashAt(CONTROL[WTF])) { PlaceCursor(0, 40); SetColor(WHITE); printf("WTF?!?!"); }

   return;
};

void CREATE_SNAKE::FindFood() {

   CreatePath();

   /* Move the Snake (next instruction) */
   //LastMove = CONTROL[MyInstructions.back()];
   //MyInstructions.pop_back();

   return;
};

void CREATE_SNAKE::AI() {

/*
   Up    = CONTROL[0]
   Down  = CONTROL[1]
   Left  = CONTROL[2]
   Right = CONTROL[3]
*/

   int Move = 0;
   if (Food[1] > Vertical[0]) { Move = 1; }   /* The food is below */
   if (Food[1] < Vertical[0]) { Move = 0; }   /* The food is above */
   if (Food[0] > Horizontal[0]) { Move = 3; } /* The food is right */
   if (Food[0] < Horizontal[0]) { Move = 2; } /* The food is left  */

   /* Initially move towards the closest position to the food */
   if (LastMove != CONTROL[Move]) { LastMove = CONTROL[Move]; }

   /* If the Snake continues to hit himself, try another move */
   while (CrashAt(CONTROL[Move])) {

       /* Cycle through all four possible moves (North, South, West, and East) */
       for (int x = 0; x < 5; x++) {
           if (x == 4) { LastMove = CONTROL[Move]; return; }
           Move = x;

           /* If you found a move that results in NO hit, use it */
           if (!CrashAt(CONTROL[Move])) { break; }
       }

       /* Assign the move */
       LastMove = CONTROL[Move];
   }

   return;
}

vector<CREATE_SNAKE> Snake;

#endif
Member
Posts: 31,680
Joined: Nov 10 2007
Gold: 1.00
Jun 19 2009 11:11pm
- Demo.h
Code
#ifndef DEMO_H
#define DEMO_H

class CREATE_DEMO_SNAKE {
   public:
       bool bCrashed;
       int Color;
       int X, Y;
       int Player;
       int PlaceX, PlaceY;
       int Width, Height;
       int Food[2];
       int CONTROL[4];
       int LastMove;
       int Horizontal[100], Vertical[100];
       int SnakeSize;
       int Speed;
       int Speedcount;
       int Timeout;
       int CrashCount;

   public:
       CREATE_DEMO_SNAKE::CREATE_DEMO_SNAKE(const int ID, const int Up, const int Down,

const int Left, const int Right);
       int GetXPos(const int X) { return Horizontal[X]; }
       int GetYPos(const int Y) { return Vertical[Y]; }
       void SetXPos(const int X) { Horizontal[0] = X; }
       void SetYPos(const int Y) { Vertical[0] = Y; }
       void DrawField() { DrawBox(Width, Height, PlaceX, PlaceY); }
       void DrawSnake();
       void DrawFood();
       void CollectFood();
       void AI();
       void Crashed();
       void ResetSnake();
       void SendSnakeMsg(const int Msg) { LastMove = Msg; }
};

CREATE_DEMO_SNAKE::CREATE_DEMO_SNAKE(const int ID, const int Up, const int Down, const int

Left, const int Right) {

   Color = CYAN;
   for (int x = 0; x < 100; x++) { Horizontal[x] = 0; Vertical[x] = 0; }
   bCrashed = false;
   Horizontal[0] = 1;
   Vertical[0] = 4;
   CONTROL[0] = Up;
   CONTROL[1] = Down;
   CONTROL[2] = Left;
   CONTROL[3] = Right;
   Food[0] = 0;
   Food[1] = 0;
   LastMove = CONTROL[3];
   Width = 80;
   Height = 25;
   PlaceX = 0;
   PlaceY = 2;
   SnakeSize = 5;
   Speed = 25;
   Speedcount = GetTickCount();
   CrashCount = 0;
   Timeout = 0;
}

void CREATE_DEMO_SNAKE::DrawSnake() {

   if (GetTickCount() - Speedcount >= Speed) {
       Speedcount = GetTickCount();

       if (LastMove == CONTROL[0]) { PlaceCursor(Horizontal[0],   Vertical[0]--); } else
       if (LastMove == CONTROL[1]) { PlaceCursor(Horizontal[0],   Vertical[0]++); } else
       if (LastMove == CONTROL[2]) { PlaceCursor(Horizontal[0]--, Vertical[0]);   } else
       if (LastMove == CONTROL[3]) { PlaceCursor(Horizontal[0]++, Vertical[0]);   }

       SetColor(Color, BLACK);
       for (int x = SnakeSize; x > 0; x--) { Horizontal[x] = Horizontal[x - 1]; Vertical[x]

= Vertical[x - 1]; }
       for (int x = SnakeSize; x >= 0; x--) { PlaceCursor(Horizontal[x], Vertical[x]);

printf("*"); }

       PlaceCursor(Horizontal[0], Vertical[0]); printf("@");
       if (Horizontal[0] != Horizontal[SnakeSize] || Vertical[0] != Vertical[SnakeSize]) {
           PlaceCursor(Horizontal[SnakeSize], Vertical[SnakeSize]); SetColor(BLACK, BLACK);

printf(" ");
       }
   }

   return;
}

void CREATE_DEMO_SNAKE::DrawFood() {

   srand(time(NULL));

   PLACEFOOD:
   Food[0] = rand() % Width + PlaceX;
   Food[1] = rand() % Height + PlaceY;

   if (Food[1] == PlaceY || Food[1] == Height + PlaceY - 1 || Food[0] == PlaceX || Food[0]

== Width + PlaceX - 1) {
       goto PLACEFOOD;
   }

   for (int x = 0; x < SnakeSize; x++) { if (Food[0] == Horizontal[x] && Food[1] ==

Vertical[x]) { goto PLACEFOOD; } }

   PlaceCursor(Food[0], Food[1]);
   SetColor(YELLOW, BLACK);
   printf("x");
}

void CREATE_DEMO_SNAKE::CollectFood() {

   /* Begin loop */
   if (Horizontal[0] == Food[0] && Vertical[0] == Food[1]) {
   PLACEFOOD:

       Food[0] = rand() % Width + PlaceX;
       Food[1] = rand() % Height + PlaceY;

       /* Check wall range - if true start over */
   if (Food[1] == PlaceY || Food[1] == Height + PlaceY - 1 || Food[0] == PlaceX || Food[0]

== Width + PlaceX - 1) {
           goto PLACEFOOD;
       }

       /* Check if it's on the snake - if true start over */
       for (int x = 0; x < SnakeSize; x++) {
           if (Food[0] == Horizontal[x] && Food[1] == Vertical[x]) {
               goto PLACEFOOD;
           }
       }

       SnakeSize++;

       PlaceCursor(Food[0], Food[1]);
       SetColor(YELLOW, BLACK);
       printf("x");

   } /* End loop */

   return;
}

void CREATE_DEMO_SNAKE::ResetSnake() {

   for (int x = 0; x < SnakeSize; x++) { PlaceCursor(Horizontal[x], Vertical[x]); printf("

"); }
   for (int x = 0; x < 100; x++) { Horizontal[x] = 0; Vertical[x] = 0; }
   Horizontal[0] = PlaceX + 2;
   Vertical[0] = PlaceY + 1;
   LastMove = CONTROL[3]; /* To the right */
   SnakeSize = 5;
   bCrashed = false;

   SetColor(WHITE, BLACK);
   DrawField();

   return;
}

void CREATE_DEMO_SNAKE::Crashed() {

   if (!bCrashed) {
       for (int x = SnakeSize - 1; x > 1; x--) {
           if (Horizontal[0] == Horizontal[x] && Vertical[0] == Vertical[x]) {
               bCrashed = true;

               PlaceCursor(Horizontal[0], Vertical[0]);
               SetColor(RED, BLACK);
               printf("X");
           }
       }
   }

   if (!bCrashed) {
       if (Vertical[0] == PlaceY || Vertical[0] == Height + PlaceY - 1 ||
           Horizontal[0] == PlaceX || Horizontal[0] == Width + PlaceX - 1) {
           bCrashed = true;

           char WallPieceHit = '\0';
           if (Vertical[0] == PlaceY || Vertical[0] == Height + PlaceY - 1) { WallPieceHit

= 'Í'; } /* Vertical Wall */
           else { WallPieceHit = 'º'; } /* Horizontal Wall */

           PlaceCursor(Horizontal[0], Vertical[0]);
           SetColor(RED, BLACK);
           printf("%c", WallPieceHit);
       }
   }


   if (bCrashed) {
       if (GetTickCount() - CrashCount >= 25) {
           CrashCount = GetTickCount();

           int TEMP = 0;
           TEMP = rand() % 4;

           switch (TEMP) {
               case 0: TEMP = GREY; break;
               case 1: TEMP = WHITE; break;
               case 2: TEMP = DARK_WHITE; break;
               case 3: TEMP = CYAN; break;
           }

           for (int x = 0; x < SnakeSize; x++) { PlaceCursor(Horizontal[x], Vertical[x]);

SetColor(TEMP); printf("*"); }

           Timeout++;
       }

       if (Timeout >= 25) { Timeout = 0; ResetSnake(); }
   }

   return;
}

void CREATE_DEMO_SNAKE::AI() {

   if (Food[1] > Vertical[0]) { if (LastMove != CONTROL[0]) { LastMove = CONTROL[1]; } else

{ LastMove = CONTROL[2]; } } else
   if (Food[1] < Vertical[0]) { if (LastMove != CONTROL[1]) { LastMove = CONTROL[0]; } else

{ LastMove = CONTROL[2]; } } else
   if (Food[0] > Horizontal[0]) { if (LastMove != CONTROL[2]) { LastMove = CONTROL[3]; }

else { LastMove = CONTROL[0]; } } else
   if (Food[0] < Horizontal[0]) { if (LastMove != CONTROL[3]) { LastMove = CONTROL[2]; }

else { LastMove = CONTROL[0]; } }

   return;
}

vector<CREATE_DEMO_SNAKE> DemoSnake;

void StartDemoSnake() {

   HANDLE OutHandle = GetStdHandle(STD_OUTPUT_HANDLE);

   ClearConsole(BLACK, BLACK);
   DemoSnake.at(0).ResetSnake();

   /* Draw the Snakes playing field */
   SetColor(WHITE, BLACK);
   DemoSnake.at(0).DrawField();
   DemoSnake.at(0).DrawFood();

   int BlinkDemo = GetTickCount();

   PlaceCursor(26, 1);
   SetColor(CYAN, BLACK);
   printf("PRESS ANY KEY TO EXIT");

   while (true) {
       if (GetTickCount() - BlinkDemo >= 1200) {
           BlinkDemo = GetTickCount();
           PlaceCursor(10, 0);
           SetColor(RED, BLACK);
           PlaceCursor(10, 30); WriteConsole(OutHandle, "        ######    ######   ##      

##   #####", 45, 0, NULL);
           PlaceCursor(10, 31); WriteConsole(OutHandle, "       ##   ##   ##       ##      

##   ##  ##", 45, 0, NULL);
           PlaceCursor(10, 32); WriteConsole(OutHandle, "      ##    ##  ##       ###    

###  ##    ##", 45, 0, NULL);
           PlaceCursor(10, 33); WriteConsole(OutHandle, "     ##    ##  ##       ###    ###

##    ##", 44, 0, NULL);
           PlaceCursor(10, 34); WriteConsole(OutHandle, "    ##    ##  ######   ####  ####  

##    ##", 43, 0, NULL);
           PlaceCursor(10, 35); WriteConsole(OutHandle, "   ##    ##  ##       ####  ####  

##    ##", 42, 0, NULL);
           PlaceCursor(10, 36); WriteConsole(OutHandle, "  ##    ##  ##       ## #### ##  

##    ##", 41, 0, NULL);
           PlaceCursor(10, 37); WriteConsole(OutHandle, " ##    ##  ##       ## #### ##  ##

  ##", 40, 0, NULL);
           PlaceCursor(10, 38); WriteConsole(OutHandle, "##   ##   ##       ##  ##  ##   ##

##", 38, 0, NULL);
           PlaceCursor(10, 39); WriteConsole(OutHandle, "######    ######  ##  ##  ##    

#####", 37, 0, NULL);
       }

       if (GetTickCount() - BlinkDemo >= 600) {
           PlaceCursor(10, 0);
           SetColor(BLACK, BLACK);
           PlaceCursor(10, 30); WriteConsole(OutHandle, "                                  

         ", 45, 0, NULL);
           PlaceCursor(10, 31); WriteConsole(OutHandle, "                                  

         ", 45, 0, NULL);
           PlaceCursor(10, 32); WriteConsole(OutHandle, "                                  

         ", 45, 0, NULL);
           PlaceCursor(10, 33); WriteConsole(OutHandle, "                                  

        ", 44, 0, NULL);
           PlaceCursor(10, 34); WriteConsole(OutHandle, "                                  

       ", 43, 0, NULL);
           PlaceCursor(10, 35); WriteConsole(OutHandle, "                                  

      ", 42, 0, NULL);
           PlaceCursor(10, 36); WriteConsole(OutHandle, "                                  

     ", 41, 0, NULL);
           PlaceCursor(10, 37); WriteConsole(OutHandle, "                                  

    ", 40, 0, NULL);
           PlaceCursor(10, 38); WriteConsole(OutHandle, "                                  

  ", 38, 0, NULL);
           PlaceCursor(10, 39); WriteConsole(OutHandle, "                                  

 ", 37, 0, NULL);
       }

       if (!DemoSnake.at(0).bCrashed) {
           DemoSnake.at(0).DrawSnake();
           DemoSnake.at(0).CollectFood();
           DemoSnake.at(0).AI();
       }

       DemoSnake.at(0).Crashed();

       if (_kbhit()) {
           DemoSnake.at(0).ResetSnake();
           DemoSnake.at(1).ResetSnake();
           ClearConsole(BLACK, BLACK);

           DemoSnake.at(0).SendSnakeMsg(0x0D); /* Go right */
           DemoSnake.at(0).SetXPos(0);
           DemoSnake.at(0).SetYPos(22);

           DemoSnake.at(1).SendSnakeMsg(0x0D); /* Go right */
           DemoSnake.at(1).SetXPos(20);
           DemoSnake.at(1).SetYPos(22);
           break;
       }
   }

   FlushConsoleInputBuffer(InHandle);

   Menu.DisplayMenu(CYAN, BLACK);

   return;
}

#endif
Member
Posts: 31,680
Joined: Nov 10 2007
Gold: 1.00
Jun 19 2009 11:11pm
- Game.h
Code
#ifndef GAME_H
#define GAME_H

int FindWinner(vector<CREATE_SNAKE> Snakes) {

   /* All the Snakes in the vector! */
   int arrayLength = Snakes.size();

   /* Exchange Sort - quick for small numbers */
   for (int i = 0; i < (arrayLength - 1); i++) {
       int SwapingContainer = 0;

       for (int j = (i + 1); j < arrayLength; j++) {
           if (Snakes.at(i).Score < Snakes.at(j).Score) {
               SwapingContainer = Snakes.at(i).Score;
               Snakes.at(i).Score = Snakes.at(j).Score;
               Snakes.at(j).Score = SwapingContainer;
           }
       }
   }

   /* return the winner (HIGHEST SCORE FOUND)! */
   return Snakes.at(0).Score;
}

void CheckPlayersHit(vector<CREATE_SNAKE> &Snake, int SnakeX) {

/* Main idea on how to check collision status (initial Snake vs. opposing):
   - Cycle through all Snakes
   - Compare each Snake (head) cycled against all other Snakes (bodies)

   Step 1) Find initial Snake to compare to all others
   Step 2) Find size of initial Snake so you can compare against all others
   Step 3) Compare initial Snake (loop based on size) against all others
*/

   /* Loop through all Snakes in the vector */
   for (int x = SnakeX - 1; x >= 0; x--) {

       /* Loop through Snake X's size */
       for (int o = SnakeX - 1; o >= 0; o--) {

           /* Loop through all (other) Snakes in the vector to check a collison status

against */
               for (int s = Snake.at(o).GetSnakeSize() - 1; s > 0; s--) {
                   if (Snake.at(x).Player != Snake.at(o).Player && !Snake.at(x).GameOver) {
                   if (Snake.at(x).GetXPos(0) == Snake.at(o).GetXPos(s) && Snake.at

(x).GetYPos(0) == Snake.at(o).GetYPos(s)) {
                   Snake.at(x).ResetSnake();
                   }
               }
           }
       }
   }

   return;
}

bool GameInProgress(vector<CREATE_SNAKE> &Snake, int SnakeX) {

   int GamesEnded = 0;
   bool Player[SnakeX]; /* All the players current states */

   /* Fill in the values */
   for (int x = SnakeX - 1; x >= 0; x--) {
       Player[x] = Snake.at(x).GameOver;

       if (Player[x] == true) {
           Snake.at(x).TotalGameTime = GetTickCount() - Snake.at(x).TotalGameTime;
           GamesEnded++;
       }
   }

   /* Return false if all players have died */
   if (GamesEnded == SnakeX) { return false; }

   return true;
}

void Start_Game() {

   ClearConsole(BLACK, BLACK);
   int Winner = 0;

   PlaceCursor((80 / 2) - 6, (50 / 2) - 1);
   SetColor(RED);
   printf("READY?");
   while (true) { static int Timer = GetTickCount(); if (GetTickCount() - Timer >= 500) {

break; } }

   PlaceCursor((80 / 2) - 6, (50 / 2) - 1);
   SetColor(YELLOW);
   printf("SET...");
   while (true) { static int Timer = GetTickCount(); if (GetTickCount() - Timer >= 500) {

break; } }

   PlaceCursor((80 / 2) - 6, (50 / 2) - 1);
   SetColor(GREEN);
   printf("GO!   ");
   while (true) { static int Timer = GetTickCount(); if (GetTickCount() - Timer >= 500) {

break; } }

   PlaceCursor((80 / 2) - 6, (50 / 2) - 1);
   SetColor(BLACK);
   printf("   ");

   for (int x = 0; x < NumSnakes; x++) {
       Snake.at(x).DrawPlayingField(WHITE);
       Snake.at(x).DrawFood();
       Snake.at(x).TotalGameTime = GetTickCount();
   }

   while (GameInProgress(Snake, NumSnakes)) {
       for (int x = 0; x < NumSnakes; x++) {
           if (!Snake.at(x).GameOver) {
               if (!Snake.at(x).bCrashed) {
                   Snake.at(x).GetUserInput();
                   Snake.at(x).DrawSnake();
                   Snake.at(x).CollectFood();
                   if (Snake.at(x).Computer) { Snake.at(x).AI(); }
                   //if (!Snake.at(x).Computer) { Snake.at(x).FindFood(); }
               }

               if (Snake.at(x).Lives >= 1) { Snake.at(x).Crashed(); }
               if (Snake.at(x).Lives <= 0) { Snake.at(x).EndGameAnimation(); }
               PlaceCursor(0, 0);
               SetColor(BLACK, BLACK);
               printf(" \b");
           }
       }

       CheckPlayersHit(Snake, NumSnakes);

       if (GetAsyncKeyState(VK_ESCAPE)) { break; }
       if (GetAsyncKeyState(VK_F1)) { Display_Help_Menu(); }
   }

   /* Determine winner (by score) */
   Winner = FindWinner(Snake);

   /* Determine the winner's subscript */
   for (int x = 0; x < NumSnakes; x++) { if (Winner == Snake.at(x).Score) { Winner =

Snake.at(x).Player - 1; break; } }

   int CenterX = ((Snake.at(Winner).Width / 2) + Snake.at(Winner).PlaceX) - (23 / 2);
   int CenterY = ((Snake.at(Winner).Height / 2) + Snake.at(Winner).PlaceY);

   PlaceCursor(CenterX, CenterY);
   SetColor(WHITE, BLACK);
   printf("THE WINNER IS: Player %i", Winner + 1);

   PlaceCursor(CenterX, CenterY + 1);
   printf("SCORE: %i", Snake.at(Winner).Score);

   _flushall();
   cin.get();
   ClearConsole(BLACK, BLACK);

   WriteScoreboard();

   SetColor(WHITE, BLACK);
   ScoreboardMenu();

   /* Reset live count, flags, etc */
   for (int x = 0; x < NumSnakes; x++) { Snake.at(x).ResetGame(); }

   ClearConsole(BLACK, BLACK);
   Menu.DisplayMenu(CYAN, BLACK);
   return;
}

#endif
Member
Posts: 31,680
Joined: Nov 10 2007
Gold: 1.00
Jun 19 2009 11:12pm
- Intro.h
Code
#ifndef INTRO_H
#define INTRO_H

void SplashScreen(const int Image, const int X, const int Y, const int foreground, const int

background) {

   HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

   SetColor(foreground, background);

   if (Image == 0) {
       PlaceCursor(X, Y);     WriteConsole(hConsole, " #####                   ##",        

  27, 0, NULL);
       PlaceCursor(X, Y + 1); WriteConsole(hConsole, "##   ##                  ##",        

  27, 0, NULL);
       PlaceCursor(X, Y + 2); WriteConsole(hConsole, "##   ##                  ##",        

  27, 0, NULL);
       PlaceCursor(X, Y + 3); WriteConsole(hConsole, "##        #####   ####   ##  ##",    

  31, 0, NULL);
       PlaceCursor(X, Y + 4); WriteConsole(hConsole, " ###     ##  ##  ##  ##  ## ##  

####",  37, 0, NULL);
       PlaceCursor(X, Y + 5); WriteConsole(hConsole, "  ###    ##  ##    ####  ####   ##  

##", 38, 0, NULL);
       PlaceCursor(X, Y + 6); WriteConsole(hConsole, "    ##   ##  ##   ## ##  ###    

######", 38, 0, NULL);
       PlaceCursor(X, Y + 7); WriteConsole(hConsole, "##  ##   ##  ##  ##  ##  ####   ##",  

  34, 0, NULL);
       PlaceCursor(X, Y + 8); WriteConsole(hConsole, "##  ##   ##  ##  ##  ##  ## ##  ##  

##", 38, 0, NULL);
       PlaceCursor(X, Y + 9); WriteConsole(hConsole, " #####   ##  ##   #####  ##  ##  

####",  37, 0, NULL);

       DrawBox(48, 12, X - 5, Y - 1);
   }

   if (Image == 1) {
       PlaceCursor(X, Y);     WriteConsole(hConsole, "      ####                     ##    

                       ##", 64, 0, NULL);
       PlaceCursor(X, Y + 1); WriteConsole(hConsole, "     #  ##                   ##      

                      ##", 63, 0, NULL);
       PlaceCursor(X, Y + 2); WriteConsole(hConsole, "   ##    #                  ##        

                     ##", 62, 0, NULL);
       PlaceCursor(X, Y + 3); WriteConsole(hConsole, "  ##        ####   #####  ##### ##  

##   ####    ####    #####", 62, 0, NULL);
       PlaceCursor(X, Y + 4); WriteConsole(hConsole, "  ##       ##  ##  ##  ##  ##  ##  ##

##  ##  ##  ##  ##  ##", 61, 0, NULL);
       PlaceCursor(X, Y + 5); WriteConsole(hConsole, " ##       ##  ##  ##  ##  ##  ##  ##  

##      ######  ##  ##", 60, 0, NULL);
       PlaceCursor(X, Y + 6); WriteConsole(hConsole, " ##       ##  ##  ##  ##  ##  ##  ##  

####   ##      ##  ##", 60, 0, NULL);
       PlaceCursor(X, Y + 7); WriteConsole(hConsole, "##    #  ##  ##  ##  ##  ##  ##  ##  

  ##  ##      ##  ##", 59, 0, NULL);
       PlaceCursor(X, Y + 8); WriteConsole(hConsole, " ##  ##  ##  ##  ##  ##  ##  ##  ##  

##  ##  ##  ##  ##  ##", 59, 0, NULL);
       PlaceCursor(X, Y + 9); WriteConsole(hConsole, " ####    ####   ##  ##  ##   #####  

####    ####    #####", 58, 0, NULL);
   }

   if (Image == 2) {
       PlaceCursor(X, Y);     WriteConsole(hConsole, "     #####            ##", 24, 0,

NULL);
       PlaceCursor(X, Y + 1); WriteConsole(hConsole, "   ##   ##          ##  ##", 26, 0,

NULL);
       PlaceCursor(X, Y + 2); WriteConsole(hConsole, "   ##   ##          ##  ##", 26, 0,

NULL);
       PlaceCursor(X, Y + 3); WriteConsole(hConsole, "  ##        ####  ##########      ##  

####   ####  ####", 55, 0, NULL);
       PlaceCursor(X, Y + 4); WriteConsole(hConsole, "   ###     ##  ##  ##  ## ##  ##  ##

##  ##  ###  ##  ##", 56, 0, NULL);
       PlaceCursor(X, Y + 5); WriteConsole(hConsole, "    ###   ##  ##  ##  ##  ## ## ##    

####  ##   ######", 55, 0, NULL);
       PlaceCursor(X, Y + 6); WriteConsole(hConsole, "      ##  ##  ##  ##  ##  ## ## ##  

## ##  ##   ##", 51, 0, NULL);
       PlaceCursor(X, Y + 7); WriteConsole(hConsole, "##   ##  ##  ##  ##  ##  ########  ##

##  ##   ##", 50, 0, NULL);
       PlaceCursor(X, Y + 8); WriteConsole(hConsole, "##   ##  ##  ##  ##  ##   ##  ##   ##

##  ##   ##  ##", 54, 0, NULL);
       PlaceCursor(X, Y + 9); WriteConsole(hConsole, "#####    ####   ##   ##  ##  ##    

#####  ##    ####", 52, 0, NULL);
   }

   if (Image == 3) {
       CREATE_MENU Menu;
       Menu.SetAttributes(30, 15, 20, 5, "SNAKE");
       Menu.AddOptions(6, "Start game", "Options", "Scoreboard", "Credits", "Demo",

"Quit");
       Menu.DisplayMenu(foreground, background);
   }

   return;
}

void SnakeIntro() {

   int WaitPeriod = GetTickCount();
   int Iteration = 0;

   while (true) {
       if (_kbhit()) { if (_getch() == 32) { SplashScreen(0, 20, 2, CYAN, BLACK); SetColor

(WHITE); DrawBox(48, 12, 15, 1); break; } }

       if (GetTickCount() - WaitPeriod >= 50) {
           WaitPeriod = GetTickCount();

           int Color = rand() % 4;

           switch (Color) {
               case 0: Color = GREY; break;
               case 1: Color = WHITE; break;
               case 2: Color = DARK_WHITE; break;
               case 3: Color = CYAN; break;
           }

           SplashScreen(0, 20, 2, Color, BLACK);

           Iteration++;
       }

       if (Iteration == 10) { Iteration = 0; SplashScreen(0, 20, 2, CYAN, BLACK); SetColor

(WHITE); DrawBox(48, 12, 15, 1); break; }
   }

   while (true) {
       if (_kbhit()) { if (_getch() == 32) { SplashScreen(1, 10, 14, CYAN, BLACK); break; }

}

       if (GetTickCount() - WaitPeriod >= 50) {
           WaitPeriod = GetTickCount();

           int Color = rand() % 4;

           switch (Color) {
               case 0: Color = GREY; break;
               case 1: Color = WHITE; break;
               case 2: Color = DARK_WHITE; break;
               case 3: Color = CYAN; break;
           }

           SplashScreen(1, 10, 14, Color, BLACK);

           Iteration++;
       }

       if (Iteration == 10) { Iteration = 0; SplashScreen(1, 10, 14, CYAN, BLACK); break; }
   }

   while (true) {
       if (_kbhit()) { if (_getch() == 32) { SplashScreen(2, 10, 25, CYAN, BLACK); break; }

}

       if (GetTickCount() - WaitPeriod >= 50) {
           WaitPeriod = GetTickCount();

           int Color = rand() % 4;

           switch (Color) {
               case 0: Color = GREY; break;
               case 1: Color = WHITE; break;
               case 2: Color = DARK_WHITE; break;
               case 3: Color = CYAN; break;
           }

           SplashScreen(2, 10, 25, Color, BLACK);

           Iteration++;
       }

       if (Iteration == 10) { Iteration = 0; SplashScreen(2, 10, 25, CYAN, BLACK); break; }
   }

   int Timer = GetTickCount();

   while (true) { if (_kbhit()) { if (_getch() == 32) { break; } } if (GetTickCount() -

Timer >= 2000) { Timer = GetTickCount(); break; } }
   SplashScreen(0, 20, 2, WHITE, BLACK);
   SplashScreen(1, 10, 14, WHITE, BLACK);
   SplashScreen(2, 10, 25, WHITE, BLACK);
   while (true) { if (_kbhit()) { if (_getch() == 32) { break; } } if (GetTickCount() -

Timer >= 200) { Timer = GetTickCount(); break; } }
   SplashScreen(0, 20, 2, GREY, BLACK);
   SplashScreen(1, 10, 14, GREY, BLACK);
   SplashScreen(2, 10, 25, GREY, BLACK);
   while (true) { if (_kbhit()) { if (_getch() == 32) { break; } } if (GetTickCount() -

Timer >= 200) { Timer = GetTickCount(); break; } }
   SplashScreen(0, 20, 2, DARK_WHITE, BLACK);
   SplashScreen(1, 10, 14, DARK_WHITE, BLACK);
   SplashScreen(2, 10, 25, DARK_WHITE, BLACK);
   while (true) { if (_kbhit()) { if (_getch() == 32) { break; } } if (GetTickCount() -

Timer >= 200) { Timer = GetTickCount(); break; } }
   SplashScreen(0, 20, 2, BLACK, BLACK);
   SplashScreen(1, 10, 14, BLACK, BLACK);
   SplashScreen(2, 10, 25, BLACK, BLACK);
   while (true) { if (_kbhit()) { if (_getch() == 32) { break; } } if (GetTickCount() -

Timer >= 150) { Timer = GetTickCount(); break; } }
   SplashScreen(3, 0, 0, DARK_WHITE, BLACK);
   while (true) { if (_kbhit()) { if (_getch() == 32) { break; } } if (GetTickCount() -

Timer >= 150) { Timer = GetTickCount(); break; } }
   SplashScreen(3, 0, 0, GREY, BLACK);
   while (true) { if (_kbhit()) { if (_getch() == 32) { break; } } if (GetTickCount() -

Timer >= 150) { Timer = GetTickCount(); break; } }
   SplashScreen(3, 0, 0, WHITE, BLACK);
   while (true) { if (_kbhit()) { if (_getch() == 32) { break; } } if (GetTickCount() -

Timer >= 150) { Timer = GetTickCount(); break; } }
   SplashScreen(3, 0, 0, CYAN, BLACK);

   return;
}

#endif
Member
Posts: 31,680
Joined: Nov 10 2007
Gold: 1.00
Jun 19 2009 11:12pm
- Scoreboard.h
Code
#ifndef SCOREBOARD_H
#define SCOREBOARD_H

struct CREATE_SNAKE_SCORES {
   int Score;
   int Color;
   int Player;
   int Size;
   int Difficulty;
   long TotalGameTime;
   char Name[14];
};

vector<CREATE_SNAKE_SCORES> SnakeScores(8);

int int_comp(const void *i, const void *j) { return ((*(int *)i) - (*(int *)j)); }

void WriteScoreboard() {

   /* TO DO:
       Make a minimum requirement to get on the scoreboard
       EG: You cannot get on unless you are higher than
       a score that's already on the highscore board
   */

   for (int x = 0; x < Snake.size(); x++) {
       for (int y = 0; y < Snake.size(); y++) {
           if (Snake[x].Score > SnakeScores[y].Score) {
               SnakeScores[y].Size = Snake[x].LongestSize;
               SnakeScores[y].Score = Snake[x].Score;
               SnakeScores[y].Color = Snake[x].Color;
               SnakeScores[y].Player = Snake[x].Player;
               SnakeScores[y].Difficulty = Snake[x].Difficulty;
               SnakeScores[y].TotalGameTime = Snake[x].TotalGameTime / 1000;

               /* If it's a computer, select a name randomly from the file */
               if (Snake[x].Computer) {
                   ifstream in("Names.dat", ios::ate);
                   int Last = in.tellg();
                   in.seekg(0, ios::beg);

                   srand(time(NULL));
                   for (int x = 0; x < rand() % Last; x++) { in >> SnakeScores[y].Name; }

                   in.close();
               } else { strcpy(SnakeScores[y].Name, Snake[x].Name); }
           }
       }
   }

   /* Open the file (binary format) */
   ofstream out("HS.dat", ios::out | ios::binary);

   /* Sort the score before writing it to the drive */
   qsort(&SnakeScores[0].Score, 8, sizeof(SnakeScores[0]), int_comp);

   /* Write the contents of the score vector */
   out.write((const char *)&SnakeScores[0].Score, sizeof(CREATE_SNAKE_SCORES) * 8);
   out.close();

   return;
}

#endif
Member
Posts: 31,680
Joined: Nov 10 2007
Gold: 1.00
Jun 19 2009 11:13pm
And last but not least...! :)

- menus.h
Code
#ifndef MENU_H
#define MENU_H

void Start_Game();
int Main_Callback(int, bool, int);
int Options_Callback(int, bool, int);
int AdvOptions_Callback(int, bool, int);

int NumSnakes = 1;
int ctr = 0;

char *Difficulty[] = {
   "Easy  ",
   "Medium",
   "Hard  "
};

void LoadValues() {

   /* Show the "loaded" values */
   PlaceCursor(41, 11);
   printf("%i ", Snake.at(ctr).Player);

   PlaceCursor(44, 12);
   printf("%i ", NumSnakes);

   PlaceCursor(38, 13);
   printf("%s", Difficulty[Snake.at(ctr).Difficulty]);

   PlaceCursor(33, 14);
   printf("%i", Snake.at(ctr).Speed);

   PlaceCursor(32, 10);
   printf("               ");
   PlaceCursor(32, 10);
   printf("%s", Snake.at(ctr).Name);

   /* Print the new color! */
   PlaceCursor(33, 16);
   SetColor(Snake.at(ctr).Color, BLACK);
   printf("Û ****@");

   PlaceCursor(33, 17);
   SetColor(WHITE, BLACK);
   printf("%i", Snake.at(ctr).MaxLives);

   return;
}

void Display_Help_Menu() {

   SetColor(WHITE, BLACK);
   DrawBox(35, 15, 26, 8);
   SetColor(BLACK, BLACK);
   FillBox(33, 13, 27, 9, ' ');

   SetColor(RED, BLACK);
   PlaceCursor(30,  9); printf("MOVING - DIRECTIONAL KEYS");
   SetColor(WHITE, BLACK);
   PlaceCursor(27, 11); printf("UP ARROW    - Moves Snake up");
   PlaceCursor(27, 12); printf("DOWN ARROW  - Moves Snake down");
   PlaceCursor(27, 13); printf("LEFT ARROW  - Moves Snake left");
   PlaceCursor(27, 14); printf("RIGHT ARROW - Moves Snake right");

   SetColor(RED, BLACK);
   PlaceCursor(30, 16); printf("SCORE - HOW TO GET IT");
   SetColor(WHITE, BLACK);
   PlaceCursor(27, 17); printf("You eat rats! Based on the");
   PlaceCursor(27, 18); printf("difficulty level you get:");
   PlaceCursor(27, 19); printf("Easy   - 12 points");
   PlaceCursor(27, 20); printf("Normal - 8  points");
   PlaceCursor(27, 21); printf("Hard   - 4  points");

   SetColor(BLACK, BLACK);
   PlaceCursor(0, 0);

   cin.get();
   ClearConsole(BLACK, BLACK);
   Menu.DisplayMenu(CYAN, BLACK);
   return;
}

void Display_Credits_Menu() {

   SetColor(WHITE, BLACK);
   DrawBox(40, 20, 26, 8);
   SetColor(BLACK, BLACK);
   FillBox(38, 18, 27, 9, ' ');

   SetColor(RED, BLACK);
   PlaceCursor(30, 9); printf("GAME DESIGN");
   SetColor(WHITE, BLACK);
   PlaceCursor(27, 10); printf("Danielle \"Dani\"");

   SetColor(RED, BLACK);
   PlaceCursor(30, 12); printf("PROJECT MANAGER");
   SetColor(WHITE, BLACK);
   PlaceCursor(27, 13); printf("Danielle \"Dani\"");

   SetColor(RED, BLACK);
   PlaceCursor(30, 15); printf("LEAD PROGRAMMER/PROGRAMMERS");
   SetColor(WHITE, BLACK);
   PlaceCursor(27, 16); printf("Danielle \"Dani\"");

   SetColor(RED, BLACK);
   PlaceCursor(30, 18); printf("LEAD SOUND/AUDIO DESIGNER");
   SetColor(WHITE, BLACK);
   PlaceCursor(27, 19); printf("Magic");

   SetColor(RED, BLACK);
   PlaceCursor(30, 21); printf("GAME TESTING");
   SetColor(WHITE, BLACK);
   PlaceCursor(27, 22); printf("Danielle \"Dani\"");

   cin.get();
   ClearConsole(BLACK, BLACK);
   Menu.DisplayMenu(CYAN, BLACK);
   return;
}

void CycleSnake(const int Top, const int Bottom, const int Left, const int Right) {

/* Snake controls (values):
   Up - 0x0A
   Down - 0x0B
   Left - 0x0C
   Right 0x0D
*/

   for (int x = 0; x < DemoSnake.size(); x++) {
       if (DemoSnake.at(x).GetXPos(0) == Right && DemoSnake.at(x).GetYPos(0) == Top) {

DemoSnake.at(x).SendSnakeMsg(0x0B); }
       if (DemoSnake.at(x).GetYPos(0) == Bottom && DemoSnake.at(x).GetXPos(0) == Right) {

DemoSnake.at(x).SendSnakeMsg(0x0C); }
       if (DemoSnake.at(x).GetXPos(0) == Left && DemoSnake.at(x).GetYPos(0) == Bottom) {

DemoSnake.at(x).SendSnakeMsg(0x0A); }
       if (DemoSnake.at(x).GetYPos(0) == Top && DemoSnake.at(x).GetXPos(0) == Left) {

DemoSnake.at(x).SendSnakeMsg(0x0D); }

       DemoSnake.at(x).DrawSnake();
       PlaceCursor(0, 0);
       SetColor(BLACK, BLACK);
   }

   return;
}

void MainMenu() {

   Menu.SetLoopValue(1, 22, 17, 52);   /* Snake's cage dimensions to cycle around */
   DemoSnake.at(0).SendSnakeMsg(0x0D); /* Go right */
   DemoSnake.at(0).SetXPos(0);
   DemoSnake.at(0).SetYPos(22);

   DemoSnake.at(1).SendSnakeMsg(0x0D); /* Go right */
   DemoSnake.at(1).SetXPos(20);
   DemoSnake.at(1).SetYPos(22);

   Menu.SetAttributes(30, 15, 20, 5, "SNAKE");
   Menu.AddOptions(6, "Start game", "Options", "Scoreboard", "Credits", "Demo", "Quit");
   Menu.Loop(Main_Callback, false, CycleSnake);

   /* Fade out (fade the Snake out along with the menu) */
   while (true) { CycleSnake(1, 22, 17, 52); if (_kbhit()) { break; } static int Timer =

GetTickCount(); if (GetTickCount() - Timer >= 100) { break; } }
   Menu.DisplayMenu(WHITE, BLACK);
   DemoSnake.at(0).Color = WHITE;
   DemoSnake.at(1).Color = WHITE;
   while (true) { CycleSnake(1, 22, 17, 52); if (_kbhit()) { break; } static int Timer =

GetTickCount(); if (GetTickCount() - Timer >= 100) { break; } }
   Menu.DisplayMenu(GREY, BLACK);
   DemoSnake.at(0).Color = GREY;
   DemoSnake.at(1).Color = GREY;
   while (true) { CycleSnake(1, 22, 17, 52); if (_kbhit()) { break; } static int Timer =

GetTickCount(); if (GetTickCount() - Timer >= 100) { break; } }
   Menu.DisplayMenu(DARK_WHITE, BLACK);
   DemoSnake.at(0).Color = DARK_WHITE;
   DemoSnake.at(1).Color = DARK_WHITE;
   while (true) { CycleSnake(1, 22, 17, 52); if (_kbhit()) { break; } static int Timer =

GetTickCount(); if (GetTickCount() - Timer >= 100) { break; } }
   Menu.DisplayMenu(BLACK, BLACK);
   DemoSnake.at(0).Color = BLACK;
   DemoSnake.at(1).Color = BLACK;

   ClearConsole(BLACK, BLACK); /* Reset the screen for another program */
   PlaceCursor(0, 0);

   return;
}

void OptionsMenu() {

   Menu.SetAttributes(30, 15, 20, 5, "OPTIONS");
   Menu.AddOptions(11, "Adv. Options", "Assistance Options", "Name: ", "Snake editing: ",

"Snakes (players): ", "Difficulty: ", "Speed: ", "Randomize Color", "Color: ", "Lives: ",

"Back");
   Menu.DisplayMenu(CYAN, BLACK);

   LoadValues();

   Menu.Loop(Options_Callback, false);

   return;
}

int Assistance_Callback(int CurrItem, bool ExitMenu, int Key) {

   char *LifeWarning[] = { "On", "Off" };

   switch (CurrItem) {

       case 0: {
           if (Key != 5) {
               if (Key == 10 && ctr < NumSnakes - 1) { ctr++; }
               if (Key == 15 && ctr > 0) { ctr--; }

               PlaceCursor(41, 8);
               printf("%i ", Snake.at(ctr).Player);

               PlaceCursor(40, 10);
               printf("%s ", LifeWarning[Snake.at(ctr).LifeWarning]);

               PlaceCursor(0, 0);
           }
       } break;

       case 1: {
           if (Key != 5) {
               if (Key == 10 && NumSnakes < 8) { Snake.push_back(CREATE_SNAKE(NumSnakes +

1, VK_UP, VK_DOWN, VK_LEFT, VK_RIGHT)); NumSnakes++; sprintf(Snake.at(NumSnakes - 1).Name,

"PLAYER %i", NumSnakes); }
               if (Key == 15 && NumSnakes > 1) { NumSnakes--; if (ctr > NumSnakes - 1) {

ctr--; } Snake.pop_back(); }

               PlaceCursor(41, 8);
               printf("%i ", Snake.at(ctr).Player);

               PlaceCursor(44, 9);
               printf("%i ", NumSnakes);

               PlaceCursor(40, 10);
               printf("%s ", LifeWarning[Snake.at(ctr).LifeWarning]);

               PlaceCursor(0, 0);
           }
       } break;

       case 2: {
           if (Key != 5) {
               if (Key == 10) { Snake.at(ctr).LifeWarning = false; }
               if (Key == 15) { Snake.at(ctr).LifeWarning = true; }

               PlaceCursor(40, 10);
               printf("%s ", LifeWarning[Snake.at(ctr).LifeWarning]);

               PlaceCursor(0, 0);
           }
       } break;

       case 3: {
           if (Key == 5) {
               ExitMenu = true;
               Menu.Clear(CYAN, BLACK);
               Menu.SetAttributes(30, 15, 20, 5, "OPTIONS");
               Menu.AddOptions(11, "Adv. Options", "Assistance Options", "Name: ", "Snake

editing: ", "Snakes (players): ", "Difficulty: ", "Speed: ", "Randomize Color", "Color: ",

"Lives: ", "Back");
               Menu.DisplayMenu(CYAN, BLACK);
           }
       } break;

       case 666: {
           ExitMenu = true;
           Menu.Clear(CYAN, BLACK);
           Menu.SetAttributes(30, 15, 20, 5, "OPTIONS");
           Menu.AddOptions(11, "Adv. Options", "Assistance Options", "Name: ", "Snake

editing: ", "Snakes (players): ", "Difficulty: ", "Speed: ", "Randomize Color", "Color: ",

"Lives: ", "Back");
           Menu.DisplayMenu(CYAN, BLACK);
       } break;
   }

   return ExitMenu;
}

void AssistanceMenu() {

   char *LifeWarning[] = { "On", "Off" };

   Menu.SetAttributes(30, 15, 20, 5, "ASSISTANCE");
   Menu.AddOptions(4, "Snake editing: ", "Snakes (players): ", "Life Warning:", "Back");
   Menu.DisplayMenu(CYAN, BLACK);

   PlaceCursor(41, 8);
   printf("%i", Snake.at(ctr).Player);

   PlaceCursor(44, 9);
   printf("%i", NumSnakes);

   PlaceCursor(40, 10);
   printf("%s ", LifeWarning[Snake.at(ctr).LifeWarning]);

   Menu.Loop(Assistance_Callback, false);

   return;
}

void AdvOptionsMenu() {

   Menu.SetAttributes(30, 15, 20, 5, "ADV. OPTIONS");
   Menu.AddOptions(5, "Snake editing: ", "Snakes (players): ", "Set snake controls", "Set

board dimensions", "Back");
   Menu.DisplayMenu(CYAN, BLACK);

   PlaceCursor(41, 8);
   printf("%i ", Snake.at(ctr).Player);
   PlaceCursor(44, 9);
   printf("%i ", NumSnakes);

   Menu.Loop(AdvOptions_Callback, false);

   return;
}

void ScoreboardMenu() {

   ClearConsole(BLACK, BLACK);

   /* Open the file (binary format) */
   ifstream in("HS.dat", ios::in | ios::binary);

   /* Read the file into the scores vector - if it doesn't exist, exit */
   in.read((char *)&SnakeScores[0].Score, sizeof(CREATE_SNAKE_SCORES) * 8);
   in.close();

   SetColor(GREY, BLACK);
   PlaceCursor(5, 0); printf("*-----------------------------------------------------------

----------*");
   PlaceCursor(5, 1); printf("|   Player Name    Score    Total Time    Color    Size    

Difficulty |");
   PlaceCursor(5, 2); printf("*-----------------------------------------------------------

----------*");

   for (int x = 7; x >= 0; x--) {
       PlaceCursor(5, 17 - x - x); printf("*");

       SetColor(WHITE, BLACK);
       PlaceCursor(7, 17 - x - x); printf("%i.", 8 - x);
       PlaceCursor(11, 17 - x - x); printf("%s", SnakeScores[x].Name);
       PlaceCursor(24, 17 - x - x); printf("%i", SnakeScores[x].Score);
       PlaceCursor(33, 17 - x - x); printf("%i", SnakeScores[x].TotalGameTime);

       SetColor(SnakeScores.at(x).Color, BLACK);
       PlaceCursor(47, 17 - x - x); printf("Û ****@");
       SetColor(WHITE, BLACK);

       PlaceCursor(56, 17 - x - x); printf("%i", SnakeScores[x].Size);
       PlaceCursor(64, 17 - x - x); printf("%s", Difficulty[SnakeScores.at(x).Difficulty]);

       SetColor(GREY, BLACK);
       PlaceCursor(75, 17 - x - x); printf("*");
       PlaceCursor(5, 18 - x - x); printf("*----------------------------------------------

-----------------------*");
   }

   cin.get();
   ClearConsole(BLACK, BLACK);
   Menu.DisplayMenu(CYAN, BLACK);
   return;
}

int Main_Callback(int CurrItem, bool ExitMenu, int Key) {

   switch (CurrItem) {
       case 0: if (Key == 5) { Start_Game(); Menu.DisplayMenu(CYAN, BLACK); } break;
       case 1: if (Key == 5) { srand(time(NULL)); OptionsMenu(); } break;
       case 2: if (Key == 5) { ScoreboardMenu(); } break;
       case 3: if (Key == 5) { Display_Credits_Menu(); } break;
       case 4: if (Key == 5) { StartDemoSnake(); } break;
       case 5: if (Key == 5) { ExitMenu = true; } break;
       case 666: ExitMenu = true; break;
   }

   return ExitMenu;
}

int SetupControls(int CurrItem, bool ExitMenu, int Key) {

   switch (CurrItem) {
       case 0: {
           if (Key == 5) {

               long startText = GetTickCount();
               long stopText = startText - 500;
               int KeyHit = 0;

               while (true) {
                   SetColor(WHITE, BLACK);
                   FLASH("UP", 31, 13, startText);

                   SetColor(BLACK, BLACK);
                   FLASH("     \b", 31, 13, stopText);

                   if (_kbhit()) {

                       /* Get the key (and reset _kbhit() */
                       KeyHit = _getch();

                       /* if it's an extended key, get it! */
                       if (KeyHit == 224) { KeyHit = _getch(); }

                       break;
                   }
               }

               SetColor(WHITE, BLACK);
               PlaceCursor(31, 13);
               printf("UP");

               PlaceCursor(39, 13);
               printf("%c  ", KeyHit);
               Snake.at(ctr).SetUpArrow(KeyHit);
           }
       } break;

       case 1: {
           if (Key == 5) {

               long startText = GetTickCount();
               long stopText = startText - 500;
               int KeyHit = 0;

               while (true) {
                   SetColor(WHITE, BLACK);
                   FLASH("DOWN", 31, 14, startText);

                   SetColor(BLACK, BLACK);
                   FLASH("     \b", 31, 14, stopText);

                   if (_kbhit()) {

                       /* Get the key (and reset _kbhit() */
                       KeyHit = _getch();

                       /* if it's an extended key, get it! */
                       if (KeyHit == 224) { KeyHit = _getch(); }

                       break;
                   }
               }

               SetColor(WHITE, BLACK);
               PlaceCursor(31, 14);
               printf("DOWN");

               PlaceCursor(39, 14);
               printf("%c  ", KeyHit);

               Snake.at(ctr).SetDownArrow(KeyHit);
           }
       } break;

       case 2: {
           if (Key == 5) {

               long startText = GetTickCount();
               long stopText = startText - 500;
               int KeyHit = 0;

               while (true) {
                   SetColor(WHITE, BLACK);
                   FLASH("LEFT", 31, 15, startText);

                   SetColor(BLACK, BLACK);
                   FLASH("     \b", 31, 15, stopText);

                   if (_kbhit()) {

                       /* Get the key (and reset _kbhit() */
                       KeyHit = _getch();

                       /* if it's an extended key, get it! */
                       if (KeyHit == 224) { KeyHit = _getch(); }

                       break;
                   }
               }

               SetColor(WHITE, BLACK);
               PlaceCursor(31, 15);
               printf("LEFT");

               PlaceCursor(39, 15);
               printf("%c  ", KeyHit);

               Snake.at(ctr).SetLeftArrow(KeyHit);
           }
       } break;

       case 3: {
           if (Key == 5) {

               long startText = GetTickCount();
               long stopText = startText - 500;
               int KeyHit = 0;

               while (true) {
                   SetColor(WHITE, BLACK);
                   FLASH("RIGHT", 31, 16, startText);

                   SetColor(BLACK, BLACK);
                   FLASH("     \b", 31, 16, stopText);

                   if (_kbhit()) {

                       /* Get the key (and reset _kbhit() */
                       KeyHit = _getch();

                       /* if it's an extended key, get it! */
                       if (KeyHit == 224) { KeyHit = _getch(); }

                       break;
                   }
               }

               SetColor(WHITE, BLACK);
               PlaceCursor(31, 16);
               printf("RIGHT");

               PlaceCursor(39, 16);
               printf("%c  ", KeyHit);

               Snake.at(ctr).SetRightArrow(KeyHit);
           }
       } break;

       case 4: {
           if (Key == 5) { ExitMenu = true; }
       } break;

       case 666: {
           ExitMenu = true;
       } break;
   }

   return ExitMenu;
}

int AdvOptions_Callback(int CurrItem, bool ExitMenu, int Key) {

   switch (CurrItem) {
       case 0: {
           if (Key != 5) {
               if (Key == 10 && ctr < NumSnakes - 1) { ctr++; }
               if (Key == 15 && ctr > 0) { ctr--; }

               PlaceCursor(41, 8);
               printf("%i ", Snake.at(ctr).Player);
           }
       } break;

       case 1: {
           if (Key != 5) {
               if (Key == 10 && NumSnakes < 8) { Snake.push_back(CREATE_SNAKE(NumSnakes +

1, VK_UP, VK_DOWN, VK_LEFT, VK_RIGHT)); NumSnakes++; sprintf(Snake.at(NumSnakes - 1).Name,

"PLAYER %i", NumSnakes); }
               if (Key == 15 && NumSnakes > 1) { NumSnakes--; if (ctr > NumSnakes - 1) {

ctr--; } Snake.pop_back(); }

               PlaceCursor(41, 8);
               printf("%i ", Snake.at(ctr).Player);

               PlaceCursor(44, 9);
               printf("%i ", NumSnakes);
           }
       } break;

       case 2: {
           if (Key == 5) {
               Menu.Store();

               Menu.SetAttributes(20, 9, 25, 10, "CONTROLS");
               Menu.AddOptions(5, "UP    =", "DOWN  =", "LEFT  =", "RIGHT =", "Back");
               Menu.DisplayMenu(CYAN, BLACK);

               PlaceCursor(39, 13);
               printf("%c", Snake.at(ctr).GetUpArrow());
               PlaceCursor(39, 14);
               printf("%c", Snake.at(ctr).GetDownArrow());
               PlaceCursor(39, 15);
               printf("%c", Snake.at(ctr).GetLeftArrow());
               PlaceCursor(39, 16);
               printf("%c", Snake.at(ctr).GetRightArrow());

               Menu.Loop(SetupControls, false);
               Menu.Restore(CYAN, BLACK);

               PlaceCursor(41, 8);
               printf("%i ", Snake.at(ctr).Player);
               PlaceCursor(44, 9);
               printf("%i ", NumSnakes);
           }
       } break;

       case 3: {
           if (Key == 5) {
               Menu.Clear(CYAN, BLACK);
               Snake.at(ctr).UserSelectedDimensions();
               Menu.SetArrow(7);
               Menu.DisplayMenu(CYAN, BLACK);

               PlaceCursor(41, 8);
               printf("%i ", Snake.at(ctr).Player);

               PlaceCursor(44, 9);
               printf("%i ", NumSnakes);
           }
       } break;

       case 4: {
           if (Key == 5) {
               ExitMenu = true;
               Menu.Clear(CYAN, BLACK);
               Menu.SetAttributes(30, 15, 20, 5, "OPTIONS");
               Menu.AddOptions(11, "Adv. Options", "Assistance Options", "Name: ", "Snake

editing: ", "Snakes (players): ", "Difficulty: ", "Speed: ", "Randomize Color", "Color: ",

"Lives: ", "Back");
               Menu.DisplayMenu(CYAN, BLACK);
           }
       } break;

       case 666: {
           ExitMenu = true;
           Menu.Clear(CYAN, BLACK);
           Menu.SetAttributes(30, 15, 20, 5, "OPTIONS");
           Menu.AddOptions(11, "Adv. Options", "Assistance Options", "Name: ", "Snake

editing: ", "Snakes (players): ", "Difficulty: ", "Speed: ", "Randomize Color", "Color: ",

"Lives: ", "Back");
           Menu.DisplayMenu(CYAN, BLACK);
       } break;
   }

   return ExitMenu;
}

int Options_Callback(int CurrItem, bool ExitMenu, int Key) {

   switch (CurrItem) {
       case 0: {
           if (Key == 5) {
               AdvOptionsMenu();
               LoadValues();
           }
       } break;

       case 1: {
           if (Key == 5) {
               AssistanceMenu();
               LoadValues();
           }
       } break;

       case 2: {
           if (Key == 5) {
               PlaceCursor(32, 10);
               printf("               ");
               SetColor(WHITE, BLACK);
               PlaceCursor(32, 10);
               cin.getline(Snake.at(ctr).Name, 14);
               FlushConsoleInputBuffer(InHandle);
               _flushall();
           }
       } break;

       case 3: {
           if (Key != 5) {
               if (Key == 10 && ctr < NumSnakes - 1) { ctr++; LoadValues(); }
               if (Key == 15 && ctr > 0) { ctr--; LoadValues(); }
           }
       } break;

       case 4: {
           if (Key != 5) {
               if (Key == 10 && NumSnakes < 8) { Snake.push_back(CREATE_SNAKE(NumSnakes +

1, VK_UP, VK_DOWN, VK_LEFT, VK_RIGHT)); NumSnakes++; sprintf(Snake.at(NumSnakes - 1).Name,

"PLAYER %i", NumSnakes); }
               if (Key == 15 && NumSnakes > 1) { NumSnakes--; if (ctr > NumSnakes - 1) {

ctr--; LoadValues(); } Snake.pop_back(); }

               PlaceCursor(41, 11);
               printf("%i ", Snake.at(ctr).Player);

               PlaceCursor(44, 12);
               printf("%i ", NumSnakes);
           }
       } break;

       case 5: {
           if (Key != 5) {
               if (Key == 10 && Snake.at(ctr).Difficulty < 2) { Snake.at(ctr).Difficulty++;

}
               if (Key == 15 && Snake.at(ctr).Difficulty > 0) { Snake.at(ctr).Difficulty--;

}
               PlaceCursor(38, 13);
               printf("%s", Difficulty[Snake.at(ctr).Difficulty]);
           }
       } break;

       case 6: {
           if (Key != 5) {
               PlaceCursor(33, 14);
               SetColor(BLACK, BLACK);
               printf("  ", Snake.at(ctr).Speed);

               if (Key == 10 && Snake.at(ctr).Speed < 50) { Snake.at(ctr).Speed++; }
               if (Key == 15 && Snake.at(ctr).Speed > 0) { Snake.at(ctr).Speed--; }

               PlaceCursor(33, 14);
               SetColor(WHITE, BLACK);
               printf("%i", Snake.at(ctr).Speed);
           }
       } break;

       case 7: {
           if (Key == 5) {

               Snake.at(ctr).Color = rand() % 15;

               /* Print the new (randomized) color! */
               PlaceCursor(33, 16);
               SetColor(Snake.at(ctr).Color, BLACK);
               printf("Û ****@");
           }
       } break;

       case 8: {
           if (Key != 5) {

               if (Key == 10 && Snake.at(ctr).Color < 15) { Snake.at(ctr).Color++; }
               if (Key == 15 && Snake.at(ctr).Color > 0) { Snake.at(ctr).Color--; }

               /* Print the new color! */
               PlaceCursor(33, 16);
               SetColor(Snake.at(ctr).Color, BLACK);
               printf("Û ****@");
           }
       } break;

       case 9: {
           if (Key != 5) {
               if (Key == 10 && Snake.at(ctr).MaxLives < 3) { Snake.at(ctr).MaxLives++; }
               if (Key == 15 && Snake.at(ctr).MaxLives > 1) { Snake.at(ctr).MaxLives--; }
               PlaceCursor(33, 17);
               printf("%i", Snake.at(ctr).MaxLives);

               /* Current Snakes lives need to be updated! */
               Snake.at(ctr).Lives = Snake.at(ctr).MaxLives;
           }
       } break;

       case 10: {
           if (Key == 5) {
               ExitMenu = true;
               Menu.Clear(CYAN, BLACK);
               Menu.SetAttributes(30, 15, 20, 5, "SNAKE");
               Menu.AddOptions(6, "Start game", "Options", "Scoreboard", "Credits", "Demo",

"Quit");
               Menu.DisplayMenu(CYAN, BLACK);
           }
       } break;

       case 666: {
           ExitMenu = true;
           Menu.Clear(CYAN, BLACK);
           Menu.SetAttributes(30, 15, 20, 5, "SNAKE");
           Menu.AddOptions(6, "Start game", "Options", "Scoreboard", "Credits", "Demo",

"Quit");
           Menu.DisplayMenu(CYAN, BLACK);
       } break;
   }

   return ExitMenu;
}

#endif
Banned
Posts: 10,875
Joined: Sep 28 2007
Gold: 0.00
Warn: 50%
Jun 19 2009 11:13pm
what.....?
Retired Moderator
Posts: 13,894
Joined: May 3 2008
Gold: 1.18
Trader: Trusted
Jun 19 2009 11:13pm
Please post in this forum, they might be able to help you better.

http://forums.d2jsp.org/forum.php?f=122
Go Back To Programming & Development Topic List
123Next
Add Reply New Topic New Poll