Changeset 29:2e88c680672c


Ignore:
Timestamp:
Aug 30, 2009, 6:46:46 PM (11 years ago)
Author:
Neil Muller <drnlmuller@…>
Branch:
default
Convert:
svn:b4e93282-eac8-4b8b-b765-0f5d36de2b68@30
Message:

Minimal fox raid logic

Location:
gamelib
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • gamelib/animal.py

    r28 r29  
    22
    33import pygame
     4import random
    45from pgu.vid import Sprite
    56
     
    4445    def __init__(self, pos):
    4546        image = pygame.image.load(data.filepath('sprites/fox.png'))
     47        self.full = False
    4648        Animal.__init__(self, image, pos)
    4749
     
    4951        """Foxes will aim to move towards the closest henhouse or free
    5052          chicken"""
    51         return self.pos
     53        if self.full:
     54            return
     55        # Find the closest chicken
     56        min_dist = 999
     57        min_vec = None
     58        closest = None
     59        for chicken in gameboard.chickens:
     60            vec = (chicken.pos[0] - self.pos[0], chicken.pos[1] - self.pos[1])
     61            dist = abs(vec[0]) + abs(vec[1])
     62            if dist < min_dist:
     63                min_dist = dist
     64                min_vec = vec
     65                closest = chicken
     66        xpos, ypos = self.pos
     67        if min_vec[0] < 0:
     68            xpos -= 1
     69        elif min_vec[0] > 0:
     70            xpos += 1
     71        if min_vec[1] < 0:
     72            ypos -= 1
     73        elif min_vec[1] > 0:
     74            ypos += 1
     75        if closest.pos == self.pos:
     76            gameboard.remove_chicken(closest)
     77            self.full = True
     78        self.pos = (xpos, ypos)
     79       
     80           
  • gamelib/engine.py

    r25 r29  
    55from pygame.locals import USEREVENT, QUIT, KEYDOWN, K_ESCAPE, K_n, K_d
    66
     7from tiles import TILE_MAP
    78import gameboard
    89import animal
     
    4344    def init(self):
    4445        """Add some chickens to the farm"""
     46        # disable timer
     47        pygame.time.set_timer(MOVE_FOX_ID, 0)
    4548        # Very simple, we walk around the tilemap, and, for each farm tile,
    4649        # we randomly add a chicken (1 in 10 chance) until we have 5 chickens
     
    6063                continue
    6164            # See if we place a chicken
    62             if tile == 1:
     65            if 'grassland' == TILE_MAP[tile]:
    6366                # Farmland
    64                 roll = random.randint(1, 10)
     67                roll = random.randint(1, 20)
    6568                # We don't place within a tile of the fence, this is to make things
    6669                # easier
     
    7275                            continue
    7376                        neighbour = self.game.gameboard.tv.get((xx, yy))
    74                         if neighbour == 2:
     77                        if 'fence' == TILE_MAP[neighbour]:
    7578                            # Fence
    7679                            roll = 10
     
    108111    def init(self):
    109112        """Add some foxes to the farm"""
     113        # Add a timer to the event queue
     114        self.cycle_count = 0
     115        pygame.time.set_timer(MOVE_FOX_ID, 300)
    110116        # Very simple, we walk around the tilemap, and, for each farm tile,
    111117        # we randomly add a chicken (1 in 10 chance) until we have 5 chickens
     
    124130                continue
    125131            # See if we place a fox
    126             if tile == 0:
     132            if TILE_MAP[tile] == 'woodland':
    127133                # Forest
    128                 roll = random.randint(1, 10)
     134                roll = random.randint(1, 20)
    129135                if roll == 1:
    130136                    # Create a fox
     
    141147        elif e.type is KEYDOWN and e.key == K_ESCAPE:
    142148            return MainMenuState(self.game)
     149        elif e.type is MOVE_FOX_ID:
     150            self.cycle_count += 1
     151            if self.cycle_count > 15:
     152                return pygame.event.post(START_DAY)
     153            return self.game.gameboard.move_foxes()
    143154        elif e.type is not QUIT:
    144155            self.game.gameboard.event(e)
     
    155166        pygame.display.update(update)
    156167
    157 
    158168# pygame events
    159169
     
    165175START_NIGHT = pygame.event.Event(USEREVENT, name="START_NIGHT")
    166176GO_MAIN_MENU = pygame.event.Event(USEREVENT, name="GO_MAIN_MENU")
     177MOVE_FOX_ID = USEREVENT + 1
     178MOVE_FOXES = pygame.event.Event(MOVE_FOX_ID, name="MOVE_FOXES")
    167179QUIT = pygame.event.Event(QUIT)
  • gamelib/gameboard.py

    r25 r29  
    8383        self.foxes = [] # Remove all the foxes
    8484
     85    def move_foxes(self):
     86        for fox in self.foxes:
     87            fox.move(self)
     88
    8589    def add_chicken(self, chicken):
    8690        self.chickens.append(chicken)
     
    9094        self.foxes.append(fox)
    9195        self.tv.sprites.append(fox)
     96
     97    def remove_fox(self, fox):
     98        if fox in self.foxes:
     99            self.foxes.remove(fox)
     100            self.tv.sprites.remove(fox)
     101
     102    def remove_chicken(self, chick):
     103        if chick in self.chickens:
     104            self.chickens.remove(chick)
     105            self.tv.sprites.remove(chick)
Note: See TracChangeset for help on using the changeset viewer.