Lua Start Code Corrections

I am going to start a thread of the correct lua start Code for the different levels since a lot of them are not correct. If on windows just use ctr + f to search for your level

1 Like

Game Development

  1. Them Bones
--Generators spawn enemies over time.
--Skeletons are afraid of lightstones.

local player = game:spawnHeroXY("champion", 15, 35)
player.attackDamage = 60
player.maxSpeed = 8

game:addSurviveGoal()
game:addDefeatGoal()

--Spawn a "generator"

--Spawn a "lightstone"

--Now beat your game!
  1. Time to Live
-- Pass an argument to addSurviveGoal() to specify a time.


-- This means the player must survive for 20 seconds.
game:addSurviveGoal(20)

-- Spawn a generator with spawnXY
-- Remember to assign the spawned generator to a variable!
-- Use the variable to configure the generator below.

-- Set the generator's spawnType to "munchkin"

-- Use spawnHeroXY to spawn a hero for the player.
-- Remember to assign the spawned hero to a variable!

-- Set the hero's maxHealth to 100

-- Set the hero's attackDamage to 10


-- Play the game!
  1. Seeing is Believing
-- Players like seeing score, so use ui:track()!
-- It will create a user-interface element for them to see.
local hero = game:spawnHeroXY("samurai", 20, 20)

local spawner = game:spawnXY("generator", 50, 50)
spawner.maxHealth = 9001
spawner.spawnType = "munchkin"

-- ui:track() displays an object's property for players to see!
ui.track(game, "time")

game:addSurviveGoal(20)

-- Use ui.track to track game's "defeated" property:

-- Change the hero's attackDamage and maxSpeed:

-- Add more spawners for more enemies on the field:

-- Press play and defeat 10 munchkins or skeletons!

18 Persistence Pays (unverified)

-- You can use a database to store persistent data.
-- Persistent data stays the same between plays of your game!

function onVictory() 
    -- Use db:add(key, value) to add the value of
    -- game.defeated to the database with the key "defeated"
    
end

local generator = game:spawnXY("generator", 60, 40)
generator.spawnType = "munchkin"

local player = game:spawnHeroXY("raider", 15, 15)
player.maxHealth = 80
player.attackDamage = 10

game:addSurviveGoal(20)

-- db stands for database
-- db.add(key, value) increments a value stored in the database.
-- This adds 1 to the "plays" key in the database.
db:add("plays", 1)

--- Show the value of the "plays" key in the db
ui.track(db, "plays")

-- Show the value of the "defeated" property of the game object

-- The code below will run when the player wins the game.


game:on("victory", onVictory)

Game Grove

-- Make your own game by changing the code below!
-- Spawn a maze. Change the number for a different maze!
game:spawnMaze(1)

-- Spawn a hero with spawnHeroXY()
local player = game:spawnHeroXY("champion", 28, 60)
player.attackDamage = 10
player.maxHealth = 200


-- Add at least one goal!
game:addCollectGoal()
game:addSurviveGoal()

-- Spawn some things to collect!
game:spawnXY("gem", 28, 27)
-- You need a key to collect a locked chest.
game:spawnXY("locked-chest", 44, 28)
game:spawnXY("silver-key", 43, 60)
game:spawnXY("potion-medium", 60, 12)

-- Spawn some enemies!
local s1 = game:spawnXY("skeleton", 43, 50)
s1.behavior = "Defends"
local s2 = game:spawnXY("skeleton", 49, 59)
s2.behavior = "Defends"
game:spawnXY("lightstone", 60, 44)

local gen = game:spawnXY("generator", 26, 44)
gen.spawnType = "munchkin"
gen.spawnDelay = 4

game:spawnXY("munchkin", 28, 19)
-- Ogre Spear Throwers have a ranged attack!
game:spawnXY("thrower", 48, 28)
-- This gargoyle shoots fire!
local spewer = game:spawnXY("fire-spewer", 37, 12)
spewer.direction = "horizontal"

-- Track plays and ogres defeated in the database.

--[[Need Help with what below this
--]]
db:add("plays", 1)
ui:track(db, "plays")
ui:track(db, "defeated")

local function onVictory() 
    db:add("defeated", game.defeated)
end

game:on("victory", onVictory)


Main Story
Brown Noise

--Collect the treasure and escape.

-- Prepare the hero and the pet.
pet:moveXY(32, 28)
hero:moveXY(10, 19)
-- Distract the skeleton.
pet:distractionNoise()
-- Sneak while the skeleton is distracted.
hero:moveXY(10, 46)

-- Repear this maneuver to get the treasure:

-- Escape from the dungeon (the red mark):

Underground Business

-- Accumulate 300 gold and escape from the dungeon.

function onSpawn() 
    --Send the pet to walk around the dungeon:
    
    -- Don't forget to return it to the hero:
    
end

pet:on("spawn", onSpawn)

while true do
    -- Guard peasants:
    
    -- When you have 300+ gold move to the red mark:
    
end

Sarven Outpost

--Ogres are attacking a nearby outpost!
-- Command the hero to defend the tiny settlement.
-- Time your patrol with a watch so no ogres can get through.

while true do
    local polarPos = hero.time / 4
    --Number between 20 and 60.
    local xPos = 40 + Math.cos(polarPos) * 20
    -- Number between 14 and 54.
    local yPos = 34 + Math.sin(polarPos) * 20
    hero:moveXY(xPos, yPos)
    --Check for ogres and defeat them!
    -- Make sure to attack while their health is above 0.
    
end

Double Cheek

-- First, defeat 6 ogres.
-- Then collect coins until you have 30 gold.

-- This variable is used for counting ogres.
local defeatedOgres = 0

--This loop is executed while defeatedOgres is less than 6.
while (defeatedOgres < 6) do
    local enemy = hero:findNearestEnemy()
    if (enemy) then
        hero:attack(enemy)
        defeatedOgres = defeatedOgres + 1
    else 
        hero:say("Ogres!")
    end    
end

-- Move to the right side of the map.
hero:moveXY(49, 36)

--This loop is executed while you have less than 30 gold.
while (hero.gold < 30) do
    -- Find and collect coins.
    
    -- Remove this say() message.
    hero:say("I should gather coins!")
end

-- Move to the exit.
hero:moveXY(76, 32)

Canyon of Storms

-- A desert storm is coming!
-- Yaks can detect when a storm is coming.

-- This variable will be used as a condition.
local yak = hero:findNearestEnemy()

-- While there is at least one sand yak:
while (yak) do
    local item = hero:findNearestItem()
    if (item) then
        hero:moveXY(item.pos.x, item.pos.y)
    end
    -- Update the value of the variable yak
    -- with findNearestEnemy()
    
end
-- The yaks have hidden.
-- Move to the red X at the hideout.

No Pain No Gain

-- Change the while condition.
-- Run while hero's health is greater than 200:
while true do -- Δ Change this line!
    hero:moveXY(48, 24)
    hero:moveXY(16, 24)
end

-- Move to Okar.
hero:moveXY(32, 40)

Spinach Power

-- Collect exactly 7 spinach potions.
-- Then you'll be strong enough to defeat the ogres.

local potionCount = 0

-- Wrap the potion collection code inside a while loop.
-- Use a condition to check the potionCount

local item = hero:findNearestItem()
if (item) then
    hero:moveXY(item.pos.x, item.pos.y)
    potionCount = potionCount + 1
end

-- When the while loop is finished,
-- Go and fight!

Don’t Rush, Be Quiet

-- Dodge the cannons and collect 8 gems.
-- Watch out, cannons are ready to fire!
-- Move slow along a special pattern to confuse them.

-- This function returns a value from 0 to 30 (0 <= n < 30)
local function mod30(n) 
    if (n >= 30) then
        return n - 30
    else 
        return n
    end
end

-- This function should return a value from 0 to 40 (0 <= n < 40)
local function mod40(n) 
  -- Use an if-statement to return the correct value.
    
    return n
end

-- You don't need to change the following code:
while true do
    local time = hero.time
    local x = mod30(time) + 25
    local y = mod40(time) + 10
    hero:moveXY(x, y)
end

Bait and Switch

-- Lure ogres in traps.

-- The function makes the hero collect enough gold.
local function collectUntil(enoughGold) 
    -- While the hero's gold less than enoughGold:
    
        --Find a coin and take it: 
        
end

-- Collect gold for one decoy and build it on the red mark.
collectUntil(25)
hero:buildXY("decoy", 40, 52)
--It's better to hide.
hero:moveXY(20, 52);
-- Use collectUntil function to collect 50 gold:

-- Build a decoy on the bone mark:

-- Build a decoy on the wooden mark:

Team Work

-- Gems will disappear soon. You'll need help!

-- findItems() returns an array of items.
local items = hero:findItems()

-- Get the first gem from the array.
-- Don't forget that the first index is 1.
local gem0 = items[1]
-- # Tell Bruno to get gem0
hero:say("Bruno " .. gem0)

-- You can reference the gem without a variable.
hero:say("Matilda " + items[2])

-- Create a variable for the last gem, items[3]:

-- Move to that gem's position using moveXY()

Bank Raid

-- Wait for ogres, defeat them and collect gold.

while true do
    local enemies = hero:findEnemies()
    -- enemyIndex is used to iterate the enemies array.
    local enemyIndex = 1
    -- While enemyIndex is less than enemies.length
    for key, enemy in pairs(enemies) do
        -- Attack the enemy at enemyIndex
        local enemy = enemies[enemyIndex]
        hero:attack(enemy)
        -- Increase enemyIndex by one.
        enemyIndex = enemyIndex + 1
    end
    local coins = hero:findItems()
    -- coinIndex is used to iterate the coins array.
    local coinIndex = 1
    for key, coin in pairs(coins) do
        -- Get a coin from the coins array using coinIndex
        
        -- Collect that coin.
        
        -- Increase coinIndex by one.
        coinIndex = coinIndex + 1
    end
end

Wandering Souls

-- Defeat skeletons and collect lightstones.

while true do
    local enemies = hero:findEnemies()
    for key, enemy in pairs(enemies) do
        -- Attack while enemy has health.
        while (enemy.health > 0) do
            hero:attack(enemy)
        end
    end
        
    local items = hero:findItems()
    -- Iterate over all items.
    for key, item in pairs(items) do
        --While the distance greater than 2:
        
            -- Try to take the item.

    end
end

Marauder

-- Destroy mechs and collect gold from them.

while (true) do
    local coin = hero:findNearestItem()
    -- While a coin exists:
    
       -- Move to the coin.
        
        -- Reassign the coin variable to the nearest item.
        
    local enemy = hero:findNearestEnemy()
    if (enemy) then
        -- While enemy's health is greater than 0.
        
            -- Attack enemy.
            
    end
end

Here are 3 in Blackwoods Forest

Tomb Ghost

-- The only exit is blocked by ogres.
-- Hide from the skeletons and defeat the ogres one by one.

-- This function should attack an enemy and hide.
local hitOrHide = function(target)

    -- If 'target' exists:
    
        -- Attack 'target'.
        
        -- Then move to the red mark.
        
end

while true do
    
    local enemy = hero:findNearestEnemy()
    hitOrHide(enemy)
end

Mail Interceptor

-- Intercept all ogre messengers from ambush. 

local ambushAttack = function(target)
    --  Attack the target if it exists and return to the mark.
    -- Write the function:
    
end

while true do
    local ogre = hero:findNearestEnemy()
    ambushAttack(ogre)
end

Seek-and-hide

-- Gather 4 lightstones to defeat the Brawler.
-- If you find a lightstone, hide.

local checkTakeHide = function(item)
    if item then
        -- The item is here, so take it.
        hero:moveXY(item.pos.x, item.pos.y)
        -- Then move to the center of the camp (40, 34)

    end
end
while true do
    -- Move to the top right X mark.
    hero:moveXY(68, 56);
    -- Search for a lightstone there.
    local lightstone = hero:findNearestItem()
    -- Call checkTakeHide with the argument: lightstone
    checkTakeHide(lightstone)
    
    -- Move to the top left mark.
    
    -- Search for a lightstone.
    
    -- Call the checkTakeHide function.
    -- Pass in the result of your search as an argument.
    
end

1 Like

Another few levels in BlackWoods Forest…

Go Fetch

-- You've been caught in a burl trap!
-- Send your pet to fetch the health potions!

local function goFetch() 
    -- You can use loops in a handler function.
    while true do
        local potion = hero:findNearestItem()
        if potion then
            -- Use pet.fetch to fetch the potion.
            
        end
    end
end

-- When your pet is summoned, it triggers a "spawn" event.
-- This tells your pet to run the goFetch function at the start of the level:
pet:on("spawn", goFetch)

Buddy’s Name B

-- The pet should greet the hero and peasant.

-- Use this function as a handler for "hear" events:
local function sayHello(event) 
    -- The pet says hello:
    pet:say("Salutations.")
end

-- Use the pet's .on("eventType", functionName) method.
-- In this level the pet should run sayHello when "hear"ing something.


-- Then greet the pet and wait for a response.
hero:say("Hello, my friend!")

Guard Dog
Note– The default pet is supposed to be a Cougar as that is what is gotten in a previous level but non-Lua start code mentions a wolf, and previous levels refer to the cougar as a kitty so I changed the comment --Teach your wolf to be a guard dog to a kitty…

-- You can't help the peasants across the river.
-- But, your pet can!
--Teach your kitty to be a guard dog.

local function onSpawn(event) 
    while true do
        -- Pets can find enemies, too.
        local enemy = pet:findNearestEnemy()
        -- If there is an enemy:
        
            -- Then have the pet say something:
            
    end
end

pet:on("spawn", onSpawn)

Forest Jogging

-- Your pet can use pet:moveXY()

local function onSpawn(event)
    while true do
        -- First, move to the left X mark:
        pet:moveXY(9, 24)
        -- Next, move to the top X mark:
        pet:moveXY(30, 43)
        -- Move your pet to the right X mark:
        
        -- Move your pet to the bottom X mark:
        
    end
end

-- Use pet:on() to handle the "spawn" event with onSpawn


-- Cheer on your pet!
-- Don't remove the commands below.
while true do
    hero:say("Good dog!")
    hero:say("You can do it!")
    hero:say("Run Run Run!")
    hero:say("Almost!")
    hero:say("One more lap!")
end

Forest Cannon Dancing

-- Your pet should run back and forth on the X marks.
-- The hero should cheer the whole time!

-- Write the event function onSpawn for the pet.
-- This function should make the wolf run back and forth:
-- First, run to the right mark, then the left one:


pet:on("spawn", onSpawn)
-- Cheer up your pet. Don't remove this:
while true do
    hero:say("Run!!!")
    hero:say("Faster!")
end

Blind Distance

-- Tell the wizard the distance to the coming ogres.

-- This function finds the nearest enemy and returns the distance to it.
local function nearestEnemyDistance()
    local enemy = hero:findNearestEnemy()
    -- If there is no enemy, the function returns 0.
    local result = 0
    if enemy then
       local result = hero:distanceTo(enemy)
    end
    return result
end

while true do
    -- Call nearestEnemyDistance() and
    -- save the result in the variable enemyDistance.
    -- If the enemyDistance is greater than 0: 
    
        -- Say the value of enemyDistance variable.
        
end

Star Shower

-- Pick up coins only if they are closer than 20m.
-- Pick up all gems.

while true do
    local item = hero:findNearestItem()
    local distance = hero:distanceTo(item)
    -- If the item's type is "gem"
    -- OR the distance to the item less than 20 meters:
    
        -- Move to item's position.
        
end