| | |
Creating Instantces of objects w/ pygame
![]() |
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!
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...
python Syntax (Toggle Plain Text)
>>> 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:
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):
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!
let me explain a little more:
I have and image:
Python Syntax (Toggle Plain Text)
# 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):
Python Syntax (Toggle Plain Text)
if cube_one_rect.bottom is >= Player_rect.top: gravity = False
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!
•
•
Join Date: Jul 2006
Posts: 608
Reputation:
Solved Threads: 150
I think I understand what you're trying to do. It still can be done with instances like so:
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
Python Syntax (Toggle Plain Text)
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
Last edited by jrcagle; Mar 2nd, 2009 at 11:45 pm.
![]() |
Other Threads in the Python Forum
- Previous Thread: Telnet to 2 routers at the same time
- Next Thread: I try to print a string which contains letter é
| Thread Tools | Search this Thread |
abrupt ansi anti apache approximation array assignment avogadro backend beginner binary bluetooth book builtin calculator character code converter countpasswordentry curved customdialog dan08 dictionaries dictionary dynamic examples exe file float format function gnu graphics gui heads homework ideas import inches input java launcher library line lines linux list lists loop mouse mysqlquery number numbers numeric output parsing path phonebook plugin pointer port prime programming progressbar projects py2exe pygame python random recursion redirect scrolledtext software statictext statistics string strings sum table terminal text textarea thread threading time tlapse trick tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable wordgame write wxpython xlib






