Commits

dr0id committed 1f1f04a

moving player, can extinguish already

Comments (0)

Files changed (6)

pyweek15/gamelib/level01.py

 import levels
 import settings
 
+from pyknicpygame.pyknic.timing import Scheduler
+
+class Player(object):
+    
+    def __init__(self, model):
+        self.position = Vec2(0, 0)
+        self.dir = Vec2(0, 0)
+        self.vel = Vec2(0, 0)
+        self.speed = settings.player_speed
+        self.model = model
+        self.left_pressed = 0
+        self.right_pressed = 0
+        self.up_pressed = 0
+        self.down_pressed = 0
+        
+    def look_at(self, x, y):
+        self.dir = Vec2(x, y) - self.position
+        if self.dir.length_sq > 40*40:
+            self.dir.length = 40
+        
+    def set_flooding(self, yes_or_no):
+        self.flooding = yes_or_no
+        
+    def update(self, dt):
+        if self.flooding:
+            px, py = (self.position + self.dir).as_tuple()
+            self.model.cool_down(px, py, 2, 2)
+            
+        self.position += self.vel * self.speed * dt
+            
+    def on_event(self, logical_event, event):
+        if settings.player_move_left == logical_event:
+            self.left_pressed = 1
+        elif settings.player_stop_left == logical_event:
+            self.left_pressed = 0
+            
+        elif settings.player_move_right == logical_event:
+            self.right_pressed = 1
+        elif settings.player_stop_right == logical_event:
+            self.right_pressed = 0
+            
+        elif settings.player_move_up == logical_event:
+            self.up_pressed = 1
+        elif settings.player_stop_up == logical_event:
+            self.up_pressed = 0
+            
+        elif settings.player_move_down == logical_event:
+            self.down_pressed = 1
+        elif settings.player_stop_down == logical_event:
+            self.down_pressed = 0
+            
+        elif settings.player_dir == logical_event:
+            ex, ey = event.pos
+            self.look_at(ex, ey)
+            
+        elif settings.player_stop_flooding == logical_event:
+            self.set_flooding(False)
+        elif settings.player_start_flooding == logical_event:
+            self.set_flooding(True)
+            
+        self.vel = Vec2(self.right_pressed - self.left_pressed, self.down_pressed - self.up_pressed).normalized
+        # print('??? palyer event', logical_event, event, self.position, self.vel)
+        # print(logical_event, self.vel)
 
 class Level01(pyknicpygame.pyknic.context.Context):
 
 
     def enter(self):
         """Called when this context is pushed onto the stack."""
-        self.model = model.Model()
-        self.model.load_level(levels.level02)
+        
+        pygame.event.clear()
+        self.scheduler = Scheduler()
+        self.model = model.Model(self.scheduler)
+        
         
         sw, sh = settings.resolution
         
         self.renderer = pyknicpygame.spritesystem.DefaultRenderer()
         self.cam = pyknicpygame.spritesystem.Camera(pygame.Rect(0, 0, sw, sh), position=Point2(sw/2, sh/2))
         
-        self.info = pyknicpygame.spritesystem.TextSprite("", Point2(0, 0), anchor='bottomleft')
+        self.info = pyknicpygame.spritesystem.TextSprite("", Point2(0, 0), anchor='bottomleft', color=(100, 100, 100))
         self.renderer.add_sprite(self.info)
         
+        self.player = Player(self.model)
         
+        surf = pygame.Surface((10, 10))
+        surf.fill((255, 255, 0))
+        p = Point2()
+        p.copy_values(self.player.position)
+        self.player_spr = pyknicpygame.spritesystem.Sprite(surf, p, anchor='center')
+        self.renderer.add_sprite(self.player_spr)
+        
+        surf = pygame.Surface((5, 5))
+        surf.fill((0, 0, 255))
+        p = Point2()
+        p.copy_values(self.player.position + self.player.dir)
+        self.water_spr = pyknicpygame.spritesystem.Sprite(surf, p, anchor='center')
+        self.renderer.add_sprite(self.water_spr)
+        
+        
+        self._load_level(levels.level04)
+        self.player.set_flooding(False)
+        
+    def _load_level(self, level):
+        self.model.load_level(levels.level04)
+        px, py = levels.level04["player_pos"]
+        self.player.position = Vec2(px, py)
+
     def exit(self):
         """Called when this context is popped off the stack."""
         pass
         pass
     def resume(self):
         """Called when another context is popped off the top of this one."""
