943,793 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 858
  • Python RSS
Mar 1st, 2009
0

Creating Instantces of objects w/ pygame

Expand Post »
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!
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
besktrap is offline Offline
58 posts
since Jul 2008
Mar 1st, 2009
0

Re: Creating Instantces of objects w/ pygame

Here's a class example...
python Syntax (Toggle Plain Text)
  1. >>> class car(object):
  2. ... def __init__(self, make='Any', model='Something', year='Unknown'):
  3. ... self.make = make
  4. ... self.model = model
  5. ... self.year = year
  6. ... def __repr__(self):
  7. ... return 'Hello, I am a %s %s %s' % (self.year, self.make, self.model)
  8. ...
  9. >>> my_car = car('Ford', 'Focus', '2008')
  10. >>> your_car = car('Pontiac', 'Grand Prix', '1998')
  11. >>> my_car
  12. Hello, I am a 2008 Ford Focus
  13. >>> your_car
  14. Hello, I am a 1998 Pontiac Grand Prix
  15. >>> your_car.year = 2008
  16. >>> your_car.model = 'Montana'
  17. >>> your_car
  18. Hello, I am a 2008 Pontiac Montana
  19. >>> unknown_car = car()
  20. >>> unknown_car
  21. Hello, I am a Unknown Any Something
  22. >>>
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008
Mar 2nd, 2009
0

Re: Creating Instantces of objects w/ pygame

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:
Python Syntax (Toggle Plain Text)
  1. # load the cube image
  2. Cube_one = pygame.image.load("Data\Cube_one.bmp")
  3. # I then get its rectangular data:
  4. Cube_one_rect = Cube_one.get_rect()
  5. # load the player image
  6. Player = pygame.image.load("Data\Player.bmp")
  7. # get rectangular data:
  8. 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):
Python Syntax (Toggle Plain Text)
  1. if cube_one_rect.bottom is >= Player_rect.top:
  2. 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!
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
besktrap is offline Offline
58 posts
since Jul 2008
Mar 2nd, 2009
0

Re: Creating Instantces of objects w/ pygame

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

Python Syntax (Toggle Plain Text)
  1. class Cube(object):
  2. def __init__(self, x, y):
  3.  
  4. self.x = x
  5. self.y = y
  6. self.image = pygame.image.load("Data\Cube_one.bmp")
  7.  
  8. ...
  9.  
  10. def isColliding(self, player):
  11. rect = self.get_rect()
  12. player_rect = player.get_rect()
  13. 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
Last edited by jrcagle; Mar 2nd, 2009 at 11:45 pm.
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006
Mar 6th, 2009
0

Re: Creating Instantces of objects w/ pygame

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?
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
besktrap is offline Offline
58 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Telnet to 2 routers at the same time
Next Thread in Python Forum Timeline: I try to print a string which contains letter é





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC