<commit_before>require 'gosu'
require_relative 'level.rb'
require_relative 'player.rb'

##
# The main game engine

class Game < Gosu::Window
  attr_reader :player, :level

  ##
  # How much to scale the window by
  SCALE = 3
  ##
  # Width of the window in pixels before scaling
  WIDTH = 32 * 10
  ##
  # Width of the window in pixels before scaling
  HEIGHT = 25 * 10
  ##
  # Light green color
  LIGHT_GREEN = 0xff00ff22
  ##
  # Dark green color
  DARK_GREEN = 0xff00660E
  ##
  # Create the game, setting up the level and the player
  def initialize
    super WIDTH * SCALE, HEIGHT * SCALE, false
    self.caption = "Commander Keen in Revenge of the Shikadi!"

    @level = Level.new(self)

    File.readlines("levels/first.lvl").each do |line|
      x, y = line.split(/\s/)
      @player = Player.new(self, x.to_i, y.to_i)
      break
    end

    @menu_options = [
      :Play,
      :Instructions,
      :"Save/Load",
      :Quit
    ]

    @menu_selection = 0

    @state = :menu
    @paused = false
  end

  ##
  # Update the logic of the game by updating the player
  # and each component of the level
  def update
    if @state == :game
      @player.update
      @level.update
    end
  end

  ##
  # draw the components of the game, the player and each element of the level
  def draw
   if @state == :menu
     i = 0
     for option in @menu_options
       color = option == @menu_options[@menu_selection] ? LIGHT_GREEN : DARK_GREEN
       Gosu::Image.from_text(self, option.to_s, "Times New Roman", 24 * SCALE).draw(100 * SCALE, 50 * i * SCALE + 30 * SCALE, 0, 1, 1, color)
       i += 1
     end
   elsif @state == :instructions
       Gosu::Image.from_text(self, "Use the arrow keys to move the player left and right.\nPress control to jump.\nAlt to toggle the pogo stick.\nSpace to shoot.\n\nCollect candy.\nAvoid enemies.\nGame over if you run out of lives.", "Times New Roman", 12 * SCALE, 10 * SCALE, 250 * SCALE, :left).draw(50 * SCALE, 25 * SCALE, 0, 1, 1, 0xffffffff)
   elsif @state == :game
      @level.draw SCALE, @player.x * SCALE, @player.y * SCALE
      @player.draw SCALE
    end
  end

  ##
  # Callback for a button press
  # If ESC or Q is pressed, quit the game
  # If LEFT_ALT is pressed, toggle the pogo stick
  def button_down(id)
    if id == Gosu::KbQ
      close
    elsif id == Gosu::KbEscape
      if @state == :menu
        close
      elsif @state == :instructions
        @state = :menu
      else
        @state = :menu
        @paused = true
      end
    elsif id == Gosu::KbLeftAlt
      if @state == :game
        @player.toggle_pogo
      end
    elsif id == Gosu::KbLeftShift
      if @state == :game
        @player.sprint
      end
    elsif id == Gosu::KbSpace  
      if @state == :game
        @player.shoot
      end
    elsif id == Gosu::KbDown || id == Gosu::GpDown
      if @state == :menu
        @menu_selection += 1
        @menu_selection %= @menu_options.size
      end
    elsif id == Gosu::KbUp || id == Gosu::GpUp
      if @state == :menu
        @menu_selection += @menu_options.size - 1
        @menu_selection %= @menu_options.size
      elsif @state == :game
        @player.leave
      end
    elsif id == Gosu::KbReturn
      if @state == :menu
        if @menu_options[@menu_selection] == :Play || @menu_options[@menu_selection] == :Resume
          @menu_options[@menu_selection] = :Resume
          @state = :game
          @paused = true
        elsif @menu_options[@menu_selection] == :Instructions
          @state = :instructions
        elsif @menu_options[@menu_selection] == :Quit
          close
        end
      end
    end
  end

  ##
  # load the next level
  def next_level
    @level.level += 1
    @level.load_level("levels/level" + @level.level + ".lvl")
  end

  ##
  # Run the game
  def run
    # show the window
    show
  end
end<commit_msg>load next level on exit<commit_after>require 'gosu'
require_relative 'level.rb'
require_relative 'player.rb'

##
# The main game engine

