def test_move_creature(self): c = Creature(name='creature', player='red', position=Point(0, 0)) self.world.insert_creature(c) self.assertTrue(self.world.move_creature(c, 1, 1)) self.assertEqual(Point(1, 1), c.position) self.assertEqual(c, self.world.get_entity_at(1, 1))
def render(self, mouse): self.level.render() self.highlight.render(iso_to_screen(screen_to_iso(mouse.pos))) self.restart.render(Point((SCREEN.center * 2).x, 0)) self.levels.render(Point(0, 0)) if self.level.win(): self.win.render(SCREEN.center)
def render(self): """Render the whole array of tiles and entities.""" for i, row in enumerate(self._tiles): for j, tile in enumerate(row): tile.render(iso_to_screen(Point(i, j))) if self.knight.pos.round() == Point(i, j): self.knight.render()
def test_move_creature_out_of_bounds(self): c = Creature(name='creature', player='red', position=Point(0, 0)) self.world.insert_creature(c) self.assertFalse(self.world.move_creature(c, -1, 1)) self.assertEqual(Point(0, 0), c.position) self.assertEqual(c, self.world.get_entity_at(0, 0))
def logic(self, mouse): SCREEN.camera = Point(0, 0) left = Point(189, 137) if mouse.clicked: normalized = mouse.pos - left if (0 < normalized.y < 66 * 5 and (0 < normalized.x < 56 or 136 < normalized.x < 136 + 56)): level = normalized.y // 66 + 1 if normalized.x > 136: level += 5 self.next = Play('level{n}.lev'.format(n=level))
class Play(GameState): """The GameState while actually playing a level.""" highlight = Renderable("resources/images/highlight.png", TILE_CENTER) win = Renderable("resources/images/cleared_screen.png", Point(605 / 2, 455 / 2), ignore_cam=True) restart = Renderable("resources/images/restart_button.png", Point(100, 0), ignore_cam=True) levels = Renderable("resources/images/levels_button.png", Point(0, 0), ignore_cam=True) def __init__(self, filepath): super(Play, self).__init__() self.filepath = filepath tileset = Tileset('resources/tileset/default.tset') self.level = Level(tileset, filepath) def logic(self, mouse): if mouse.clicked: scr = SCREEN.center * 2 # restart button if (scr.x - SCREEN.camera.x - mouse.pos.x <= 100 and SCREEN.camera.y + mouse.pos.y <= 65): self.next = Play(self.filepath) # levels button elif (SCREEN.camera.x + mouse.pos.x <= 100 and SCREEN.camera.y + mouse.pos.y <= 65): self.next = LevelPicker() else: # pass the win dialogs if self.level.win(): if (SCREEN.center - SCREEN.camera - mouse.pos).x > 0: self.next = Title() else: self.next = LevelPicker() # pass the click to the level tile = screen_to_iso(mouse.pos) self.level.tile_clicked(tile) self.level.knight.logic() def render(self, mouse): self.level.render() self.highlight.render(iso_to_screen(screen_to_iso(mouse.pos))) self.restart.render(Point((SCREEN.center * 2).x, 0)) self.levels.render(Point(0, 0)) if self.level.win(): self.win.render(SCREEN.center)
class Mouse(object): """Basic Input/Gesture Recognition Abstraction Class""" pos = Point(0, 0) down = False clicked = False def update(self, screen): """This should be called every frame to recalculate input state.""" # check events for e in pygame.event.get(): if e.type == pygame.QUIT or (e.type == pygame.KEYDOWN and e.key == pygame.K_ESCAPE): sys.exit() # update position m = Point(pygame.mouse.get_pos()) self.pos = m - screen.camera # reset click state self.clicked = False # update down and click state was_down = self.down self.down = pygame.mouse.get_pressed()[0] # MB1 if not self.down and was_down: self.clicked = True
def get_state(self, game): head = game.snake[0] last_actions = self.get_previous_actions(self.agent_cfg.state.lookback) point_l = Point(head.x - game.block_size, head.y) point_r = Point(head.x + game.block_size, head.y) point_u = Point(head.x, head.y - game.block_size) point_d = Point(head.x, head.y + game.block_size) dir_l = game.direction == Direction.LEFT dir_r = game.direction == Direction.RIGHT dir_u = game.direction == Direction.UP dir_d = game.direction == Direction.DOWN state = [ # Danger straight (dir_r and game.is_collision(point_r)) or (dir_l and game.is_collision(point_l)) or (dir_u and game.is_collision(point_u)) or (dir_d and game.is_collision(point_d)), # Danger right (dir_u and game.is_collision(point_r)) or (dir_d and game.is_collision(point_l)) or (dir_l and game.is_collision(point_u)) or (dir_r and game.is_collision(point_d)), # Danger left (dir_d and game.is_collision(point_r)) or (dir_u and game.is_collision(point_l)) or (dir_r and game.is_collision(point_u)) or (dir_l and game.is_collision(point_d)), # Move direction dir_l, dir_r, dir_u, dir_d, # Food location game.food.x < game.head.x, # food left game.food.x > game.head.x, # food right game.food.y < game.head.y, # food up game.food.y > game.head.y, # food down, # distance from food (game.food.x - game.head.x) / game.w, (game.food.y - game.head.y) / game.h ] state += [direction for action in last_actions for direction in action] assert len(state) == self.model.input_size return np.array(state, dtype=int)
def test_insert_creature(self): c = Creature(name='creature', player='red', position=Point(0, 0)) count = len(self.world.creatures) self.world.insert_creature(c) self.assertGreater(len(self.world.creatures), count) self.assertEqual(c, self.world.get_entity_at(c.position.x, c.position.y)) self.assertEqual(c, self.world.creatures[c.id])
class Instructions(GameState): """The GameState that displays the help instructions.""" instructions = Renderable("resources/images/instructions.png", Point(605 / 2, 455 / 2), ignore_cam=True) def logic(self, mouse): if mouse.clicked: self.next = Title() def render(self, mouse): self.instructions.render(SCREEN.center)
class Screen(object): """The global screen object.""" pygame.init() _screen = pygame.display.set_mode((800, 600)) _camera = Point(0, 0) center = Point(800 / 2, 600 / 2) def clear(self): """Fills the screen with black""" self._screen.fill(COLOR_BLACK) def blit(self, renderable, point): """Draws the renderable on the screen""" self._screen.blit(renderable, point) @property def camera(self): return self._camera @camera.setter def camera(self, point): self._camera.x, self._camera.y = point.x, point.y
class Knight: """Contains the Knight entity that moves around to complete the puzzle. """ _ne = Renderable("resources/images/knight_ne.png", Point(64, 110), frames=8) _se = Renderable("resources/images/knight_se.png", Point(64, 110), frames=8) _nw = Renderable("resources/images/knight_nw.png", Point(64, 110), frames=8) _sw = Renderable("resources/images/knight_sw.png", Point(64, 110), frames=8) _renderable = _ne prev = Point(0, 0) target = Point(0, 0) move_counter = 0 def __init__(self, level, start_point): self.level = weakref.ref(level) self.pos = start_point self.target = start_point level._tiles[self.target.x][self.target.y].on_enter(self) def move(self, point): point = point.round() # perform the move level = self.level() if not self.move_counter and level.is_legal(point): self.prev = self.pos.round() level._tiles[self.prev.x][self.prev.y].on_leave() self.target = point.round() self.move_counter = TILE_STEPS # reset animation direction vector = self.target - self.prev if vector.x == 1: self._renderable = self._sw elif vector.x == -1: self._renderable = self._ne elif vector.y == 1: self._renderable = self._se elif vector.y == -1: self._renderable = self._nw def slide(self): self.move(self.pos + (self.pos - self.prev)) def teleport(self, target): # TODO: Animate self.pos = target self.target = target def logic(self): if self.move_counter: self.move_counter -= 1 self.pos += ((self.target - self.prev) * (1.0 / TILE_STEPS)) if not self.move_counter: self.level()._tiles[self.target.x][self.target.y].on_enter(self) pos = iso_to_screen(self.pos) SCREEN.camera = SCREEN.center - pos def render(self): """Renders the knight to the screen at his current location.""" self._renderable.render(iso_to_screen(self.pos))
def __init__(self, tileset, filename): # tileset self.tileset = tileset # layer data with open('resources/levels/' + filename) as f: lines = f.readlines() self._tiles = [[copy(self.tileset[int(tile)]) for tile in row.strip(' \r\n,').split(',')] for row in lines] # find start point for i, row in enumerate(lines): for j, tile in enumerate(row.strip(' \r\n,').split(',')): if tile == '0': start_point = Point(i, j) # player self.knight = Knight(self, start_point) SCREEN.camera = SCREEN.center - iso_to_screen(start_point)
class Title(GameState): """The GameState while viewing the main Title image.""" start = Renderable("resources/images/start.png", Point(605 / 2, 455 / 2), ignore_cam=True) def __init__(self): super(Title, self).__init__() SCREEN.camera = Point(0, 0) def logic(self, mouse): if mouse.clicked: if mouse.pos.y < SCREEN.center.y: self.next = LevelPicker() else: self.next = Instructions() def render(self, mouse): self.start.render(SCREEN.center)
class LevelPicker(GameState): """Lets you choose which level you want to play.""" levels = Renderable("resources/images/levels_screen.png", Point(605 // 2, 455 // 2), ignore_cam=True) def __init__(self): super(LevelPicker, self).__init__() SCREEN.camera = Point(0, 0) def logic(self, mouse): SCREEN.camera = Point(0, 0) left = Point(189, 137) if mouse.clicked: normalized = mouse.pos - left if (0 < normalized.y < 66 * 5 and (0 < normalized.x < 56 or 136 < normalized.x < 136 + 56)): level = normalized.y // 66 + 1 if normalized.x > 136: level += 5 self.next = Play('level{n}.lev'.format(n=level)) def render(self, mouse): self.levels.render(SCREEN.center)
def test_init(self): p1 = Point(5) p2 = Point(3, 5) p2.x = 5 self.assertEqual(p1, p2)
def setUp(self): self.attacker = Creature(name="attacker", player="player_1", level=1, position=Point(0, 0)) self.defender = Creature(name="defender", player="player_2", level=1, position=Point(1, 0))
def __init__(self): super(Title, self).__init__() SCREEN.camera = Point(0, 0)
def setUp(self): self.creature = Creature(name="creature_1", player="player_1", level=1, position=Point(0, 0)) self.resource = Resource(name="resource_1", gold_amount=100, gold_flux=10)
def __init__(self): super(LevelPicker, self).__init__() SCREEN.camera = Point(0, 0)