Forum Gry Hobby Sprzęt Rozmawiamy Archiwum Regulamin

Forum: Snake w C++ (CodeBlocks)

14.04.2018 10:14
1
R3TR0
13
Legionista

Snake w C++ (CodeBlocks)

Witam, mam problem z grą Snake w C++ (CodeBlocks), nie chce działać, gdy chcę w nią zagrać pojawia się plansza, lecz zaczyna bardzo szybko "migać", przez co gra jest niemożliwa. Pomoże ktoś? Tu jest kod źródłowy:

#include <iostream>
#include <conio.h>
#include <string>
#include <windows.h>
using namespace std;

bool gameover;
bool shutDown;
const int width = 60;
const int height = 40;
int x, y, fruitX, fruitY, score;
int tailX[400], tailY[400], nTail;
enum eDirection ‹STOP, LEFT, RIGHT, UP, DOWN›;
eDirection dir;

void StartScreen()

system("cls");
cout << "\nWitam w grze SNAKE"
<< "\n\nNacisnij 'e', aby GRAC"
<< "\n\nNacisnij 'q', aby WYJSC"
<< "\n\nNacisnij 'i', aby zobaczyc INSTRUKCJE."
<< "\n\n\nPoprzedni wynik: " << score << endl;

void Instructions()

system("cls");
cout << "\nUzyj klawisza 'w' aby ruszac sie w GORE."
<< "\nUzyj klawisza 'a' aby ruszac sie w LEWO."
<< "\nUzyj klawisza 's' aby ruszac sie DOL."
<< "\nUzyj klawisza 'd' aby ruszac sie w PRAWO."
<< "\n\nNacisnij klawisz 'x', aby WYJSC z gry w dowolnym momencie."
<< "\n\nNie dotykaj scian, bo PRZEGRASZ."
<< "\n\nNie jedz wlasnego ogona, bo PRZEGRASZ."
<< "\n\n\nNacisnij 'm', aby wrocic do MENU.\n";
string input;
cin >> input;
if(input=="m")
StartScreen();


void FruitSpawn()

fruitX = rand() % width;
fruitY = rand() % height;

void Setup()

gameover = false;
shutDown = false;
dir = STOP;
x = width / 2;
y = height / 2;
FruitSpawn();
score = 0;
nTail = 0;

void Draw()

system("cls");
for (int i = 0; i < width + 2; i++) ‹
cout << "#";›
cout << endl;

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

for (int j = 0; j < width; j++)

if (j == 0)
cout << "#";

if (i == y && j == x)
cout << "O";

else if (i == fruitY && j == fruitX)
cout << "F";

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 << " ";

if (j == width - 1)
cout << "#";

cout << endl;

for (int i = 0; i < width + 2; i++) ‹
cout << "#";

cout << endl;
cout << "Score: " << score << endl;

void Input()

if (_kbhit()) ‹
switch (_getch())

case 'a':
dir = LEFT;
break;
case 'w':
dir = UP;
break;
case 's':
dir = DOWN;
break;
case 'd':
dir = RIGHT;
break;
case 'x':
gameover = true;
break;



void Logic()

int prevX = tailX[0];
int prevY = tailY[0];
int prev2X, prev2Y;
tailX[0] = x;
tailY[0] = y;

for (int i = 1; i < nTail; i++) ‹
prev2X = tailX;
prev2Y = tailY;
tailX = prevX;
tailY = prevY;
prevX = prev2X;
prevY = prev2Y;

switch (dir)

case LEFT:
x--;
break;
case RIGHT:
x++;
break;
case UP:
y--;
break;
case DOWN:
y++;
break;

if (x > width || x < 0 || y > height || y < 0) ‹
gameover = true;
StartScreen();

for (int i = 0; i < nTail; i++) ‹
if (tailX == x && tailY == y)
gameover = true;

if (x == fruitX && y == fruitY) ‹
score++;
nTail++;
FruitSpawn();


int main()

do ‹
StartScreen();
string input;
cin >> input;
if (input == "e")

Setup();
while (!gameover)

Draw();
Input();
Logic();
Sleep(100);


else if (input == "i")
Instructions();
else if (input == "q")
shutDown = true;
› while (shutDown == false);

return 0;

Forum: Snake w C++ (CodeBlocks)