ComputerCraft Archive

FuseFox Demo Game

computer game unknown forum

Description

This is a example program that was made using the FuseFox game engine. I exported it into one file; you can get it with pastebin get 38rh1QX1 fruitgameScreenshot:

Installation

Copy one of these commands into your ComputerCraft terminal:

Pastebin:pastebin get 38rh1QX1 fusefox_demo_game
wget:wget https://pastebin.com/raw/38rh1QX1 fusefox_demo_game
Archive:wget https://cc.shobie.xyz/cc/get/pb-38rh1QX1 fusefox_demo_game
Quick Install: wget https://cc.shobie.xyz/cc/get/pb-38rh1QX1 FuseFox Demo Game

Usage

Run the program after downloading

Tags

gameforumgames

Source

View Original Source

Code Preview

width, height = term.getSize()

local arguments = {...}
if arguments and arguments[1] then
  local outputPath = arguments[2] or arguments[1]..".pin"
  local gameFile = fs.open(arguments[1], "r")
  local game = gameFile:readAll()
  gameFile.close()
  local engineFile = fs.open("engine", "r")
  local engine = engineFile.readAll()
  engineFile.close()

  local outputFile = fs.open(outputPath, "w")
  outputFile.write(engine)
  outputFile.write("\n\nengine=getfenv()\n-- Game Code")
  outputFile.write(game)
  outputFile.close()
  print("Exported game "..arguments[1].." to "..outputPath.." successfully")
end

-- EVENT HANDLER MODULE
mouse = {x = 0, y = 0}
keyboard = {lastChar = ""}
eventHandler = {
  mouse_click = function(button, x, y)
    mouse.x, mouse.y = x, y
    mouse.isDown = true
  end,
  mouse_up = function(button, x, y)
    mouse.x, mouse.y = x, y
    mouse.isDown = false
    mouse.isDragged = false
  end,
  mouse_drag = function(button, x, y)
    mouse.x, mouse.y = x, y
    mouse.isDown = true
    mouse.dragX, mouse.dragY = x, y
  end,

  key = function(key)
    keyboard[keys.getName(key)] = true
  end,
  key_up = function(key)
    keyboard[keys.getName(key)] = false
  end,
  char = function(char)
    keyboard.lastChar = char
    keyboard.newChar = true
  end
}

-- DYNAMIC LIGHT MODULE
dynamicLight = {}
dynamicLight.positions = {
  [0] = {0, -1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}, {-1, -1}, {-1, 0}, {-1, 1}, {0, 1}
}
dynamicLight.lightPixel = function(self, x, y)
  local degree = math.deg(math.atan2(self.lightSource.x-x, self.lightSource.y-y))+180
  local eightDegree = math.floor(degree/45)

  local tileX, tileY = x-dynamicLight.positions[eightDegree][1], y-dynamicLight.positions[eightDegree][2]
  if not (self.light[tileX] and self[tileX] and self[tileX][tileY]) then return end
  if self[tileX] and self[x] then
    if self.light[tileX][tileY] == true and not self[tileX][tileY].solid then
      self.light[x][y] = true
    else
      self.light[x][y] = false
    end
  end
end


-- ELEMENT MODULE
moveTo = function(self, tilemap, xmove, ymove)
  local canMove = true
  for x = 1, self.w do
    for y = 1, self.h do
      local moveToX = self.x+xmove+y-1
      local moveToY = self.y+ymove+y-1
      if moveToX < 1 or moveToX > width or moveToY < 1 or moveToY > height then canMove = false end
      local tile = tilemap:getTile(moveToX, moveToY)
      if (not tile) or tile.solid then canMove = false end
    end
  end
  if canMove then
    self.x = self.x + xmove
    self.y = self.y + ymove
  end
end
isBoxClicked = function(box)
  return mouse.isDown and box.x<=mouse.x and box.y<=mouse.y and box.x+box.w>mouse.x and box.y+box.h>mouse.y
end
newElement = function(elementTab)
  return function(tab)
    return setmetatable(
      tab,
      {
        __index = elementTab
      }
    )
  end
end
elements = {
  tilemap = newElement({
    generated = false,
    getTile = function(self, x, y)
      if self[x] then
        return self[x][y]
      else
        return {}
      end
    end,
    setTile = function(self, x, y, tile)
      if not self[x] then self[x] = {} end
      self[x][y] = self.tileset[tile]
      self[x][y].tile = tile
    end,
    setRectangle = function(self, rx, ry, w, h, tile)
      for x = rx, rx+w do
        for y = ry, ry+h do
          self:setTile(x, y, tile)
        end
      end
    end,
    [1] = {},
    light = {},
    lightUpdateTime = 7,
    DRAW = function(self)
      for y = 1, #self[1] do
        term.setCursorPos(1, y)
        for x = 1, #self do
          local tile = self[x][y]
          if tile then
            if self.dynamicLight and not tile.solid and not self.light[x][y] then
              term.setBackgroundColor(colors.black)
              term.write(" ")
            else
              term.setBackgroundColor(tile.bc)
              term.setTextColor(tile.tc)
              term.write(tile.char)
            end
          else
            term.write(" ")
          end
        end
      end
    end,
    UPDATE = function(self)
      if self.generate and not self.generated then
        self:generate()
        self.generated = true
        if self.dynamicLight then
          for x = 1, #self do
            self.light[x] = {}
          end
        end
      end
      if self.dynamicLight then
        for x = 1, 3 do
          for y = 1, 3 do
            if self.light[self.lightSource.x+x-2] then
              self.light[self.lightSource.x+x-2][self.lightSource.y+y-2] = true
            end
          end
        end

        for i = 1, self.lightUpdateTime do
          for x = 1, #self do
            for y = 1, #self[1] do
              dynamicLight.lightPixel(self, x, y)
            end
          end
        end
      end
    end
  }),
  button = newElement({
    UPDATE = function(self)
      if isBoxClicked(self) then
        if not self.clicked and self.clickedFunction then self:clickedFunction() end
        self.clicked = true
      else
        self.clicked = false
      end
    end,
    DRAW = function(self)
      if self.clicked then
        term.setBackgroundColor(colors.white)
        term.setTextColor(colors.lightGray)
      else
        term.setBackgroundColor(colors.lightGray)
        term.setTextColor(colors.gray)
      end
      paintutils.drawFilledBox(self.x, self.y, self.w+self.x-1, self.h+self.y-1)
      term.setCursorPos(self.x+self.w/2-#self.label/2, self.y+self.h/2)
      term.write(self.label)
    end
  }),
  sprite = newElement({
    path = "",
    DRAW = function(self)
      if not self.image then
        self.image = paintutils.loadImage(self.path)
        self.w = #self.image[1]
        self.h = #self.image
      end
      paintutils.drawImage(self.image, self.x, self.y)
    end
  }),
  template = newElement({
    UPDATE = function(self)
    end,
    DRAW = function(self)
    end
  }),
}


-- LIFECYCLE FUNCTIONS
lastUpdate = os.clock()
runProtected = function(func)
  local succ, mess = pcall(func)
  if not succ then
    quit(mess)
  end
end
update = function(elements, wantedDt, func)
  runProtected(function()
  local timer = os.startTimer(wantedDt)
  local event, var1, var2, var3 = os.pullEventRaw()
  if event == "terminate" then quit(true) end
  os.cancelTimer(timer)

  if event ~= "mouse_drag" then mouse.isDragged = false end
  if event ~= "char" then keyboard.newChar = false end
  if eventHandler[event] then eventHandler[event](var1, var2, var3) end

  if os.clock()-lastUpdate >= wantedDt then
    for elementName, element in pairs(elements) do
      if element.UPDATE then
        element:UPDATE()
      end
      if element.update then
        element:update()
      end
    end
    if func then func() end
    lastUpdate = os.clock()
  end
  end)
end
buffer = window.create(term.current(), 1, 1, width, height)
draw = function(elements, func)
  runProtected(function()
  buffer.setVisible(false)
  local oldterm = term.redirect(buffer)
  term.setBackgroundColor(colors.black)
  term.clear()
  for elementName, element in pairs(elements) do
    if element.DRAW then
      element:DRAW()
    end
    if element.draw then
      element:draw()
    end
  end
  if func then func() end
  buffer.setVisible(true)
  term.redirect(oldterm)
  end)
end
quit = function(hard)
  buffer.setVisible(true)
  term.setBackgroundColor(colors.black)
  term.setTextColor(colors.yellow)
  term.clear()
  term.setCursorPos(1, 1)
  print("Thanks for using the Pinwyn Game Engine by Jummit!")
  if type(hard) == "string" then
    print("The game crashed! Here is the error:")
    term.setTextColor(colors.red)
    print(hard)
  elseif hard then
    print("The game was forcefully closed")
  else
    print("The game was closed without any errors")
  end
  error()
end

engine=getfenv()
-- Game Codeos.loadAPI("engine")

elements = {
  player = {
    fruits = 0,
    path = "player.nfp",
    x = 5, y = 5, w = 1, h = 1,
    moves = {
      up =  {0, -1},
      down= {0,  1},
      left= {-1, 0},
      right={1,  0}
    },
    draw = function(self)
      term.setBackgroundColor(colors.lightBlue)
      term.setTextColor(colors.blue)
      term.setCursorPos(1, 1)
      term.write("Fruits collected: "..self.fruits)
      paintutils.drawPixel(self.x, self.y, colors.blue)
    end,
    update = function(self)
      for key, move in pairs(self.moves) do
        if engine.keyboard[key] then
          engine.moveTo(self, elements.tilemap, move[1], move[2])
        end
      end
      elements.tilemap.lightSource.x = self.x
      elements.tilemap.lightSource.y = self.y
      if elements.tilemap[self.x] and elements.tilemap[self.x][self.y].tile == "fruit" then
        elements.tilemap:setTile(self.x, self.y, "floor")
        self.fruits = self.fruits + 1
        while true do
          local fx, fy = math.random(1, engine.width), math.random(1, engine.height)
          if elements.tilemap:getTile(fx, fy).tile == "floor" then
            elements.tilemap:setTile(fx, fy, "fruit")
            break
          end
        end
      end
    end
  },
  --[[cow = {
    x = 5, y = 10, w = 1, h = 1, move = {1, 0}, isMoving = true,
    draw = function(self)
      paintutils.drawPixel(self.x, self.y, colors.black)
    end,
    update = function(self)
      if math.random(1, 10) == 1 then self.isMoving = not self.isMoving end
      if math.random(1, 10) == 1 then self.move[math.random(1, 2)] = math.random(0, 1) end
    end
  },]]
  tilemap = engine.elements.tilemap({
    dynamicLight = false,
    lightUpdateTime=1,
    lightSource = {x=3, y=3},
    tileset = {
      floor = {bc=colors.green,tc=colors.gray,char=" "},
      grass = {bc=colors.green,tc=colors.lime,char="\""},
      wall = {bc=colors.gray,tc=colors.lightGray,char=" ",solid=true},
      fruit = {bc=colors.red,tc=colors.orange,char="o",solid=false}
    },
    generate = function(self)
      elements.tilemap:setRectangle(1, 1, engine.width, engine.height, "floor")
      for x = 1, 20 do
        self:setTile(math.random(1, engine.width), math.random(1, engine.height), "fruit")
      end
      for x = 1, #self do
        for y = 1, #self[x] do
          if math.random(1, 10) == 1 then
            self:setTile(x, y, "grass")
          end
        end
      end
      for i = 1, 10 do
        elements.tilemap:setRectangle(math.random(1, engine.width-4), math.random(1, engine.height-3), 3, 2, "wall")
      end
    end
  })
}

while not exit do
  engine.update(elements, 0.06, function()
    if engine.keyboard.q then exit = true end
  end)
  engine.draw(elements)
end
engine.quit()