def test_blinker(self):
        world = [
            ['.', '.', '.'],
            ['a', 'a', 'a'],
            ['.', '.', '.'],
        ]

        world_helper.updateWorld(world)

        expected = [
            ['.', 'a', '.'],
            ['.', 'a', '.'],
            ['.', 'a', '.'],
        ]

        self.assertEqual(world, expected)
Exemple #2
0
def main():
    pygame.init()

    win = pygame.display.set_mode((cfg['screen_width'], cfg['screen_height']))
    pygame.display.set_caption('Life')

    cell_size = cfg['cell_size']
    cell_count_across = int(cfg['screen_width'] / cell_size)
    cell_count_down = int(cfg['screen_height'] / cell_size)

    world = world_helper.generateSeed(cell_count_across, cell_count_down)

    color_type = cfg['starting_color_type']

    generation_count = 0
    run = True
    while run:
        # Events listener. Right now it's only listening for QUIT
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_q:
                    run = False

        # Clear the screen
        win.fill((0, 0, 0))

        # Update the world object based on the rules for the next tick
        world_helper.updateWorld(world)

        # Render it
        renderWorld(win, world, color_type)

        pygame.display.update()
        pygame.time.delay(cfg['tick_delay'])

        generation_count += 1

    print(f'\nThis Game of Life ran for {generation_count} generations.')
    print('Thanks for playing. Goodbye.')

    pygame.quit()
Exemple #3
0
def update(dt):
  """
  Update game. Called once per frame.
  dt is the amount of time passed since last frame.
  If you want to have constant apparent movement no matter your framerate,
  what you can do is something like
  
  x += v * dt
  
  and this will scale your velocity based on time. Extend as necessary."""
  
  # Go through events that are passed to the script by the window.

  for event in pygame.event.get():

    # We need to handle these events. Initially the only one you'll want to care
    # about is the QUIT event, because if you don't handle it, your game will crash
    # whenever someone tries to exit.
    if event.type == QUIT:
      pygame.quit() # Opposite of pygame.init
      sys.exit() # Not including this line crashes the script on Windows. Possibly
      # on other operating systems too, but I don't know for sure.
    # Handle other events as you wish.

    if event.type == KEYDOWN:
      if event.key == pygame.K_w:
        player.moveY = -player.SPEED
      if event.key == pygame.K_s:
        player.moveY = player.SPEED
      if event.key == pygame.K_a:
        player.moveX = -player.SPEED
      if event.key == pygame.K_d:
        player.moveX = player.SPEED
      if event.key == pygame.K_e:
        if player.choppable and player.wood < 5:
          if player.thirsty or player.hungry:
            if "Oof" not in player.speech:
              player.speech.append("Oof")
          else:
            if world.trees[player.closestTree].length > 1:
              world.trees[player.closestTree].length -= 1
              player.wood += 1
              player.hunger += 5
              player.thirst += 5
      if event.key == pygame.K_j:
        if not player.hasJacket:
          if player.leather >= 2:
            player.hasJacket = True
            player.leather -= 2
      if event.key == pygame.K_h:
        if player.shelterValid():          
          if not world.shelterBuilt:
            if player.wood >= 3:
              world.createShelter()
              player.wood -= 3
            else:
              if "Not enough wood" not in player.speech:
                player.speech.append("Not enough wood")
          else:
            if "Cannot build here" not in player.speech:
              player.speech.append("Cannot build here")
        else:
          if "Cannot build here" not in player.speech:
            player.speech.append("Cannot build here")
      if event.key == pygame.K_f:
        if player.campfireValid():
          if player.wood >= 1:
            world.createFire()
            player.wood -= 1
      if event.key == pygame.K_1:
        player.eat()
      if event.key == pygame.K_2:
        player.drank()
      if event.key == pygame.K_SPACE and player.combatValid:
        if player.thirst > 70 or player.hunger > 70 or player.comfort < 30:
          if "Oof" not in player.speech:
            player.speech.append("Oof")
        else:
          if not world.animals[player.combatIndex].dead:
            world.animals[player.combatIndex].health -= 20 
            player.hunger += 10
            player.thirst += 10
          else:
            player.leather += 1
            world.animals.pop(player.combatIndex)

    if event.type == KEYUP:
      if event.key == pygame.K_w:
        player.moveY = 0
      elif event.key == pygame.K_s:
        player.moveY = 0
      elif event.key == pygame.K_a:
        player.moveX = 0
      elif event.key == pygame.K_d:
        player.moveX = 0
  # Has player nommed?
  if len(player.noms) > 0:
    player.nomTime += 1
    if player.nomTime >= 60:
      player.noms.pop(0)
      player.nomTime = 0
  if not player.gameOver:
    player.talk()
    player.gotWood()
    world.updateWorld()
    player.update()
    for animal in world.animals:
      animal.update()