Commits

dr0id  committed 2699577

added lock stepper (making draw and updates independent)
added more test levels

  • Participants
  • Parent commits 9bf10aa

Comments (0)

Files changed (7)

File pyweek15/gamelib/level01.py

 import pygame
 
 import pyknicpygame
+from pyknicpygame.pyknic.mathematics import Point2
+from pyknicpygame.pyknic.mathematics import Vec2
+
 
 import model
 import levels
+import settings
 
 
 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)
+        self.model.load_level(levels.level03)
+        
+        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.renderer.add_sprite(self.info)
+        
         
     def exit(self):
         """Called when this context is popped off the stack."""
         self.model.update(delta_time)
         for event in pygame.event.get():
             # if pygame.MOUSEBUTTONDOWN == event.type:
-            if pygame.MOUSEMOTION == event.type and pygame.mouse.get_pressed() != (0, 0, 0):
+            if pygame.MOUSEMOTION == event.type:
                 x, y = event.pos
                 cx = x // 25
                 cy = y // 25
-                amount = 2
-                self.model.cool_down(cx, cy, 2, amount)
+                if pygame.mouse.get_pressed() != (0, 0, 0):
+                    amount = 2
+                    self.model.cool_down(cx, cy, 2, amount)
+                self.info.position.x = x
+                self.info.position.y = y
+                cell = self.model.cells.get((cx, cy), None)
+                if cell:
+                    self.info.text = str(cell.temp)
+                else:
+                    self.info.text = "None"
             elif pygame.QUIT == event.type:
                 pyknicpygame.pyknic.context.pop()
         
                 rect.topleft = (x*rw, y*rh)
                 color = (max(0, min(255, int(self.model.cells[(x, y)].temp * 10))), )*3
                 screen.fill(color, rect)
+        self.renderer.draw(screen, self.cam)
         pygame.display.flip()

File pyweek15/gamelib/levels.py

 
 level01 = { "cells_count_x" : 3,
             "cells_count_y" : 3,
+            "max_temp"      : 1000,
             "temp"          : [   0, 0, 0,
                                   0, 0, 0,
                                   0, 0, 0,],
             "ignition"      : [  5, 5, 1,
                                  5, 5, 1,
                                  5, 5, 1,],
+                    # x, y, size, max_size, burn_rate, update_interval
             "fires"         : [ (5, 5, 2, 2, 1),]
             }
 
 level02 = { "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, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                  5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
                                  5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
                                  5, 5, 5, 5, 5, 5, 5, 5, 5, 5,],
-                                 # x, y, size, max_size, burn_rate, update_interval
+                    # x, y, size, max_size, burn_rate, update_interval
             "fires"         : [ (3, 3, 2, 20000, 0.5, 1),]
             }
+
+level03 = { "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, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+                                  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+                                  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+                                  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+                                  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+                                  0, 0, 0, 0, 0, 0, 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,],
+                    # x, y, size, max_size, burn_rate, update_interval
+            "fires"         : [ (5, 5, 2, 20000, 0.5, 1),]
+            }
+            
+            

File pyweek15/gamelib/main.py

     co = menu.Menu()
     pyknicpygame.pyknic.context.push(co)
     
+    lock_stepper = pyknicpygame.pyknic.timing.LockStepper()
 
     clock = pygame.time.Clock()
     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))
+    
     while context_len():
-        dt = clock.tick() / 1000.0
+        # limit the fps
+        dt = clock.tick() / 1000.0 # convert to seconds
         context_top().draw(screen)
-        context_top().think(dt)
+        alpha = lock_stepper.update(dt, timestep_seconds=settings.sim_time_step)
+        if __debug__:
+            print('fps: ', str(clock.get_fps()))
         
     pygame.quit()
+    
+    
 
+        
 
 # this is needed in order to work with py2exe
 if __name__ == '__main__':

File pyweek15/gamelib/menu.py

         
     def think(self, delta_time):
         selected_item = None
-        
-        for event in pygame.event.get():
+
+        #todo: use even.wait() here
+        # for event in pygame.event.get():
+        for event in [pygame.event.wait()]:
             if event.type == pygame.QUIT:
                 self.go_exit()
             elif event.type == pygame.KEYDOWN:

File pyweek15/gamelib/model.py

         self.scheduler = Scheduler()
         self.cells_count_x = 0
         self.cells_count_y = 0
+        self.max_temp = 100
 
     def update(self, dt):
         self.scheduler.update(dt)
                 if cell.ignition > 0:
                     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
                     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)
         
-        # todo: fire size growth
+        # todo: fire size growth independent of burn_rate?
+        # todo: fire size growth depending on cell temp?
         if fire.size < fire.max_size:
-            fire.size += 1
+            fire.size += int(1.5 - self.cells[(fire.x, fire.y)].temp / self.max_temp)
         
         if self.cells[(fire.x, fire.y)].temp > 0:
             return fire.update_interval

File pyweek15/gamelib/pyknicpygame/pyknic/timing.py

     TIMESTEP_SECONDS = 0.01
 
 
-    def __init__(self, max_steps=8):
+    def __init__(self, max_steps=8, max_dt=0.05):
         """
         """
         self._accumulator = 0.0
         self._sim_time = 0
         self.event_integrate = events.Signal()
         self.max_steps = max_steps
+        self.max_dt = max_dt
 
     def update(self, dt_seconds, timestep_seconds=TIMESTEP_SECONDS):
         """
         :rtype: float
 
         """
+        # prevent spiral of death
+        dt_seconds = self.max_dt if dt_seconds > self.max_dt else dt_seconds 
+        
         self._accumulator += dt_seconds
         num_steps = 0
         while self._accumulator >= timestep_seconds and \

File pyweek15/gamelib/settings.py

 caption = "pyweek15 - The Fire"  # todo
 
 
+sim_time_step = 0.025
 
-