Найти в Дзене
Сапбот

Love2D. Полная имплементация PONG.

local isLove = false if love ~= nil then lutro = love isLove = true end local ball_x = 10 local ball_y = 10 local ball_speed_x = 1 local ball_speed_y = 1 local points = 0 local paddle_y = 0 local control = require("controllutro") if isLove then control = require("control") end local function round(value) return math.floor(value + 0.5) end local screeny = lutro.graphics.getHeight() local screenx = lutro.graphics.getWidth() local grid_x = round(screenx / 5) local grid_y = round(screeny / 5) - 6 local paddle_hght = round(screeny / 5) local paddle_wght = round(paddle_hght / 6) local font local debugfont local fontsize = 10 if isLove then font = lutro.graphics.newFont(80) debugfont = lutro.graphics.newFont(10) fontsize = 80 end local speed = 6 function lutro.load() if isLove then lutro.window.setMode(640, 480, {["resizable"] = true}) end screenx = lutro.graphics.getWidth() screeny = lutro.graphics.getHeight() grid_x = round(screenx / 5) grid_y = round(screeny / 5) end local function update
Оглавление

main.lua

local isLove = false
if love ~= nil then
lutro = love
isLove = true
end
local ball_x = 10
local ball_y = 10
local ball_speed_x = 1
local ball_speed_y = 1
local points = 0
local paddle_y = 0
local control = require("controllutro")
if isLove then
control = require("control")
end
local function round(value)
return math.floor(value + 0.5)
end
local screeny = lutro.graphics.getHeight()
local screenx = lutro.graphics.getWidth()
local grid_x = round(screenx / 5)
local grid_y = round(screeny / 5) - 6
local paddle_hght = round(screeny / 5)
local paddle_wght = round(paddle_hght / 6)
local font
local debugfont
local fontsize = 10
if isLove then
font = lutro.graphics.newFont(80)
debugfont = lutro.graphics.newFont(10)
fontsize = 80
end
local speed = 6
function lutro.load()
if isLove then
lutro.window.setMode(640, 480, {["resizable"] = true})
end
screenx = lutro.graphics.getWidth()
screeny = lutro.graphics.getHeight()
grid_x = round(screenx / 5)
grid_y = round(screeny / 5)
end
local function updategame()
-- Update ball position
ball_x = ball_x + ball_speed_x
ball_y = ball_y + ball_speed_y
-- Collision with stage edges
if ball_x < 0 then
-- Ball missed the paddle
points = 0 -- Reset points or handle game over logic
ball_x = round(screenx / 100)  -- Optionally reset ball position
ball_y = round(screeny / 10)  -- Reset to the center
ball_speed_x = 1 -- Reset ball speed
elseif ball_x > grid_x then
ball_speed_x = -1
points = points + 1
end
-- Ball collision with paddle
if (ball_x * 5) < paddle_wght and ball_x > 0 then
if (ball_y * 5) > paddle_y and (ball_y * 5) < (paddle_y + paddle_hght) then
-- Ball hits the paddle
ball_speed_x = 1 -- Change direction
points = points + 1 -- Increment points for a successful hit
end
end
-- Ball collision with top and bottom edges
if ball_y < 0 then
ball_speed_y = 1
elseif ball_y > grid_y then
ball_speed_y = -1
end
end
function lutro.resize(w, h)
screenx = w
screeny = h
ball_x = round(screenx / 100)
ball_y = round(screeny / 10)
ball_speed_x = 1
ball_speed_y = 1
-- Update screen
grid_x = round(screenx / 5)
grid_y = round(screeny / 5) - 6
paddle_hght = round(screeny / 5)
paddle_wght = round(paddle_hght / 6)
if isLove then
fontsize = round(screenx / 10)
font = lutro.graphics.newFont(fontsize)
end
speed = round(screeny / 100)
end
function lutro.update()
-- Paddle movement
if control.isDown() and ((paddle_y + paddle_hght) < screeny) then
paddle_y = paddle_y + speed
elseif control.isUp() and (paddle_y > 0) then
paddle_y = paddle_y - speed
end
for i=1,round(speed / 6) do
updategame()
end
end
function lutro.draw()
-- Draw ball
lutro.graphics.circle("fill", ball_x * 5, ball_y * 5, 10)
-- Draw paddle
lutro.graphics.rectangle("fill", 0, paddle_y, paddle_wght, paddle_hght)
-- Draw GUI
if isLove then
lutro.graphics.setFont(font)
end
lutro.graphics.print(tostring(points), round((screenx / 2) - (fontsize / 2)), 10)
if isLove then
lutro.graphics.setFont(debugfont)
lutro.graphics.print("This game been about to support a Lutro, but lazy devs don't fixed the bugs", 10, screeny - 30)
lutro.graphics.print("in functions that my game used.", 10, screeny - 20)
end
end

control.lua

if love ~= nil then
lutro = love
end
local idklol = "c"
function lutro.gamepadpressed(joystick, button)
if button == "dpup" then
idklol = "u"
end
if button == "dpdown" then
idklol = "d"
end
end
function lutro.gamepadreleased(joystick, button)
if button == "dpup" or button == "dpdown" then
idklol = "c"
end
end
local function isUp()
if idklol == "u" then return true end
if lutro.keyboard.isDown("up") then return true end
if lutro.keyboard.isDown("w") then return true end
return false
end
local function isDown()
if idklol == "d" then return true end
if lutro.keyboard.isDown("down") then return true end
if lutro.keyboard.isDown("s") then return true end
return false
end
return {
isUp = isUp, isDown = isDown
}

controllutro.lua

if love ~= nil then
lutro = love
end
local function isUp()
if lutro.keyboard.isDown("up") then return true end
if lutro.keyboard.isDown("w") then return true end
return false
end
local function isDown()
if lutro.keyboard.isDown("down") then return true end
if lutro.keyboard.isDown("s") then return true end
return false
end
return {
isUp = isUp, isDown = isDown
}