Hi everyone!

I am currently creating somewhat of a platformer/puzzle game in pygame and ran into a problem with creating levels. I really don't want to take the time to program collisions with the player and each platform on the game, because that would obviously take forever. I was thinking of having a text file contain certain characters that would act as different objects, but I do not know how to go about creating instances of objects. Any help would be appreciated!

Here's a class example...

>>> class car(object):
...     def __init__(self, make='Any', model='Something', year='Unknown'):
...         self.make = make
...         self.model = model
...         self.year = year
...     def __repr__(self):
...         return 'Hello, I am a %s %s %s' % (self.year, self.make, self.model)
...     
>>> my_car = car('Ford', 'Focus', '2008')
>>> your_car = car('Pontiac', 'Grand Prix', '1998')
>>> my_car
Hello, I am a 2008 Ford Focus
>>> your_car
Hello, I am a 1998 Pontiac Grand Prix
>>> your_car.year = 2008
>>> your_car.model = 'Montana'
>>> your_car
Hello, I am a 2008 Pontiac Montana
>>> unknown_car = car()
>>> unknown_car
Hello, I am a Unknown Any Something
>>>

thanks, but the only problem is that each instance of the object (in this, it would a cube that the player can collide with) needs to have specific code that will determine if it is touching the player.

let me explain a little more:

I have and image:

# load the cube image
Cube_one = pygame.image.load("Data\Cube_one.bmp")
# I then get its rectangular data:
Cube_one_rect = Cube_one.get_rect()
# load the player image
Player = pygame.image.load("Data\Player.bmp")
# get rectangular data:
Player_rect = Player.get_rect()

now that I have that data, I can access information such as the location of the left side of cube_one on the screen by using cube_one_rect.left ( you can use .left, .right, .top, and .bottom )

the collision would be like so (for the top of cube one):

if cube_one_rect.bottom is >= Player_rect.top:
     gravity = False

and so forth.
that's the main idea.

now, I need multiple 'cube_one's. But the only problem is that I would have to write code for collisions of each cube, which would obviously be pain painstakingly long. So, I need instances of, in this case, the cube that would maintain its own rectangular data and be able to support collisions on its own.

sorry if it's a little confusing, but if you have any ideas, please let me know!

I think I understand what you're trying to do. It still can be done with instances like so:

class Cube(object):
     def __init__(self, x, y):

         self.x = x
         self.y = y
         self.image = pygame.image.load("Data\Cube_one.bmp")

    ...

    def isColliding(self, player):
        rect = self.get_rect()
        player_rect = player.get_rect()
        if ...  # inequalities go here

That gives the idea. Basically what's happening is that each cube knows about itself where it is, so it can check itself for collision.

Also, unless there's a good reason not to, you should check out the Pygame Sprite class, which has collision detection built in.

Jeff

thats good, but how would I say that each block is, for example, to the right other block + 50 pixels. Also, how would I specify the number of blocks so the program doesn't spawn blocks forever?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.