""" Peli: 1. Take """ import pygame, sys, os, math, random from pygame.locals import * def load_image(name): """Loads image 'name' from data/images/.""" fullname = os.path.join('data', 'images', name) try: image = pygame.image.load(fullname) except pygame.error, message: print 'ERROR: Cannot load image: ', name raise SystemExit, message image = image.convert_alpha() return image, image.get_rect() # TODO: add sounds class Charactor(pygame.sprite.Sprite): """Represents Players charactors.""" def __init__(self, spec): pygame.sprite.Sprite.__init__(self) #super self.image, self.rect = load_image(spec.pop('image', 'bunny.png')) self.spec = spec self.alive = True self.position = [random.randint(3,10), random.randint(3,10)] self.rect.center = Map.getCoords(Map(), self.position) def update(self): pass def takeDamage(self, amount): """Reduces hitpoints, uses armor, kills if necessary""" self.spec['hp'] -= amount / self.spec['armor'] if self.spec['hp'] >= 0: self.die() def die(self): self.alive = False class Map(pygame.sprite.Sprite): """Represents the tile map""" def __init__(self): pygame.sprite.Sprite.__init__(self) self.changedTiles = [] self.image = pygame.Surface((590,506)) self.rect = self.image.get_rect() self.hex_xm = 36 self.hex_xs = self.hex_xm/3 self.hex_y = 42 for x in range(-1,17): for y in range(-1,13): self.changedTiles.append([[x,y], False]) self.update() def update(self): tempdirty = [] pos = pygame.mouse.get_pos() if pos[0] < 640 or pos[1] < 480: self.changedTiles.append([self.getLocation(pos), True]) for tile in self.changedTiles: self.drawTile(tile[0], tile[1]) if tile[1]: tempdirty.append([tile[0], False]) self.changedTiles = [] self.changedTiles.extend(tempdirty) def drawTile(self, tile, hilight=False): if hilight: fill = (169, 99, 49) edge = (255, 255, 255) else: fill = (139, 69, 19) edge = (0, 0, 0) edge_w = 2 points = self.getPoints(tile) pygame.draw.polygon(self.image, fill, points, 0) pygame.draw.polygon(self.image, edge, points, edge_w) def getLocation(self, coords): """Get hex-coords from absolute coords""" location = [0,0] location[0] = coords[0]/self.hex_xm if coords[0]%self.hex_xm < self.hex_xs: if coords[1] < coords[0] * 3 / 2: location[0] -= 1 if location[0]%2 == 0: location[1] = coords[1]/self.hex_y else: location[1] = (coords[1]-(self.hex_y/2))/self.hex_y return location def getCoords(self, location): """Get absolute coords from hex-coords""" coords = [0,0] coords[0] = (location[0]+1)*self.hex_xm-((self.hex_xm-self.hex_xs)/2) coords[1] = location[1]*self.hex_y if location[0]%2 == 1: coords[1] += self.hex_y else: coords[1] += self.hex_y/2 return coords def getPoints(self, location): """Get coordinates of vertices of a hex in location""" points = [] for a in range(0,360,60): points.append( (round(self.getCoords(location)[0]+(2*self.hex_xs*math.cos(math.radians(a)))), round(self.getCoords(location)[1]+(2*self.hex_xs*math.sin(math.radians(a)))))) return points def main(): pygame.init() window = pygame.display.set_mode((800, 600)) pygame.display.set_caption("Peli") clock = pygame.time.Clock() map = Map() sprites = pygame.sprite.RenderPlain(map) player = Charactor({'hp': 10, 'armor': 2}) players = pygame.sprite.RenderPlain(player) while 1: clock.tick(30) for event in pygame.event.get(): if event.type == QUIT: return elif event.type == KEYDOWN and event.key == K_ESCAPE: return sprites.update() sprites.draw(window) players.update() players.draw(window) pygame.display.flip() if __name__=="__main__": main()