Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ul0r.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- /*
- Snake
- Author: BlueMelon
- bluemelon.info
- */
- #include <iostream>
- #include "windows.h"
- #include <list>
- char Map[20][60] = {"###########################################################",
- "# #",
- "# #",
- "# #",
- "# #",
- "# #",
- "# #",
- "# #",
- "# #",
- "# #",
- "# #",
- "# #",
- "# #",
- "# #",
- "# #",
- "# #",
- "# #",
- "# #",
- "# #",
- "###########################################################"};
- /* -=- Fruit Spawn -=- */
- bool Fruit = true;
- /* -=- Interval -=- */
- unsigned short MovementInterval = 1; // 1 millisecond = 0.001 seconds
- /* -=- Player Score -=- */
- unsigned int PScore = 0;
- /* -=- Functions -=- */
- void gotoxy(unsigned short x, unsigned short y);
- void DrawMap();
- void DropFruit();
- void AddScore(int points);
- /* -=- Key Press -=- */
- #define RIGHT 0
- #define LEFT 1
- #define DOWN 2
- #define UP 3
- class Snake
- {
- private:
- bool Dead;
- unsigned short Dir;
- unsigned short Hx,Hy;
- std::list<unsigned int> Lx,Ly;
- public:
- void SetDirection(unsigned short direction){Dir = direction;}
- void HandleKeys();
- void SnakeMove();
- bool CheckDead(){return Dead;};
- void Grow();
- void SetSnake();
- };
- void Snake::Grow()
- {
- Lx.push_front(Lx.front());
- Ly.push_front(Ly.front());
- return;
- }
- void Snake::SnakeMove()
- {
- if(Dir == RIGHT){
- if(Map[Hy][Hx+1] == '#' || Map[Hy][Hx+1] == 'o') //Collision
- {
- Dead = true;
- }else
- {
- if(Map[Hy][Hx+1] == '*') // Fruit
- {
- Fruit = true;
- AddScore(100);
- }
- Hx++;
- }
- }else if(Dir == LEFT){
- if(Map[Hy][Hx-1] == '#' || Map[Hy][Hx-1] == 'o')
- {
- Dead = true;
- }else
- {
- if(Map[Hy][Hx-1] == '*')
- {
- Fruit = true;
- AddScore(100);
- }
- Hx--;
- }
- }else if(Dir == DOWN){
- if(Map[Hy+1][Hx] == '#' || Map[Hy+1][Hx] == 'o')
- {
- Dead = true;
- }else
- {
- if(Map[Hy+1][Hx] == '*')
- {
- Fruit = true;
- AddScore(100);
- }
- Hy++;
- }
- }
- else if(Dir == UP){
- if(Map[Hy-1][Hx] == '#' || Map[Hy-1][Hx] == 'o')
- {
- Dead = true;
- }else
- {
- if(Map[Hy-1][Hx] == '*')
- {
- Fruit = true;
- AddScore(100);
- }
- Hy--;
- }
- }
- if(Dead != true)
- {
- if(Fruit == true)
- {
- Grow();
- }
- Ly.push_back(Hy);
- Lx.push_back(Hx);
- Map[Ly.back()][Lx.back()] = 'o';
- Map[Ly.front()][Lx.front()] = ' ';
- Ly.pop_front();
- Lx.pop_front();
- gotoxy(0,0);
- DrawMap();
- }
- return;
- }
- void Snake::HandleKeys()
- {
- if(GetAsyncKeyState(VK_RIGHT) != 0)
- {
- if(Dir != LEFT)
- SetDirection(RIGHT);
- }
- else if(GetAsyncKeyState(VK_UP) != 0)
- {
- if(Dir != DOWN)
- SetDirection(UP);
- }
- else if(GetAsyncKeyState(VK_DOWN) != 0)
- {
- if(Dir != UP)
- SetDirection(DOWN);
- }
- else if(GetAsyncKeyState(VK_LEFT) != 0)
- {
- if(Dir != RIGHT)
- SetDirection(LEFT);
- }
- return;
- }
- void Snake::SetSnake()
- {
- Hx = 10;
- Hy = 15;
- Lx.push_back(8);
- Lx.push_back(9);
- Lx.push_back(10);
- Ly.push_back(15);
- Ly.push_back(15);
- Ly.push_back(15);
- Map[Ly.back()][Lx.back()] = 'o';
- Map[Ly.back()][Lx.back()-1] = 'o';
- Map[Ly.back()][Lx.back()-2] = 'o';
- return;
- }
- int main()
- {
- SetConsoleTitleA("BlueMelon's Snake");
- Snake PSnake;
- PSnake.SetSnake ();
- PSnake.SetDirection(RIGHT);
- while(PSnake.CheckDead() != true)
- {
- Sleep(MovementInterval);
- if(Fruit == true)
- {
- DropFruit();
- }
- PSnake.HandleKeys();
- PSnake.SnakeMove();
- if(PSnake.CheckDead() == true)
- {
- using namespace std;
- system("cls"); // This is only a one time thing, do not use this on a loop, bad practice.
- cout << "Game Over!\n";
- cout << "Press \'Return\' Key to Continue...";
- cin.get();
- return 0;
- }
- }
- return 0;
- }
- void DropFruit()
- {
- srand(GetTickCount());
- unsigned short Fruitx = rand() % 57 +1;
- unsigned short Fruity = rand() % 18 +1;
- if(Map[Fruity][Fruitx] == 'o') // Check if its going to drop on snake
- {
- DropFruit();
- }else
- {
- Map[Fruity][Fruitx] = '*';
- }
- Fruit = false;
- return;
- }
- void gotoxy(unsigned short x, unsigned short y)
- {
- static HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
- static COORD WritePos;
- WritePos.X = x;
- WritePos.Y = y;
- SetConsoleCursorPosition(hStdOut,WritePos);
- }
- void DrawMap()
- {
- using std::cout;
- for(unsigned short rows=0; rows < 20 ; rows++)
- cout << Map[rows] << "\n";
- //Menu Points etc.
- cout << "# #\n";
- cout << "# #\n";
- cout << "# #\n";;
- cout << "###########################################################";
- gotoxy(46,22);
- cout << "Score: " << PScore;
- gotoxy(5,25);
- cout << "<-- BY : BlueMelon | DOWNLOAD : MASOKIS.COM -->";
- return;
- }
- void AddScore(int points)
- {
- PScore += points;
- return ;
- }
Advertisement
Add Comment
Please, Sign In to add comment