Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

The program is supposed to read the pixel color values from an image, and use that data as a map for where to place objects in a game world.

Getting the color values was easy enough:

import numpy
from PIL import Image

img = Image.open("colors.png")
data = numpy.array(img)

The color value should be used to choose a type of object, and the data[ ] index values translated as coordinates to choose where on a surface the object is placed. It's not clear to me how to go about implementing this, I'm still trying to learn python.

I need help with understanding how to structure a system which goes through all the values of the data[ ] array, and creates a map based off of those values. For example, data[0,0] is a red pixel, therefore make an instance of object_type_1 and place it in the top left corner of the screen surface, data[0,1] is a blue pixel, make an instance of object_type_2 and place it 20 pixels to the right of the first object.

share|improve this question
    
Some more info needed. Like do you want the image to be editable in your program, or do you just want the program to load an image and convert it into a map object of some sort when executed? –  Djentleman Jan 8 '13 at 10:18
    
The latter! I want the program to create a map according to the image data. –  user11177 Jan 8 '13 at 11:06
2  
What's the actual problem? You say you've got the color values, so what's the specific step that you need help with? –  Kylotan Jan 8 '13 at 13:14
    
I need help with understanding how to structure a system which goes through all the values of the data[ ] array, and creates a map based off of those values. For exmple, data[0,0] is a red pixel, therefore make an instance of objecttype1 and place it in the top left corner of the screen surface. –  user11177 Jan 8 '13 at 13:51
    
I'll give you a short answer but what you're asking for requires a lot more decisions to be made first. –  Kylotan Jan 8 '13 at 14:18

1 Answer 1

up vote 1 down vote accepted

The general process here is simply something like this:

for row_number in rows:
  for column_number in columns:
    value = data[column,row]
    if value == 1:
      obj = ObjectType1()
    elif value == 2:
      obj = ObjectType2()
    else value == 3:
      obj = ObjectType3()
    obj.x_position = column * 20
    obj.y_position = row * 20

I have assumed objects are 20 units in width because your question implied it.

This gives the objects a position, but it does not place them on the screen - how to draw objects depends on the graphics library you choose to use. But the process there will look a bit like this:

for object in all_objects:
    screen.draw(object.graphic, object.position_x, object.position_y)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.