Witam, zacząłem tworzyć grę coś typu snake w konsoli, Najpierw chciałem zrobić pojedynczy '#' aby się poruszał. Sterowanie działało jak je sprawdzałem, ale gdy podpiąłem to do całości, nic się nie dzieje. Oto kod:
#include <iostream>
#include <conio.h>
#include <windows.h>
#define SZEROKOSC 30
#define WYSOKOSC 20
using namespace std;
char bufor[SZEROKOSC][WYSOKOSC];
int x=SZEROKOSC/2, y=WYSOKOSC/2;
int xLast=x, yLast=y;
void gotoxy(int x, int y)
{
COORD c;
c.X = x - 1;
c.Y = y - 1;
SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), c );
}
void sprawdzanieKlawiszy(int *x, int *y)
{
bool jesli=0;
int klawisz;
int klawisz2;
while (jesli==0)
{
klawisz2=getch();
klawisz=getch();
if(klawisz2==224)
{
switch (klawisz)
{
case 72:
if(*y>WYSOKOSC-1)
*y=1;
else *y+=1;
jesli=1;
break;
case 80:
if(*y<1)
*y=WYSOKOSC-1;
else *y-=1;
jesli=1;
break;
case 75:
if(*x<1)
*x=SZEROKOSC-1;
else *x-=1;
jesli=1;
break;
case 77:
if(*x>SZEROKOSC-1)
*x=1;
else *x+=1;
jesli=1;
break;
}
}
}
}
void rysujMape()
{
gotoxy(3, 2);
for (int i; i<WYSOKOSC; i++)
{
for (int j; j<SZEROKOSC; j++)
{
cout << bufor[j][i];
}
cout<<endl;
gotoxy(2, i);
}
}
void ramka()
{
for(int i=1; i<=SZEROKOSC; i++)
{
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 0x02);
cout<<"#";
}
cout<<endl;
for(int i=0; i<WYSOKOSC-2; i++)
{
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 0x02);
cout<<"#";
for(int j=1; j<SZEROKOSC-1; j++)
{
cout<<" ";
}
cout<<"#"<<endl;
}
for(int i=1; i<=SZEROKOSC; i++)
{
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 0x02);
cout<<"#";
}
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 0x08);
}
void aktualizacjaBufora(int x, int *xLast, int y, int *yLast, char bufor[SZEROKOSC][WYSOKOSC]){
bufor[x][y]='#';
bufor[*xLast][*yLast]=' ';
*xLast=x;
*yLast=y;
}
int main()
{
cout << " SNAKE v1.0" << endl;
ramka();
Sleep(4000);
while (true)
{
sprawdzanieKlawiszy(&x, &y);
aktualizacjaBufora(x, &xLast, y, &yLast, bufor);
rysujMape();
//cout<<"x: "<<x<<" y: "<<y<<endl;
}
return 0;
}