Source

pymissile / intermediate.py

import pygame

from scene import Scene, NavigateBackException
from constants import *
from sound import play_dark
from resource import render_cached

class Intermediate(Scene):

  key = 'intermediate'
  
  def __init__(self, game):
    super(Intermediate, self).__init__(game)
    self._back_step = 0
    self._back_timer = 3
    self._added = False
    self._event_handler = {pygame.MOUSEBUTTONUP: self._next_level,
                           pygame.KEYUP: self._next_level}

  def _next_level(self, event):
    if self._back_step < 20: return
    self._state.next_level()
    raise NavigateBackException()
                           
  def enter(self, **kwargs):
    self._back_step = 0
    self._added = False
    self._state = kwargs['state']
    self._background = kwargs['copy']
    self._background_org = kwargs['copy']
    self._next_back_step()
    play_dark()

  def _next_back_step(self):
    if self._back_step >= 20: return
    self._back_step += 1
    self._background = pygame.transform.smoothscale(self._background_org, (SCREEN_WIDTH/self._back_step, SCREEN_HEIGHT/self._back_step))
    self._background = pygame.transform.smoothscale(self._background, (SCREEN_WIDTH, SCREEN_HEIGHT))

  def update_state(self):
    self._back_timer -= 1
    if not self._back_timer:
      self._back_timer = 3
      self._next_back_step()

  def draw_state(self, screen):
    super(Intermediate, self).draw_state(screen)
    if self._back_step < 20: return
    cities = len([c for c in self._state.cities if c])
    silos = len([s for s in self._state.silos if s])
    bonus = cities*500 + silos*300
    # todo: don't do this in draw method
    if not self._added:
      self._state.points += bonus
      self._added = True
    lines = ('LEVEL COMPLETE',
            ' ',
            'CITY BONUS: %i x 500 = %i' % (cities, cities*500),
            'SILO BONUS: %i x 300 = %i' % (silos, silos*300),
            ' ',
            '---------------------------',
            'TOTAL BONUS: %i' % (bonus),
            ' ',
            'TOTAL POINTS: %i' % (self._state.points))
            
    for i, line in enumerate(lines):
      text = render_cached(text=line, color=DINGLEY)
      screen.blit(text, (20, 20*(i+2)))