• 11
• 9
• 11
• 9
• 11

# Better way to do bounding?

This topic is 2218 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Very simple problem I could use some input on. I'm working on a Dr. Mario-esque game, so a simple falling blocks game with control over a pair of blocks. Currently, I'm having a tough time with bounding in regards to rotating the blocks. Below are two snipets of code that control bounding:

keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: self.x -= 20 if self.rect.left <= 240: self.x = 240 if keys[pygame.K_RIGHT]: if self.rect.right <= 380: self.x += 20 if keys[pygame.K_DOWN]: self.y += 20 self.wait = 0 if self.rect.bottom >= 400: self.y = 400

if event.key == pygame.K_RCTRL: if self.mana.dir == 90 or 270: if self.mana.x == 380: self.mana.x -= 20 self.mana.dir += 90 if self.mana.dir > 360: self.mana.dir = 90

For clarity sake, I thought I would also mention that I am rotating objects by the Bottom Left corner of the object to allow easy and accurate snapping to the grid and to better emulate the base material. The problem becomes that if the user presses the rotate button at exactly the right time, the game piece can get out of bounds overlapping the border for the grid the game takes place on. Is there a better way to make sure that there is absolutely no way that any part of my sprite can ever cross a particular point in the window the way I have things now? I tried adding a line of code in the sprite update method that checks to see if the right side of the sprite is past a certain point and if it is to move x back by 20, but it's still exhibiting the same behavior.

This only occurs on the right side, which leads me to believe this may all originate from how I'm rotating the piece and the challenges it produces in managing x in that manner. I'm going to try a few more methods in the morning, but I thought I would post this here to get some additional insight in better ways to manage boundaries.