Başlangıç > Kodlar > C++ ile yılan oyunu

C++ ile yılan oyunu

Not: C++11 ile derlenmiştir.

Kodlama: TD Software

snake

Kodlar:

#include <iostream>
#include <Windows.h>
#include <time.h>
#include <conio.h>
#include <list>
#include <algorithm>

using namespace std;

const int row = 24;
const int column = 40;

enum DIRECTION{ LEFT, RIGHT, UP, DOWN };

void clearScreen(const HANDLE&hOut = GetStdHandle(STD_OUTPUT_HANDLE)){
_CONSOLE_SCREEN_BUFFER_INFO buffInfo;
COORD point = { 0, 0 };
DWORD var;
GetConsoleScreenBufferInfo(hOut, &buffInfo);
FillConsoleOutputCharacter(hOut, ' ', buffInfo.dwSize.X * buffInfo.dwSize.Y, point, &var);
SetConsoleCursorPosition(hOut, point);
}

void setCursorPos(const int&x, const int&y, const HANDLE&hOut = GetStdHandle(STD_OUTPUT_HANDLE)){
COORD point = { x, y };
SetConsoleCursorPosition(hOut, point);
}

void printTable(){
for (int i = 0; i < row; ++i){
for (int j = 0; j < column; ++j){
if (i == 0 || i == (row - 1))
cout << "|";
else if (i > 0 && i < (row - 1) && (j == 0 || j == (column - 1)))
cout << "|";
else
cout << ' ';
}
cout << endl;
}
}

void replace(const int&x, const int&y, const char&ch, const HANDLE&hOut = GetStdHandle(STD_OUTPUT_HANDLE)){
COORD point = { x, y };
DWORD var;
SetConsoleCursorPosition(hOut, point);
FillConsoleOutputCharacter(hOut, ch, 1, point, &var);
}

void printStar(const int&x, const int&y){
replace(y, x, '*');
}

class Point{
public:
int x, y;
DIRECTION direction;
Point(const int&x, const int&y, const DIRECTION&direction=LEFT) :x(x), y(y), direction(direction){}
Point&operator++(){
switch (direction){
case LEFT:--y; break;
case RIGHT:++y; break;
case UP:--x; break;
case DOWN:++x; break;
}
return *this;
}
bool operator==(const Point&rhs)const{
if (x == rhs.x && y == rhs.y)
return true;
return false;
}
};

class Snake{
public:
list<Point> snake;
Snake(){
for (int i = 1; i < 6; ++i){
snake.push_back(Point(1, i, RIGHT));
}
}
void move(){
replace(snake.front().y, snake.front().x, ' ');
for (auto i = snake.begin(); i != snake.end(); ++i){
auto temp = i;
++temp;
++(*i);
if (temp != snake.end())
i->direction = temp->direction;
}
replace(snake.back().y, snake.back().x, 'o');
}
void turn(const DIRECTION&direction){
snake.back().direction = direction;
}
void enlarge(){
Point temp(snake.back());
++temp;
snake.push_back(temp);
replace(snake.back().y, snake.back().x, 'o');
}
void print(){
for (auto ptr = snake.cbegin(); ptr != snake.cend(); ++ptr){
replace(ptr->y, ptr->x, 'o');
}
}
};

class Game{
public:
int randX, randY, point;
Snake snake;
Game():point(0){
srand((unsigned int)time(NULL));
randX = rand() % (row - 2) + 1;
randY = rand() % (column - 2) + 1;
}
void play(){
CONSOLE_CURSOR_INFO curInfo;
curInfo.bVisible = FALSE;
curInfo.dwSize = 1;
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &curInfo);
printTable();
printStar(randX, randY);
snake.print();
setCursorPos(0, row);
cout << "Point: " << point;
while (true){
Sleep(65);
if(GetAsyncKeyState(32)){
setCursorPos((column / 2) - 3, row / 2);
cout << "PAUSED";
Sleep(250);
while (true){
Sleep(250);
if (GetAsyncKeyState(32)){
setCursorPos((column / 2) - 3, row / 2);
cout << "      ";
break;
}
}
}
if (_kbhit()){
char key = _getch();
switch (key){
case 'a':
if (snake.snake.back().direction!=RIGHT)
snake.turn(LEFT);
break;
case 'd':
if (snake.snake.back().direction != LEFT)
snake.turn(RIGHT);
break;
case 'w':
if (snake.snake.back().direction != DOWN)
snake.turn(UP);
break;
case 's':
if (snake.snake.back().direction != UP)
snake.turn(DOWN);
break;
}
}
snake.move();
if (snake.snake.back().x == randX && snake.snake.back().y == randY){
++point;
snake.enlarge();
replace(randY, randX, 'o');
while(find(snake.snake.cbegin(), snake.snake.cend(), Point(randX, randY))!=snake.snake.cend()){
randX = rand() % (row - 2) + 1;
randY = rand() % (column - 2) + 1;
}
printStar(randX, randY);
setCursorPos(0, row);
cout << "Point: " << point;
}
if (count(snake.snake.cbegin(), snake.snake.cend(), snake.snake.back()) == 2){
Sleep(1000);
clearScreen();
cout << "GAME OVER!" << endl;
cout << "Point: " << point << endl;
break;
}
else if (snake.snake.back().x == 0 || snake.snake.back().x == (row - 1) || snake.snake.back().y == 0 || snake.snake.back().y == (column - 1)){
Sleep(1000);
clearScreen();
cout << "GAME OVER!" << endl;
cout << "Point: " << point << endl;
break;
}
}
}
};

int main(){
Game a;
a.play();

getchar();
return 0;
}

