Snake - nie chce się poruszać

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

using namespace std;

bool gameover = false;
const int width = 30;
const int height = 20;
int x, y, fruitx, fruity, score;
enum eDirecton { STOP = 0,
    LEFT,
    RIGHT,
    UP,
    DOWN };
eDirecton dir;
void setup()
{
    gameover = false;
    dir = STOP;
    x = width / 2;
    y = height / 2;
    fruitx = rand() % width;
    fruity = rand() % height;
    score = 0;
}

void draw()
{
    system("cls");
    for (int i(0); i < width + 1; i++)
        cout << "0";
    cout << endl;

    for (int i = 0; i < height; i++) {
        cout << "0";
        cout << endl;
        for (int j = 0; j < width; j++) {
            if (j == 0)
                cout << "0";
            if (i == x && j == y)
                cout << "G";
            else if (i == fruitx && j == fruity)
                cout << "F";
            else
                cout << " ";
            if (j == width - 1)
                cout << "0";
        }
        cout << endl;
    }
    for (int i = 0; i < width + 1; i++)
        cout << "0";
    cout << 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 'q':
            gameover = true;
            break;
        }
    }
}

void logic()
{
    switch (dir) {
    case 'a':
        x--;
        break;
    case 'w':
        y--;
        break;
    case 's':
        y++;
        break;
    case 'd':
        x++;
        break;
    default:
        break;
    }
    if (x > width || x < 0 || y > height || y < 0)
        gameover = true;
}

int main()
{
    setup();
    while (!gameover) {
        draw();
        input();
        logic();
    }

    return 0;
}

Gdy kompiluje , i naciskam wywołany przycisk x albo y nie chcą się poruszać. Nie wiem gdzie popełniłem błąd. Poza tym w codeblocks Wszystko jest niewyraźne przy kompilacji , ponieważ pętle ciągle się buferują. Jest na to jakiś sposób?.

2

Cześć,
Inkrementację zmiennych x oraz y oparłeś o wartość zmiennej dir:

void logic()
{
    switch (dir) {
    case 'a':
        x--;
        break;

Natomiast wartości jakie wpisywane są do niej wcale nie są równe 'a', 'b' itp... Są równe tyle, ile zadeklarowałeś tutaj:

enum eDirecton { STOP = 0,
    LEFT,
    RIGHT,
    UP,
    DOWN };

Z tego powodu w funkcji logic() case zawsze będzie default...

Dodatkowo: UNIKAJ ZMIENNYCH GLOBALNYCH. :)

Pozdrawiam

1 użytkowników online, w tym zalogowanych: 0, gości: 1