Grid based movement in Snake Game C++ - c++

I have basic knowledge of c++ and am trying to create a simple Snake game that will run on a 84 x 48 pixel LCD screen. The game works fine, but there is one thing I have been stuck on for a while, and I haven't found any help in any previous post:
so far, the movement of the snake is free, i.e. it moves pixel by pixel, meaning that, the snake being composed of 3 x 3 pixel squares, most of the times it doesn't automatically align with the fruit. I would like to implement a "grid system", so turn my 84 x 48 game space into a 28 x 16 grid in which the snake moves and eats the fruit without the player having to adjust its trajectory many times before getting the right alignment with the fruit.
Here's the part of my code in which I print the snake to the screen:
void Snake::head_init(int size)
{
_size = size;
_x = (GRIDX/2 - _size/2)*_size;
_y = (GRIDY/2 - _size/2)*_size;
printf("Snake initially at x = %i, y = %i\n", _x,_y);
}
void Snake::draw(N5110 &lcd)
{
lcd.drawRect(_x,_y,_size,_size, FILL_BLACK); // draw head
for(int k = 0; k < _tail_length; k++) {
lcd.drawRect(_tail_x[k],_tail_y[k],_size,_size, FILL_BLACK); // print tail
}
}
GRIDX = 28, GRIDY = 16, _size = 3
I tried to multiply _x and _y by the size of the snake when printing it, but this doesn't give me the result I am aiming to.
Also, I would like the fruit to spawn in the same 28 x 16 grid.
Here is how my fruit is printed for now:
void Fruit::init(int size)
{
_size = size;
srand(time(NULL)); // initialise random number generator for fruit positions
_x = rand() % 80 + 1; // generate a random x coordinate for fruit within the frame x range
_y = rand() % 43 + 1; // generate a random y coordinate for fruit within the frame y range
printf("Fruit initially at x = %i, y = %i \n", _x, _y); // print the random fruit coordinates over terminal
}
void Fruit::draw(N5110 &lcd)
{
lcd.drawSprite(_x,_y,3,3,(int *)fruit); // draw fruit at random position
}
Any help would be very appreciated

Related

Graphing issues in data output

I have a program that is taking input from an external source (right now, a joystick), and then plotting it on a graph. The graph displays the last 60 frames of data, which is about 1-2 seconds.
here is the data input:
nextDataPoint(double x){
if (x > max){ max = x; }
if (x < min){ min = x; }
dataInput.enqueue(x) //dataInput is a QQueue<double>
while (dataInput.size() > 60){
dataInput.dequeue();
}
update(); //this triggers the paint event
}
here is the graphing function
graph function:
//this draws the min line and the max line
QPainter painter(this);
int lineDist = 25;
QPen myPen(Qt::black, 3);
QPoint maxText(10,20);
painter.drawText(maxText, "max");
QPoint maxLineLeft(0, lineDist);
QPoint maxLineRight(width(), lineDist);
painter.drawLine(maxLineLeft, maxLineRight);
QPoint minText(10, height()-10);
painter.drawText(minText, "min");
QPoint minLineLeft(0, height()-lineDist);
QPoint minLineRight(width(), height()-lineDist);
//this draws the actual graph
myPen.setColor(Qt::blue);
myPen.setWidth(2);
painter.setPen(myPen);
double dist = (double)(heigh() - 2*lineDist);
int stepSize = (int)((double)width() / 60.0);
int heightStep = (max-min)/dist;
double x;
QPoint lastPoint(0,0);
QPoint nextPoint(0,0);
int i = 0;
if (!dataInput.empty()){ //checks that there is data
if (dataInput.size() < 60) { //ignoring for sake of brevety
} else {
x = dataInput.at(i);
x = max - x; // this inverts the data, necessary because (0,0) is the upper left corner
x = (x-min)/heightStep;
nextPoint.setX(0);
nextPoint.setY(x+lineDist);
for (i = 1; i < 60; i++){
x = dataInput.at(i);
x = max - x;
x = (x-min)/heightStep;
lastPoint = nextPoint;
nextPoint.setX(i*stepSize);
nextPoint.setY(x+lineDist);
painter.drawLine(lastPoint, nextPoint);
}
}
}
So, with one joystick that I am testing with, this program works without issue. As I move the joystick, the program draws the blue line within the bounds listed on the screen. x is input as a number between 0 and 65535, and after moving the joystick around a little bit the system recognizes that as the max and the min, and then proceeds to function as expected.
The second "joystick" is less of a joystick and more of a pressure sensor. This outputs either x = 1023 or x is some number between 5 and 9. In this case however, the blue line appears on the same level as the max line, or when the data output is x in the single digits, the blue line appears below min. I haven't been able to figure out why this would be the case, the first joystick can also produce results of 0, but it never goes below the minimum line. What might be causing this particular issue?
Two changes were needed to correct the issues.
First, stepSize and heightStep were both changed from int to double.
second, the section
x = dataInput.at(i);
x = max - x;
x = (x-min)/heightStep;
nextPoint.setX(0);
nextPoint.setY(x+lineDist);
for (i = 1; i < 60; i++){
x = dataInput.at(i);
x = max - x;
x = (x-min)/heightStep;
lastPoint = nextPoint;
nextPoint.setX(i*stepSize);
nextPoint.setY(x+lineDist);
painter.drawLine(lastPoint, nextPoint);
}
was changed to:
x = dataInput.at(i);
x = (x-min)/heightStep;
nextPoint.setX(0);
nextPoint.setY(dist-x+lineDist);
for (i = 1; i < 60; i++){
x = dataInput.at(i);
x = (x-min)/heightStep;
lastPoint = nextPoint;
nextPoint.setX(i*stepSize);
nextPoint.setY(dist-x+lineDist);
painter.drawLine(lastPoint, nextPoint);
}
This solved the issue and made the system always draw the data between the min and max lines.

