Help on getting started with a hex base game

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
Rishavs
Party member
Posts: 103
Joined: Sat Oct 17, 2009 5:29 am
Contact:

Help on getting started with a hex base game

Post by Rishavs »

I am thinking of trying my hand at a hex based tbs game which i should be able to handle. the artwork will be later laid on top of the hex. Units will be little cards with attack, defense and hp. Can you folks point me to a tutorial where they discuss a tbs hex game? Alternatively if any of you have tried anything like this, can you give me some sage?

thanks and regards.
Rishav.

ps. My starting concern is how do i resolve individual hexes as objects. i can get hold of a hex sheet in image format and put it in as a background. :o
after that i run out of ideas. lets say i want to use the hampster ball triangle as a unit which navigates the 5 hexes on mouse clicks. so, my startup challange is to;
1. bring up game window.
2. have hexes on it
3. have the hampster on the leftmost hex which is the starting hex.
4. mouse click and move the hampster 5 hexes.
5. end turn. the hampster moves to the selected hex.
6. rinse and repeat till hampster is at the last hex on the right.
this gives me a nice startup milestone. except that i have no idea how to resolves hexes. i don't want to use pixels in this case for unit movement but the hexes themselves as units.
pekka
Party member
Posts: 206
Joined: Thu Jan 07, 2010 6:48 am
Location: Oulu, Finland
Contact:

Re: Help on getting started with a hex base game

Post by pekka »

If you would like, I can show you how to rewrite this little bit of code to deal with hexes instead of squares. Currently it is supposed to present you with a 30x30 scrollable grid of squares that you can light up and turn off by clicking on them with a mouse. You only need the mouse and its left button to use this program. It's written in rather simplistic style, because I tried to keep it short. It has some awkward features, but I hope you won't be distracted by them. They should be easy to fix later.

Before going into the geometry of hex maps, I'd like you to explain what kind of a hex map and interface you envision your game having. The general idea in most turn-based games that display the map is the same. You either fit the whole map on screen at once, or make scrollable one like here. In the latter case, you have a camera position that tells you where you currently are on the map. This camera position is used when deciding what & where to draw on the screen and how to deal with mouse picking.

Mouse picking means choosing the object that is currently under the mouse cursor when the user interacts with the program. And the reason I used a square map is that mouse picking iwth squares is super easy. It is not much harder with hexes, either, and we can go on to that if you like.

Note that if you can fit the entire map on the screen at once, this is just really a special case where the camera position is always constant. So we can solve the more general problem and use our solution in either case.

You can post a response to this message or PM me to continue with this, assuming you want to continue. (Perhaps you found an answer elsewhere already?) We'll find a passably good interface for your game. I am sure. Just have a little patience, as I don't necessarily visit here every day.

Code: Select all

local camera = {0, 0}
local screen = {800, 600}
local squares = nil
local squaresize = 50
local maxx, maxy = 30, 30

function love.load()
	-- build the map
	squares = {}
	for y = 1, maxy do
		local row = {}
		for x = 1, maxx do
			local sq = {set = false}
			table.insert(row, sq)
		end
		table.insert(squares, row)
	end
	screen[1] = love.graphics.getWidth()
	screen[2] = love.graphics.getHeight()
end

function love.draw()
	-- draw the currently visible portion of the map
	-- this draws every partially visible square and relies
	-- on love drawing code to clip the edges
	local camx, camy = unpack(camera)
	local scrx, scry = unpack(screen)
	local vleft = math.floor(camx/squaresize)
	local vup = math.floor(camy/squaresize)
	local horc = math.ceil(scrx/squaresize) + 1
	local verc = math.ceil(scry/squaresize) + 1
	for y = 1, verc do
		for x = 1, horc do
			if squares[vup+y] and squares[vup+y][vleft+x] then
				local mode = "line"
				if squares[vup+y][vleft+x].set then
					mode = "fill"
				end
				love.graphics.rectangle(mode, (vleft+x-1)*squaresize - camx,
					(vup+y-1)*squaresize - camy, squaresize, squaresize)
			end
		end
	end
end

local scrollspeed = 200
local edge = 100

function love.update(dt)
	-- scroll the screen if mouse is near edges
	local scrx, scry = unpack(screen)
	local mx, my = love.mouse.getPosition()
	if mx < edge/2 then camera[1] = camera[1] - scrollspeed * dt end
	if mx > scrx - edge/2 then camera[1] = camera[1] + scrollspeed * dt end
	if my < edge/2 then camera[2] = camera[2] - scrollspeed * dt end
	if my > scry - edge/2 then camera[2] = camera[2] + scrollspeed * dt end
	-- limit the camera position from wandering off
	if camera[1] < -edge then camera[1] = -edge end
	if camera[2] < -edge then camera[2] = -edge end
	local xlimit, ylimit = maxx*squaresize + edge - scrx, maxy*squaresize + edge - scry
	if camera[1] > xlimit then camera[1] = xlimit end
	if camera[2] > ylimit then camera[2] = ylimit end
end

