def __init__(self): self.map = Map() self.towers = Towers(self.map) self.enemies = Enemies(self.map, self.towers) self.menu = Menu(self.map, self.towers, self.enemies, screen_width, screen_height) self.dragging_tower = False self.round = 1 self.start_round()
def test_simple_run(self, height=3, verbose=True): assert height >= 1, height tower = Towers(height, verbose=verbose) print(tower) print('moves required: {moves}'.format(moves=tower.moves_for_height(height))) with tower: for i in tower: print(i) print(tower) print('moves taken: {moves}'.format(moves=tower.moves)) x = json.dumps(tower, cls=Towers.JsonEncoder) y = Towers.from_json(x) print(y) self.assertEqual(tower, y)
def set_selected(tower_name): global SELECTED try: name = SELECTED.name SELECTED.kill() except AttributeError: name = None if name != tower_name or name is None: SELECTED = Towers(tower_name, (0, 0), False) all_sprites.add(SELECTED) towers.add(SELECTED) else: SELECTED = None
def handle_input(): """Process and appropriately react to user input""" # Regulate frame rate and store frame time in seconds global DT, SELECTED, SELECTED_POS, RED_COINS, RED_COINS_TIME DT = clock.tick(FPS)/1000 # Test for a quit command for event in pg.event.get(): if event.type == pg.QUIT: global ACTIVE ACTIVE = False else: if event.type == pg.MOUSEBUTTONDOWN: pos = event.pos[0], event.pos[1] if event.button == 1 or event.button == 3: global GG, CONGRATS, user if GG: if user.try_again.rect.collidepoint(pos): user = UserInfo(5, 20, 500) reset_game_vars() elif CONGRATS: if user.try_again.rect.collidepoint(pos): CONGRATS = False user = UserInfo(5, 20, 500) reset_game_vars() else: global WAVE_READY, CREATE_WAVE # Check for updates on the selected tower from the menu if SELECTED is not None and SELECTED_POS: # Pay coins and create tower user.coins -= SELECTED.cost t = Towers(SELECTED.name, SELECTED.pos, True) all_sprites.add(t) towers.add(t) SELECTED.kill() SELECTED, SELECTED_POS = None, False for tower in menu_towers: if tower.rect.collidepoint(pos): if user.coins >= tower.cost: set_selected(tower.name) else: RED_COINS = True RED_COINS_TIME = 0 # Check for the start_wave button if WAVE_READY and user.start_wave is not None: if user.start_wave.rect.collidepoint(pos): user.start_wave = None user.wave += 1 WAVE_READY = False CREATE_WAVE = True pg.event.clear()
def test_context(self, height=3, verbose=True): tower = Towers(height, verbose=verbose) tower_old = Towers.from_json(tower.to_json()) print(tower) with tower.context(): for _ in tower: pass tower_new = tower.to_json() self.assertNotEqual(tower_new, tower_old) print(tower) self.assertEqual(tower_old, tower) self.assertNotEqual(tower_new, tower)
def run(self): running = True tower_menu = Menus() frame = 1000 tick = 16 spawn_tick = 250 #proof = Towers((5, 5)) tower_menu.draw(screen) ground = self.load_level(2) sprite_terrain.draw(background) # Draws background terrain window.blit(background, (0, 0)) pygame.display.update() grid = pathfinder.AStar() grid.init_path(grid_row, grid_col, self.road, self.start, self.end) route = grid.process() render_sprites = pygame.sprite.LayeredUpdates(sprite_terrain, sprite_path, layer=1) while running: for event in pygame.event.get(): mouse_pos = pygame.mouse.get_pos() ratio_x = (window.get_width() / screen.get_width()) ratio_y = (window.get_height() / screen.get_height()) mouse_scaled = (mouse_pos[0] / ratio_x, mouse_pos[1] / ratio_y) if event.type == pygame.QUIT: running = False sys.exit(0) if event.type == pygame.MOUSEBUTTONDOWN: # check for left button if event.button == 1: # if tower_menu.basic_tower_button.collidepoint(mouse_scaled): # tower_menu.click(screen, tower_menu.basic_tower_button, True) # elif tower_menu.ice_tower_button.collidepoint(mouse_scaled): # tower_menu.click(screen, tower_menu.ice_tower_button, True) for x in range(grid_col): for y in range(grid_row): if pygame.Rect(ground[x][y].location).collidepoint(mouse_scaled): if ground[x][y].can_place: self.towers.append(Towers(ground[x][y].location)) ground[x][y].can_place = False render_sprites.add(sprite_towers, layer=2) for num in range(len(self.minion)): if self.minion[num].sprite.rect.collidepoint(mouse_scaled): self.minion[num].health = 0 # if event.type == pygame.MOUSEBUTTONUP: # if event.button == 1: # # if tower_menu.state: # # tower_menu.click(screen, tower_menu.basic_tower_button, False) # # if tower_menu.basic_tower_button.collidepoint(mouse_scaled) and not tower_menu.over: # # tower_menu.hover(screen, tower_menu.basic_tower_button) # # elif tower_menu.over and not tower_menu.basic_tower_button.collidepoint(mouse_scaled): # # tower_menu.hover_off(screen, tower_menu.basic_tower_button) if time() > tick: tick = time() + 17 kill = [] minion_hitboxs = [] for num in range(len(self.minion)): if self.minion[num]: if not self.minion[num].alive: kill.append(num) elif self.minion[num].health > 0: self.minion[num].update_location(route) minion_hitboxs.append(self.minion[num].sprite.rect) if len(kill) > 0: # If there are minions to kill in the list this del them for d in range(len(kill)): del self.minion[kill[d]] kill = [] for num in range(len(self.towers)): current_tower = self.towers[num] collision_index = current_tower.hit_box.rect.collidelist(minion_hitboxs) if not collision_index == -1 and current_tower.target == None: current_tower.target = collision_index elif not current_tower.target == collision_index: current_tower.target = None if not current_tower.target == None: self.minion[current_tower.target].health -= current_tower.damage #Clears then render sprites to display render_sprites.remove_sprites_of_layer(3) render_sprites.add(sprite_creeps, layer=3) if time() > frame: # Update minion animation frames frame = 80 + time() for num in range(len(self.minion)): if self.minion[num].health <= 0: if self.minion[num].update(12) == "dead": self.minion[num].alive = False else: self.minion[num].update() for num in range(len(self.towers)): self.towers[num].update() if time() > spawn_tick: if wave_Count >= len(self.minion): spawn_type = random.choice([Dwarf, Deer, Satyr, Hunter, Druid, Pixie]) #spawn_type = [Dwarf, Deer, Satyr, Hunter, Druid, Pixie] self.minion.append(spawn_type(self.spawn)) spawn_tick = time() + 500 render_sprites.add(sprite_creeps, layer = 3) #if wave_Count >= len(self.minion): # self.minion.append(Deer(self.spawn)) # spawn_tick = time() + 500 #render_sprites.add(sprite_creeps) clock.tick() render_sprites.draw(screen) #for num in range(len(self.towers)): # screen.blit(self.towers[num].hit_box.image, (self.towers[num].sprite.rect.center)) fps = myfont.render(str(int(clock.get_fps())), 1, (255, 255, 255), (15, 210, 50)) screen.blit(fps, (20, screen_height - 30)) window.blit((render_to_window(screen)), (0, 0)) pygame.display.update()
class Game: def __init__(self): self.map = Map() self.towers = Towers(self.map) self.enemies = Enemies(self.map, self.towers) self.menu = Menu(self.map, self.towers, self.enemies, screen_width, screen_height) self.dragging_tower = False self.round = 1 self.start_round() def start_round(self, round=0): difficulty = round + 3 self.enemies.new_round(difficulty=difficulty) def round_traker(self): if all([ enemy.deprecated == True or enemy.in_the_end == True for enemy in self.enemies.enemies ]): self.round += 1 self.start_round(round=self.round) def on_mouse_motion(self, x, y, dx, dy): self.map.on_mouse_motion(x, y, dx, dy) def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers): self.menu.on_mouse_drag(x, y, dx, dy, buttons, modifiers) def update(self, dt): self.round_traker() self.towers.update(dt, self.enemies) self.enemies.update(dt, self.towers) def user_text_draw(self): self.enemies_left = pyglet.text.Label( f'{len([enemy for enemy in self.enemies.enemies if not enemy.deprecated or enemy.in_the_end])} over {len(self.enemies.enemies)} enemies left', font_name='Helvetica', font_size=12, x=window.width - 200, y=window.height - 100, anchor_x='center', anchor_y='center') self.enemies_left.draw() self.round_nb = pyglet.text.Label(f'round {self.round}', font_name='Helvetica', font_size=12, x=window.width - 200, y=window.height - 120, anchor_x='center', anchor_y='center') self.round_nb.draw() def draw(self): window.clear() self.map.draw() self.towers.draw() self.enemies.draw() self.menu.draw() self.user_text_draw() def on_mouse_press(self, x, y, button, modifiers): self.menu.on_mouse_press(x, y) def on_mouse_release(self, x, y, button, modifiers): self.menu.on_mouse_release(x, y)
(23, 15), (23, 14), (23, 13), (25, 16), (25, 15), (26, 15), (27, 15), (27, 16), (33, 13), (33, 12), (33, 11), (33, 10), (33, 9), (33, 7), (33, 6), (33, 5), (33, 4), (33, 3), (35, 4), (35, 5), (36, 4), (37, 4), (38, 4), (39, 4), (42, 3), (42, 4), (42, 5), (42, 6), (42, 7), (42, 8), (42, 9), (42, 10), (43, 10), (43, 14), (42, 18), (6, 9), (40, 12), (43, 15), (43, 16), (43, 17), (41, 13), (41, 14), (41, 15), (40, 16), (41, 16), (43, 18), (42, 19), (42, 20), (42, 22), (40, 25), (41, 25), (42, 25), (43, 25), (44, 25), (45, 25), (46, 25), (47, 25), (47, 23), (46, 23), (45, 23), (44, 23), (43, 23), (42, 23), (0, 10), (0, 12), (1, 10), (1, 12), (2, 10), (2, 12), (3, 12), (3, 10), (4, 10), (5, 10), (32, 16), (33, 16), (34, 16), (35, 16), (29, 13), (30, 14), (31, 14), (32, 14), (33, 14), (48, 23), (48, 24), (48, 25)] for wall in walls: my_grid.walls.append(wall) # Create a Levels instance my_levels = Levels(my_grid) # Create instances of inactive Towers for the menu dart_monkey = Towers('dart', (49.25*TILE_SIZE, 4*TILE_SIZE), False) sniper = Towers('sniper', (52.25*TILE_SIZE, 4*TILE_SIZE), False) # Alter the Towers' rects to blow them up dart_monkey.rect = pg.Rect(dart_monkey.pos, (2*TILE_SIZE, 2*TILE_SIZE)) sniper.rect = pg.Rect(sniper.pos, (2*TILE_SIZE, 2*TILE_SIZE)) menu_towers = [dart_monkey, sniper] tower_menu = Menus(my_screen, pg.Rect(48*TILE_SIZE, 0, SCREEN_WIDTH - 48*TILE_SIZE, SCREEN_HEIGHT), BLUE, menu_towers) # Create Message instances to display on the menu t_m_title1 = Message(my_screen, (50*TILE_SIZE, TILE_SIZE/2), 'Tower', font=pg.font.SysFont('comicsansms', 28), bg_color=BLUE) t_m_title2 = Message(my_screen, (49.5*TILE_SIZE, 2*TILE_SIZE), 'Selection', font=pg.font.SysFont('comicsansms', 28), bg_color=BLUE) t_m_msgs = [t_m_title1, t_m_title2]