changeset 25:6d6ab0c1479d

Add placing some chickens and foxes
author Neil Muller <drnlmuller@gmail.com>
date Sun, 30 Aug 2009 18:14:07 +0000
parents 7584453f4944
children ad735b5b1555
files gamelib/animal.py gamelib/engine.py gamelib/gameboard.py
diffstat 3 files changed, 160 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gamelib/animal.py	Sun Aug 30 18:14:07 2009 +0000
@@ -0,0 +1,52 @@
+"""Class for the various animals in the game"""
+
+import pygame
+from pgu.vid import Sprite
+
+import data
+
+class Animal(Sprite):
+   """Base class for animals"""
+
+   def __init__(self, image, pos):
+       Sprite.__init__(self, image, pos)
+       self.pos = pos
+
+   def loop(self, tv, _sprite):
+       ppos = tv.tile_to_view(self.pos)
+       self.rect.x = ppos[0]
+       self.rect.y = ppos[1]
+
+   def move(self, state):
+      """Given the game state, return a new position for the object"""
+      # Default is not to move
+      return self.pos
+
+class Chicken(Animal):
+   """A chicken"""
+
+   def __init__(self, pos):
+       image = pygame.image.load(data.filepath('sprites/chkn.png'))
+       Animal.__init__(self, image, pos)
+
+   def move(self, gameboard):
+       """A free chicken will move away from other free chickens"""
+       return self.pos
+
+class Egg(Animal):
+   """An egg"""
+
+   # Eggs don't move
+
+class Fox(Animal):
+   """A fox"""
+
+   def __init__(self, pos):
+       image = pygame.image.load(data.filepath('sprites/fox.png'))
+       Animal.__init__(self, image, pos)
+
+   def move(self, gameboard):
+       """Foxes will aim to move towards the closest henhouse or free
+          chicken"""
+       return self.pos
+       
--- a/gamelib/engine.py	Sun Aug 30 18:11:30 2009 +0000
+++ b/gamelib/engine.py	Sun Aug 30 18:14:07 2009 +0000
@@ -2,9 +2,11 @@
 
 from pgu.engine import Game, State, Quit
 import pygame
-from pygame.locals import USEREVENT, QUIT, KEYDOWN, K_ESCAPE
+from pygame.locals import USEREVENT, QUIT, KEYDOWN, K_ESCAPE, K_n, K_d
 
 import gameboard
+import animal
+import random
 
 class Engine(Game):
     def __init__(self, main_menu_app):
@@ -38,11 +40,54 @@
         pygame.display.update(update)
 
 class DayState(State):
+    def init(self):
+        """Add some chickens to the farm"""
+        # 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
+        self.game.gameboard.clear_foxes()
+        chickens = len(self.game.gameboard.chickens)
+        x, y = 0, 0
+        width, height = self.game.gameboard.tv.size
+        while chickens < 5:
+            if x < width:
+                tile = self.game.gameboard.tv.get((x, y))
+            else:
+                y += 1
+                if y >= height:
+                    break
+                x = 0
+                continue
+            # See if we place a chicken
+            if tile == 1:
+                # Farmland
+                roll = random.randint(1, 10)
+                # We don't place within a tile of the fence, this is to make things
+                # easier
+                for xx in range(x-1, x+2):
+                    if xx >= height or xx < 0:
+                        continue
+                    for yy in range(y-1, y+2):
+                        if yy >= height or yy < 0:
+                            continue
+                        neighbour = self.game.gameboard.tv.get((xx, yy))
+                        if neighbour == 2:
+                            # Fence
+                            roll = 10
+                if roll == 1:
+                    # Create a chicken
+                    chickens += 1
+                    chick = animal.Chicken((x, y))
+                    self.game.gameboard.add_chicken(chick)
+            x += 1
+
     def event(self, e):
         if events_equal(e, START_NIGHT):
             return NightState(self.game)
         elif e.type is KEYDOWN and e.key == K_ESCAPE:
             return MainMenuState(self.game)
+        elif e.type is KEYDOWN and e.key == K_n:
+            return pygame.event.post(START_NIGHT)
         elif events_equal(e, GO_MAIN_MENU):
             return MainMenuState(self.game)
         elif e.type is not QUIT:
@@ -60,9 +105,55 @@
         self.game.gameboard.loop()
 
 class NightState(State):
+    def init(self):
+        """Add some foxes to the farm"""
+        # 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
+        foxes = len(self.game.gameboard.foxes)
+        x, y = 0, 0
+        width, height = self.game.gameboard.tv.size
+        while foxes < 5:
+            if x < width:
+                tile = self.game.gameboard.tv.get((x, y))
+            else:
+                y += 1
+                if y >= height:
+                    break
+                x = 0
+                continue
+            # See if we place a fox
+            if tile == 0:
+                # Forest
+                roll = random.randint(1, 10)
+                if roll == 1:
+                    # Create a fox
+                    foxes += 1
+                    fox = animal.Fox((x, y))
+                    self.game.gameboard.add_fox(fox)
+            x += 5
+
     def event(self, e):
         if events_equal(e, START_DAY):
             return DayState(self.game)
+        elif e.type is KEYDOWN and e.key == K_d:
+            return pygame.event.post(START_DAY)
+        elif e.type is KEYDOWN and e.key == K_ESCAPE:
+            return MainMenuState(self.game)
+        elif e.type is not QUIT:
+            self.game.gameboard.event(e)
+
+    def loop(self):
+        self.game.gameboard.loop()
+
+    def paint(self, screen):
+        self.game.gameboard.paint(screen)
+        pygame.display.flip()
+
+    def update(self, screen):
+        update = self.game.gameboard.update(screen)
+        pygame.display.update(update)
+
 
 # pygame events
 
--- a/gamelib/gameboard.py	Sun Aug 30 18:11:30 2009 +0000
+++ b/gamelib/gameboard.py	Sun Aug 30 18:14:07 2009 +0000
@@ -20,6 +20,8 @@
         self.tools.tga_load_tiles(data.filepath('tiles.tga'), self.TILE_DIMENSIONS)
         self.tools.png_folder_load_tiles(data.filepath('tiles'))
         self.populate_toolbar()
+        self.chickens = []
+        self.foxes = []
 
         self.selected_tool = None
 
@@ -53,11 +55,7 @@
         return updates
 
     def loop(self):
-        return
-        x = random.randint(0, self.tv.size[0]-1)
-        y = random.randint(0, self.tv.size[1]-1)
-        tile = random.randint(0, 4)
-        self.tv.set((x, y), tile)
+        self.tv.loop()
 
     def select_tool(self, e):
         tool_pos = self.tools.screen_to_tile(e.pos)
@@ -78,3 +76,16 @@
                 self.select_tool(e)
             else:
                 self.use_tool(e)
+
+    def clear_foxes(self):
+        for fox in self.foxes:
+            self.tv.sprites.remove(fox)
+        self.foxes = [] # Remove all the foxes
+
+    def add_chicken(self, chicken):
+        self.chickens.append(chicken)
+        self.tv.sprites.append(chicken)
+
+    def add_fox(self, fox):
+        self.foxes.append(fox)
+        self.tv.sprites.append(fox)