Creating Instantces of objects w/ pygame

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2008
Posts: 58
Reputation: besktrap is an unknown quantity at this point 
Solved Threads: 1
besktrap's Avatar
besktrap besktrap is offline Offline
Junior Poster in Training

Creating Instantces of objects w/ pygame

 
0
  #1
Mar 1st, 2009
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!
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,054
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 265
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: Creating Instantces of objects w/ pygame

 
0
  #2
Mar 1st, 2009
Here's a class example...
  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. >>>
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 58
Reputation: besktrap is an unknown quantity at this point 
Solved Threads: 1
besktrap's Avatar
besktrap besktrap is offline Offline
Junior Poster in Training

Re: Creating Instantces of objects w/ pygame

 
0
  #3
Mar 2nd, 2009
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:
  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):
  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!
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: Creating Instantces of objects w/ pygame

 
0
  #4
Mar 2nd, 2009
I think I understand what you're trying to do. It still can be done with instances like so:

  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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 58
Reputation: besktrap is an unknown quantity at this point 
Solved Threads: 1
besktrap's Avatar
besktrap besktrap is offline Offline
Junior Poster in Training

Re: Creating Instantces of objects w/ pygame

 
0
  #5
Mar 6th, 2009
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?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC