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

  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. Log Out / Değiştir )

Twitter resmi

Twitter hesabınızı kullanarak yorum yapıyorsunuz. Log Out / Değiştir )

Facebook fotoğrafı

Facebook hesabınızı kullanarak yorum yapıyorsunuz. Log Out / Değiştir )

Google+ fotoğrafı

Google+ hesabınızı kullanarak yorum yapıyorsunuz. Log Out / Değiştir )

Connecting to %s

%d blogcu bunu beğendi: