0
\$\begingroup\$

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.

\$\endgroup\$
5
  • \$\begingroup\$ 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? \$\endgroup\$
    – Djentleman
    Commented Jan 8, 2013 at 10:18
  • \$\begingroup\$ The latter! I want the program to create a map according to the image data. \$\endgroup\$ Commented Jan 8, 2013 at 11:06
  • 2
    \$\begingroup\$ What's the actual problem? You say you've got the color values, so what's the specific step that you need help with? \$\endgroup\$
    – Kylotan
    Commented Jan 8, 2013 at 13:14
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Jan 8, 2013 at 13:51
  • \$\begingroup\$ I'll give you a short answer but what you're asking for requires a lot more decisions to be made first. \$\endgroup\$
    – Kylotan
    Commented Jan 8, 2013 at 14:18

1 Answer 1

1
\$\begingroup\$

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)
\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .