def mouse_control(e): global mouse_tower, menu_width, cell_size, screen_width, money, towers, display, cells, playfield_w if e.type == pygame.MOUSEBUTTONDOWN: m_pos = pygame.mouse.get_pos() if e.button == 1: c = find_cell(m_pos, menu_width, cell_size, screen_width) if c != -1: if mouse_tower.follow_mouse: if (towers[c].t_num == TowerType.ZEROTOWER.value) and (cells[c].t != Terrain.OBSTRUCTED): if money.can_spend(100): # cells[c].t = Terrain((cells[c].t.value + 1) % (Terrain.__len__())) # towers[c].t_num = (towers[c].t_num + 1) % TowerType.__len__() towers[c] = Tower(c, towers[c].x, towers[c].y, cell_size, mouse_tower.t_num) display.register(towers[c]) display.remove(mouse_tower) mouse_tower.follow_mouse = False money.spend(100) else: if money.can_spend(towers[c].upgrade_cost): money.spend(towers[c].upgrade_cost) towers[c].upgrade() # for n in find_neighbors(cells[c].identity, len(cells), playfield_w): # cells[n].t = Terrain.OBSTRUCTED if c == -1: if not mouse_tower.follow_mouse: for item in menu: if item.identity == -1: if item.is_in(m_pos): mouse_tower = Tower(-1, m_pos[0], m_pos[1], cell_size, item.t_num) display.register(mouse_tower) mouse_tower.follow_mouse = True else: display.remove(mouse_tower) mouse_tower.follow_mouse = False
def start_level(e): global level_active, menu, wave_number if not level_active: if e.type == pygame.MOUSEBUTTONDOWN: if e.button == 1: m_pos = pygame.mouse.get_pos() if find_cell(m_pos, menu_width, cell_size, screen_width) == -1: if menu[2].is_in(m_pos): level_active = True display.remove(menu[2]) display.remove(menu[3]) display.register(menu[4]) initialize_wave()
def initialize_wave(): global enemies, wave_number, level_active, victorious, level_number, cell_size wave_number += 1 # we are showing the user that this is wave one, but we are accessing wave 0 in the list if wave_number - 1 < len(current_waves_list): print(wave_number, " Wave ") counter = 0 for num in range(len(current_waves_list[wave_number - 1])): enemies.append(Enemy(counter, cell_size / 2, current_waves_list[wave_number - 1][num], level_number, wave_number, (menu_width, -cell_size - (counter * 30)))) display.register(enemies[counter]) print(counter, current_waves_list[wave_number - 1][num]) counter += 1 else: level_active = False victorious = True
import map WIDTH = 384 HEIGHT = 384 # Event handlers def quit(e): """Event hander for quit events""" # Any shutdown code should go here sys.exit() pygame.display.init() display.init(WIDTH, HEIGHT) display.register(map.sample_map()) display.register(objects.sample_statue()) minotaur = objects.sample_minotaur() display.register(minotaur) ehandler.register("quit", quit) while True: minotaur.update() display.render() ehandler.run() pygame.quit()
# Begin game and game loop import event playing = True def quit(e): global playing if e.type == pygame.QUIT: playing = False elif e.type == pygame.KEYUP: if ((e.key == pygame.K_F4) and (e.mod and pygame.KMOD_ALT)): playing = False player = units.player(500, 500) display.register(player) event.register(player.handler) event.register(quit) event.register(audio.handler) event.register(display.handler) audio.init() clock = pygame.time.Clock() while(playing): clock.tick(30) event.update() player.update() display.update()
heart_max = heart_number money = Resource() money.gain(350) victorious = False # clean up the level for i in range(len(current_level) - 1): current_level[i] = Terrain(current_level[i]) # locations for towers, color can be changed by changing the terrain type cells = [] for i in range(playfield_w * playfield_h): x = int(i % playfield_w) * cell_size + menu_width y = int(i / playfield_w) * cell_size cells.append(Cell(i, x, y, cell_size, current_level[i])) display.register(cells[i]) # the path of positions that enemies must follow # they should start at (-cell_size, 0) so they walk onto the screen # then they move to the next square at the end of the path and make you lose a heart path = [cells[0].pos(), cells[1].pos(), cells[2].pos(), cells[3].pos(), cells[18].pos(), cells[33].pos(), cells[48].pos(), cells[63].pos(), cells[78].pos(), cells[93].pos(), cells[108].pos(), cells[123].pos(), cells[138].pos(), cells[153].pos(), cells[168].pos(), cells[183].pos(), cells[184].pos(), cells[185].pos(), cells[186].pos(), cells[187].pos(), cells[188].pos(), cells[173].pos(), cells[158].pos(), cells[143].pos(), cells[128].pos(), cells[113].pos(), cells[98].pos(), cells[99].pos(), cells[100].pos(), cells[101].pos(), cells[102].pos(), cells[117].pos(), cells[132].pos(), cells[147].pos(), cells[162].pos(), cells[177].pos(), cells[192].pos(), cells[207].pos(), cells[222].pos(), cells[223].pos(), cells[224].pos()