This commit is contained in:
2025-01-23 05:45:07 +01:00
parent 18581fca52
commit 7b9cf2063b
4 changed files with 89 additions and 24 deletions

View File

@@ -79,6 +79,9 @@ add_custom_command(TARGET CopyLuaScripts POST_BUILD
${CMAKE_CURRENT_SOURCE_DIR}/lua
$<TARGET_FILE_DIR:${PROJECT_NAME}>/)
# add dependencies
add_dependencies(${PROJECT_NAME} CopyLuaScripts)
add_custom_target(CopyResources ALL
COMMENT "Copying resources to output directory"
)
@@ -88,4 +91,6 @@ add_custom_command(TARGET CopyResources POST_BUILD
${CMAKE_CURRENT_SOURCE_DIR}/resources
$<TARGET_FILE_DIR:${PROJECT_NAME}>/resources)
add_dependencies(${PROJECT_NAME} CopyResources)

View File

@@ -195,6 +195,8 @@ end
function updateLost()
if GameEngine:isKeyDown("R") then
currentGameState = PLAYING
score = 0
ai_score = 0
end
end
@@ -204,12 +206,14 @@ end
function drawWin()
GameEngine:fillScreen(Color.new(0, 0,0))
GameEngine:setColor(Color.new(255,255,255))
GameEngine:drawBitmap(lostBitmap, 0, 0)
GameEngine:drawBitmap(winBitmap, 0, 0)
end
function updateWin()
if GameEngine:isKeyDown("R") then
currentGameState = PLAYING
score = 0
ai_score = 0
end
end
@@ -226,7 +230,7 @@ end
--- the set_keylist function
--- @return string
function set_keylist()
function set_keylist()
return "WASD "
end
@@ -299,6 +303,5 @@ function quit()
print("bye")
local name = GameEngine:getName()
local data = "{ \"name\": \"" .. name .. "\" }"
print(data)
GameEngine:getRequest("https://api.brammie15.dev/game-close", data)
end

View File

@@ -27,6 +27,24 @@ 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
}
local wallKicks = {
-- These values come from the official Super Rotation System (SRS)
-- https://harddrop.com/wiki/SRS
I = {
[0] = { {0, 0}, {-2, 0}, {1, 0}, {-2, -1}, {1, 2} },
[1] = { {0, 0}, {2, 0}, {-1, 0}, {2, 1}, {-1, -2} },
[2] = { {0, 0}, {-1, 0}, {2, 0}, {-1, 2}, {2, -1} },
[3] = { {0, 0}, {1, 0}, {-2, 0}, {1, -2}, {-2, 1} }
},
Default = {
[0] = { {0, 0}, {-1, 0}, {-1, 1}, {0, -2}, {-1, -2} },
[1] = { {0, 0}, {1, 0}, {1, -1}, {0, 2}, {1, 2} },
[2] = { {0, 0}, {1, 0}, {1, 1}, {0, -2}, {1, -2} },
[3] = { {0, 0}, {-1, 0}, {-1, -1}, {0, 2}, {-1, 2} }
}
}
--- endregion
-- Game State
@@ -51,6 +69,8 @@ local SUBMITTING_NAME = 3
local gameState = MAIN_MENU
local combo = 0
local backToBackTetris = false
local nameTextBox
@@ -63,6 +83,10 @@ local leaderboardFrameTimer = 0
local leaderboardFrameIndex = 1
local leaderboardFreamSpeed = 0.05
local FALL_SPEED = 0.5
local linesClearedTotal = 0
local level = 1
--- region Bitmaps
local titleScreenBitmap
@@ -72,6 +96,8 @@ local pressRBitmap
local enterNameBitmap
local boardBitmap
local controlsBitmap
--- endregion
local yesButton = button.new(350, 400, 100, 50, "Yes", Color.new(0, 255, 0), function()
@@ -103,6 +129,16 @@ local function shuffleBag()
end
end
local function getPieceType()
if #currentPiece.shape == 4 then
return "I"
elseif #currentPiece.shape == 2 then
return "O"
else
return "Default"
end
end
local function getNextPiece()
if #pieceBag == 0 then
shuffleBag()
@@ -185,7 +221,6 @@ local function clearLines()
local linesCleared = 0
local newGrid = {}
-- Build a new grid without full lines
for y = 1, GRID_HEIGHT do
local full = true
for x = 1, GRID_WIDTH do
@@ -195,36 +230,40 @@ local function clearLines()
end
end
if not full then
table.insert(newGrid, grid[y]) -- Keep non-full rows
table.insert(newGrid, grid[y])
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
table.insert(newGrid, 1, emptyRow)
end
-- Update the grid reference
grid = newGrid
-- Score calculation based on cleared lines
local scoreTable = { 100, 300, 500, 800 }
-- Update total lines cleared and level
if linesCleared > 0 then
score = score + (scoreTable[linesCleared] or 0)
linesClearedTotal = linesClearedTotal + linesCleared
level = math.floor(linesClearedTotal / 10) + 1 -- Level up every 10 lines
FALL_SPEED = math.max(0.1, 0.5 - (level * 0.05)) -- Speed up, but never below 0.1
end
end
local function freezePiece()
for y = 1, #currentPiece.shape do
for x = 1, #currentPiece.shape[y] do
if currentPiece.shape[y][x] == 1 then
grid[pieceY + y][pieceX + x] = { value = 1, color = currentPiece.color }
local gridY = pieceY + y
local gridX = pieceX + x
-- Ensure it's within bounds before assigning
if gridY >= 1 and gridY <= GRID_HEIGHT and gridX >= 1 and gridX <= GRID_WIDTH then
grid[gridY][gridX] = { value = 1, color = currentPiece.color }
end
end
end
end
@@ -241,8 +280,24 @@ local function rotatePiece()
newShape[x][#currentPiece.shape - y + 1] = currentPiece.shape[y][x]
end
end
if not checkCollision(pieceX, pieceY, { shape = newShape }) then
local pieceType = getPieceType()
if pieceType == "O" then
currentPiece.shape = newShape
return
end
local rotationIndex = (currentPiece.rotation or 0) % 4
local kickTable = wallKicks[pieceType][rotationIndex]
for _, offset in ipairs(kickTable) do
local newX, newY = pieceX + offset[1], pieceY + offset[2]
if not checkCollision(newX, newY, { shape = newShape }) then
currentPiece.shape = newShape
pieceX, pieceY = newX, newY
currentPiece.rotation = (rotationIndex + 1) % 4
return
end
end
end
@@ -328,7 +383,6 @@ function start()
-- load frame0 - 4
for i = 0, 4 do
print("loading frame" .. i)
local bmp = Bitmap.new("resources/leaderboard/frame" .. i .. ".bmp", true)
bmp:SetTransparencyColor(Color.new(255, 0, 255))
table.insert(leaderboardFrames, bmp)
@@ -384,22 +438,23 @@ function update()
pieceY = pieceY + 1
end
if shiftPressed and canHold then
if shiftPressed and not lastKeyState.Shift 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()
currentPiece = getNextPiece()
end
canHold = false -- Prevent multiple swaps until next piece is placed
-- Reset position for the new piece
pieceX, pieceY = 4, 0
canHold = false -- Prevent repeated swaps until the piece is placed
end
if spacePressed and not lastKeyState.Space then
while not checkCollision(pieceX, pieceY + 1, currentPiece) do
pieceY = pieceY + 1
end
local dropDistance = getGhostPieceY() - pieceY
score = score + (dropDistance * 2) -- 2 points per row
pieceY = getGhostPieceY()
freezePiece()
end
@@ -452,6 +507,7 @@ function update()
if GameEngine:isKeyDown("R") then
gameState = PLAYING
score = 0
level = 1
netError = false
hasGottenLeaderBoard = false
leaderboard = {}
@@ -522,6 +578,8 @@ function drawGame()
drawNextPiece() -- Draw the next piece preview
GameEngine:setColor(Color.new(255, 255, 255))
GameEngine:drawText("Score: " .. score, 650, 50)
GameEngine:drawText("Level: " .. level, 650, 75)
end
function drawGameOver()
@@ -569,6 +627,5 @@ function quit()
print("bye")
local name = GameEngine:getName()
local data = "{ \"name\": \"" .. name .. "\" }"
print(data)
GameEngine:getRequest("https://api.brammie15.dev/game-close", data)
end

View File

@@ -71,7 +71,7 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance
GAME_ENGINE->SetGame(new Game(convertedString)); // any class that implements AbstractGame
} else {
GAME_ENGINE->SetGame(new Game("./Pong.lua")); // any class that implements AbstractGame
GAME_ENGINE->SetGame(new Game("./Game_ShittyTetris.lua")); // any class that implements AbstractGame
}