Reputation: 1
I know, collision detection is covered a lot all over this site and many others. I don't fully understand it though. I need some help understanding some of the ways I can use classes and collision to get what I want for my program.
I'm working on an RPG game with Pygame. I want my player to stay in the center of the screen and have the map move around the player as opposed to the player moving around a map. So far I have a character that stays in the middle of the screen but has a great animation that runs perfectly with a background image that moves around according to the direction and speed of the player. The only thing that really moves is the background image.
What I want to add at this point is a complicated maze of collision boxes to match the background image. I want the collision boxes to move with the background image. I need a single collision box for the player that sits in the middle of the screen and I need a maze of collision boxes that move around and collide with the player.
I want to know what the best and most compact way of doing this would be. I had an idea, but I don't quite know how I would put it in action. I'm wondering if I could draw out a maze like this:
Map = [
"WWWWWWWWWW",
"W W",
"W W",
"W W",
"W W",
"W W",
"WWWWWWWWWW"
]
Would there be a way to create a collision box for each of the "W"'s in the "Map"? I've seen it done before, but I don't fully understand it, and I would love to get some more insight as to how I would use it for my program.
Just a small detail for my program is that the player and each block or space is 50 x 50 pixels. I thought I'd mention that in case it helps you help me.
Upvotes: 0
Views: 746
Reputation: 2501
You could draw another picture, one that you won't have shown in game, where the lines or marks represent collision boundaries. If you have a drawing program that can do layers, open up the world map image and draw the collision boundaries on a separate layer, then save that layer to its own image. In the game loop, test the sprite locations in the world map against the collision image to see if they are colliding with any collision boundaries.
Upvotes: 0
Reputation: 11180
This is the wrong approach. Imagine that you have a lot of objects. You would have to move all of them along, instead of just the player.
You should create a Camera class, that will move the frame along with the player. The camera should only display the sprites on the screen, the collision logic should be separate.
First try making a small map, where the player will move and collide with the walls. Only then should you make it so that the camera is always centered on the player.
As for the maze creation, this is a good way. It would look a bit like this:
for x,line in enumerate(Map):
for y,c in enumerate(line):
wallList.append(new Wall(x*WALL_HEIGHT,y*WALL_WIDTH))
Upvotes: 1