Step One - Preparation
Before getting into the making of a hexagonal map generation script, you will need to create a sprite and two simple objects.
The image for the sprite should be a single 32 by 32 hexagon. If you do not have your own hexagon sprite image you can use the one supplied below,
i222.photobucket.com/albums/dd180/Keydigit/hexagon.png
Feel free to use it freely in your commercial or non-commercial games.
Create a new sprite and use either your own image or the one provided by this tutorial, and call it spr_Hex.
Next, create an object called obj_Hex and set its sprite to the hexagon sprite you just made a minute ago called spr_Hex.
Once you have created obj_Hex, make another object and call it obj_Generator. This will be the object that will execute the map generator script you will make in the next step. Before you go there however, add this GML source code to the creation event of the new obj_Generator,
// Execute the Script to make a Grid
Generate_Grid(48, 48, 32, 32);
Save the changes and add obj_Generator to your room.
Step Two - Script Creation
Now comes the actual hexagonal map making script. Create a new script and call it Generate_Grid. Either type or copy and paste in the following script,
// Generate_Grid(width, height, hex_width, hex_height);
var width, height, hex_object;
width = argument0;
height = argument1;
hex_width = argument2;
hex_height = argument3;
// Get the total amount of tiles
tiles = width * height;
// Go through and create each tile
ay = 0;
ax = hex_width * -1;
zx = 0;
use_mod = false;
x_mod = hex_width / 2;
y_mod = x_mod / 2;
for (b = 1; b {
if(zx = width)
{
ax = 0;
zx = 1;
ay= ay + hex_height - y_mod;
use_mod = !use_mod;
}
else
{
ax = ax + hex_width;
zx+=1;
}
if(use_mod)
obj_temp = instance_create(ax + x_mod, ay, obj_Hex)
else
obj_temp = instance_create(ax, ay, obj_Hex)
}
Make sure to save the script. The comments inside the script give a brief explanation as to the operations performed in the script.
Conclusion
The newly created Generate_Grid script will create a hexagonal grid, using some basic math, with the supplied dimensions. It takes the width and the height of the desired grid, as well as the width and height of the hexagonal sprite that you are using for the obj_Hex object as parameters. Whenever you wish to create a hexagonal grid just call the Generate_Grid function and pass the desired parameters.
NOTE:
It is typical in the Game Maker community to ask for credit, however these scripts are provided absolutely free and require no credit what so ever.
Published by James Cloud
I like to program and do basically anything that has to do with technology and computers. View profile
- Game Maker Mplay ExampleLearn to code GML to make a mplay game to play over the internet.
Building a Video Game Development Team - Part OnePart 1 of a couple of guides on building a video game development team and keeping them.
How to Get Video Game Tester JobsVideo game tester jobs are now available as part time jobs from home. They are available over the internet and gamers can use this opportunity to play games and earn money at th...
How to Create a Simple Computer GameIn this tutorial I will teach you how to create a simple game using software called the Game Maker. It is free program created by YoYogames and the greatest part of it is that n...- Review: Monopoly City Streets - the Largest Game of Monopoly in the WorldMonopoly City Streets is the newest creation from Google and Hasbro that allows people to play the classic Monopoly game with Google Maps.
- Hexwar.com Revives War Boardgames Online
- Free Game Maker Software
- A Review of Game Maker
- Want to Make Your Own Computer Games? a Game Maker 7 Review
- Game Maker: Every Programmer's Dream
- Software Review: Game Maker
- Game Maker 8 Tutorial: Draw HUD Elements in 3D





2 Comments
Post a CommentI modified it, the code should be:
// Generate_Grid(width, height, hex_width, hex_height);
var width, height, hex_object;
width = argument0;
height = argument1;
hex_width = argument2;
hex_height = argument3;
// Get the total amount of tiles
tiles = width * height;
// Go through and create each tile
ay = 0;
ax = hex_width * -1;
zx = 0;
use_mod = false;
x_mod = hex_width / 2;
y_mod = x_mod / 2;
for (b = 1; b<=height;b+=1){
for (a=1;a<=width;a+=1){
if(zx = width)
{
ax = 0;
zx = 1;
ay= ay + hex_height - y_mod;
use_mod = !use_mod;
}
else
{
ax = ax + hex_width;
zx+=1;
}
if(use_mod)
obj_temp = instance_create(ax + x_mod, ay, obj_Hex)
else
obj_temp = instance_create(ax, ay, obj_Hex)
}}
Mmmm...I am very interested in this script, but it seems to have some lines missing.
"for (b = 1; b {" makes no sense and the script gives an error