import random, pygame, time, sys # Global Variables BLACK = (0, 0, 0) WHITE = (255, 255, 255) WIN_W = 32 * 20 WIN_H = 32 * 20 class Camera(object): def __init__(self, total_width, total_height): self.state = pygame.Rect(0, 0, total_width, total_height) def apply(self, target): return target.rect.move(self.state.topleft) def update(self, target_rect): x = -target_rect.x + WIN_W / 2 y = -target_rect.y + WIN_H / 2 # Stop scrolling at left edge if x > 0: x = 0 # Stop scrolling at the right edge elif x < -(self.state.width - WIN_W): x = -(self.state.width - WIN_W) # Stop scrolling at top if y > 0: y = 0 # Stop scrolling at the bottom elif y < -(self.state.height - WIN_H): y = -(self.state.height - WIN_H) self.state = pygame.Rect(x, y, self.state.width, self.state.height) class Hero(pygame.sprite.Sprite): def __init__(self): pygame.sprite.Sprite.__init__(self) self.speed = 4 self.image = pygame.Surface((32, 32)).convert() self.rect = self.image.get_rect() self.rect.x = 2 * 32 self.rect.y = 21 * 32 def collide(self, platform_group, dir): for p in platform_group: if pygame.sprite.collide_rect(self, p): if dir == "up" and self.rect.top < p.rect.bottom: self.rect.top = p.rect.bottom elif dir == "down" and self.rect.bottom > p.rect.top: self.rect.bottom = p.rect.top elif dir == "left" and self.rect.left < p.rect.right: self.rect.left = p.rect.right elif dir == "right" and self.rect.right > p.rect.left: self.rect.right = p.rect.left def update(self, platform_group, hero): # Movement key = pygame.key.get_pressed() # Movement and barriers if key[pygame.K_w] or key[pygame.K_UP]: self.rect.y -= self.speed self.collide(platform_group, "up") elif key[pygame.K_s] or key[pygame.K_DOWN]: self.rect.y += self.speed self.collide(platform_group, "down") if key[pygame.K_a] or key[pygame.K_LEFT]: self.rect.x -= self.speed self.collide(platform_group, "left") elif key[pygame.K_d] or key[pygame.K_RIGHT]: self.rect.x += self.speed self.collide(platform_group, "right") # Function# class Platform(pygame.sprite.Sprite): def __init__(self, x, y, col): pygame.sprite.Sprite.__init__(self) self.image = pygame.Surface((32, 32)) self.image.convert() self.col = col self.rect = pygame.Rect(x, y, 32, 32) def main(): # variables screen = pygame.display.set_mode((WIN_W, WIN_H), pygame.SRCALPHA) hero = Hero() map = [ "PPPPPPPPPPPPPPPPPPPPPPPPPPPP", "P P", "P P", "P P", "P P P P", "P P", "P PPP PP P", "P P", "P PPP PP P", "P PPP PP P", "P P", "P P", "P PP PPP P", "P PP PPP P", "P P", "P P", "P PPP PP P", "P PPP PP P", "P P", "P PPP PP P", "P PPP PP P", "P P", "P P", "PPPPPPPPPPPPPPPPPPPPPPPPPPPP", ] platform_group = pygame.sprite.Group() x = y = 0 for row in map: for col in row: if col == "P": p = Platform(x, y, col) platform_group.add(p) x += 32 y += 32 x = 0 total_width = len(map[0]) * 32 total_height = len(map) * 32 camera = Camera(total_width, total_height) play = True clock = pygame.time.Clock() fps = 60 while play: # Checks if window exit button pressed for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: pygame.quit() sys.exit() # Update hero.update(platform_group, hero) camera.update(hero.rect) # Blits screen.fill(WHITE) screen.blit(hero.image, camera.apply(hero)) for p in platform_group: screen.blit(p.image, camera.apply(p)) # Limits FPS clock.tick(fps) # Writes to surface pygame.display.flip() main()