Files
SE_Exam/lua/script_old.lua
2025-01-22 00:16:29 +01:00

131 lines
2.9 KiB
Lua

-- annotation.lua contains EmmyLua annotations for cpp_function & cpp_variable\
-- ball player
player = {
x = 0,
y = 0,
size = 20
}
avoid_cubes = {}
timer = 0
max_timer = 10
function create_cube(x, y, size, timer_to_explode, exploded, death_timer)
return {
x = x,
y = y,
size = size,
timer_to_explode = timer_to_explode,
death_timer = death_timer,
exploded = exploded
}
end
function create_random_cube()
local x = math.random(0, 800)
local y = math.random(0, 600)
local size = math.random(10, 50)
local timer_to_explode = math.random(50, 200)
local exploded = false
local death_timer = math.random(50, 200)
return create_cube(x, y, size, timer_to_explode, exploded, death_timer)
end
--- @param Engine GameEngine
--- @param cube table
function draw_cube(Engine, cube)
if cube.exploded then
Engine:setColor(Color.new(255, 0, 0))
Engine:fillRect(cube.x, cube.y, cube.size, cube.size)
else
Engine:setColor(Color.new(0, 255, 0))
Engine:drawRect(cube.x, cube.y, cube.size, cube.size)
end
end
function update_cubes()
for i, cube in ipairs(avoid_cubes) do
cube.timer_to_explode = cube.timer_to_explode - 1
if cube.timer_to_explode < 0 then
cube.exploded = true
end
if cube.exploded then
cube.death_timer = cube.death_timer - 1
if cube.death_timer < 0 then
table.remove(avoid_cubes, i)
end
end
end
end
function draw_player(Engine)
Engine:setColor(Color.new(255, 0, 0))
Engine:fillRect(player.x, player.y, player.size, player.size)
end
--- the setup function
--- @param Engine GameEngine # The GameEngine instance.
--- @return nil
function setup_window(Engine)
Engine:setTitle("Hello World new")
Engine:setWidth(800)
Engine:setHeight(600)
Engine:setFrameRate(60)
end
--- the set_keylist function
--- @return string
function set_keylist()
return "WASD"
end
--- the update function
--- @param Engine GameEngine # The GameEngine instance.
--- @return nil
function update(Engine)
if Engine:isKeyDown("W") then
player.y = player.y - 5
end
if Engine:isKeyDown("A") then
player.x = player.x - 5
end
if Engine:isKeyDown("S") then
player.y = player.y + 5
end
if Engine:isKeyDown("D") then
player.x = player.x + 5
end
timer = timer + 1
if timer > max_timer then
table.insert(avoid_cubes, create_random_cube())
timer = 0
end
update_cubes()
player.x = Engine:getMouseX()
player.y = Engine:getMouseY()
end
--- the draw function
--- @param Engine GameEngine # The GameEngine instance.
--- @return nil
function draw(Engine)
Engine:fillScreen(Color.new(0, 0, 0))
draw_player(Engine)
for i, cube in ipairs(avoid_cubes) do
draw_cube(Engine, cube)
end
end