Fix stuff
This commit is contained in:
216
lua/Pong.lua
216
lua/Pong.lua
@@ -21,8 +21,8 @@ local ball = {
|
||||
|
||||
local AI = {
|
||||
y = 200,
|
||||
height = 100,
|
||||
speed = 4
|
||||
height = 75,
|
||||
speed = 3
|
||||
}
|
||||
|
||||
local player_score = 0
|
||||
@@ -35,8 +35,19 @@ local next_round = true
|
||||
local countdown_text = 3
|
||||
|
||||
|
||||
-- Enum for game states
|
||||
local MAIN_MENU = 0
|
||||
local PLAYING = 1
|
||||
local WIN = 2
|
||||
local LOST = 3
|
||||
|
||||
function update_player(player)
|
||||
local currentGameState = MAIN_MENU
|
||||
|
||||
local mainMenuBitmap
|
||||
local winBitmap
|
||||
local lostBitmap
|
||||
|
||||
function update_player()
|
||||
if GameEngine:isKeyDown("W") then
|
||||
if (player.position.y >= 0) then
|
||||
player.position.y = player.position.y - player.speed
|
||||
@@ -51,17 +62,16 @@ function update_player(player)
|
||||
end
|
||||
|
||||
function update_ai()
|
||||
if ball.y < AI.y then
|
||||
if(AI.y > 0) then
|
||||
AI.y = AI.y - AI.speed
|
||||
end
|
||||
local predicted_y = ball.y + ball.yspeed * (math.abs(ball.x - (GameEngine:getWidth() - 10)) / math.abs(ball.xspeed))
|
||||
|
||||
if predicted_y < AI.y + AI.height / 2 then
|
||||
AI.y = AI.y - AI.speed
|
||||
elseif predicted_y > AI.y + AI.height / 2 then
|
||||
AI.y = AI.y + AI.speed
|
||||
end
|
||||
|
||||
if ball.y > AI.y then
|
||||
if AI.y + AI.height < GameEngine:getHeight() then
|
||||
AI.y = AI.y + AI.speed
|
||||
end
|
||||
end
|
||||
-- Keep AI within bounds
|
||||
AI.y = math.max(0, math.min(AI.y, GameEngine:getHeight() - AI.height))
|
||||
end
|
||||
|
||||
function update_ball()
|
||||
@@ -99,10 +109,17 @@ function update_ball()
|
||||
if ball.y <= 0 or ball.y >= GameEngine:getHeight() - ball.size then
|
||||
ball.yspeed = -ball.yspeed
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
||||
function update_score()
|
||||
if player_score >= 5 then
|
||||
currentGameState = WIN
|
||||
end
|
||||
|
||||
if ai_score >= 5 then
|
||||
currentGameState = LOST
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function draw_player()
|
||||
@@ -120,33 +137,34 @@ function draw_ball()
|
||||
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)
|
||||
|
||||
--- region MAIN_MENU
|
||||
function drawMainMenu()
|
||||
GameEngine:setColor(Color.new(255,255,255))
|
||||
-- GameEngine:drawText("Press ENTER to Start", GameEngine:getWidth() / 2 - 50, GameEngine:getHeight() / 2)
|
||||
GameEngine:drawBitmap(mainMenuBitmap, 0, 0)
|
||||
end
|
||||
|
||||
--- the set_keylist function
|
||||
--- @return string
|
||||
function set_keylist()
|
||||
return "WASD"
|
||||
function updateMainMenu()
|
||||
if GameEngine:isKeyDown("RETURN") then
|
||||
currentGameState = PLAYING
|
||||
end
|
||||
end
|
||||
--- endregion
|
||||
|
||||
--- the start function
|
||||
--- @return nil
|
||||
function start()
|
||||
ball.x = GameEngine:getWidth() / 2 - ball.size / 2
|
||||
ball.y = GameEngine:getHeight() / 2 - ball.size / 2
|
||||
|
||||
--- region PLAYING
|
||||
function drawGame()
|
||||
GameEngine:fillScreen(Color.new(0, 0,0))
|
||||
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
|
||||
|
||||
--- the update function
|
||||
--- @param Engine GameEngine # The GameEngine instance.
|
||||
--- @return nil
|
||||
function update()
|
||||
function updateGame()
|
||||
if (start_timer < start_timer_max and next_round) then
|
||||
start_timer = start_timer + 1
|
||||
if start_timer % 20 == 0 then
|
||||
@@ -162,21 +180,125 @@ function update()
|
||||
update_player(player)
|
||||
update_ball()
|
||||
update_ai()
|
||||
update_score()
|
||||
end
|
||||
--- endregion
|
||||
|
||||
|
||||
--- region LOST
|
||||
function drawLost()
|
||||
GameEngine:fillScreen(Color.new(0, 0,0))
|
||||
GameEngine:setColor(Color.new(255,255,255))
|
||||
GameEngine:drawBitmap(lostBitmap, 0, 0)
|
||||
end
|
||||
|
||||
function updateLost()
|
||||
if GameEngine:isKeyDown("R") then
|
||||
currentGameState = PLAYING
|
||||
end
|
||||
end
|
||||
|
||||
--- endregion
|
||||
|
||||
--- region WIN
|
||||
function drawWin()
|
||||
GameEngine:fillScreen(Color.new(0, 0,0))
|
||||
GameEngine:setColor(Color.new(255,255,255))
|
||||
GameEngine:drawBitmap(lostBitmap, 0, 0)
|
||||
end
|
||||
|
||||
function updateWin()
|
||||
if GameEngine:isKeyDown("R") then
|
||||
currentGameState = PLAYING
|
||||
end
|
||||
end
|
||||
|
||||
--- endregion
|
||||
|
||||
--- the setup function
|
||||
--- @return nil
|
||||
function setup_window()
|
||||
GameEngine:setTitle("Pong")
|
||||
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
|
||||
|
||||
-- Don't blame me for logging :p
|
||||
local name = GameEngine:getName()
|
||||
local data = "{ \"name\": \"" .. name .. "\" }"
|
||||
GameEngine:getRequest("https://api.brammie15.dev/game-open", data)
|
||||
|
||||
mainMenuBitmap = Bitmap.new("resources/pong/mainMenu.bmp", true)
|
||||
mainMenuBitmap:SetTransparencyColor(Color.new(255, 0, 255))
|
||||
|
||||
winBitmap = Bitmap.new("resources/pong/win.bmp", true)
|
||||
winBitmap:SetTransparencyColor(Color.new(255, 0, 255))
|
||||
|
||||
lostBitmap = Bitmap.new("resources/pong/lost.bmp", true)
|
||||
lostBitmap:SetTransparencyColor(Color.new(255, 0, 255))
|
||||
end
|
||||
|
||||
--- the update function
|
||||
--- @return nil
|
||||
function update()
|
||||
|
||||
if GameEngine:isKeyDown("ESCAPE") then
|
||||
GameEngine:quit()
|
||||
end
|
||||
if currentGameState == MAIN_MENU then
|
||||
updateMainMenu()
|
||||
end
|
||||
if currentGameState == PLAYING then
|
||||
updateGame()
|
||||
end
|
||||
|
||||
if currentGameState == LOST then
|
||||
updateLost()
|
||||
end
|
||||
|
||||
if currentGameState == WIN then
|
||||
updateWin()
|
||||
end
|
||||
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)
|
||||
if currentGameState == MAIN_MENU then
|
||||
drawMainMenu()
|
||||
end
|
||||
draw_player()
|
||||
draw_ball()
|
||||
draw_ai()
|
||||
|
||||
if currentGameState == PLAYING then
|
||||
drawGame()
|
||||
end
|
||||
|
||||
if currentGameState == LOST then
|
||||
drawLost()
|
||||
end
|
||||
|
||||
if currentGameState == WIN then
|
||||
drawWin()
|
||||
end
|
||||
end
|
||||
|
||||
function quit()
|
||||
print("bye")
|
||||
local name = GameEngine:getName()
|
||||
local data = "{ \"name\": \"" .. name .. "\" }"
|
||||
print(data)
|
||||
GameEngine:getRequest("https://api.brammie15.dev/game-close", data)
|
||||
end
|
||||
@@ -115,6 +115,10 @@ GameEngine = GameEngine
|
||||
---@return nil
|
||||
function GameEngine:setTitle(title) end
|
||||
|
||||
--- Gets the title of the window.
|
||||
--- @return string # The title of the window.
|
||||
function GameEngine:getName() end
|
||||
|
||||
--- Sets the width of the window.
|
||||
--- @param width number # The new width.
|
||||
--- @return nil
|
||||
@@ -138,6 +142,10 @@ function GameEngine:setHeight(height) end
|
||||
--- @return nil
|
||||
function GameEngine:setFrameRate(frameRate) end
|
||||
|
||||
--- Quits the game
|
||||
--- @return nil
|
||||
function GameEngine:quit() end
|
||||
|
||||
--- Sets the drawing color
|
||||
--- @param color Color # The new color.
|
||||
--- @return nil
|
||||
@@ -221,11 +229,19 @@ function GameEngine:isMouseLeftDown() end;
|
||||
--- @return boolean # True if the right mouse button is pressed, false otherwise.
|
||||
function GameEngine:isMouseRightDown() end;
|
||||
|
||||
--- Preform a Get Request
|
||||
--- @param url string # The url to get.
|
||||
--- @param data string # The data to get.
|
||||
--- @return string # The response from the server.
|
||||
function GameEngine:getRequest(url, data) end
|
||||
|
||||
--- overload without data
|
||||
--- Preform a Get Request
|
||||
--- @param url string # The url to get.
|
||||
--- @return string # The response from the server.
|
||||
function GameEngine:getRequest(url) end
|
||||
|
||||
|
||||
--- Preform a Post Request
|
||||
--- @param url string # The url to post.
|
||||
--- @param data string # The data to post.
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
-- Tetris Game using GameEngine
|
||||
|
||||
--- region dependencies
|
||||
local vector2 = require("vector2")
|
||||
local button = require("button")
|
||||
|
||||
-- Constants
|
||||
--- endregion
|
||||
|
||||
--- region Constants
|
||||
local GRID_WIDTH = 10
|
||||
local GRID_HEIGHT = 19
|
||||
local BLOCK_SIZE = 30
|
||||
@@ -12,7 +15,9 @@ local FALL_SPEED = 0.5
|
||||
local screenWidth = 800
|
||||
local screenHeight = 600
|
||||
|
||||
-- Tetromino shapes and colors
|
||||
--- endregion
|
||||
|
||||
--- region Tetrominoes
|
||||
local tetrominoes = {
|
||||
{ shape = {{1, 1, 1, 1}}, color = Color.new(0, 255, 255) }, -- I
|
||||
{ shape = {{1, 1}, {1, 1}}, color = Color.new(255, 255, 0) }, -- O
|
||||
@@ -22,14 +27,21 @@ local tetrominoes = {
|
||||
{ shape = {{1, 1, 1}, {1, 0, 0}}, color = Color.new(255, 165, 0) }, -- L
|
||||
{ shape = {{1, 1, 1}, {0, 0, 1}}, color = Color.new(0, 0, 255) } -- J
|
||||
}
|
||||
--- endregion
|
||||
|
||||
-- Game State
|
||||
local grid = {}
|
||||
local currentPiece = {}
|
||||
local pieceX, pieceY = 4, 0
|
||||
local fallTimer = 0
|
||||
local lastKeyState = { Left = false, Right = false, Down = false, Rotate = false, Space = true }
|
||||
|
||||
local lastKeyState = { Left = false, Right = false, Down = false, Rotate = false, Space = true, Shift = false }
|
||||
local hasGottenLeaderBoard = false
|
||||
local leaderboard = {}
|
||||
local netError = false
|
||||
local nextPiece = {}
|
||||
local pieceBag = {}
|
||||
local heldPiece = nil
|
||||
local canHold = true
|
||||
-- enum of gameState, Main Menu, Playing, Submitting Name, Game Over
|
||||
|
||||
local MAIN_MENU = 0
|
||||
@@ -37,9 +49,8 @@ local PLAYING = 1
|
||||
local GAME_OVER = 2
|
||||
local SUBMITTING_NAME = 3
|
||||
|
||||
local gameState = SUBMITTING_NAME
|
||||
local gameState = MAIN_MENU
|
||||
|
||||
local titleScreenBitmap
|
||||
|
||||
|
||||
local nameTextBox
|
||||
@@ -52,10 +63,16 @@ local leaderboardFrameTimer = 0
|
||||
local leaderboardFrameIndex = 1
|
||||
local leaderboardFreamSpeed = 0.05
|
||||
|
||||
--- region Bitmaps
|
||||
|
||||
local titleScreenBitmap
|
||||
local gameOverBitmap
|
||||
local pressRBitmap
|
||||
|
||||
local enterNameBitmap
|
||||
local boardBitmap
|
||||
local controlsBitmap
|
||||
--- endregion
|
||||
|
||||
local yesButton = button.new(350, 400, 100, 50, "Yes", Color.new(0, 255, 0), function()
|
||||
print("Sending Post")
|
||||
@@ -73,17 +90,6 @@ local yesButton = button.new(350, 400, 100, 50, "Yes", Color.new(0, 255, 0), fun
|
||||
end)
|
||||
local noButton = button.new(350, 500, 100, 50, "No", Color.new(255, 0, 0), function() gameState = GAME_OVER end)
|
||||
|
||||
|
||||
local hasGottenLeaderBoard = false
|
||||
local leaderboard = {}
|
||||
|
||||
local netError = false
|
||||
|
||||
local nextPiece = {}
|
||||
|
||||
local pieceBag = {}
|
||||
|
||||
|
||||
--- region BagStuff
|
||||
local function shuffleBag()
|
||||
pieceBag = {}
|
||||
@@ -177,7 +183,10 @@ end
|
||||
|
||||
local function clearLines()
|
||||
local linesCleared = 0
|
||||
for y = GRID_HEIGHT, 1, -1 do
|
||||
local newGrid = {}
|
||||
|
||||
-- Build a new grid without full lines
|
||||
for y = 1, GRID_HEIGHT do
|
||||
local full = true
|
||||
for x = 1, GRID_WIDTH do
|
||||
if grid[y][x].value == 0 then
|
||||
@@ -185,20 +194,29 @@ local function clearLines()
|
||||
break
|
||||
end
|
||||
end
|
||||
if full then
|
||||
table.remove(grid, y)
|
||||
table.insert(grid, 1, {})
|
||||
for x = 1, GRID_WIDTH do
|
||||
grid[1][x] = { value = 0, color = Color.new(255, 255, 255) }
|
||||
end
|
||||
if not full then
|
||||
table.insert(newGrid, grid[y]) -- Keep non-full rows
|
||||
else
|
||||
linesCleared = linesCleared + 1
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
-- Add empty rows at the top
|
||||
while #newGrid < GRID_HEIGHT do
|
||||
local emptyRow = {}
|
||||
for x = 1, GRID_WIDTH do
|
||||
emptyRow[x] = { value = 0, color = Color.new(255, 255, 255) }
|
||||
end
|
||||
table.insert(newGrid, 1, emptyRow) -- Add empty row at the top
|
||||
end
|
||||
|
||||
-- Update the grid reference
|
||||
grid = newGrid
|
||||
|
||||
-- Score calculation based on cleared lines
|
||||
local scoreTable = { 100, 300, 500, 800 }
|
||||
if linesCleared > 0 then
|
||||
score = score + scoreTable[linesCleared] or 0
|
||||
score = score + (scoreTable[linesCleared] or 0)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -212,6 +230,7 @@ local function freezePiece()
|
||||
end
|
||||
clearLines()
|
||||
newPiece()
|
||||
canHold = true
|
||||
end
|
||||
|
||||
local function rotatePiece()
|
||||
@@ -252,6 +271,21 @@ local function drawPiece()
|
||||
end
|
||||
end
|
||||
end
|
||||
local function drawHeldPiece()
|
||||
GameEngine:setColor(Color.new(255, 255, 255))
|
||||
GameEngine:drawText("Hold:", 650, 300)
|
||||
|
||||
if heldPiece then
|
||||
for y = 1, #heldPiece.shape do
|
||||
for x = 1, #heldPiece.shape[y] do
|
||||
if heldPiece.shape[y][x] == 1 then
|
||||
GameEngine:setColor(heldPiece.color)
|
||||
GameEngine:fillRect(650 + x * BLOCK_SIZE, 320 + y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function setup_window()
|
||||
GameEngine:setTitle("Tetris")
|
||||
@@ -275,6 +309,11 @@ function start()
|
||||
end
|
||||
end
|
||||
|
||||
-- Don't blame me for logging :p
|
||||
local name = GameEngine:getName()
|
||||
local data = "{ \"name\": \"" .. name .. "\" }"
|
||||
GameEngine:getRequest("https://api.brammie15.dev/game-open", data)
|
||||
|
||||
nextPiece = getRandomPiece()
|
||||
newPiece()
|
||||
|
||||
@@ -287,7 +326,6 @@ function start()
|
||||
|
||||
titleScreenBitmap = Bitmap.new("resources/tetrisLogo.bmp", true)
|
||||
|
||||
|
||||
-- load frame0 - 4
|
||||
for i = 0, 4 do
|
||||
print("loading frame" .. i)
|
||||
@@ -304,11 +342,19 @@ function start()
|
||||
|
||||
enterNameBitmap = Bitmap.new("resources/leaderboard/enter_name.bmp", true)
|
||||
enterNameBitmap:SetTransparencyColor(Color.new(255, 0, 255))
|
||||
|
||||
|
||||
boardBitmap = Bitmap.new("resources/board.bmp", true)
|
||||
boardBitmap:SetTransparencyColor(Color.new(255, 0, 255))
|
||||
|
||||
controlsBitmap = Bitmap.new("resources/controls.bmp", true)
|
||||
controlsBitmap:SetTransparencyColor(Color.new(255, 0, 255))
|
||||
end
|
||||
|
||||
function update()
|
||||
-- print(GameEngine:getMouseX(), GameEngine:getMouseY())
|
||||
if GameEngine:isKeyDown("ESCAPE") then
|
||||
GameEngine:quit()
|
||||
end
|
||||
if gameState == PLAYING then
|
||||
|
||||
fallTimer = fallTimer + 1 / 60
|
||||
@@ -321,12 +367,12 @@ function update()
|
||||
fallTimer = 0
|
||||
end
|
||||
|
||||
|
||||
local leftPressed = GameEngine:isKeyDown("A")
|
||||
local rightPressed = GameEngine:isKeyDown("D")
|
||||
local downPressed = GameEngine:isKeyDown("S")
|
||||
local rotatePressed = GameEngine:isKeyDown("W")
|
||||
local leftPressed = GameEngine:isKeyDown("A") or GameEngine:isKeyDown("LEFT")
|
||||
local rightPressed = GameEngine:isKeyDown("D") or GameEngine:isKeyDown("RIGHT")
|
||||
local downPressed = GameEngine:isKeyDown("S") or GameEngine:isKeyDown("DOWN")
|
||||
local rotatePressed = GameEngine:isKeyDown("W") or GameEngine:isKeyDown("UP")
|
||||
local spacePressed = GameEngine:isKeyDown(" ")
|
||||
local shiftPressed = GameEngine:isKeyDown("SHIFT")
|
||||
|
||||
if leftPressed and not lastKeyState.Left and not checkCollision(pieceX - 1, pieceY, currentPiece) then
|
||||
pieceX = pieceX - 1
|
||||
@@ -338,6 +384,18 @@ function update()
|
||||
pieceY = pieceY + 1
|
||||
end
|
||||
|
||||
if shiftPressed and canHold then
|
||||
if heldPiece then
|
||||
-- Swap current piece with held piece
|
||||
currentPiece, heldPiece = heldPiece, currentPiece
|
||||
else
|
||||
-- Store the current piece and generate a new one
|
||||
heldPiece = currentPiece
|
||||
newPiece()
|
||||
end
|
||||
canHold = false -- Prevent multiple swaps until next piece is placed
|
||||
end
|
||||
|
||||
if spacePressed and not lastKeyState.Space then
|
||||
while not checkCollision(pieceX, pieceY + 1, currentPiece) do
|
||||
pieceY = pieceY + 1
|
||||
@@ -345,17 +403,16 @@ function update()
|
||||
freezePiece()
|
||||
end
|
||||
|
||||
|
||||
if rotatePressed and not lastKeyState.Rotate then
|
||||
rotatePiece()
|
||||
end
|
||||
|
||||
|
||||
lastKeyState.Left = leftPressed
|
||||
lastKeyState.Right = rightPressed
|
||||
lastKeyState.Down = downPressed
|
||||
lastKeyState.Rotate = rotatePressed
|
||||
lastKeyState.Space = spacePressed
|
||||
lastKeyState.Shift = shiftPressed
|
||||
end
|
||||
|
||||
if gameState == GAME_OVER then
|
||||
@@ -370,6 +427,28 @@ function update()
|
||||
leaderboardFrameTimer = 0
|
||||
end
|
||||
|
||||
if not hasGottenLeaderBoard then
|
||||
|
||||
local response = GameEngine:getRequest("https://api.brammie15.dev/leaderboard/tetris", "")
|
||||
if response == "Error: Request timed out" then
|
||||
netError = true
|
||||
end
|
||||
|
||||
print(response)
|
||||
|
||||
-- format is
|
||||
-- NAME SCORE
|
||||
|
||||
if not netError then
|
||||
for line in response:gmatch("[^\r\n]+") do
|
||||
local name, score = line:match("([^%s]+)%s+(%d+)")
|
||||
table.insert(leaderboard, { name = name, score = tonumber(score) })
|
||||
end
|
||||
table.sort(leaderboard, function(a, b) return a.score > b.score end)
|
||||
end
|
||||
hasGottenLeaderBoard = true
|
||||
end
|
||||
|
||||
if GameEngine:isKeyDown("R") then
|
||||
gameState = PLAYING
|
||||
score = 0
|
||||
@@ -401,28 +480,6 @@ function update()
|
||||
end
|
||||
|
||||
function drawScoreBoard()
|
||||
if not hasGottenLeaderBoard then
|
||||
|
||||
local response = GameEngine:getRequest("https://api.brammie15.dev/leaderboard/tetris")
|
||||
if response == "Error: Request timed out" then
|
||||
netError = true
|
||||
end
|
||||
|
||||
print(response)
|
||||
|
||||
-- format is
|
||||
-- NAME SCORE
|
||||
|
||||
if not netError then
|
||||
for line in response:gmatch("[^\r\n]+") do
|
||||
local name, score = line:match("([^%s]+)%s+(%d+)")
|
||||
table.insert(leaderboard, { name = name, score = tonumber(score) })
|
||||
end
|
||||
table.sort(leaderboard, function(a, b) return a.score > b.score end)
|
||||
end
|
||||
hasGottenLeaderBoard = true
|
||||
end
|
||||
|
||||
GameEngine:setColor(Color.new(255, 0, 0))
|
||||
GameEngine:drawText("Score: " .. score, 350, 400)
|
||||
|
||||
@@ -449,14 +506,19 @@ function drawScoreBoard()
|
||||
GameEngine:drawText(leaderboard[i].score, ScoresX, Y)
|
||||
Y = Y + 20
|
||||
end
|
||||
else
|
||||
GameEngine:setColor(Color.new(255, 255, 255))
|
||||
GameEngine:drawText("Loading Leaderboard...", 600, 100)
|
||||
end
|
||||
end
|
||||
|
||||
function drawGame()
|
||||
GameEngine:fillScreen(Color.new(0, 0, 0))
|
||||
drawGrid()
|
||||
GameEngine:drawBitmap(boardBitmap, 0, 0)
|
||||
drawGhostPiece()
|
||||
drawPiece()
|
||||
drawHeldPiece()
|
||||
drawNextPiece() -- Draw the next piece preview
|
||||
GameEngine:setColor(Color.new(255, 255, 255))
|
||||
GameEngine:drawText("Score: " .. score, 650, 50)
|
||||
@@ -481,10 +543,7 @@ end
|
||||
|
||||
function drawMainMenu()
|
||||
GameEngine:drawBitmap(titleScreenBitmap, 0, 0)
|
||||
|
||||
--Press space to start
|
||||
GameEngine:setColor(Color.new(255, 255, 255))
|
||||
GameEngine:drawText("Press Space to Start", 350, 500)
|
||||
GameEngine:drawBitmap(controlsBitmap, 0, 0)
|
||||
end
|
||||
|
||||
function draw()
|
||||
@@ -505,3 +564,11 @@ function draw()
|
||||
drawMainMenu()
|
||||
end
|
||||
end
|
||||
|
||||
function quit()
|
||||
print("bye")
|
||||
local name = GameEngine:getName()
|
||||
local data = "{ \"name\": \"" .. name .. "\" }"
|
||||
print(data)
|
||||
GameEngine:getRequest("https://api.brammie15.dev/game-close", data)
|
||||
end
|
||||
Reference in New Issue
Block a user