# HG changeset patch # User Neil Muller # Date 1251658006 0 # Node ID 2e88c680672c31b7d2fb0b5ca10e4d3ac083fceb # Parent ac3a74352b7490d9d915f6b4e45844f09ca1c168 Minimal fox raid logic diff -r ac3a74352b74 -r 2e88c680672c gamelib/animal.py --- a/gamelib/animal.py Sun Aug 30 18:23:40 2009 +0000 +++ b/gamelib/animal.py Sun Aug 30 18:46:46 2009 +0000 @@ -1,6 +1,7 @@ """Class for the various animals in the game""" import pygame +import random from pgu.vid import Sprite import data @@ -43,9 +44,37 @@ def __init__(self, pos): image = pygame.image.load(data.filepath('sprites/fox.png')) + self.full = False Animal.__init__(self, image, pos) def move(self, gameboard): """Foxes will aim to move towards the closest henhouse or free chicken""" - return self.pos + if self.full: + return + # Find the closest chicken + min_dist = 999 + min_vec = None + closest = None + for chicken in gameboard.chickens: + vec = (chicken.pos[0] - self.pos[0], chicken.pos[1] - self.pos[1]) + dist = abs(vec[0]) + abs(vec[1]) + if dist < min_dist: + min_dist = dist + min_vec = vec + closest = chicken + xpos, ypos = self.pos + if min_vec[0] < 0: + xpos -= 1 + elif min_vec[0] > 0: + xpos += 1 + if min_vec[1] < 0: + ypos -= 1 + elif min_vec[1] > 0: + ypos += 1 + if closest.pos == self.pos: + gameboard.remove_chicken(closest) + self.full = True + self.pos = (xpos, ypos) + + diff -r ac3a74352b74 -r 2e88c680672c gamelib/engine.py --- a/gamelib/engine.py Sun Aug 30 18:23:40 2009 +0000 +++ b/gamelib/engine.py Sun Aug 30 18:46:46 2009 +0000 @@ -4,6 +4,7 @@ import pygame from pygame.locals import USEREVENT, QUIT, KEYDOWN, K_ESCAPE, K_n, K_d +from tiles import TILE_MAP import gameboard import animal import random @@ -42,6 +43,8 @@ class DayState(State): def init(self): """Add some chickens to the farm""" + # disable timer + pygame.time.set_timer(MOVE_FOX_ID, 0) # Very simple, we walk around the tilemap, and, for each farm tile, # we randomly add a chicken (1 in 10 chance) until we have 5 chickens # or we run out of board @@ -59,9 +62,9 @@ x = 0 continue # See if we place a chicken - if tile == 1: + if 'grassland' == TILE_MAP[tile]: # Farmland - roll = random.randint(1, 10) + roll = random.randint(1, 20) # We don't place within a tile of the fence, this is to make things # easier for xx in range(x-1, x+2): @@ -71,7 +74,7 @@ if yy >= height or yy < 0: continue neighbour = self.game.gameboard.tv.get((xx, yy)) - if neighbour == 2: + if 'fence' == TILE_MAP[neighbour]: # Fence roll = 10 if roll == 1: @@ -107,6 +110,9 @@ class NightState(State): def init(self): """Add some foxes to the farm""" + # Add a timer to the event queue + self.cycle_count = 0 + pygame.time.set_timer(MOVE_FOX_ID, 300) # Very simple, we walk around the tilemap, and, for each farm tile, # we randomly add a chicken (1 in 10 chance) until we have 5 chickens # or we run out of board @@ -123,9 +129,9 @@ x = 0 continue # See if we place a fox - if tile == 0: + if TILE_MAP[tile] == 'woodland': # Forest - roll = random.randint(1, 10) + roll = random.randint(1, 20) if roll == 1: # Create a fox foxes += 1 @@ -140,6 +146,11 @@ return pygame.event.post(START_DAY) elif e.type is KEYDOWN and e.key == K_ESCAPE: return MainMenuState(self.game) + elif e.type is MOVE_FOX_ID: + self.cycle_count += 1 + if self.cycle_count > 15: + return pygame.event.post(START_DAY) + return self.game.gameboard.move_foxes() elif e.type is not QUIT: self.game.gameboard.event(e) @@ -154,7 +165,6 @@ update = self.game.gameboard.update(screen) pygame.display.update(update) - # pygame events def events_equal(e1, e2): @@ -164,4 +174,6 @@ START_DAY = pygame.event.Event(USEREVENT, name="START_DAY") START_NIGHT = pygame.event.Event(USEREVENT, name="START_NIGHT") GO_MAIN_MENU = pygame.event.Event(USEREVENT, name="GO_MAIN_MENU") +MOVE_FOX_ID = USEREVENT + 1 +MOVE_FOXES = pygame.event.Event(MOVE_FOX_ID, name="MOVE_FOXES") QUIT = pygame.event.Event(QUIT) diff -r ac3a74352b74 -r 2e88c680672c gamelib/gameboard.py --- a/gamelib/gameboard.py Sun Aug 30 18:23:40 2009 +0000 +++ b/gamelib/gameboard.py Sun Aug 30 18:46:46 2009 +0000 @@ -82,6 +82,10 @@ self.tv.sprites.remove(fox) self.foxes = [] # Remove all the foxes + def move_foxes(self): + for fox in self.foxes: + fox.move(self) + def add_chicken(self, chicken): self.chickens.append(chicken) self.tv.sprites.append(chicken) @@ -89,3 +93,13 @@ def add_fox(self, fox): self.foxes.append(fox) self.tv.sprites.append(fox) + + def remove_fox(self, fox): + if fox in self.foxes: + self.foxes.remove(fox) + self.tv.sprites.remove(fox) + + def remove_chicken(self, chick): + if chick in self.chickens: + self.chickens.remove(chick) + self.tv.sprites.remove(chick)