2D city builders

Started by
2 comments, last by Captain P 14 years, 6 months ago
Hi guys I was trying to get some ideas for how to program a 2D city builder in C# and XNA. I was thinking that for the tiles I could use like an array of buttons all corresponding to a set point on the screen. That way I could tell where to place buildings and such but if I wanted to scroll past the buttons in view how would I do that? would I just draw say 200 * 200 buttons and make it so that the player could only see like 50*50 of them at a time? Also can anyone actually recommend a good tutorial for XNA buttons, I did google for such but I would like to know if you have any that you think use good coding practices and would help me out here as well. Thanks!

Game Development Tutorials - My new site that tries to teach LWJGL 3.0 and OpenGL to anyone willing to learn a little.

Advertisement
I've done this using C++; They were not buttons but nodes. I created a separate struct and set copies of them into an array. The struct had x,y, width, height variables. I edited the x and y as I added them to the list. The Array would be used to show grid lines on the screen and to handle path-finding and collision detection.

I created offsetx, offsety in a class Grid which manage the Array; I added that to all the Arrays occupants x and y locations when a key was pressed. It was similar to character velocity movement.

In order to only show certain ones: I checked the x and y of every node in the array(about 1072). If the x+width and y+height values were greater than 0 and less than the width and height of the screen, I drew there Grid lines on the screen. If a node was 'hanging' on the screen(part on part off), I edited the line length for the 'leftover'. I did this by using the difference between the node x,y and screen width,height. Whether this is the best way to do this or not is unknown to me. This is just how I knew how to do it.

I have never messed with C# or XNA so I cannot give you an example(or tutorial).


Note: The array was actually a C++ vector container. I did not notice a speed deduction.
-----------------------------I heard the voice of the fourth beast say, Come and see.And I looked, and behold a pale horse: and his name that sat on him was Death, and Hell followed with him. And power was given unto them over the fourth part of the earth, to kill with sword, and with hunger, and with death, and with the beasts of the earth.Revelation 6:7-8 (King James Version)
Don't use a mess of 'buttons'. Just use a tilemap. There's a sample on the creator's club website that should help you. If I'm wrong and there's not, try ziggyware. There's a decent article on iso maps in xna there. http://www.ziggyware.com/
Quote:Original post by emforce
I was thinking that for the tiles I could use like an array of buttons all corresponding to a set point on the screen.

Using buttons for tiles strikes me as odd, but it's hard to tell without knowing more about your game idea. How exactly do you want the player to build things?

Quote:That way I could tell where to place buildings and such but if I wanted to scroll past the buttons in view how would I do that? would I just draw say 200 * 200 buttons and make it so that the player could only see like 50*50 of them at a time?

No - have 200 x 200 tiles/buttons and calculate which ones are visible. If you're using a rectangular grid, it's pretty easy to determine which tiles are visible and which ones aren't (as long as you keep track of your camera position and the size of the screen). Then only draw those that need to be drawn.

Quote:Original post by rpg gamer 17
I created offsetx, offsety in a class Grid which manage the Array; I added that to all the Arrays occupants x and y locations when a key was pressed. It was similar to character velocity movement.

That comes down to moving the grid itself. A better approach is to keep the grid at the same location, but to render the tiles with an offset (that's where a camera position is useful).

Quote:In order to only show certain ones: I checked the x and y of every node in the array(about 1072). If the x+width and y+height values were greater than 0 and less than the width and height of the screen, I drew there Grid lines on the screen. If a node was 'hanging' on the screen(part on part off), I edited the line length for the 'leftover'. I did this by using the difference between the node x,y and screen width,height. Whether this is the best way to do this or not is unknown to me. This is just how I knew how to do it.

Assuming a regular grid, you don't need to check each tile. You can calculate which tiles are within the screen area. Also, for a regular grid, you only need to store tile dimensions once - all tiles have the same size after all. You can also leave out the coordinates for each tile: their place in the grid, combined with the grid's location and the tile dimensions, determine the tile locations.

Here's an example: a while ago I wrote a grid display class for the level editor I'm working on. Here's the Python code (xrange takes 3 arguments: start, end and increment, so it's similar to for(int i = start; i < end; i += increment)):
import pygameclass Grid:	def __init__(self):		self.granularity = 64		self.color = (130, 200, 235)	#		def draw(self, screen, camera):		# Draw vertical lines		for x in xrange(camera.x % self.granularity,		                screen.get_width(),		                self.granularity):			pygame.draw.line(screen,			                 self.color,			                 (x, 0),			                 (x, screen.get_height()))		# Draw horizontal lines		for y in xrange(camera.y % self.granularity,		                screen.get_height(),		                self.granularity):			pygame.draw.line(screen,			                 self.color,			                 (0, y),			                 (screen.get_width(), y))	##
Create-ivity - a game development blog Mouseover for more information.

This topic is closed to new replies.

Advertisement