C++ rendering shapes

I'm trying to generate shapes within a specific area. the game i am making has a resolution of 1024x768 but the game board occupies only 768x768 in the centre of the screen.
Currently, i can get the shapes to randomly generate and not exceed the MAXIMUM_WIDTH and MAXIMUM_HEIGHT constraints. However, the shapes can still render too far left which is off the game board. I tried adding MINIMUM_WIDTH and MINUMUM_HEIGHT but i'm not sure where to apply this in the x= and y= stage
I've included the relevant code below.
What can i add/modify to this code to make the shapes be limited to the game board?
Thanks.
const size_t MaxShapes {5};
const unsigned int MaxScale {5};
const unsigned int MINIMUM_WIDTH = 128;
const unsigned int MAXIMUM_WIDTH = 896;
const unsigned int MINUMUM_HEIGHT = 0;
const unsigned int MAXIMUM_HEIGHT = 768;
bool PlayState::onCreate()
{
snakes_.push_back(new AISnake);
snakes_.back()->setPosition(Position(40,40));
snakes_.push_back(new PlayerSnake);
snakes_.back()->setPosition(Position(20,20));
double x, y;
for(unsigned shape = 0;shape < MaxShapes;shape++)
{
x = (double)(rand() % MAXIMUM_WIDTH);
y = (double)(rand() % MAXIMUM_HEIGHT);
shapes_.push_back(Triangle({x, y}));
}
return true;
}
You could try
x = (double)((rand() % (MAXIMUM_WIDTH - MINIMUM_WIDTH)) + MINIMUM_WIDTH);
y = (double)((rand() % (MAXIMUM_HEIGHT - MINIMUM_HEIGHT)) + MINIMUM_HEIGHT);
Say you want to find a random number between 5 and 15. The difference between 5 and 15 is 10. So you first find a random number between 0 and 10 and then add that to 5 to bring it back into the proper range (between 5 and 15).

Determine which tile is clicked in a window