-        pass
+        self.player.set_flooding(False)
         
     def think(self, delta_time):
         """Called once per frame"""
-        self.model.update(delta_time)
+        # self.model.update(delta_time)
+        self.scheduler.update(delta_time)
+        self.player.update(delta_time)
+        self.player_spr.position.copy_values(self.player.position)
+        self.water_spr.position.copy_values(self.player.position + self.player.dir)
         for event in pygame.event.get():
+            logical_event = None
             # if pygame.MOUSEBUTTONDOWN == event.type:
-            if pygame.MOUSEMOTION == event.type:
+            if pygame.MOUSEBUTTONDOWN == event.type:
+                # self.player.set_flooding(True)
+                # print(event)
+                logical_event = settings.key_map.get((event.type, event.button), None)
+            elif pygame.MOUSEBUTTONUP == event.type:
+                logical_event = settings.key_map.get((event.type, event.button), None)
+                # self.player.set_flooding(False)
+            elif pygame.MOUSEMOTION == event.type:
+                logical_event = settings.key_map.get((event.type, None), None)
+                
                 x, y = event.pos
-                cx = x // 25
-                cy = y // 25
-                if pygame.mouse.get_pressed() != (0, 0, 0):
-                    amount = 2
-                    self.model.cool_down(cx, cy, 2, amount)
+                # self.player.look_at(x, y)
+                # if pygame.mouse.get_pressed() != (0, 0, 0):
+                    # amount = 2
+                    # self.model.cool_down(x, y, 2, amount)
                 self.info.position.x = x
                 self.info.position.y = y
+                cx = x // 10
+                cy = y // 10
                 cell = self.model.cells.get((cx, cy), None)
                 if cell:
-                    self.info.text = str(cell.temp)
+                    self.info.text = "{0}: {1}".format((cx, cy), str(cell.temp))
                 else:
                     self.info.text = "None"
+                
             elif pygame.QUIT == event.type:
                 pyknicpygame.pyknic.context.pop()
+            elif event.type in [pygame.KEYDOWN, pygame.KEYUP]:
+                logical_event = settings.key_map.get((event.type, event.key), None)
+                
+            if logical_event:
+                self.player.on_event(logical_event, event)
+                
         
     def draw(self, screen):
         """Refresh the screen"""
                 # s +=  "{0}, ".format(str(self.model.cells[(x, y)].temp))
             # print(s)
         screen.fill((0, 0, 0))
-        rw = rh = 25
+        rw = rh = 10
         rect = pygame.Rect(0, 0, rw, rh)
         for y in range(self.model.cells_count_y):
             for x in range(self.model.cells_count_x):

pyweek15/gamelib/levels.py

 __version__ = '$Id: __init__.py 27 2009-05-06 21:37:20Z dr0iddr0id $'
 
 
