Jumat, 28 Oktober 2016

SNAKE MANIA DENGAN C++


SNAKE MANIA DENGAN C++


Sangatlah mudah membuat game dengan C++.Dari pada kita ribet langsung aja source code nya.


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

using namespace std;
bool gameOver;
const int width = 20;
const int height = 20;
int x,y,fruitX,fruitY,score;
int tailX[100],tailY[100];
int nTail;
enum eDirecton{ STOP = 0,LEFT,RIGHT,UP,DOWN
};
eDirecton dir;

void Setup(){
   
    gameOver = false;
    x = width / 2;
    y = height / 2;
    fruitX = rand() % width;
    fruitY = rand() % height;
    score = 0;
   
}

void Draw(){
    system("cls");
    for(int i = 0;i < width;i++){
        cout << " #";       
    }   
    cout << "         Score : " << score;
    cout << endl;

    for(int i = 0;i < height;i++){

        for(int j = 0;j < width;j++){
           
            if(j == x && i == y){
                cout << " 0";               
            }else if(j == fruitX && i == fruitY){
                cout << " F";
            }else if(j == 0 || j == width  - 1){
                cout << " #";
            }else{
               
                bool print = false;
               
                for(int k = 0;k < nTail;k++){
                   
                    if(tailX[k] == j && tailY[k] == i){
                        cout << " o";   
                        print = true;
                    }
                       
                }
               
                if(!print){
                    cout << "  ";
                }
               
            }           
        }   
        cout << endl;   
    }
   
    for(int i = 0;i < width;i++){
        cout << " #";
    }

    cout << endl;

}
void Input(){
    if(_kbhit()){
        switch(_getch()){
            case 'a':
                dir = LEFT;
                break;
            case 'd':
                dir = RIGHT;
                break;
            case 'w':
                dir = UP;
                break;
            case 's':
                dir = DOWN;
                break;
        }
    }   
}

void Logic(){
   
    int prevX = tailX[0];
    int prevY = tailY[0];
    int prev2X,prev2Y;
    tailX[0] = x;
    tailY[0] = y;
    for(int k = 1;k < nTail;k++){
        prev2X = tailX[k];
        prev2Y = tailY[k];
        tailX[k] = prevX;
        tailY[k] = prevY;
        prevX = prev2X;
        prevY = prev2Y;
    }
   
    switch(dir){
        case LEFT:
            x--;
            break;
        case RIGHT:
            x++;
            break;
        case DOWN:
            y++;
            break;
        case UP:
            y--;
            break;
        default:
            break;
    }
   
    if(x > width || y > height || x < 0 || y < 0){
        gameOver = true;
    }
   
    if(x == fruitX && y == fruitY){
        score+=10;
        fruitX = rand() % width;
        fruitY = rand() % height;
        nTail++;
    }
   
   
}

int main(){
   
    Setup();   
    while(!gameOver){
        Draw();
        Input();
        Logic();
    }
   
    return 0;   
}

Ok Sekian dan Terima Kasih.. ^-^ ..

0 komentar:

Posting Komentar