1. Need to learn rects. This will do a lot of the work for you.
2. Vector2 will keep your floats and handle different math for you.
3. pygame has over 400 builtin colors.
4. Use pygame.time.Clock over pygame.time.delay. This will lead to smoother movement and constant movement
5. Use delta from pygame.time.Clock to move player smoothly. delta = clock.tick(fps)
Example how I would do it.
Less error prone way is just return player back to old position.
2. Vector2 will keep your floats and handle different math for you.
3. pygame has over 400 builtin colors.
4. Use pygame.time.Clock over pygame.time.delay. This will lead to smoother movement and constant movement
5. Use delta from pygame.time.Clock to move player smoothly. delta = clock.tick(fps)
Example how I would do it.
import pygame class Player: def __init__(self, rect, color): # Player position self.rect = pygame.Rect(rect) # Hold floats since rects are only ints self.center = pygame.Vector2(self.rect.center) self.vel = 10 * 0.01 self.color = color def collision(self, walls): collided = [] for wall_rect in walls: if wall_rect.colliderect(self.rect): collided.append(wall_rect) if len(collided) > 0: # Keep smooth movement center = self.center - pygame.Vector2(int(self.center.x), int(self.center.y)) for wall_rect in collided: # Get what over lapping clip_rect = self.rect.clip(wall_rect) # Check if wall is still overlapping. # Needed if collided with more than one wall. # Else player would start jumping up and down the walls if clip_rect.w > 0 or clip_rect.h > 0: # Take the smallest overlap and move player over if clip_rect.w < clip_rect.h: center.x = 0 if self.rect.centerx < wall_rect.centerx: self.rect.right = wall_rect.left else: self.rect.left = wall_rect.right else: center.y = 0 if self.rect.centery < wall_rect.centery: self.rect.bottom = wall_rect.top else: self.rect.top = wall_rect.bottom self.center = pygame.Vector2(self.rect.center) + center def move(self, direction, delta, walls): self.center += direction * delta * self.vel self.rect.center = self.center self.collision(walls) def draw(self, surface): surface.fill(self.color, self.rect) def main(): pygame.init() # Basic pygame setup pygame.display.set_caption("Example") main_surface = pygame.display.set_mode((600,500)) rect = main_surface.get_rect() clock = pygame.time.Clock() delta = 0 fps = 60 # Game varaibles player = Player((290, 400, 20, 20), pygame.Color("yellow")) walls = [pygame.Rect(0, 0, 30, 500), pygame.Rect(570, 0, 30, 500), pygame.Rect(0, 470, 250, 30), pygame.Rect(350, 470, 250, 30), pygame.Rect(0, 0, 200, 30), pygame.Rect(400, 0, 200, 30), pygame.Rect(210, 0, 10, 30), pygame.Rect(230, 0, 10, 30), pygame.Rect(250, 0, 10, 30), pygame.Rect(380, 0, 10, 30), pygame.Rect(360, 0, 10, 30), pygame.Rect(340, 0, 10, 30), pygame.Rect(160, 30, 100, 200), pygame.Rect(340, 30, 100, 240), pygame.Rect(160, 130, 200, 140), pygame.Rect(180, 130, 100, 240), pygame.Rect(320, 130, 100, 240)] # Create a map surface map_surface = pygame.Surface(rect.size) map_surface.fill(pygame.Color('gray50')) for rect in walls: map_surface.fill(pygame.Color("yellow"), rect) # Main loop running = True while running: # Event loop for event in pygame.event.get(): if event.type == pygame.QUIT: running = False keys = pygame.key.get_pressed() direction = pygame.Vector2(0, 0) if keys[pygame.K_LEFT]: direction.x -= 1 if keys[pygame.K_RIGHT]: direction.x += 1 if keys[pygame.K_UP]: direction.y -= 1 if keys[pygame.K_DOWN]: direction.y += 1 if direction != pygame.Vector2(0, 0): # Normalize for player is moving in a circle instead of a square. direction.normalize_ip() player.move(direction, delta, walls) main_surface.blit(map_surface, (0, 0)) player.draw(main_surface) pygame.display.update() # Idle. delta is time between frames in milliseconds. delta = clock.tick(fps) main()
Less error prone way is just return player back to old position.
class Player: def __init__(self, rect, color): # Player position self.rect = pygame.Rect(rect) # Hold floats since rects are only ints self.center = pygame.Vector2(self.rect.center) self.vel = 10 * 0.01 self.color = color def collision(self, rect, walls): for wall_rect in walls: if wall_rect.colliderect(rect): return True return False def move(self, direction, delta, walls): center = self.center + direction * delta * self.vel rect = self.rect.copy() rect.center = center if not self.collision(rect, walls): self.center = center self.rect.center = center def draw(self, surface): surface.fill(self.color, self.rect)
99 percent of computer problems exists between chair and keyboard.