Quote:
Originally Posted by HamletEagle
Not the ideal place to get help with your homework, but w/e.
Now, you have some big crystal clear issues, wondering how you didn't catch them:
1.In Logic() you are not using break; in any switch case, meaning that moving up and left is going to do nothing, while moving down and right is working because of how you ordered the switch cases.
2.But you will quickly notice that if you fix the above mistake once you press one key the cursor will not move only one position, but it will move until it gets out of the board.
3.2 (and also your weird cursor movement) happens because you are always redrawing the map, while redrawing should be done only when you actually got some input.
(also _kbhit is not needed, _getch already waits for input)
|
Not homework, simply trying to learn new languages. Also most of the scripters here are also experienced C++ programmers.
My apologies, I haven't ever coded in C++ so that's why there's lots of mistakes. I'm going to fix the moving out of the map logic later.
I did as you told me and now it's okay, but I still don't get why it worked for the guy in the video but doesn't work when I use his code.
Video Link:
https://www.youtube.com/watch?v=W1e5wO7XR2w&t=446s
Code:
PHP Code:
#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;
bool gameOver;
const int width = 20;
const int height = 20;
int x, y, fruitX, fruitY, score;
int tailX[100], tailY[100];
int nTail;
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"); //system("clear");
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 'd':
dir = RIGHT;
break;
case 'w':
dir = UP;
break;
case 's':
dir = DOWN;
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[i];
prev2Y = tailY[i];
tailX[i] = prevX;
tailY[i] = prevY;
prevX = prev2X;
prevY = prev2Y;
}
switch (dir)
{
case LEFT:
x--;
break;
case RIGHT:
x++;
break;
case UP:
y--;
break;
case DOWN:
y++;
break;
default:
break;
}
//if (x > width || x < 0 || y > height || y < 0)
// gameOver = true;
if (x >= width) x = 0; else if (x < 0) x = width - 1;
if (y >= height) y = 0; else if (y < 0) y = height - 1;
for (int i = 0; i < nTail; i++)
if (tailX[i] == x && tailY[i] == y)
gameOver = true;
if (x == fruitX && y == fruitY)
{
score += 10;
fruitX = rand() % width;
fruitY = rand() % height;
nTail++;
}
}
int main()
{
Setup();
while (!gameOver)
{
Draw();
Input();
Logic();
Sleep(10); //sleep(10);
}
return 0;
}
Besides from that, I managed to fix the spamming cursor, but now I'm trying to slow down the process of moving by using Sleep() but I'm not sure why it's not slowing it down
PHP Code:
#include <iostream>
#include <stdlib.h>
#include <conio.h>
#include <ctime>
#include <Windows.h>
using namespace std;
int MapX = 20;
int MapY = 20;
int SnakeX;
int SnakeY;
int FruitX;
int FruitY;
int Score;
bool gameOver;
// Map Over-view
/*
012345678911234567892
0: ########################### --> X
1: # #
2: # #
3: # #
4: # #
5: # #
6: # #
7: # # Y
8: ###########################*/
enum eDirections
{
STOP = 0,
UP,
DOWN,
LEFT,
RIGHT
};
eDirections dir;
void Setup( )
{
srand( time ( 0 ) );
dir = STOP;
SnakeX = MapX / 2;
SnakeY = MapY / 2;
FruitX = rand( ) % MapX;
FruitY = rand( ) % MapY;
Score = 0;
}
void DrawMap( )
{
system( "cls" );
int x, y;
for( y = 0; y < MapY; y++ )
{
if( ( y == 0 || y == MapY - 1 ) ) // first or last row
{
for( x = 0; x < MapX; x++ ) // print X 10 times
{
cout << "#";
}
}
else // 1-9 row
{
cout << "#"; // # which goes to the first space of the line
int spaces;
spaces = 0;
while( spaces != MapX - 1 ) // keep putting spaces till we reach the end of the line - 1
{
if( spaces == SnakeX && y == SnakeY )
{
cout << "O";
}
else if( spaces == FruitX && y == FruitY )
{
cout << "F";
}
else
{
cout << " ";
}
spaces++;
}
cout << "#"; // finally print # at the end of the line
}
cout << endl;
}
}
void Logic( )
{
switch( dir )
{
case LEFT:
{
SnakeX--;
break;
}
case RIGHT:
{
SnakeX++;
break;
}
case UP:
{
SnakeY--;
break;
}
case DOWN:
{
SnakeY++;
break;
}
}
}
int Input( )
{
switch( _getch( ) )
{
case 'a':
{
dir = LEFT;
break;
}
case 'd':
{
dir = RIGHT;
break;
}
case 'w':
{
dir = UP;
break;
}
case 's':
{
dir = DOWN;
break;
}
case 'x':
{
gameOver = true;
dir = STOP;
break;
}
}
Logic();
return 1;
}
int main( )
{
Setup( );
while( ! gameOver )
{
if( Input( ) == 1 )
DrawMap( );
}
return 0;
}
__________________