-- annotation.lua contains EmmyLua annotations for cpp_function & cpp_variable package.path = package.path .. ";../?.lua" local utils = require("utils") local vector2 = require("vector2") local player = { position = vector2.new(0, 0), height = 100, speed = 5 } local ball = { x = 0, y = 0, size = 20, xspeed = 5, yspeed = 5, color = Color.new(0,255,255) } local AI = { y = 200, height = 100, speed = 4 } local player_score = 0 local ai_score = 0 local start_timer = 0 local start_timer_max = 60 local next_round = true local countdown_text = 3 function update_player(player) if GameEngine:isKeyDown("W") then if (player.position.y >= 0) then player.position.y = player.position.y - player.speed end end if GameEngine:isKeyDown("S") then if player.position.y + player.height < GameEngine:getHeight() then player.position.y = player.position.y + player.speed end end end function update_ai() if ball.y < AI.y then if(AI.y > 0) then AI.y = AI.y - AI.speed end end if ball.y > AI.y then if AI.y + AI.height < GameEngine:getHeight() then AI.y = AI.y + AI.speed end end end function update_ball() ball.x = ball.x + ball.xspeed ball.y = ball.y + ball.yspeed if utils.check_collision(ball.x, ball.y, ball.size, ball.size, player.position.x, player.position.y, 10, player.height) then ball.xspeed = -ball.xspeed * 1.1 end if utils.check_collision(ball.x, ball.y, ball.size, ball.size, GameEngine:getWidth() - 10, AI.y, 10, AI.height) then ball.xspeed = -ball.xspeed * 1.1 end if ball.x <= 0 then ai_score = ai_score + 1 ball.x = math.floor(GameEngine:getWidth() / 2 - ball.size / 2) ball.y = math.floor(GameEngine:getHeight() / 2 - ball.size / 2) next_round = true start_timer = 0 countdown_text = 3 end if ball.x >= GameEngine:getWidth() - ball.size then player_score = player_score + 1 ball.x = math.floor(GameEngine:getWidth() / 2 - ball.size / 2) ball.y = math.floor(GameEngine:getHeight() / 2 - ball.size / 2) next_round = true start_timer = 0 countdown_text = 3 end if ball.y <= 0 or ball.y >= GameEngine:getHeight() - ball.size then ball.yspeed = -ball.yspeed end end function draw_player() GameEngine:setColor(Color.new(255,255,255)) GameEngine:fillRect(0, math.floor(player.position.y), 10, player.height) end function draw_ai() GameEngine:setColor(Color.new(255,255,0)) GameEngine:fillRect(GameEngine:getWidth() - 10, AI.y, 10, AI.height) end function draw_ball() GameEngine:setColor(ball.color) GameEngine:fillOval(ball.x, ball.y, ball.size, ball.size) end --- the setup function --- @return nil function setup_window() GameEngine:setTitle("BreakOut") GameEngine:setWidth(800) GameEngine:setHeight(600) GameEngine:setFrameRate(60) end --- the set_keylist function --- @return string function set_keylist() return "WASD" end --- the start function --- @return nil function start() ball.x = GameEngine:getWidth() / 2 - ball.size / 2 ball.y = GameEngine:getHeight() / 2 - ball.size / 2 end --- the update function --- @param Engine GameEngine # The GameEngine instance. --- @return nil function update() if (start_timer < start_timer_max and next_round) then start_timer = start_timer + 1 if start_timer % 20 == 0 then countdown_text = countdown_text - 1 end return end if(next_round) then ball.xspeed = 5 ball.yspeed = 5 next_round = false end update_player(player) update_ball() update_ai() end --- the draw function --- @return nil function draw() GameEngine:fillScreen(Color.new(0, 0,0)) -- draw the score GameEngine:setColor(Color.new(255,255,255)) GameEngine:drawText(tostring(player_score), 10, 10) GameEngine:drawText(tostring(ai_score), GameEngine:getWidth() - 20, 10) if next_round then GameEngine:drawText(tostring(countdown_text), GameEngine:getWidth() / 2,GameEngine:getHeight() / 2 - 30) end draw_player() draw_ball() draw_ai() end