function love.mousepressed(x, y, button)
	-- toggle the clicked square on left button click
	if button ~= 'l' then return end
	local wx, wy = x + camera[1], y + camera[2]
	local x, y = math.floor(wx/squaresize)+1, math.floor(wy/squaresize)+1
	if squares[y] and squares[y][x] then
		local value = squares[y][x].set
		squares[y][x].set = not value -- toggle bool flag
	end
end
I tested this on the Windows version, but it should of course work everywhere. Do let me know if there are mistakes or problems I didn't find while quickly writing & testing it.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Help on getting started with a hex base game

Post by kikito »

Rishavs wrote:Can you folks point me to a tutorial where they discuss a tbs hex game?
The best tutorial I could find for drawing hexagonal stuff is this one on gamedev (great site for this kind of questions)

Once you have implemented basic hexagon drawing you will need some art.

I hope you are familiar with Battle of Wesnoth then. It is an awesome opensource, turn-based, hexed game. If you don't know it, first thing I recommend is downloading it and playing it for a couple hours. I'll wait.

...

Great, isn't it?
Now have a look at Wesnoth's Create Art wiki page. There are lots of useful guidelines about how to make the sprites of the game "work correctly" with the tiles (for example, the maximum height and with they should have, where should the feet go, etc)

I hope this helps!
When I write def I mean function.
pekka
Party member
Posts: 206
Joined: Thu Jan 07, 2010 6:48 am
Location: Oulu, Finland
Contact:

Re: Help on getting started with a hex base game

Post by pekka »

Here are some other solutions to finding the hex from mouse pointer coordinates. They are all equally good once you get them to work, so picking the one that makes most sense to you is the best idea.

http://www-cs-students.stanford.edu/~am ... agon1.html

There's also an oddball solution I've rarely seen used. instead of using hexes, you can just simulate the topology of a hexmap by moving the vertical position of every other column downwards in a square map. Then every square is bordered by six other squares and your game logic can be the same as for a proper hex map.

It probably won't look as good as proper hex map, so going for hexes from the start is probably the best thing to do. However, it is easier to make some WIP art for a square-based pseudo-hex game, if that is an issue for you. Straight borders make drawing simpler.
User avatar
Rishavs
Party member
Posts: 103
Joined: Sat Oct 17, 2009 5:29 am
Contact:

Re: Help on getting started with a hex base game

Post by Rishavs »

whoa!! i didnt realize i had replies to my thread. Thanks a lot folks. :o
I'll assimilate what has already been said and come back for specifics. brb.

Update: @Pekka, just checked your code. its perfect! contains all i needed to know. It will help me immensely. I will try to play around with it for a week and see where I get. and i agree that square looks easier to start off with than hexes. :ultrahappy:
thanks a lot.

also, i have previously read amit's articles but being a raw starter was having trouble with it. I also did think of using a staggered sqare grid, but when i potted it on a piece of paper, i felt that the distances between the square centres still wouldn'ty be equal. either way, i suppose its better to start with squares. once i reach my personal milestone, i'll try hexes.

@Kikito; thanks for the links. i did hear a bit about wesnorth on the nets, but i didnt know it was open source. and the wiki looks great. I haven't reached that far but I am certain once i do start using art in my code, those insights will be extremely helpful. again, thanks a lot for taking the time folks.
really appreciate the help.
User avatar
hdon
Prole
Posts: 36
Joined: Tue Mar 17, 2009 10:54 pm
Location: Pittsburgh, PA, USA
Contact:

Re: Help on getting started with a hex base game

Post by hdon »

Rishavs wrote:I am thinking of trying my hand at a hex based tbs game which i should be able to handle... Alternatively if any of you have tried anything like this, can you give me some sage?
An expanse of hexagonal cells defines an identical pattern of cellular interconnects as a "brick" pattern. Each brick is adjacent to six other bricks.

http://www.freefoto.com/preview/33-06-64?ffid=33-06-64

Assuming you want your program to characterize a finite grid of brick-pattern/hexagonal cells, it will probably be easiest to define your grids as a rectangular expanse of the brick-pattern cell space, deciding whether even or odd rows should be a half cell left or right of the other.
Rishavs wrote:ps. My starting concern is how do i resolve individual hexes as objects. i can get hold of a hex sheet in image format and put it in as a background. :o
Again the brick pattern helps us, but we have to change it a little. If you can think of a simple method to draw a rectangle at the position of each brick, imagine modifying that method to draw each brick so that its center is not moved, but it is now twice its height. The rectangle defined by this new method may be a square, it depends on whether you want a top-down view of the hexagons or a angled look like you're sitting at a table and the hexagon grid is on the table.

This way, each of your tile graphics can be defined as a simple rectangular sprite, but with transparent areas around the corners of the hexagon, where the graphics for neighboring tiles will appear when composed together by your grid rendering method.
Rishavs wrote:4. mouse click and move the hampster 5 hexes.
Try to write a program that draws a grid of hexagonal cells! Once you have written this method, try to write its inverse method, which accepts coordinates in the result image from the first method, and locates which cell it is inside of.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 64 guests