def _compute_direction(self, time_passed): """ Finds out where to go """ coord = xy2coord(self.pos) x_mid, y_mid = coord2xy_mid(coord) # if ((x_mid - self.pos.x) * (x_mid - self.prev_pos.x) < 0 or # (y_mid - self.pos.y) * (y_mid - self.prev_pos.y) < 0): if self.pos == (x_mid, y_mid): next_coord = self.next_on_path(coord) self.direction = v(next_coord[1] - coord[1], next_coord[0] - coord[0]).normalized()
def _compute_direction(self, time_passed): """ Finds out where to go """ coord = xy2coord(self.pos) x_mid, y_mid = coord2xy_mid(coord) # if ((x_mid - self.pos.x) * (x_mid - self.prev_pos.x) < 0 or # (y_mid - self.pos.y) * (y_mid - self.prev_pos.y) < 0): if self.pos == (x_mid,y_mid): next_coord = self.next_on_path(coord) self.direction = v(next_coord[1] - coord[1], next_coord[0] - coord[0]).normalized()
def _get_start_coord(self): start = (self.field.entrance.left+TILE_SIZE/2, self.field.entrance.top+TILE_SIZE/2) row, col = xy2coord(start) if self.field.is_blocked((row+1,col)): start = (self.field.entrance.left+TILE_SIZE+TILE_SIZE/2, self.field.entrance.top+TILE_SIZE/2) end = self.field.goal row, col = end if self.field.is_blocked((row-1,col)): end = (self.field.entrance.left-TILE_SIZE+TILE_SIZE/2, self.field.entrance.top+TILE_SIZE/2) self.field.goal = (row,col+1) return start
def _create_blocks(self): rows = self.field_size[1]/self.tile_size cols = self.field_size[0]/self.tile_size for x in range(1,9)+range(11,cols-1): rect = pygame.Rect(self.field.bounds.left+x*self.tile_size, self.field.bounds.top, self.tile_size, self.tile_size) block = Block(self.screen, rect, v(rect.topleft)) self.sprites.add(block) coord = xy2coord((self.field.bounds.left+x*self.tile_size, self.field.bounds.top)) self.field.block(coord) rect = pygame.Rect(self.field.bounds.left+x*self.tile_size, self.field.bounds.bottom-self.tile_size, self.tile_size, self.tile_size) block = Block(self.screen, rect, v(rect.topleft)) self.sprites.add(block) coord = xy2coord((self.field.bounds.left+x*self.tile_size, self.field.bounds.bottom-self.tile_size)) self.field.block(coord) for y in range(rows): rect = pygame.Rect(self.field.bounds.left, self.field.bounds.top+y*self.tile_size, self.tile_size, self.tile_size) block = Block(self.screen, rect, v(rect.topleft)) self.sprites.add(block) coord = xy2coord((self.field.bounds.left, self.field.bounds.top+y*self.tile_size)) self.field.block(coord) rect = pygame.Rect(self.field.bounds.right-self.tile_size, self.field.bounds.top+y*self.tile_size, self.tile_size, self.tile_size) block = Block(self.screen, rect, v(rect.topleft)) self.sprites.add(block) coord = xy2coord((self.field.bounds.right-self.tile_size, self.field.bounds.top+y*self.tile_size)) self.field.block(coord)
def _is_buildable(self,row,col): if self.player.money == 0: self.is_building = False return (False, "insufficient funds") coords = [] for i in range(2): for j in range(2): coords.append(xy2coord((self.field.bounds.left+(col+i)*self.tile_size, self.field.bounds.top+(row+j)*self.tile_size))) if self.field.is_blocked((row+i,col+j)): return (False, "invalid placement") for coord in coords: self.field.block(coord) start = xy2coord(self.field.entrance.topleft) # if no path is found; tower is blocking the creep path if self.field.get_next(start) == None: # unblock all tower coords for coord in coords: self.field.unblock(coord) return (False, "blocking") return (True, "")
def build_tower(self, pos, color, group): row,col = xy2coord(pos) #@todo: center tower over cursor? # pos = pos[0]-self.tile_size/2,pos[1]-self.tile_size/2 # row,col = xy2coord(pos) rect = pygame.Rect(self.field.bounds.left+col*self.tile_size, self.field.bounds.top+row*self.tile_size, 2*self.tile_size, 2*self.tile_size) buildable, message = self._is_buildable(row,col) if buildable: tower = Tower(rect, v(rect.topleft), color) group.add(tower) self.sprites.add(tower) return (buildable,message)
def update(self, time_passed): if self.build_time < 45000 and self.is_building: self.build_time += time_passed elif self.time == 0 and not self.round_over: self.time += time_passed self.is_building = False self.spawn_creep() else: self.time += time_passed # # update all creeps positions for creep in self.creeps: if xy2coord(creep.pos) == self.field.goal: self.creeps.remove(creep) self.sprites.remove(creep) # lap_time = float(self.time)/1000.0 # print "Creep finished in",lap_time,"seconds" self.round_over = True creep.update(time_passed)
def __init__(self): pygame.Surface.__init__(self, FIELD_RECT.bottomright) self.convert() self.show_grid = True self.fill((100,100,100)) self.bounds = self.get_rect() # define the entrance self.entrance = pygame.Rect(self.bounds.left+9*TILE_SIZE, self.bounds.top, 2*TILE_SIZE, TILE_SIZE) # define the exit self.exit = pygame.Rect(self.bounds.right-11*TILE_SIZE, self.bounds.bottom-TILE_SIZE, 2*TILE_SIZE,TILE_SIZE) # Create the grid-path representation of the field self.rows = self.bounds.h/TILE_SIZE self.cols = self.bounds.w/TILE_SIZE end = xy2coord(self.exit.topleft) # will be changed later upon creep spawn self.gridpath = GridPath(self.rows, self.cols, end)
def run(self): self.is_building = True while not self.game_over: # wating; 60 FPS time_passed = self.clock.tick(60) # If too long has passed between two frames, don't update (the game # must have been suspended for some reason, and we don't want it to # "jump forward" suddenly) if time_passed > 100: continue # handle user input for event in pygame.event.get(): if event.type == pygame.QUIT: self.quit() elif event.type == pygame.MOUSEBUTTONDOWN and not self.paused: # left click if event.button == 1: built,message = self.build_tower(event.pos, (255,255,50), self.player_towers) if built: self.player.money -= 1 self.message_text = "" else: self.message_text = message # # right click # elif event.button == 3: # print xy2coord(event.pos) # show building marker elif event.type == pygame.MOUSEMOTION: if self.is_building: dx = event.pos[0]-self.building_marker.rect.left dy = event.pos[1]-self.building_marker.rect.top row, col = xy2coord((dx,dy)) x = col*TILE_SIZE y = row*TILE_SIZE # rect = self.building_marker.rect.copy() # rect.move_ip(x,y) # xmax = FIELD_RECT.w-TILE_SIZE # ymax = FIELD_RECT.h-TILE_SIZE # if (rect.top >= TILE_SIZE and rect.bottom <= ymax and # rect.left >= TILE_SIZE and rect.right <= xmax): self.building_marker.rect.move_ip(x,y) elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: self.quit() elif event.key == pygame.K_r or event.key == pygame.K_F2 or event.key == pygame.K_SPACE: self.restart() elif event.key == pygame.K_p or event.key == pygame.K_PAUSE: self.pause() elif event.key == pygame.K_m: self.field.gridpath.map.printme() # update if not paused if not self.paused and not self.round_over: self.update(time_passed) # if self.round_over: # pygame.time.wait(10*1000) # self.player_towers.empty() # self.is_building = True # draw self.draw()