# 2D city builders

This topic is 3332 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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!

##### Share on other sites
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.

##### Share on other sites
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/

##### Share on other sites
Quote:
 Original post by emforceI 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 17I 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))	##

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 10
• 9
• 9
• 11
• 11
• ### Forum Statistics

• Total Topics
633690
• Total Posts
3013339
×