def testRow1(self): rect = Rect(3 * view.TILE_SIZE + 4, 5 * view.TILE_SIZE + 4, 24, 24) baseRect = rect.move(0, 0) self.assertEqual(1, len(rpgMap.getRectTiles(baseRect))) self.assertEqual((True, 2), rpgMap.isMoveValid(2, baseRect)) baseRect = rect.move(16, 0) self.assertEqual(2, len(rpgMap.getRectTiles(baseRect))) self.assertEqual((False, 2), rpgMap.isMoveValid(2, baseRect)) baseRect = rect.move(32, 0) self.assertEqual(1, len(rpgMap.getRectTiles(baseRect))) self.assertEqual((True, 2.5), rpgMap.isMoveValid(2, baseRect))
class Stone: def __init__(self, game_speed): self.x = WIDTH self.y = 0 self.dy = 0 self.delta = 3 + randrange(4) self.speed = game_speed self.surface = pygame.Surface((30, 30)) self.surface.fill(color.THECOLORS["yellow"]) self.rect = Rect(self.surface.get_rect()) def on_the_ground(self): if self.y >= 4 / 5 * HEIGHT - 30: return True else: return False def on_the_top(self): if self.y <= 0: return True else: return False def update(self, screen, tick, speed): self.speed = speed if self.on_the_top(): self.dy = self.delta if self.on_the_ground(): self.y = 4 / 5 * HEIGHT - 30 self.dy = -self.delta self.y += self.dy self.rect = self.rect.move(-self.speed / tick, self.dy) screen.blit(self.surface, self.rect)
def testLevel3(self): rect = Rect(10 * view.TILE_SIZE + 4, 2 * view.TILE_SIZE + 4, 24, 24) baseRect = rect.move(0, 0) self.assertEqual(1, len(rpgMap.getRectTiles(baseRect))) self.assertEqual((False, 3), rpgMap.isMoveValid(3, baseRect)) baseRect = rect.move(0, 16) self.assertEqual(2, len(rpgMap.getRectTiles(baseRect))) self.assertEqual((False, 3), rpgMap.isMoveValid(3, baseRect)) baseRect = rect.move(0, 32) self.assertEqual(1, len(rpgMap.getRectTiles(baseRect))) self.assertEqual((True, 3), rpgMap.isMoveValid(3, baseRect)) baseRect = rect.move(0, 48) self.assertEqual(2, len(rpgMap.getRectTiles(baseRect))) self.assertEqual((False, 3), rpgMap.isMoveValid(3, baseRect)) baseRect = rect.move(0, 64) self.assertEqual(1, len(rpgMap.getRectTiles(baseRect))) self.assertEqual((False, 3), rpgMap.isMoveValid(3, baseRect))
def animate(self): s = pygame.Surface(self.size) s.set_colorkey((0, 0, 0)) height = self.size[1] self.offset += 0.1 for step in self.steps: src = Rect(step, 0, 2, height) dst = src.move(0, math.cos(self.offset + step * .02) * self.amount) s.blit(self.base, dst, src) return s
class Tunnel: def __init__(self, game_speed): self.x = WIDTH self.tunnel_length = 250 self.upper_length = HEIGHT / 2 - 100 - randrange(10) * 15 self.lower_length = 4 / 5 * HEIGHT - self.upper_length - self.tunnel_length self.speed = game_speed self.upper_surface = pygame.Surface((25, self.upper_length)) self.upper_surface.fill(color.THECOLORS["green"]) self.lower_surface = pygame.Surface((25, self.lower_length)) self.lower_surface.fill(color.THECOLORS["green"]) self.rect = Rect(self.upper_surface.get_rect()) self.lower_rect = Rect(self.lower_surface.get_rect()) def update(self, screen, tick, speed): self.speed = speed self.rect = self.rect.move(-self.speed / tick, 0) self.lower_rect = self.lower_rect.move(-self.speed / tick, 0) screen.blit(self.upper_surface, self.rect) screen.blit(self.lower_surface, self.lower_rect)
class MatchRectangle: def __init__(self, game_speed): self.x = randrange(350, WIDTH) self.y = randrange(4 / 5 * HEIGHT - 80) self.speed = game_speed self.surface = pygame.Surface((35, 35)) self.surface.fill(color.THECOLORS["orange"]) self.rect = Rect(self.surface.get_rect()) def update(self, screen, tick, speed): self.speed = speed self.rect = self.rect = self.rect.move(-self.speed / tick, 0) screen.blit(self.surface, self.rect)
class GroundStone: def __init__(self, game_speed): self.x = WIDTH + 100 self.y = 4 / 5 * HEIGHT - 60 self.speed = 2.25 * game_speed self.surface = pygame.Surface((60, 60)) self.surface.fill(color.THECOLORS["purple"]) self.rect = Rect(self.surface.get_rect()) def update(self, screen, tick, speed): self.speed = 2.25 * speed self.rect = self.rect = self.rect.move(-self.speed / tick, 0) screen.blit(self.surface, self.rect)
class RectangleObstacle: def __init__(self, game_speed): self.height = HEIGHT / 2 - 100 - randrange(10) * 15 self.x = WIDTH self.y = self.height self.speed = game_speed self.surface = pygame.Surface((50, self.y)) self.surface.fill(color.THECOLORS["blue"]) self.rect = Rect(self.surface.get_rect()) def update(self, screen, tick, speed): self.speed = speed self.rect = self.rect.move(-self.speed / tick, 0) screen.blit(self.surface, self.rect)
class ScoreObject: def __init__(self, score, speed): self.x = 0 self.surface = pygame.Surface((100, 1 / 5 * HEIGHT)) self.surface.fill(color.THECOLORS["gray"]) self.score = score self.speed = speed self.rect = Rect(self.surface.get_rect()) self.rect = pygame.draw.line(self.surface, color.THECOLORS["black"], (0, 0), (0, 1 / 10 * HEIGHT), 5) font = pygame.font.Font(None, 20) font_surface = font.render(str(score), True, color.THECOLORS["black"], color.THECOLORS["gray"]) self.surface.blit(font_surface, (5, 1 / 10 * HEIGHT)) def update(self, screen, tick, speed): self.speed = speed self.rect = self.rect.move(-self.speed / tick, 0) screen.blit(self.surface, self.rect)
class MapCamera: def __init__(self, x_init_world, y_init_world, width_view_camera, height_view_camera): # rect that represents the surface visible of the camera self.rect_view_camera = Rect(x_init_world, y_init_world, width_view_camera, height_view_camera) self.pos_dest_screen = (0, 0) # Position to draw the map captured by this Camera self.map_handler = None # Controller of the map self.map_tile_surface = None # Surface Buffer that contains the map drawn self.rect_hidden_camera = None # Rect of the surface buffer with pos on map self.following = None self.can_follow = {"left": False, "top": False, "bottom": False, "right": True} # Start the camera setting buffers def init(self): assert self.map_handler is not None, "There's not a " + str(MapHandler.__class__) + " instance " assert self.map_handler.has_map_loaded(), "MapHandler has no map loaded yet!" self.__create_buffer() def __create_buffer(self): self.rect_hidden_camera = create_camera_rect(self.rect_view_camera, self.map_handler.get_tile_size()) self.map_tile_surface = self.map_handler.create_image(self.rect_hidden_camera) def set_pos_screen(self, x_screen, y_screen): # Set position to draw the map on screen self.pos_dest_screen = (x_screen, y_screen) def set_maphandler(self, map_handler): self.map_handler = map_handler # Move camera x y pixels def move(self, x_world, y_world): rect_moved = self.rect_view_camera.move(x_world, y_world) if self.map_handler.is_rect_out_of_map_bounds(rect_moved): return self.rect_view_camera.move_ip(x_world, y_world) if not self.rect_hidden_camera.contains(self.rect_view_camera): # If new pos is out of the buffer, we create a new one! self.__create_buffer() def draw(self, screen): rect_area_visible = Rect(self.rect_view_camera.x - self.rect_hidden_camera.x, self.rect_view_camera.y - self.rect_hidden_camera.y, self.rect_view_camera.width, self.rect_view_camera.height) screen.blit(self.map_tile_surface, self.pos_dest_screen, rect_area_visible) def get_x(self): return self.rect_view_camera.x def get_y(self): return self.rect_view_camera.y def follow(self, player): self.following = player def update(self): if self.following: x_p, y_p = self.following.x, \ self.following.y if self.can_follow['right']: if x_p > self.rect_view_camera.x + self.rect_view_camera.width / 4: rect_moved = self.rect_view_camera.copy() rect_moved.x = x_p - self.rect_view_camera.width / 4 if self.map_handler.is_rect_out_of_map_bounds(rect_moved): return self.rect_view_camera = rect_moved if not self.rect_hidden_camera.contains(self.rect_view_camera): # If new pos is out of the buffer, we create a new one! self.__create_buffer() def is_rect_out_of_camera_bounds(self, rect): return not self.rect_view_camera.contains(rect)
# In[9]: r.bottomleft # In[10]: r.width # In[11]: r.move(100, 100) # In[12]: pygame.init() # In[13]: SURFACE = pygame.display.set_mode((800,600)) pygame.display.set_caption('Game Window')