masokis

Ujang

Mar 20th, 2011
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.87 KB | None | 0 0
  1. // ul0r.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5.  
  6.  
  7. /*
  8. Snake
  9. Author: BlueMelon
  10.  
  11. bluemelon.info
  12. */
  13.  
  14. #include <iostream>
  15. #include "windows.h"
  16. #include <list>
  17.  
  18. char Map[20][60] = {"###########################################################",
  19.     "#                                                         #",
  20.     "#                                                         #",
  21.     "#                                                         #",
  22.     "#                                                         #",
  23.     "#                                                         #",
  24.     "#                                                         #",
  25.     "#                                                         #",
  26.     "#                                                         #",
  27.     "#                                                         #",
  28.     "#                                                         #",
  29.     "#                                                         #",
  30.     "#                                                         #",
  31.     "#                                                         #",
  32.     "#                                                         #",
  33.     "#                                                         #",
  34.     "#                                                         #",
  35.     "#                                                         #",
  36.     "#                                                         #",
  37.     "###########################################################"};
  38.  
  39. /* -=- Fruit Spawn -=- */
  40. bool Fruit = true;
  41. /* -=- Interval -=- */
  42. unsigned short MovementInterval = 1; // 1 millisecond = 0.001 seconds
  43. /* -=- Player Score -=- */
  44. unsigned int PScore = 0;
  45. /* -=- Functions -=- */
  46. void gotoxy(unsigned short x, unsigned short y);
  47. void DrawMap();
  48. void DropFruit();
  49. void AddScore(int points);
  50.  
  51. /* -=- Key Press -=- */
  52. #define RIGHT 0
  53. #define LEFT 1
  54. #define DOWN 2
  55. #define UP    3
  56.  
  57. class Snake
  58. {
  59.  
  60. private:
  61.     bool Dead;
  62.     unsigned short Dir;
  63.     unsigned short Hx,Hy;
  64.     std::list<unsigned int> Lx,Ly;
  65.  
  66. public:
  67.     void SetDirection(unsigned short direction){Dir = direction;}
  68.     void HandleKeys();
  69.     void SnakeMove();
  70.     bool CheckDead(){return Dead;};
  71.     void Grow();
  72.     void SetSnake();
  73. };
  74. void Snake::Grow()
  75. {
  76.     Lx.push_front(Lx.front());
  77.     Ly.push_front(Ly.front());
  78.     return;
  79. }
  80. void Snake::SnakeMove()
  81. {
  82.     if(Dir == RIGHT){
  83.  
  84.         if(Map[Hy][Hx+1] == '#' || Map[Hy][Hx+1] == 'o') //Collision
  85.         {
  86.             Dead = true;
  87.         }else
  88.         {
  89.             if(Map[Hy][Hx+1] == '*') // Fruit
  90.             {
  91.                 Fruit = true;
  92.                 AddScore(100);
  93.             }
  94.             Hx++;
  95.         }
  96.  
  97.     }else if(Dir == LEFT){
  98.  
  99.         if(Map[Hy][Hx-1] == '#' || Map[Hy][Hx-1] == 'o')
  100.         {
  101.             Dead = true;
  102.         }else
  103.         {
  104.             if(Map[Hy][Hx-1] == '*')
  105.             {
  106.                 Fruit = true;
  107.                 AddScore(100);
  108.             }
  109.             Hx--;
  110.         }
  111.  
  112.     }else if(Dir == DOWN){
  113.  
  114.         if(Map[Hy+1][Hx] == '#' || Map[Hy+1][Hx] == 'o')
  115.         {
  116.             Dead = true;
  117.         }else
  118.         {
  119.             if(Map[Hy+1][Hx] == '*')
  120.             {
  121.                 Fruit = true;
  122.                 AddScore(100);
  123.             }
  124.             Hy++;
  125.         }
  126.  
  127.     }
  128.     else if(Dir == UP){
  129.  
  130.         if(Map[Hy-1][Hx] == '#' || Map[Hy-1][Hx] == 'o')
  131.         {
  132.             Dead = true;
  133.         }else
  134.         {
  135.             if(Map[Hy-1][Hx] == '*')
  136.             {
  137.                 Fruit = true;
  138.                 AddScore(100);
  139.             }
  140.             Hy--;
  141.         }
  142.     }
  143.  
  144.     if(Dead != true)
  145.     {
  146.         if(Fruit == true)
  147.         {
  148.             Grow();
  149.         }
  150.  
  151.         Ly.push_back(Hy);
  152.         Lx.push_back(Hx);
  153.  
  154.         Map[Ly.back()][Lx.back()] = 'o';
  155.         Map[Ly.front()][Lx.front()] = ' ';
  156.  
  157.         Ly.pop_front();
  158.         Lx.pop_front();
  159.  
  160.         gotoxy(0,0);
  161.         DrawMap();
  162.     }
  163.  
  164.     return;
  165. }
  166. void Snake::HandleKeys()
  167. {
  168.  
  169.     if(GetAsyncKeyState(VK_RIGHT) != 0)
  170.     {
  171.         if(Dir != LEFT)
  172.             SetDirection(RIGHT);
  173.     }
  174.     else if(GetAsyncKeyState(VK_UP) != 0)
  175.     {
  176.         if(Dir != DOWN)
  177.             SetDirection(UP);
  178.     }
  179.     else if(GetAsyncKeyState(VK_DOWN) != 0)
  180.     {
  181.         if(Dir != UP)
  182.             SetDirection(DOWN);
  183.     }
  184.     else if(GetAsyncKeyState(VK_LEFT) != 0)
  185.     {
  186.         if(Dir != RIGHT)
  187.             SetDirection(LEFT);
  188.     }
  189.     return;
  190. }
  191. void Snake::SetSnake()
  192. {
  193.     Hx = 10;
  194.     Hy = 15;
  195.     Lx.push_back(8);
  196.     Lx.push_back(9);
  197.     Lx.push_back(10);
  198.  
  199.     Ly.push_back(15);
  200.     Ly.push_back(15);
  201.     Ly.push_back(15);
  202.  
  203.     Map[Ly.back()][Lx.back()] = 'o';
  204.     Map[Ly.back()][Lx.back()-1] = 'o';
  205.     Map[Ly.back()][Lx.back()-2] = 'o';
  206.     return;
  207. }
  208.  
  209. int main()
  210. {
  211.     SetConsoleTitleA("BlueMelon's Snake");
  212.     Snake PSnake;
  213.  
  214.     PSnake.SetSnake ();
  215.     PSnake.SetDirection(RIGHT);
  216.  
  217.     while(PSnake.CheckDead() != true)
  218.     {
  219.         Sleep(MovementInterval);
  220.         if(Fruit == true)
  221.         {
  222.             DropFruit();
  223.         }
  224.  
  225.         PSnake.HandleKeys();
  226.         PSnake.SnakeMove();
  227.  
  228.         if(PSnake.CheckDead() == true)
  229.         {
  230.             using namespace std;
  231.             system("cls"); // This is only a one time thing, do not use this on a loop, bad practice.
  232.             cout << "Game Over!\n";
  233.             cout << "Press \'Return\' Key to Continue...";
  234.             cin.get();
  235.             return 0;
  236.         }
  237.  
  238.     }
  239.  
  240.     return 0;
  241. }
  242.  
  243. void DropFruit()
  244. {
  245.     srand(GetTickCount());
  246.     unsigned short Fruitx = rand() % 57 +1;
  247.     unsigned short Fruity = rand() % 18 +1;
  248.  
  249.     if(Map[Fruity][Fruitx] == 'o') // Check if its going to drop on snake
  250.     {
  251.         DropFruit();
  252.     }else
  253.     {
  254.         Map[Fruity][Fruitx] = '*';
  255.     }
  256.  
  257.     Fruit = false;
  258.     return;
  259. }
  260.  
  261. void gotoxy(unsigned short x, unsigned short y)
  262. {
  263.     static HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  264.     static COORD WritePos;
  265.     WritePos.X = x;
  266.     WritePos.Y = y;
  267.     SetConsoleCursorPosition(hStdOut,WritePos);
  268. }
  269.  
  270. void DrawMap()
  271. {
  272.     using std::cout;
  273.     for(unsigned short rows=0; rows < 20 ; rows++)
  274.         cout << Map[rows] << "\n";
  275.  
  276.     //Menu Points etc.
  277.     cout << "#                                                         #\n";
  278.     cout << "#                                                         #\n";
  279.     cout << "#                                                         #\n";;
  280.     cout << "###########################################################";
  281.     gotoxy(46,22);
  282.     cout << "Score: " << PScore;
  283.     gotoxy(5,25);
  284.     cout << "<-- BY : BlueMelon | DOWNLOAD : MASOKIS.COM -->";
  285.     return;
  286. }
  287.  
  288. void AddScore(int points)
  289. {
  290.     PScore += points;
  291.     return ;
  292. }
Advertisement
Add Comment
Please, Sign In to add comment