-level01 = { "cells_count_x" : 3,
+level01 = { 
+
+            "cell_size_x"    : 10,
+            "cell_size_y"    : 10,
+            "cells_count_x" : 3,
             "cells_count_y" : 3,
             "max_temp"      : 1000,
             "temp"          : [   0, 0, 0,
             "fires"         : [ (5, 5, 2, 2, 1),]
             }
 
-level02 = { "cells_count_x" : 10,
+level02 = { 
+
+            "cell_size_x"    : 10,
+            "cell_size_y"    : 10,
+            "cells_count_x" : 10,
             "cells_count_y" : 10,
             "max_temp"      : 1000,
             "temp"          : [   0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
             "fires"         : [ (3, 3, 2, 20000, 0.5, 1),]
             }
 
-level03 = { "cells_count_x" : 10,
+level03 = { 
+            "cell_size_x"    : 10,
+            "cell_size_y"    : 10,
+            "cells_count_x" : 10,
             "cells_count_y" : 10,
             "max_temp"      : 1000,
             "temp"          : [   0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-                                  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+                                  0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                   0, 0, 0, 0, 0, 0, 0, 0, 0, 0,],
-            "ignition"      : [   0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-                                  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-                                  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-                                  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-                                  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-                                  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-                                  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-                                  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-                                  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-                                  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,],
+            "ignition"      : [  10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
+                                 10,  1,  1,  1,  1,  1, 10, 10, 10, 10,
+                                 10, 10, 10, 10, 10,  1, 10, 10, 10, 10,
+                                 10, 10, 10, 10, 10,  1, 10, 10, 10, 10,
+                                 10,  1,  1,  1,  1,  1, 10, 10, 10, 10,
+                                 10,  1, 10, 10, 10, 10, 10, 10, 10, 10,
+                                 10,  1,  0,  0,  0,  0,  0,  0,  0, 10,
+                                 10,  1, 10, 10, 10, 10, 10, 10, 10, 10,
+                                 10,  1,  1,  1,  1,  1, 10, 10, 10, 10,
+                                 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,],
                     # x, y, size, max_size, burn_rate, update_interval
-            "fires"         : [ (5, 5, 2, 20000, 0.5, 1),]
+            "fires"         : [ (1, 1, 2, 20000, 0.1, 1),]
             }
             
+level04 = { 
+            "cell_size_x"    : 10,
+            "cell_size_y"    : 10,
+            "cells_count_x" : 20,
+            "cells_count_y" : 20,
+            "max_temp"      : 100,
+            "player_pos"    : (10, 200),
+            "temp"          : [
+
+
+
+
+
+
+
+
+            
+                                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   
+                                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   
+                                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   
+                                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   
+                                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   
+                                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   
+                                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   
+                                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   
+                                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   
+                                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   
+
+                                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   
+                                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   
+                                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   
+                                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   
+                                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   
+                                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   
+                                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   
+                                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   
+                                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   
+                                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0,     0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   
+                                  ],
+            "ignition"      : [
+
+
+
+
+
+
+
+                                    1, 1, 1, 1, 1, 1, 1, 1, 1, 1,     1, 1, 1, 1, 1, 1, 1, 1, 1, 1,   
+                                    1, 1, 1, 1, 1, 1, 1, 1, 1, 1,     1, 1, 1, 1, 1, 1, 1, 1, 1, 1,   
+                                    1, 1, 1, 1, 1, 1, 1, 1, 1, 1,     1, 1, 1, 1, 1, 1, 1, 1, 1, 1,   
+                                    1, 1, 1, 1, 1, 1, 1, 1, 1, 1,     1, 1, 1, 1, 1, 1, 1, 1, 1, 1,   
+                                    1, 1, 1, 1, 1, 1, 1, 1, 1, 1,     1, 1, 1, 1, 1, 1, 1, 1, 1, 1,   
+                                    1, 1, 1, 1, 1, 1, 1, 1, 1, 1,     1, 1, 1, 1, 1, 1, 1, 1, 1, 1,   
+                                    1, 1, 1, 1, 1, 1, 1, 1, 1, 1,     1, 1, 1, 1, 1, 1, 1, 1, 1, 1,   
+                                    1, 1, 1, 1, 1, 1, 1, 1, 1, 1,     1, 1, 1, 1, 1, 1, 1, 1, 1, 1,   
+                                    1, 1, 1, 1, 1, 1, 1, 1, 1, 1,     1, 1, 1, 1, 1, 1, 1, 1, 1, 1,   
+                                    1, 1, 1, 1, 1, 1, 1, 1, 1, 1,     1, 1, 1, 1, 1, 1, 1, 1, 1, 1,   
+                                    
+                                    1, 1, 1, 1, 1, 1, 1, 1, 1, 1,     1, 1, 1, 1, 1, 1, 1, 1, 1, 1,   
+                                    1, 1, 1, 1, 1, 1, 1, 1, 1, 1,     1, 1, 1, 1, 1, 1, 1, 1, 1, 1,   
+                                    1, 1, 1, 1, 1, 1, 1, 1, 1, 1,     1, 1, 1, 1, 1, 1, 1, 1, 1, 1,   
+                                    1, 1, 1, 1, 1, 1, 1, 1, 1, 1,     1, 1, 1, 1, 1, 1, 1, 1, 1, 1,   
+                                    1, 1, 1, 1, 1, 1, 1, 1, 1, 1,     1, 1, 1, 1, 1, 1, 1, 1, 1, 1,   
+                                    1, 1, 1, 1, 1, 1, 1, 1, 1, 1,     1, 1, 1, 1, 1, 1, 1, 1, 1, 1,   
+                                    1, 1, 1, 1, 1, 1, 1, 1, 1, 1,     1, 1, 1, 1, 1, 1, 1, 1, 1, 1,   
+                                    1, 1, 1, 1, 1, 1, 1, 1, 1, 1,     1, 1, 1, 1, 1, 1, 1, 1, 1, 1,   
+                                    1, 1, 1, 1, 1, 1, 1, 1, 1, 1,     1, 1, 1, 1, 1, 1, 1, 1, 1, 1,   
+                                    1, 1, 1, 1, 1, 1, 1, 1, 1, 1,     1, 1, 1, 1, 1, 1, 1, 1, 1, 1,   
+
+                                    ],
+                    # x, y, size, max_size, burn_rate, update_interval
+            "fires"         : [ (1, 1, 2, 20000, 1, 3),]
+            }
             

pyweek15/gamelib/main.py

     context_len = pyknicpygame.pyknic.context.length
     context_top = pyknicpygame.pyknic.context.top
     
-    lock_stepper.event_integrate.add(lambda ls, dt, simt: context_top().think(dt))
+    lock_stepper.event_integrate.add(lambda ls, dt, simt: context_top().think(dt) if context_top() else None)
     
     while context_len():
         # limit the fps
         dt = clock.tick() / 1000.0 # convert to seconds
         context_top().draw(screen)
         alpha = lock_stepper.update(dt, timestep_seconds=settings.sim_time_step)
-        if __debug__:
-            print('fps: ', str(clock.get_fps()), alpha)
+        # if __debug__:
+            # print('fps: ', str(clock.get_fps()), alpha)
         
     pygame.quit()
     

pyweek15/gamelib/model.py

 log = logging.getLogger('model')
 
 import pyknicpygame.pyknic.timing
-from pyknicpygame.pyknic.timing import Scheduler
+
 
 class Fire(object):
     
 
 class Model(object):
 
-    def __init__(self):
+    def __init__(self, scheduler):
         self.cells = {} # {(x,y) : Cell}
-        self.scheduler = Scheduler()
+        self.scheduler = scheduler
         self.cells_count_x = 0
         self.cells_count_y = 0
         self.max_temp = 100
+        self.cell_size_x = 0
+        self.cell_size_y = 0
+        
 
     def update(self, dt):
-        self.scheduler.update(dt)
+        # self.scheduler.update(dt)
+        pass
         
-    def cool_down(self, cx, cy, size, amount):
+    def cool_down(self, x, y, size, amount):
+        cx = int(x // self.cell_size_x)
+        cy = int(y // self.cell_size_y)
         for y in range(cy - size, cy + size, 1):
             for x in range(cx - size, cx + size, 1):
                 if x < 0 or x >= self.cells_count_x:
                         # cell.fire = None
                         # cell.temp = cell.ignition
                         self.remove_fire(cell)
-                        print("removing fire: {0}, {1}".format(x, y))
+                        # print("removing fire: {0}, {1}".format(x, y))
                         
     def remove_fire(self, cell):
         if cell.has_fire():
 
     def create_fire(self, x, y, size, max_size, burn_rate, update_interval):
         # log.info("creating fire: {0}, {1}, {2}, {3}, {4}, {5}".format(x, y, size, max_size, burn_rate, update_interval))
-        print("creating fire: {0}, {1}, {2}, {3}, {4}, {5}".format(x, y, size, max_size, burn_rate, update_interval))
+        # print("creating fire: {0}, {1}, {2}, {3}, {4}, {5}".format(x, y, size, max_size, burn_rate, update_interval))
         fire = Fire(x, y, size, max_size, burn_rate, update_interval)
         fire.id = self.scheduler.schedule(self._update_fire, update_interval, random.random() * update_interval, fire)
         cell = self.cells.get((x, y), Cell())
                     dist_inv = 1.0 / (((fire.x - x)**2 + (fire.y - y)**2 )**0.5 + 1)
                     cell.temp = cell.temp + fire.burn_rate * dist_inv
                     cell.temp = cell.temp if cell.temp < self.max_temp else self.max_temp
+                    # ignite additional fires
                     if cell.temp > cell.ignition:
                         if not cell.has_fire():
                             self.create_fire(x, y, 1, fire.max_size, fire.burn_rate, fire.update_interval)
     def load_level(self, data):
         self.cells_count_x = data["cells_count_x"]
         self.cells_count_y = data["cells_count_y"]
+        self.cell_size_x = data["cell_size_x"]
+        self.cell_size_y = data["cell_size_y"]
         for y in range(self.cells_count_y):
             for x in range(self.cells_count_x):
                 cell = self.cells.get((x, y), Cell())

pyweek15/gamelib/pyknicpygame/pyknic/mathematics.py

         assert isinstance(other, self.__class__)
         self.x = other.x
         self.y = other.y
-        self.w = other.w
+        # self.w = other.w
 
     def set_from_iterable(self, other):
         """
         self.x = other.x
         self.y = other.y
         self.z = other.z
-        self.w = other.w
+        # self.w = other.w
 
     def set_from_iterable(self, other):
         """Set x,y and z u_sing an iterator on other"""

pyweek15/gamelib/settings.py

 
 __version__ = '$Id: __init__.py 27 2009-05-06 21:37:20Z dr0iddr0id $'
 
+import pygame
+
 
 resolution = (800, 600)
 caption = "pyweek15 - The Fire"  # todo
 
 sim_time_step = 0.025
 
+player_speed = 20
+
+player_move_left = 1000
+player_move_right = 1001
+player_move_up = 1002
+player_move_down = 1003
+player_stop_left = 1004
+player_stop_right = 1005
+player_stop_up = 1006
+player_stop_down = 1007
+player_dir = 1008
+player_start_flooding = 1009
+player_stop_flooding = 1010
+
+key_map = {
+            (pygame.KEYDOWN, pygame.K_a) : player_move_left, 
+            (pygame.KEYDOWN, pygame.K_d) : player_move_right,
+            (pygame.KEYDOWN, pygame.K_w) : player_move_up,
+            (pygame.KEYDOWN, pygame.K_s) : player_move_down,
+            (pygame.KEYUP, pygame.K_a) : player_stop_left, 
+            (pygame.KEYUP, pygame.K_d) : player_stop_right,
+            (pygame.KEYUP, pygame.K_w) : player_stop_up,
+            (pygame.KEYUP, pygame.K_s) : player_stop_down,
+            (pygame.KEYDOWN, pygame.K_LEFT) : player_move_left, 
+            (pygame.KEYDOWN, pygame.K_RIGHT) : player_move_right,
+            (pygame.KEYDOWN, pygame.K_UP) : player_move_up,
+            (pygame.KEYDOWN, pygame.K_DOWN) : player_move_down,
+            (pygame.KEYUP, pygame.K_LEFT) : player_stop_left, 
+            (pygame.KEYUP, pygame.K_RIGHT) : player_stop_right,
+            (pygame.KEYUP, pygame.K_UP) : player_stop_up,
+            (pygame.KEYUP, pygame.K_DOWN) : player_stop_down,
+            (pygame.MOUSEBUTTONDOWN, 1) : player_start_flooding,
+            (pygame.MOUSEBUTTONUP, 1) : player_stop_flooding,
+            (pygame.MOUSEMOTION, None) : player_dir,
+            }
+
+