Grid based game

Non-platform specific questions.

Grid based game

Postby foleyjo » Wed Oct 19, 2011 2:23 pm

Hi all

I want to make a grid based game but I don't know how to do it. I have read the tutorials but don't understand it. Any chance someone could talk me through what I need to do.

I am practicing with a small 3*3 grid which I have uploaded.

For the game I need to be able to click a square of the grid to select it and then view the adjacent squares.

I really don't know where to start
Attachments
Grid1.png
Grid1.png (1.12 KiB) Viewed 2070 times
KISS -Keep It Simple Stoopid
foleyjo
 
Posts: 275
Joined: Mon Jul 09, 2007 1:15 pm
Score: 15 Give a positive score

Re: Grid based game

Postby schnellboot » Wed Oct 19, 2011 2:57 pm

sensors?
schnellboot
 
Posts: 819
Joined: Sat Mar 31, 2007 1:35 pm
Location: Germany
Score: 102 Give a positive score

Re: Grid based game

Postby foleyjo » Wed Oct 19, 2011 3:03 pm

I'm trying to go for the approach on the tutorial pages on the website.

I've made some progress. I can select the squares but it gives me the wrong number.
Attachments
gridtesting.ged
(1.53 KiB) Downloaded 68 times
KISS -Keep It Simple Stoopid
foleyjo
 
Posts: 275
Joined: Mon Jul 09, 2007 1:15 pm
Score: 15 Give a positive score

Re: Grid based game

Postby EvanBlack » Wed Oct 19, 2011 3:05 pm

one of the easiest, to use and implement, is a linked list for accessing a grid.

First you create a structure that holds your data, then you uses pointers from that struct to point to the adjacent squares.

To be able to construct a linked list you must first understand stucts and pointers.

structs are easy to understand and implement. If you have used C++ they are like classes without functions attached.


This is a struct:
Code: Select all
struct myStruct  //define a struct called myStruct
{
    int a;
    int b;
}

struct myStruct Square1; //create a new struct called Square1

Square1.a = 3;  //Give struct variable a the value of 3
Square1.b = 1;  //Give struct variable b the value of 1;



To implement a linked list, you have to understand how to create pointers and allocate memory for structs. You can learn more by searching google for topics: Pointers, structs, and linked lists. You can also use these links below for more information on pointers and linked lists.

A topic on pointers:
viewtopic.php?f=1&t=11162

Example of a linked list:
viewtopic.php?f=1&t=11159&p=77564&hilit=Linked+List#p77564
(\__/) ( Soon... The world)
(O.o )< will be mine!____)
(> < )
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Bunny Overlord 2012!
EvanBlack
 
Posts: 202
Joined: Fri Sep 30, 2011 10:17 am
Score: 19 Give a positive score

Re: Grid based game

Postby foleyjo » Wed Oct 19, 2011 3:16 pm

No offence but now I'm getting more confused.

The tutorial I was reading has nothing about structures and linked lists.
http://game-editor.com/Creating_board/strategy_games

I think I understand how the structure works but making the grid using it is what confuses me.

Using the tutorial I've been working off I've made it so I can click the individual squares but it starts the grid at 22 instead of 0.

Just can't seem to get my head round this I might give up :shock:
KISS -Keep It Simple Stoopid
foleyjo
 
Posts: 275
Joined: Mon Jul 09, 2007 1:15 pm
Score: 15 Give a positive score

Re: Grid based game

Postby jimmynewguy » Wed Oct 19, 2011 4:16 pm

Something like this? I guess I didn't fully get what you meant by:
For the game I need to be able to click a square of the grid to select it and then view the adjacent squares.


If this is for a match 3 type game, then you could just do one mouse down do this pseudocode if(near){switch x,y with selected x,y and selected = 0}
Attachments
Grid.zip
(196.52 KiB) Downloaded 82 times
Working on a probably too ambitious project! Wild-west-adventure-RPG-shooter-thing.
User avatar
jimmynewguy
 
Posts: 1137
Joined: Sat Mar 31, 2007 6:27 pm
Score: 89 Give a positive score

Re: Grid based game

Postby foleyjo » Wed Oct 19, 2011 4:23 pm

Thanks Jimmy that is exactly what I'm looking for with my request.

Now all I need to do is understand what you have done so I can make it into a game
KISS -Keep It Simple Stoopid
foleyjo
 
Posts: 275
Joined: Mon Jul 09, 2007 1:15 pm
Score: 15 Give a positive score

Re: Grid based game

Postby foleyjo » Wed Oct 19, 2011 4:29 pm

Actually looking at what you have done it might not be the best method.
The method you have used with the clones is fine for the small grid and is probably the method I will end up using.

However the tutorial I've been looking at seems to allow for making any size grid much easier.

I'm going to study your code for a bit but I do want to know why the code I used in the .ged I uploaded starts the grid at 22.

Code: Select all
int findcell(int xx, int yy) //returns the current cell
{
int vcell=round((xx-Grids.x)/50);
int hcell=round((yy-Grids.y)/50);
int mycell=vcell+(hcell*3);
return mycell;
}


This is for a 3*3 grid with each square having a size of 50x50


Sorry for being persistent with this but for me to make my next game work I really need to understand it
KISS -Keep It Simple Stoopid
foleyjo
 
Posts: 275
Joined: Mon Jul 09, 2007 1:15 pm
Score: 15 Give a positive score

Re: Grid based game

Postby EvanBlack » Wed Oct 19, 2011 5:04 pm

Slow down :)

Honestly, that code confuses me.

But thats why I am trying to help you learn to implement a method that will be easier to handle and work with.

This isn't exactly a linked list, but its a linked set of structs to help you understand a linked list.

With a linked list you just create your squares such as

Code: Select all

typedef struct Square
{
    Actor* mySquareActor;
    int SquareNumber;
    struct Square* TopSquare;
    struct Square* BottomSquare;
    struct Square* LeftSquare;
    struct Square* RightSquare;
} Square;


That would be your square. It takes one actor which you create, then you assign the actor to the square, then you tell the square left right up or down.

The code for that looks like this:

Code: Select all

int Position = 4; // Start at the middle square

// Create all the square structures

Square TopLeftSquare, TopMiddleSquare, TopRightSquare,
      CenterLeftSquare, CenterMiddleSquare, CenterRightSquare,
      BottomLeftSquare, BottomMiddleSquare, BottomRightSquare;


// Put all the squares into an array for later use.

Square grid[9] = { TopLeftSquare, TopMiddleSquare, TopRightSquare,
                  CenterLeftSquare, CenterMiddleSquare, CenterRightSquare,
                  BottomLeftSquare, BottomMiddleSquare, BottomRightSquare };


void CreateGrid()
{

    //Top portion of the grid
    TopLeftSquare.SquareNumber = 0;     // Programmer's count starts at 0
    TopLeftSquare.TopSquare = NULL;     //Doesn't point to a square because there isn't one there
    TopLeftSquare.LeftSquare = NULL;
    TopLeftSquare.RightSquare = &TopMiddleSquare;
    TopLeftSquare.BottomSquare = &CenterLeftSquare;

    TopMiddleSquare.SquareNumber = 1;
    TopMiddleSquare.TopSquare = NULL;
    TopMiddleSquare.LeftSquare = &TopLeftSquare;
    TopMiddleSquare.RightSquare = &TopRightSquare;
    TopMiddleSquare.BottomSquare = &CenterMiddleSquare;

    TopRightSquare.SquareNumber = 2;
    TopRightSquare.TopSquare = NULL;
    TopRightSquare.RightSquare = NULL;
    TopRightSquare.LeftSquare = &TopMiddleSquare;
    TopRightSquare.BottomSquare = &CenterRightSquare;

    // Center Portion of the grid

    CenterLeftSquare.SquareNumber = 3;
    CenterLeftSquare.LeftSquare = NULL;
    CenterLeftSquare.TopSquare = &TopLeftSquare;
    CenterLeftSquare.RightSquare = &CenterMiddleSquare;
    CenterLeftSqaure.BottomSquare = &BottomLeftSquare;


    CenterMiddleSquare.SquareNumber = 4;
    CenterMiddleSquare.TopSquare = &TopMiddleSquare;
    CenterMiddleSquare.LeftSquare = &CenterLeftSquare;
    CenterMiddleSquare.BottomSquare = &BottomMiddleSquare;
    CenterMiddleSquare.RightSquare = &CenterRightSquare;


    CenterRightSquare.SquareNumber = 5;
    CenterRightSquare.RightSquare = NULL;
    CenterRightSquare.TopSquare = &TopRightSquare;
    CenterRightSquare.RightSquare = &CenterMiddleSquare;
    CenterRightSqaure.BottomSquare = &BottomRightSquare;

    //Bottom portion

    BottomLeftSquare.SquareNumber = 6;
    BottomLeftSquare.BottomSquare = NULL;
    BottomLeftSquare.LeftSquare = NULL;
    BottomLeftSquare.RightSquare = &BottomMiddleSquare;
    BottomLeftSquare.TopSquare = &CenterLeftSquare;

    BottomMiddleSquare.SquareNumber = 7;
    BottomMiddleSquare.BottomSquare = NULL;
    BottomMiddleSquare.LeftSquare = &BottomLeftSquare;
    BottomMiddleSquare.RightSquare = &BottomRightSquare;
    BottomMiddleSquare.TopSquare = &CenterMiddleSquare;

    BottomRightSquare.SquareNumber = 8;
    BottomRightSquare.BottomSquare = NULL;
    BottomRightSquare.RightSquare = NULL;
    BottomRightSquare.LeftSquare = &BottomMiddleSquare;
    BottomRightSquare.TopSquare = &CenterRightSquare;



Now that all your squares are connected you just need to make functions to navigate them.

Code: Select all

void MoveUp()
{
    if(grid[Position]->TopSquare != NULL)
    {
        Position = grid[Position]->TopSquare->SquareNumber;
    }
}

void MoveDown()
{
    if(grid[Position]->BottomSquare != NULL)
    {
        Position = grid[Position]->BottomSquare->SquareNumber;
    }
}

void MoveLeft()
{
    if(grid[Position]->LeftSquare != NULL)
    {
        Position = grid[Position]->LeftSquare->SquareNumber;
    }
}

void MoveRight()
{
    if(grid[Position]->RightSquare != NULL)
    {
        Position = grid[Position]->RightSquare->SquareNumber;
    }
}


Now you have your grid, you have your move functions, and you know what square you are in at all times.


To make it where your grid is attached to actors just takes a little bit more work in adding actors and having the grid positions point to those actors. I can create an example to help you understand this.


Also, with linked lists you can create grids of any size and they are fast and easy to implement and are fast to access. The issue is that you have to understand how to allocate memory for structs in order to create a linked list.

Here is an example loop that would allocate memory for the struct above.

Code: Select all
int MapHeight = 100;   // Change to any value
int MapWidth  = 100;

Square *** Grid;  //This is a triple pointer to create a 2D array for a grid of squares.

int AllocateMemoryForGrid()
{
    int i, a;
    //Allocate memory for 2D array Grid
    Grid = malloc(MapHeight * sizeof(Square **));
    if(Grid == NULL)
    {
        //ERROR: OUT OF MEMORY
   return 1;
    }
    for(i = 0; i < MapHeight; i++)
    {
        Grid[i] = malloc(MapWidth * sizeof(Square*));
   if(Grid[i] == NULL)
   {
            //ERROR: OUT OF MEMORY
            return 1;
   }
 
    }
    for(i = 0; i < MapHeight; i++)
    {
        for(a = 0; a < MapWidth; a++)
   {
       Grid[i][a] = (Square *)malloc(sizeof(Square));
       if(Grid[i][a] == NULL)
            {
                //ERROR: OUT OF MEMORY
                return 1;
            }
        }
    }
    return 0;
}


Then you just need to create other functions to set the variables and navigate the list just by going through the pointers.

Then when your finished with it, free the memory:
Code: Select all
void FreeGrid()
{
    for(i = 0; i < MapHeight; i++)
    {
        for(a = 0; a < MapWidth; a++)
        {
            free(Grid[i][a]);
        }
        free(Grid[i]);
    }
}
Last edited by EvanBlack on Wed Oct 19, 2011 5:22 pm, edited 2 times in total.
(\__/) ( Soon... The world)
(O.o )< will be mine!____)
(> < )
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Bunny Overlord 2012!
EvanBlack
 
Posts: 202
Joined: Fri Sep 30, 2011 10:17 am
Score: 19 Give a positive score

Re: Grid based game

Postby foleyjo » Wed Oct 19, 2011 5:18 pm

Thanks Evan.

I'll scrap what I've read on the tutorial and work with your method.
Hopefully learn some more about structs and linked lists in the process.

Running out of time now so I'll have to work on it tomorrow but will let you know how I get on.
KISS -Keep It Simple Stoopid
foleyjo
 
Posts: 275
Joined: Mon Jul 09, 2007 1:15 pm
Score: 15 Give a positive score

Re: Grid based game

Postby jimmynewguy » Wed Oct 19, 2011 5:20 pm

Evan's method probably is better than mine by far. But just to let you know, the size of the grid would not matter for the code I showed you. You just have to make clones and then place them in a grid. Works with the whole screen filled with squares, or even non square shaped grids. So I'm not sure what you meant, but oh well :) Good luck.
Working on a probably too ambitious project! Wild-west-adventure-RPG-shooter-thing.
User avatar
jimmynewguy
 
Posts: 1137
Joined: Sat Mar 31, 2007 6:27 pm
Score: 89 Give a positive score

Re: Grid based game

Postby EvanBlack » Wed Oct 19, 2011 5:29 pm

If you really want something to study about linked lists and grids take a look at my map editor. It has a bunch of code in which I use several grids and linked lists that makes a web of linked structures.

It could be a bit intimidating but if you look at it and poke through it, you might get a feel and understanding of how it all fits and is manipulated.

But you can also create smaller linked lists too, but the thing is, once you have the functions made for it, you don't really have to look at them again.
(\__/) ( Soon... The world)
(O.o )< will be mine!____)
(> < )
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Bunny Overlord 2012!
EvanBlack
 
Posts: 202
Joined: Fri Sep 30, 2011 10:17 am
Score: 19 Give a positive score

Re: Grid based game

Postby foleyjo » Wed Oct 19, 2011 5:34 pm

Sorry Jimmy what I meant was rather than have lots of clones I was looking at using 1 large actor with the grid on it and then the code works out which square of the grid you are clicking on.
So the code I had made using the tutorial I was looking at allows me to select any of the squares on the image and give a different number. However the problem I was having was it started at 22 rather than 0

Just thought that might be easier if I decided to change something like the resolution of the game. So I didn't have to go and replace all the squares. I could just resize the entire grid.

What I did like with your method though was the idea of non square shaped grids.

Evan I looked at your map editor when you first uploaded it. It felt a bit too advanced for me :lol: . I tend to learn better when I'm trying to accomplish something.
KISS -Keep It Simple Stoopid
foleyjo
 
Posts: 275
Joined: Mon Jul 09, 2007 1:15 pm
Score: 15 Give a positive score

Re: Grid based game

Postby EvanBlack » Wed Oct 19, 2011 6:02 pm

Just to let you know, linked lists allow for many dimensions of travel, including 10,000 degree travel. Which means you can travel in 10,000 different directions. Its not even limited to that either.

A linked list is like an invisible grid that floats in your computer. It could even be a linear path in which you can use it to drive a story line. Even a multi-directional story line with conditions.

For what you want, with a single actor, you would just have to have 4 values in the structure, or 2 if you're good with math ;)


the 4 value method would be Height, Width, Top right Corner, Bottom left corner. This way you get the mouse position and relate it to list.
(\__/) ( Soon... The world)
(O.o )< will be mine!____)
(> < )
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Bunny Overlord 2012!
EvanBlack
 
Posts: 202
Joined: Fri Sep 30, 2011 10:17 am
Score: 19 Give a positive score

Re: Grid based game

Postby jimmynewguy » Wed Oct 19, 2011 6:16 pm

I don't like you anymore Evan. You're two too much better than me. :P And yes, two.

EDIT: Now I shall read! http://cslibrary.stanford.edu/103/LinkedListBasics.pdf
Working on a probably too ambitious project! Wild-west-adventure-RPG-shooter-thing.
User avatar
jimmynewguy
 
Posts: 1137
Joined: Sat Mar 31, 2007 6:27 pm
Score: 89 Give a positive score

Next

Return to General

Who is online

Users browsing this forum: No registered users and 1 guest