I am drawing a tilemap on a SFML renderwindow. I want to determine which tile is clicked by the user, but I just cant seem to find a solution. First of all, each tile has 32 width and height.
What i try at the moment : Get the position of the click. Loop trough the tilemap until a tile is found which position is between 100. So if I click on (100,100) the tile should begin at (96,96) but this does not seem to work.
Here is my code snippet from the function getTile(mousepos x,mousepos y)
Tile* TileMap::getTile(int x, int y)
{
Tile *t = NULL;
for(int i = 0; i < tilemap.size(); i++)
{
for(int j = 0; j < tilemap[i].size(); j++)
{
if(x > tilemap[i][j].sprite.getPosition().x
&& x < (tilemap[i][j].sprite.getPosition().x+32))
{
if(y > tilemap[i][j].sprite.getPosition().y
&& y < (tilemap[i][j].sprite.getPosition().y+32))
{
t = &tilemap[i][j];
break;
}
}
}
}
return t;
}
Based on your code, I am going to assume that you are basing your tilemap on a 2d array of Tiles: tilemap[x][y]. I am also going to assume that tilemap[0][0] is the top left tile.
There should be a much easier way to find out which tile is being clicked on instead of testing every single tile.
If you are at 100,100 and tiles are 32x32, then we can get the x and y of the tile within the tilemap by doing something as simple as:
x = 100 / 32 = 3
y = 100 / 32 = 3
Therefor the tile in your tilemap that corresponds to a mouse position of (100,100) is tilemap[3][3].

Optimizing a simple 2D Tile engine (+potential bugfix)

Preface
Yes, there is plenty to cover here... but I'll do my best to keep this as well-organized, informative and straight-to-the-point as I possibly can!
Using the HGE library in C++, I have created a simple tile engine.
And thus far, I have implemented the following designs:
A CTile class, representing a single tile within a CTileLayer, containing row/column information as well as an HGE::hgeQuad (which stores vertex, color and texture information, see here for details).
A CTileLayer class, representing a two-dimensional 'plane' of tiles (which are stored as a one-dimensional array of CTile objects), containing the # of rows/columns, X/Y world-coordinate information, tile pixel width/height information, and the layer's overall width/height in pixels.
A CTileLayer is responsible for rendering any tiles which are either fully or partially visible within the boundaries of a virtual camera 'viewport', and to avoid doing so for any tiles which are outside of this visible range. Upon creation, it pre-calculates all information to be stored within each CTile object, so the core of engine has more room to breathe and can focus strictly on the render loop. Of course, it also handles proper deallocation of each contained tile.
Issues
The problem I am now facing essentially boils down to the following architectural/optimization issues:
In my render loop, even though I am not rendering any tiles which are outside of visible range, I am still looping through all of the tiles, which seems to have a major performance impact for larger tilemaps (i.e., any thing above 100x100 rows/columns # 64x64 tile dimensions still drops the framerate down by 50% or more).
Eventually, I intend to create a fancy tilemap editor to coincide with this engine.
However, since I am storing all two-dimensional information inside one or more 1D arrays, I don't have any idea how possible it would be to implement some sort of rectangular-select & copy/paste feature, without some MAJOR performance hit -- involving looping through every tile twice per frame. And yet if I used 2D arrays, there would be a slightly less but more universal FPS drop!
Bug
As stated before... In my render code for a CTileLayer object, I have optimized which tiles are to be drawn based upon whether or not they are within viewing range. This works great, and for larger maps I noticed only a 3-8 FPS drop (compared to a 100+ FPS drop without this optimization).
But I think I'm calculating this range incorrectly, because after scrolling halfway through the map you can start to see a gap (on the topmost & leftmost sides) where tiles aren't being rendered, as if the clipping range is increasing faster than the camera can move (even though they both move at the same speed).
This gap gradually increases in size the further along into the X & Y axis you go, eventually eating up nearly half of the top & left sides of the screen on a large map.
My render code for this is shown below...
Code
//
// [Allocate]
// For pre-calculating tile information
// - Rows/Columns = Map Dimensions (in tiles)
// - Width/Height = Tile Dimensions (in pixels)
//
void CTileLayer::Allocate(UINT numColumns, UINT numRows, float tileWidth, float tileHeight)
{
m_nColumns = numColumns;
m_nRows = numRows;
float x, y;
UINT column = 0, row = 0;
const ULONG nTiles = m_nColumns * m_nRows;
hgeQuad quad;
m_tileWidth = tileWidth;
m_tileHeight = tileHeight;
m_layerWidth = m_tileWidth * m_nColumns;
m_layerHeight = m_tileHeight * m_nRows;
if(m_tiles != NULL) Free();
m_tiles = new CTile[nTiles];
for(ULONG l = 0; l < nTiles; l++)
{
m_tiles[l] = CTile();
m_tiles[l].column = column;
m_tiles[l].row = row;
x = (float(column) * m_tileWidth) + m_offsetX;
y = (float(row) * m_tileHeight) + m_offsetY;
quad.blend = BLEND_ALPHAADD | BLEND_COLORMUL | BLEND_ZWRITE;
quad.tex = HTEXTURE(nullptr); //Replaced for the sake of brevity (in the engine's code, I used a globally allocated texture array and did some random tile generation here)
for(UINT i = 0; i < 4; i++)
{
quad.v[i].z = 0.5f;
quad.v[i].col = 0xFF7F7F7F;
}
quad.v[0].x = x;
quad.v[0].y = y;
quad.v[0].tx = 0;
quad.v[0].ty = 0;
quad.v[1].x = x + m_tileWidth;
quad.v[1].y = y;
quad.v[1].tx = 1.0;
quad.v[1].ty = 0;
quad.v[2].x = x + m_tileWidth;
quad.v[2].y = y + m_tileHeight;
quad.v[2].tx = 1.0;
quad.v[2].ty = 1.0;
quad.v[3].x = x;
quad.v[3].y = y + m_tileHeight;
quad.v[3].tx = 0;
quad.v[3].ty = 1.0;
memcpy(&m_tiles[l].quad, &quad, sizeof(hgeQuad));
if(++column > m_nColumns - 1) {
column = 0;
row++;
}
}
}
//
// [Render]
// For drawing the entire tile layer
// - X/Y = world position
// - Top/Left = screen 'clipping' position
// - Width/Height = screen 'clipping' dimensions
//
bool CTileLayer::Render(HGE* hge, float cameraX, float cameraY, float cameraTop, float cameraLeft, float cameraWidth, float cameraHeight)
{
// Calculate the current number of tiles
const ULONG nTiles = m_nColumns * m_nRows;
// Calculate min & max X/Y world pixel coordinates
const float scalarX = cameraX / m_layerWidth; // This is how far (from 0 to 1, in world coordinates) along the X-axis we are within the layer
const float scalarY = cameraY / m_layerHeight; // This is how far (from 0 to 1, in world coordinates) along the Y-axis we are within the layer
const float minX = cameraTop + (scalarX * float(m_nColumns) - m_tileWidth); // Leftmost pixel coordinate within the world
const float minY = cameraLeft + (scalarY * float(m_nRows) - m_tileHeight); // Topmost pixel coordinate within the world
const float maxX = minX + cameraWidth + m_tileWidth; // Rightmost pixel coordinate within the world
const float maxY = minY + cameraHeight + m_tileHeight; // Bottommost pixel coordinate within the world
// Loop through all tiles in the map
for(ULONG l = 0; l < nTiles; l++)
{
CTile tile = m_tiles[l];
// Calculate this tile's X/Y world pixel coordinates
float tileX = (float(tile.column) * m_tileWidth) - cameraX;
float tileY = (float(tile.row) * m_tileHeight) - cameraY;
// Check if this tile is within the boundaries of the current camera view
if(tileX > minX && tileY > minY && tileX < maxX && tileY < maxY) {
// It is, so draw it!
hge->Gfx_RenderQuad(&tile.quad, -cameraX, -cameraY);
}
}
return false;
}
//
// [Free]
// Gee, I wonder what this does? lol...
//
void CTileLayer::Free()
{
delete [] m_tiles;
m_tiles = NULL;
}
Questions
What can be done to fix those architectural/optimization issues, without greatly impacting any other rendering optimizations?
Why is that bug occurring? How can it be fixed?
Thank you for your time!
Optimising the iterating of the map is fairly straight forward.
Given a visible rect in world coordinates (left, top, right, bottom) it's fairly trivial to work out the tile positions, simply by dividing by the tile size.
Once you have those tile coordinates (tl, tt, tr, tb) you can very easily calculate the first visible tile in your 1D array. (The way you calculate any tile index from a 2D coordinate is (y*width)+x - remember to make sure the input coordinate is valid first though.) You then just have a double for loop to iterate the visible tiles:
int visiblewidth = tr - tl + 1;
int visibleheight = tb - tt + 1;
for( int rowidx = ( tt * layerwidth ) + tl; visibleheight--; rowidx += layerwidth )
{
for( int tileidx = rowidx, cx = visiblewidth; cx--; tileidx++ )
{
// render m_Tiles[ tileidx ]...
}
}
You can use a similar system for selecting a block of tiles. Just store the selection coordinates and calculate the actual tiles in exactly the same way.
As for your bug, why do you have x, y, left, right, width, height for the camera? Just store camera position (x,y) and calculate the visible rect from the dimensions of your screen/viewport along with any zoom factor you have defined.
This is a pseudo codish example, geometry variables are in 2d vectors. Both the camera object and the tilemap has a center-position and a extent (half size). The math is just the same even if you decide to stick with pure numbers. Even if you don't use center coordinates and extent, perhaps you'll get an idea on the math. All of this code is in the render function, and is rather simplified. Also, this example assume you already got a 2D array -like object that holds the tiles.
So, first a full example, and I'll explain each part further down.
// x and y are counters, sx is a placeholder for x start value as x will
// be in the inner loop and need to be reset each iteration.
// mx and my will be the values x and y will count towards too.
x=0,
y=0,
sx=0,
mx=total_number_of_tiles_on_x_axis,
my=total_number_of_tiles_on_y_axis
// calculate the lowest and highest worldspace values of the cam
min = cam.center - cam.extent
max = cam.center + cam.extent
// subtract with tilemap corners and divide by tilesize to get
// the anount of tiles that is outside of the cameras scoop
floor = Math.floor( min - ( tilemap.center - tilemap.extent ) / tilesize)
ceil = Math.ceil( max - ( tilemap.center + tilemap.extent ) / tilesize)
if(floor.x > 0)
sx+=floor.x
if(floor.y > 0)
y+=floor.y
if(ceil.x < 0)
mx+=ceil.x
if(ceil.y < 0)
my+=ceil.y
for(; y<my; y++)
// x need to be reset each y iteration, start value are stored in sx
for(x=sx; x<mx; x++)
// render tile x in tilelayer y
Explained bit by bit. First thing in the render function, we will use a few variables.
// x and y are counters, sx is a placeholder for x start value as x will
// be in the inner loop and need to be reset each iteration.
// mx and my will be the values x and y will count towards too.
x=0,
y=0,
sx=0,
mx=total_number_of_tiles_on_x_axis,
my=total_number_of_tiles_on_y_axis
To prevent rendering all tiles, you need to provide either a camera-like object or information on where the visible area starts and stops (in worldspace if the scene is movable)
In this example I'm providing a camera object to the render function which has a center and an extent stored as 2d vectors.
// calculate the lowest and highest worldspace values of the cam
min = cam.center - cam.extent
max = cam.center + cam.extent
// subtract with tilemap corners and divide by tilesize to get
// the anount of tiles that is outside of the cameras scoop
floor = Math.floor( min - ( tilemap.center - tilemap.extent ) / tilesize)
ceil = Math.ceil( max - ( tilemap.center + tilemap.extent ) / tilesize)
// floor & ceil is 2D vectors
Now, if floor is higher than 0 or ceil is lower than 0 on any axis, it means that there just as many tiles outside of the camera scoop.
// check if there is any tiles outside to the left or above of camera
if(floor.x > 0)
sx+=floor.x// set start number of sx to amount of tiles outside of camera
if(floor.y > 0)
y+=floor.y // set startnumber of y to amount of tiles outside of camera
// test if there is any tiles outisde to the right or below the camera
if(ceil.x < 0)
mx+=ceil.x // then add the negative value to mx (max x)
if(ceil.y < 0)
my+=ceil.y // then add the negative value to my (max y)
A normal render of the tilemap would go from 0 to number of tiles that axis, this using a loop within a loop to account for both axis. But thanks to the above code x and y will always stick to the space within the border of the camera.
// will loop through only the visible tiles
for(; y<my; y++)
// x need to be reset each y iteration, start value are stored in sx
for(x=sx; x<mx; x++)
// render tile x in tilelayer y
Hope this helps!

Giant Char or multiple Chars for console game map?

I'm a pretty novice c++ coder and I am starting to make a console adventure game.
My adventure game currently consists of a player character that walks around inside a console application window with an 80 character width x 40 lines.
I am not sure how to approach storing the maps for my game. Each map will consist of 80 x 40 ASCII characters with colour attributes.
Should I store each 80 x 40 map in its own char? so a single map would look something like...
int cHeight = 5; // Reduced size for this example
int cHeight = 10; // Reduced size for this example
// Set up the characters:
char map[cHeight][cWidth+1] = {
"1234567890",
"1234567890",
"1234567890",
"1234567890",
"1234567890",
};
CHAR_INFO mapA[cWidth * cHeight];
for (int y = 0; y < cHeight; ++y) {
for (int x = 0; x < cWidth; ++x) {
mapA[x + cWidth * y].Char.AsciiChar = map[y][x];
mapA[x + cWidth * y].Attributes = FOREGROUND_BLUE | Black; //I have an enum setup with background colours.
}
}
// Set up the positions:
COORD charBufSize = {cWidth,cHeight};
COORD characterPos = {0,0};
SMALL_RECT writeArea = {0,0,cWidth-1,cHeight-1};
// Write the characters:
WriteConsoleOutputA(wHnd, mapA, charBufSize, characterPos, &writeArea);
Im not sure if this is entirely the correct way to display the characters but I didn't think it was a good idea to just cout every character in the for loop.
So.. lets say my console window (in the above code) is 10 characters wide and 5 lines high.
In the above code I have a single map in the Char, so when loading each map I would put each one in their own array.
I was thinking of putting the entire map into a single Char, but then only displaying what I needed by offsetting the x and y in the for loop.
mapA[x + cWidth * y].Char.AsciiChar = map[y+offset][x+offset];
So the map would look more like this;
char map[cHeight][cWidth+1] = {
"1234567890ABCDEFGHIJ",
"1234567890ABCDEFGHIJ",
"1234567890ABCDEFGHIJ",
"1234567890ABCDEFGHIJ",
"1234567890ABCDEFGHIJ",
};
with the offset I could display '1234567890' on 5 rows separately from 'ABCDEFGHIJ' on 5 rows.
So in short I would like to know the most effective way to do this, should I have multiple Chars? Should I create a class? then I could store the characters an colours? (class' are still new to me in c++).
Should I draw the terrain only in the map and then add objects (houses, trees)?
Or just draw it all in the map manually?
I think I've just thought about this too long and need a bit of direction
Thanks!
The way I would do it would be to create a map of
Node* map[height][width]
This means you would create the map which are pointers to Node* elements and you could define the Node* element to be...
class Node{
char displayCharacter;
int posx,poxy
unsigned int r; //red
unsigned int g; //green
unsigned int b; //blue
unsigned int a; //alpha
display(); // This function will know how to display a node using the colour etc
};
Then you could for example if you wanted to create a house you would give it the center point of the model etc... to draw to a function
void createHouse(Node* center)
{
if((center->posh > 0)&&(center->posh< maxheight))
{
if(map[center->posy-1][center->posx]!=NULL)
{
map[center->posy-1][center->posx]->displayCharacter = '_';
map[center->posy-1][center->posx]->r = 255;
}
}
}
Then in main you would have something like...
while(true)
{
for(int i=0; i<maxheight; i++)
{
for(int j=0; j< maxwidth; j++)
{
map[i][j]->Display();
}
}
}
I hope all this sample code is of help to you and answered your question. I have not debugged or looked for any syntax errors. If there any errors in the code, you will have to fix them!
Good luck to you!