Question

I am trying to make a platformer in python 2.7 using pygame. I am very inexperienced in using lists in python. i want to make the platforms tileable so that they can be any length. Lets say i wanted it to be 250 pixels long and each individual image was 50 long, could i do something like this:

list = [0,1,2,3,4]
screen.blit(platformimg,(list*50+x,y)

Also, i figure this part is pretty easy, but how would i make a list that is the length of a certain integer. So if the integer was 3, how could i make the list [0,1,2]?

Was it helpful?

Solution

You're going to need to do something like this:

for i in range(6):
    screen.blit(platformimg, (i*50+x, y))

See the section titled Going From The List To The Screen in this pygame tutorial.

The reason being because screen.blit() does not accept a list argument for the position.

If you have an arbitrary list of x,y positions, then it would be something like:

positions = [(100, 30), (250, 90), (42, 623)]
for position in positions:
    screen.blit(platformimg, position)

OTHER TIPS

valuesList = [x for x in range(listLen)]

Where listLen is the length you want. Example: [x for x in range(3)] produces [0, 1, 2]

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top