Reklamlar
  1. sfs
    01/07/2014, 19:19

    çalışmıyor hata veriyor

    Beğen

  2. dasdf
    24/12/2017, 14:11

    //—————————————————————————

    #include
    #include
    #include
    #include
    #include

    #pragma hdrstop

    //—————————————————————————

    #pragma argsused
    struct yilan{
    int x,y;
    };
    int len=4;
    int score=0;
    int col=20,row=10;
    char map[20][40];
    int tamammi_devammi(struct yilan snake[]){
    int i,k1=0,k2=0;
    if(snake[0].x==0||snake[0].x==19||snake[0].y==0||snake[0].y==39){
    k1=1;
    }
    for(i=1;i<len;i++){
    if(snake[0].x==snake[i].x&&snake[0].y==snake[i].y){
    k2=1;
    break;
    }
    }
    if(k1==1||k2==1) return 1;
    else return 0;
    }

    void yemleme(){
    srand(time(NULL));
    row=rand()%18+1;
    col=rand()%38+1;
    }
    void yem(char dizi[20][40],struct yilan snake[]){
    if(snake[len-1].x==row&&snake[len-1].y==col){
    snake[len].x=row;
    snake[len].y=col;
    len++;
    score=score+10;
    yemleme();
    printf("\a");

    }
    }
    void duvar(char dizi[20][40]){
    int i,j;
    for(i=0;i<20;i++){
    if(i==0||i==19){
    for(j=0;j<40;j++){
    dizi[i][j]='=';
    }
    }
    else{
    dizi[i][0]='|';
    for(j=1;j<39;j++){
    dizi[i][j]=' ';
    }
    dizi[i][39]='|';
    }

    }
    map[row][col]='x';
    }
    void ciz(char dizi[20][40]){
    system("CLS");
    printf("Score: %d\n",score);
    int i,j;
    for(i=0;i<20;i++){
    for(j=0;j<40;j++){
    printf("%c",dizi[i][j]);
    }
    printf("\n");
    }

    }
    void baslangic(struct yilan yil[]){
    yil[0].x=1;
    yil[0].y=4;
    yil[1].x=1;
    yil[1].y=3;
    yil[2].x=1;
    yil[2].y=2;
    yil[3].x=1;
    yil[3].y=1;
    }
    void yilan (char dizi[20][40],struct yilan yil[]){
    yem(map,yil);
    int i;
    for(i=0;i0;i–){
    snake[i].x=snake[i-1].x;
    snake[i].y=snake[i-1].y;
    }
    snake[0].y++;
    }
    void sol(struct yilan snake[],int uzunluk){
    int i;
    for(i=uzunluk-1;i>0;i–){
    snake[i].x=snake[i-1].x;
    snake[i].y=snake[i-1].y;
    }
    snake[0].y–;
    }
    void yukari(struct yilan snake[],int uzunluk){
    int i;
    for(i=uzunluk-1;i>0;i–){
    snake[i].x=snake[i-1].x;
    snake[i].y=snake[i-1].y;
    }
    snake[0].x–;
    }
    void asagi(struct yilan snake[],int uzunluk){
    int i;
    for(i=uzunluk-1;i>0;i–){
    snake[i].x=snake[i-1].x;
    snake[i].y=snake[i-1].y;
    }
    snake[0].x++;

    }

    int main(int argc, char* argv[])
    {
    char right=’d’;
    char left=’a’;
    char up=’w’;
    char down=’s’;
    char yon;
    long i;
    int kontrol=0;
    struct yilan snake[100];

    duvar(map);
    baslangic(snake);
    yilan(map,snake);
    ciz(map);

    while(1){
    yon=getch();
    i=0;

    if(yon==right){
    while(i<100000){
    if(i%2000==0){
    if(tamammi_devammi(snake)){
    ciz(map);
    kontrol=1;
    break;

    }

    sag(snake,len);
    duvar(map);
    yilan(map,snake);
    ciz(map);
    }
    i++;
    if(kbhit()) break;
    }

    }
    else if(yon==left){
    while(i<100000){
    if(i%2000==0){
    if(tamammi_devammi(snake)){
    ciz(map);
    kontrol=1;
    break;
    }
    sol(snake,len);
    duvar(map);
    yilan(map,snake);
    ciz(map);
    }
    i++;
    if(kbhit()) break;
    }
    }
    else if(yon==up){
    while(i<100000){
    if(i%2000==0){
    if(tamammi_devammi(snake)){
    ciz(map);
    kontrol=1;
    break;
    }
    yukari(snake,len);
    duvar(map);
    yilan(map,snake);
    ciz(map);
    }
    i++;
    if(kbhit()) break;
    }
    }
    else if(yon==down){
    while(i<100000){
    if(i%2000==0){
    if(tamammi_devammi(snake)){
    ciz(map);
    kontrol=1;
    break;
    }
    asagi(snake,len);
    duvar(map);
    yilan(map,snake);
    ciz(map);
    }
    i++;
    if(kbhit()) break;
    }
    }
    if(kontrol==1) break;

    }

    getch();

    return 0;
    }
    //—————————————————————————

    Beğen

  1. No trackbacks yet.

Bir Cevap Yazın

Aşağıya bilgilerinizi girin veya oturum açmak için bir simgeye tıklayın:

WordPress.com Logosu

WordPress.com hesabınızı kullanarak yorum yapıyorsunuz. Çıkış  Yap /  Değiştir )

Google+ fotoğrafı

Google+ hesabınızı kullanarak yorum yapıyorsunuz. Çıkış  Yap /  Değiştir )

Twitter resmi

Twitter hesabınızı kullanarak yorum yapıyorsunuz. Çıkış  Yap /  Değiştir )

Facebook fotoğrafı

Facebook hesabınızı kullanarak yorum yapıyorsunuz. Çıkış  Yap /  Değiştir )

w

Connecting to %s

%d blogcu bunu beğendi: