0% found this document useful (0 votes)
8 views2 pages

Character Coin COllection System!23

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

Character Coin COllection System!23

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

-- Define the character and some environment variables

local character = {x = 0, y = 0, coinsCollected = 0, health = 100}


local coinLocations = {}
local obstacleLocations = {}

-- Generate random coin locations


for i = 1, 10 do
table.insert(coinLocations, {x = math.random(-50, 50), y = math.random(-50,
50)})
end

-- Generate random obstacle locations


for i = 1, 5 do
table.insert(obstacleLocations, {x = math.random(-50, 50), y = math.random(-50,
50)})
end

-- Function to print character stats


local function printCharacterStats()
print("Character Stats:")
print("Position: (" .. character.x .. ", " .. character.y .. ")")
print("Coins Collected: " .. character.coinsCollected)
print("Health: " .. character.health)
print("-------------")
end

-- Function to move the character randomly


local function moveCharacter()
local directions = {"up", "down", "left", "right"}
local direction = directions[math.random(1, #directions)]

if direction == "up" then


character.y = character.y + 1
elseif direction == "down" then
character.y = character.y - 1
elseif direction == "left" then
character.x = character.x - 1
elseif direction == "right" then
character.x = character.x + 1
end

print("Moved " .. direction .. " to (" .. character.x .. ", " .. character.y ..


")")
end

-- Function to check if character is on a coin


local function checkForCoin()
for i, coin in ipairs(coinLocations) do
if character.x == coin.x and character.y == coin.y then
character.coinsCollected = character.coinsCollected + 1
table.remove(coinLocations, i)
print("Coin collected! Total coins: " .. character.coinsCollected)
break
end
end
end

-- Function to check if character hit an obstacle


local function checkForObstacle()
for _, obstacle in ipairs(obstacleLocations) do
if character.x == obstacle.x and character.y == obstacle.y then
character.health = character.health - 10
print("Hit an obstacle! Health is now " .. character.health)
if character.health <= 0 then
print("Game Over! The character has no health left.")
return true -- Game over condition
end
end
end
return false
end

-- Main game loop


print("Starting Character Simulation...")
while character.health > 0 and character.coinsCollected < #coinLocations do
moveCharacter()
checkForCoin()
if checkForObstacle() then
break -- End the loop if game over
end
printCharacterStats()
wait(1) -- Pause for a second for readability
end

-- End of game summary


if character.health > 0 then
print("Simulation complete! Coins collected: " .. character.coinsCollected)
else
print("Simulation ended with character's health at zero.")
end

You might also like