Code improvement (First time Lua-enet)
Posted: Tue May 28, 2019 5:03 pm
Hey guys!
I've been into Love2D for quiete a while now and decided to come in the forums to ask for some suggestions and improvements, especially on this program I just made. It's a simple 2D pong which has the focus on just gameplay and playing with other people.
Please give me feedback on the structure and possible better coding solutions.. I think I took way too long to write the code.
Server conf.lua:
Server main.lua:
Client conf.lua:
Client main.lua:
I've been into Love2D for quiete a while now and decided to come in the forums to ask for some suggestions and improvements, especially on this program I just made. It's a simple 2D pong which has the focus on just gameplay and playing with other people.
Please give me feedback on the structure and possible better coding solutions.. I think I took way too long to write the code.
Server conf.lua:
Code: Select all
function love.conf(tbl)
tbl.window.width = 1200
tbl.window.height = 700
tbl.window.x = 100
tbl.window.y = 100
tbl.window.title = "Server Pong"
tbl.console = false
end
Code: Select all
-- Pong program server --
enet = require "enet"
ipAndPort = "localhost:11000"
connectionStatus = "Offline"
inputText = ""
hostExist = false
connected = false
pointsP1 = 0
pointsP2 = 0
player1X, player1Y, player1W, player1H, player1S = '40', '290', '20', '80', '200'
player2X, player2Y, player2W, player2H, player2S = '1140', '290', '20', '80', '200'
function love.load()
-- Relevant game information --
ballX, ballY, ballW, ballH, ballS = '570', '290', '20', '20','500'
ballMovingLeft = false
ballMovingUp = false
gameStarted = false
ready = false
otherReady = false
end
function love.update(dt)
if hostExist == true then
local event = host:service(0)
while event do
dataPacket = split(event.data, '-')
-- Unpack the received data --
if event.type == "receive" then
if dataPacket[1] == '1' then
player2X, player2Y, player2W, player2H = dataPacket[2], dataPacket[3], dataPacket[4], dataPacket[5]
end
if dataPacket[1] == '2' then
player2X, player2Y, player2W, player2H = dataPacket[2], dataPacket[3], dataPacket[4], dataPacket[5]
end
if dataPacket[1] == '3' then
end
if dataPacket[1] == '4' then
otherReady = true
end
if dataPacket[1] == '5' then
otherReady = false
end
end
if event.type == "connect" then
print(event.peer, "connected.")
connected = true
connectionStatus = "Online"
client = event.peer
end
if event.type == "disconnect" then
print(event.peer, "disconnected.")
connected = false
connectionStatus = "Offline"
end
event = host:service()
end
end
-- Player inputs --
if love.keyboard.isDown("down") then
player1Y = player1Y + player1S * dt
if 620 <= tonumber(player1Y) + tonumber(player1H) then
player1Y = 620 - tonumber(player1H)
end
end
if love.keyboard.isDown("up") then
player1Y = player1Y - player1S * dt
if 20 >= tonumber(player1Y) then
player1Y = '20'
end
end
if love.keyboard.isDown("left") then
player1W = player1W + 1
end
if love.keyboard.isDown("right") then
player1H = player1H + 1
end
-- When connection establishes --
if connected == true then
if gameStarted == true then
-- Ball Logic --
if ballMovingLeft == true then
ballX = ballX - ballS * dt
else
ballX = ballX + ballS * dt
end
if ballMovingUp == true then
ballY = ballY - ballS * dt
else
ballY = ballY + ballS * dt
end
-- Ball collides with upper and lower borders --
if ballY <= 20 then
ballMovingUp = false
end
if ballY >= 600 then
ballMovingUp = true
end
-- Ball collides with right or left border --
if tonumber(ballX) <= 20 then
pointsP2 = pointsP2 + 1
client:send('6-' .. pointsP1 .. '-' .. pointsP2)
love.load()
end
if tonumber(ballX) >= 1160 then
pointsP1 = pointsP1 + 1
client:send('6-' .. pointsP1 .. '-' .. pointsP2 )
love.load()
end
-- Ball collides with player 1 and 2 --
if tonumber(ballX) <= tonumber(player1X) + tonumber(player1W) and tonumber(ballY) <= tonumber(player1Y) + tonumber(player1H) and tonumber(ballY) + tonumber(ballH) >= tonumber(player1Y) then
ballMovingLeft = false
end
if ballX + ballW >= tonumber(player2X) and ballY <= tonumber(player2Y) + tonumber(player2H) and ballY + ballH >= tonumber(player2Y) then
ballMovingLeft = true
end
end
-- End of ball logic --
if ready == true and otherReady == true then
gameStarted = true
client:send('3')
end
-- Send positions to client --
player1Positions = '1' .. '-' .. player1X .. '-' .. player1Y .. '-' .. player1W .. '-' .. player1H
ballPositions = '2' .. '-' .. ballX .. '-' .. ballY .. '-' .. ballW .. '-' .. ballH
client:send(player1Positions)
client:send(ballPositions)
end
end
function love.keypressed(key, scancode, isrepeat)
if key == "escape" then
if connected == true then
client:disconnect()
connected = false
hostExist = false
connectionStatus = "Offline"
love.load()
end
end
if connected == true then
if gameStarted == false then
if key == "r" then
if ready == false then
ready = true
client:send('4')
else
ready = true
client:send('5')
end
end
end
else
if key == "return" then
ipAndPort = inputText
host = enet.host_create(ipAndPort)
inputText = ""
hostExist = true
end
end
end
function love.textinput(input)
if connected == false then
inputText = inputText .. input
end
end
function love.draw()
-- Drawing players --
love.graphics.setColor(0, 0, 255)
love.graphics.rectangle("fill", player1X, player1Y, player1W, player1H)
love.graphics.setColor(255, 0, 0)
love.graphics.rectangle("fill", player2X, player2Y, player2W, player2H)
-- Drawing borders --
love.graphics.setColor(255, 255, 255)
love.graphics.rectangle("line", 20, 20, 1160, 600)
-- Drawing ball --
love.graphics.rectangle("fill", ballX, ballY, ballW, ballH)
-- Drawing prints --
if connected == false then
love.graphics.setColor(255, 0, 0)
else
love.graphics.setColor(0, 255, 0)
end
love.graphics.print(connectionStatus, 20, 660, 0, 2)
if gameStarted == false then
if ready == false then
love.graphics.setColor(255, 0, 0)
else
love.graphics.setColor(0, 255, 0)
end
love.graphics.print("Ready", 20, 630, 0, 2)
if otherReady == false then
love.graphics.setColor(255, 0, 0)
else
love.graphics.setColor(0, 255, 0)
end
love.graphics.print("Ready", 1105, 630, 0, 2)
end
-- Printing score --
love.graphics.setColor(0, 0, 255)
love.graphics.print(pointsP1, 200, 630, 0, 4)
love.graphics.setColor(255, 0, 0)
love.graphics.print(pointsP2, 900, 630, 0, 4)
love.graphics.setColor(255, 255, 255)
love.graphics.print(inputText)
love.graphics.print(ipAndPort, 300, 0)
end
function split(s, delimiter)
result = {}
for match in (s..delimiter):gmatch("(.-)"..delimiter) do
table.insert(result, match)
end
return result
end
Code: Select all
function love.conf(tbl)
tbl.window.width = 1200
tbl.window.height = 700
tbl.window.x = 800
tbl.window.y = 100
tbl.window.title = "Client Pong"
tbl.console = false
end
Code: Select all
-- Pong program client --
enet = require "enet"
ipAndPort = "localhost:11000"
host = enet.host_create()
server = host:connect(ipAndPort)
connectionStatus = "Offline"
inputText = ""
hostExists = false
connected = false
pointsP1 = 0
pointsP2 = 0
player1X, player1Y, player1W, player1H, player1S = '40', '290', '20', '80', '200'
player2X, player2Y, player2W, player2H, player2S = '1140', '290', '20', '80', '200'
dataPacket = {}
dataPacket[1], dataPacket[2], dataPacket[3], dataPacket[4], dataPacket[5] = '0', '0', '0', '0' , '0'
function love.load()
ballX, ballY, ballW, ballH = '570', '290', '20', '20'
gameStarted = false
ready = false
otherReady = false
end
function love.update(dt)
if hostExists == true then
event = host:service(0)
end
while event do
dataPacket = split(event.data, '-')
if event.type == "receive" then
if dataPacket[1] == '1' then
player1X, player1Y, player1W, player1H = dataPacket[2], dataPacket[3], dataPacket[4], dataPacket[5]
end
if dataPacket[1] == '2' then
ballX, ballY, ballW, ballH = dataPacket[2], dataPacket[3], dataPacket[4], dataPacket[5]
end
if dataPacket[1] == '3' then
gameStarted = true
end
if dataPacket[1] == '4' then
otherReady = true
end
if dataPacket[1] == '5' then
otherReady = false
end
if dataPacket[1] == '6' then
pointsP1, pointsP2 = dataPacket[2], dataPacket[3]
love.load()
end
end
if event.type == "connect" then
print(event.peer, "connected.")
event.peer:send( "ping" )
data = event.data
connected = true
connectionStatus = "Online"
end
if event.type == "disconnect" then
print(event.peer, "disconnected.")
connected = false
connectionStatus = "Offline"
end
event = host:service()
end
if love.keyboard.isDown("down") then
player2Y = player2Y + player2S * dt
if 620 <= tonumber(player2Y) + tonumber(player2H) then
player2Y = 620 - tonumber(player2H)
end
end
if love.keyboard.isDown("up") then
player2Y = player2Y - player2S * dt
if 20 >= tonumber(player2Y) then
player2Y = '20'
end
end
if connected == true then
player2Positions = '1' .. '-' .. player2X .. '-' .. player2Y .. '-' .. player2W .. '-' .. player2H
server:send(player2Positions)
end
end
function love.keypressed(key, scancode, isrepeat)
if key == "escape" then
server:disconnect()
connected = false
hostExist = false
connectionStatus = "Offline"
love.load()
end
if connected == true then
if gameStarted == false then
if key == "r" then
if ready == false then
ready = true
server:send('4')
else
ready = false
server:send('5')
end
end
end
else
if key == "return" then
ipAndPort = inputText
server = host:connect(ipAndPort)
inputText = ""
hostExists = true
end
end
end
function love.textinput(input)
if connected == false then
inputText = inputText .. input
end
end
function love.draw()
love.graphics.setColor(0, 0, 255)
love.graphics.rectangle("fill", player1X, player1Y, player1W, player1H)
love.graphics.setColor(255, 0, 0)
love.graphics.rectangle("fill", player2X, player2Y, player2W, player2H)
love.graphics.setColor(255, 255, 255)
love.graphics.rectangle("line", 20, 20, 1160, 600)
love.graphics.rectangle("fill", ballX, ballY, ballW, ballH)
if connected == false then
love.graphics.setColor(255, 0, 0)
else
love.graphics.setColor(0, 255, 0)
end
love.graphics.print(connectionStatus, 20, 660, 0, 2)
if gameStarted == false then
if ready == false then
love.graphics.setColor(255, 0, 0)
else
love.graphics.setColor(0, 255, 0)
end
love.graphics.print("Ready", 1105, 630, 0, 2)
if otherReady == false then
love.graphics.setColor(255, 0, 0)
else
love.graphics.setColor(0, 255, 0)
end
love.graphics.print("Ready", 20, 630, 0, 2)
end
love.graphics.setColor(0, 0, 255)
love.graphics.print(pointsP1, 200, 630, 0, 4)
love.graphics.setColor(255, 0, 0)
love.graphics.print(pointsP2, 900, 630, 0, 4)
love.graphics.setColor(255, 255, 255)
love.graphics.print(inputText)
love.graphics.print(ipAndPort, 300, 0)
end
function split(s, delimiter)
result = {}
for match in (s..delimiter):gmatch("(.-)"..delimiter) do
table.insert(result, match)
end
return result
end