class Game < Gosu::Window
  attr_reader :player, :level

  ##
  # How much to scale the window by
  SCALE = 3
  ##
  # Width of the window in pixels before scaling
  WIDTH = 32 * 10
  ##
  # Width of the window in pixels before scaling
  HEIGHT = 25 * 10
  ##
  # Light green color
  LIGHT_GREEN = 0xff00ff22
  ##
  # Dark green color
  DARK_GREEN = 0xff00660E
  ##
  # Create the game, setting up the level and the player
  def initialize
    super WIDTH * SCALE, HEIGHT * SCALE, false
    self.caption = "Commander Keen in Revenge of the Shikadi!"

    @level = Level.new(self)

    File.readlines("levels/level0.lvl").each do |line|
      x, y = line.split(/\s/)
      @player = Player.new(self, x.to_i, y.to_i)
      break
    end

    @menu_options = [
      :Play,
      :Instructions,
      :"Save/Load",
      :Quit
    ]

    @menu_selection = 0

    @state = :menu
    @paused = false
  end

  ##
  # Update the logic of the game by updating the player
  # and each component of the level
  def update
    if @state == :game
      @player.update
      @level.update
    end
  end

  ##
  # draw the components of the game, the player and each element of the level
  def draw
   if @state == :menu
     i = 0
     for option in @menu_options
       color = option == @menu_options[@menu_selection] ? LIGHT_GREEN : DARK_GREEN
       Gosu::Image.from_text(self, option.to_s, "Times New Roman", 24 * SCALE).draw(100 * SCALE, 50 * i * SCALE + 30 * SCALE, 0, 1, 1, color)
       i += 1
     end
   elsif @state == :instructions
       Gosu::Image.from_text(self, "Use the arrow keys to move the player left and right.\nPress control to jump.\nAlt to toggle the pogo stick.\nSpace to shoot.\n\nCollect candy.\nAvoid enemies.\nGame over if you run out of lives.", "Times New Roman", 12 * SCALE, 10 * SCALE, 250 * SCALE, :left).draw(50 * SCALE, 25 * SCALE, 0, 1, 1, 0xffffffff)
   elsif @state == :game
      @level.draw SCALE, @player.x * SCALE, @player.y * SCALE
      @player.draw SCALE
    end
  end

  ##
  # Callback for a button press
  # If ESC or Q is pressed, quit the game
  # If LEFT_ALT is pressed, toggle the pogo stick
  def button_down(id)
    if id == Gosu::KbQ
      close
    elsif id == Gosu::KbEscape
      if @state == :menu
        close
      elsif @state == :instructions
        @state = :menu
      else
        @state = :menu
        @paused = true
      end
    elsif id == Gosu::KbLeftAlt
      if @state == :game
        @player.toggle_pogo
      end
    elsif id == Gosu::KbLeftShift
      if @state == :game
        @player.sprint
      end
    elsif id == Gosu::KbSpace  
      if @state == :game
        @player.shoot
      end
    elsif id == Gosu::KbDown || id == Gosu::GpDown
      if @state == :menu
        @menu_selection += 1
        @menu_selection %= @menu_options.size
      end
    elsif id == Gosu::KbUp || id == Gosu::GpUp
      if @state == :menu
        @menu_selection += @menu_options.size - 1
        @menu_selection %= @menu_options.size
      elsif @state == :game
        @player.leave
      end
    elsif id == Gosu::KbReturn
      if @state == :menu
        if @menu_options[@menu_selection] == :Play || @menu_options[@menu_selection] == :Resume
          @menu_options[@menu_selection] = :Resume
          @state = :game
          @paused = true
        elsif @menu_options[@menu_selection] == :Instructions
          @state = :instructions
        elsif @menu_options[@menu_selection] == :Quit
          close
        end
      end
    end
  end

  ##
  # load the next level
  def next_level
    load_level("levels/level" + @level.level.to_s + ".lvl")
  end

  def load_level file_name
    File.readlines(file_name).each do |line|
      x, y = line.split(/\s/)
      @player = Player.new(self, x.to_i, y.to_i)
      break
    end
    @level.level += 1
    @level.load_level file_name
  end

  ##
  # Run the game
  def run
    # show the window
    show
  end
end
{'fraction_non_alphanumeric': 0.10980392156862745, 'fraction_numerical': 0.01353573687539532, 'mean_word_length': 2.264244426094137, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1957913', 'n_tokens_mistral': 2813, 'n_tokens_neox': 2639, 'n_words': 825}