def drawPrices(self): x, y = pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1] if Tower.clickedMonkeys(x, y) != None: monkey = Tower.clickedMonkeys(x, y) white = (255, 255, 255) priceFontSize = 20 priceFont = pygame.font.SysFont("myriadpro", priceFontSize) price = priceFont.render( "Price: $%d" % towerTypes[monkey]["price"], 1, white) pricePos = price.get_rect() pricePos.center = (890, 405) descriptionFontSize = 13 descriptionFont = pygame.font.SysFont("myriadpro", descriptionFontSize) desc = descriptionFont.render(towerTypes[monkey]["description"], 1, white) descriptionPos = desc.get_rect() descriptionPos.center = (890, 428) player.screen.blit(price, pricePos) player.screen.blit(desc, descriptionPos)
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 test_invalid_move_piece(self): from towers import Tower, Rod, Piece, TowerError tower = Tower() tower.rods = [Rod() for i in range(3)] tower.rods[0].pieces = [Piece(3 - i) for i in range(3)] tower.rods[1].pieces = [Piece(4)] with self.assertRaises(TowerError): tower.move_piece(1, 0)
def test_get_biggest_size(self): from towers import Tower, Rod, Piece from towersvisualizer import Visualizer tower = Tower() tower.rods = [Rod() for i in range(3)] tower.rods[1].pieces = [Piece(2), Piece(1)] tower.rods[2].pieces = [Piece(3)] visualizer = Visualizer(tower) self.assertEqual(visualizer._get_biggest_size(), 3)
def pressedButton(self, x, y): #pressed start button if x > 829 and x < 952 and y > 461 and y < 500: #only start next level when player just started, or completed level if player.spriteBloons == None or len(player.spriteBloons) == 0: player.level += 1 if player.level < 11: player.spriteBloons = Levels.runLevel(player.level) else: player.level = 10 player.gameWon = True #pressed monkey sidebar if x > 825 and x < 950 and y > 140 and y < 380: #check what monkey you clicked on player.selectedTower = Tower.clickedMonkeys(x, y) #pressed music button if x > Coord.music[0][0] and y < Coord.music[0][1]: if y > Coord.music[1][0] and y < Coord.music[1][1]: Music.play = not Music.play if Music.play == True: pygame.mixer.music.unpause() else: pygame.mixer.music.pause() #pressed home button if x > 840 and x < 872 and y > 538 and y < 568: self.splashScreenActive = True #place a monkey onto the canvas if player.selectedTower != None: monkeyName = player.selectedTower if player.money < towerTypes[monkeyName]["price"]: #you don't have enough money to buy it player.selectedTower = None else: monkey = Tower(player.selectedTower) #check if monkey is placed in a "legal" area if monkey.legalPlacements(x, y): monkey.rect.center = [x, y] monkey.originalImageRect.center = [x, y] monkey.sellButtonRect.center = [x, y + 50] monkey.upgradeButtonRect.center = [x, y - 50] monkey.drawUpgradePriceRect.center = [x, y - 70] player.towers.add(monkey) player.money -= towerTypes[monkeyName]["price"] player.selectedTower = None #reset selected tower
def test_move_piece(self): from towers import Tower, Rod, Piece tower = Tower() tower.rods = [Rod() for i in range(3)] tower.rods[0].pieces = [Piece(3 - i) for i in range(3)] tower.move_piece(0, 2) self.assertEqual(len(tower.rods[0].pieces), 2) self.assertEqual(len(tower.rods[2].pieces), 1) tower.move_piece(2, 0) self.assertEqual(len(tower.rods[0].pieces), 3) self.assertEqual(len(tower.rods[2].pieces), 0)
def timerFired(self): if player.gameOver == False: #creates bullet if monkey is not on cooldown for monkey in player.towers: if "sniper" in monkey.name: monkey.shootBloons() if monkey.ready: bloon = Tower.furthestBloon(player.spriteBloons) if bloon != None: sniperStrength = towerTypes[monkey.name]["damage"] bloon.updateDamage(sniperStrength, 0, player.screen) else: monkey.shootBloons() #updates bullets (make them move toward targets) for bullet in player.bullets: bullet.update(player.screen)
def test_20_discs(self): t = Tower() self.assertEqual(1048575, t.solve(20, 'A', 'B', 'C'))
def test_6_discs(self): t = Tower() self.assertEqual(63, t.solve(6, 'A', 'B', 'C'))
def test_5_discs(self): t = Tower() self.assertEqual(31, t.solve(5, 'A', 'B', 'C'))
def test_4_discs(self): t = Tower() self.assertEqual(15, t.solve(4, 'A', 'B', 'C'))
def test_3_discs(self): t = Tower() self.assertEqual(7, t.solve(3, 'A', 'B', 'C'))
def test_init(self): from towers import Tower rods = [1, 2, 3] tower = Tower(rods) self.assertEqual(tower.rods, rods)
def run(self): clock = pygame.time.Clock() screen = pygame.display.set_mode((self.width, self.height)) # set the title of the window pygame.display.set_caption(self.title) self.splashScreenActive = True # stores all the keys currently being held down self._keys = dict() # call game-specific initialization self.__init__() playing = True while playing: x, y = pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1] if self.splashScreenActive == True: self.tutorial = False self.initializeSplashScreen() for event in pygame.event.get(): if event.type == pygame.MOUSEBUTTONDOWN: if self.splashScreenMousePressed(event) != None: if self.splashScreenMousePressed( event) == "tutorial": self.tutorial = True self.tutorialPage = 1 self.splashScreenActive = False player.__init__() #reset player values player.spriteBloons == Levels.runLevel(0) else: map = self.splashScreenMousePressed(event) self.map = pygame.image.load("images/%s.png" % map) self.map.convert() player.game = map self.splashScreenActive = False player.__init__() #reset player values player.spriteBloons == Levels.runLevel(0) elif event.type == QUIT: pygame.quit() sys.exit() elif self.tutorial == True: self.map = pygame.image.load("images/slide%d.png" % self.tutorialPage).convert() player.screen.blit(self.map, (0, 0)) self.drawPlayerStats() self.drawPrices() for event in pygame.event.get(): if event.type == pygame.MOUSEBUTTONDOWN: if self.tutorialPage == 7: self.tutorial = False self.splashScreenActive = True else: self.tutorialPage += 1 elif event.type == QUIT: pygame.quit() sys.exit() elif player.gameOver == True: self.lost = pygame.image.load("images/lost.png").convert() player.screen.blit(self.lost, (0, 0)) for event in pygame.event.get(): if event.type == pygame.MOUSEBUTTONDOWN: if x > 223 and x < 723: if y > 343 and y < 410: #go back to splash screen player.gameOver = False self.splashScreenActive = True elif event.type == QUIT: pygame.quit() sys.exit() elif player.gameWon == True: self.won = pygame.image.load("images/won.png").convert() player.screen.blit(self.won, (0, 0)) for event in pygame.event.get(): if event.type == pygame.MOUSEBUTTONDOWN: if x > 223 and x < 723: if y > 343 and y < 410: #go back to splash screen player.gameWon = False self.splashScreenActive = True elif event.type == QUIT: pygame.quit() sys.exit() else: self.initializeMap() self.timerFired() self.moveBloons(player.spriteBloons) if player.selectedTower != None: monkeyName = player.selectedTower if player.money >= towerTypes[monkeyName]["price"]: monkeyName = player.selectedTower monkey = Tower(monkeyName) legal = monkey.legalPlacements(x, y) monkey.drawMonkeyAndRange(legal, x, y) for monkey in player.towers: if monkey.selected == True: monkey.drawRangeAndSell(player.screen) for event in pygame.event.get(): if event.type == pygame.MOUSEBUTTONDOWN: self.mousePressed(event) elif event.type == KEYDOWN: if event.key == K_ESCAPE: #press "esc" key to deselect a tower player.selectedTower = None elif event.type == QUIT: pygame.quit() sys.exit() if player.spriteBloons == None or len( player.spriteBloons) == 0: pass self.redrawAll() self.drawPrices() player.checkGameOver(player.spriteBloons) player.checkGameWon(player.spriteBloons) pygame.display.flip() clock.tick(self.fps) pygame.quit()
def test_1_disc(self): t = Tower() self.assertEqual(1, t.solve(1, 'A', 'B', 'C'))
def test_2_discs(self): t = Tower() self.assertEqual(3, t.solve(2, 'A', 'B', 'C'))
cells[192].pos(), cells[207].pos(), cells[222].pos(), cells[223].pos(), cells[224].pos() ] # a collection of tuples (x, y) for enemies to follow # for i in range(len(cells)): # if cells[i].t == Terrain.PATH: # path.append((cells[i].x, cells[i].y)) # if i+ playfield_w < len(cells): # if cells[i+1].t != Terrain.PATH: # i += playfield_w # towers have their own list that mirrors cells, can't place in path cells towers = [] for i in range(len(cells)): c = cells[i] if c.t == Terrain.PATH: towers.append(Tower(i, c.x, c.y, cell_size, TowerType.PATH.value)) # users can't replace this #display.register(towers[len(towers) - 1]) else: towers.append(Tower(i, c.x, c.y, cell_size, TowerType.ZEROTOWER.value)) # users can replace this #display.register(towers[len(towers) - 1]) # enemies enemies = [] # menu objects for buying and placing # position should be relative to cell_size menu = [] # 0-1 ; Title menu.append(Textbox((6, 6), dark(red), font, "Neon Tower Defence")) display.register(menu[0]) menu.append(Textbox((5, 5), text_color, font, "Neon Tower Defence"))