From 7b9cf2063b8e2085e25ace84eb66f986e7fcb9b4 Mon Sep 17 00:00:00 2001 From: Bram Verhulst Date: Thu, 23 Jan 2025 05:45:07 +0100 Subject: [PATCH] Exam ig --- CmakeLists.txt | 5 + lua/{Pong.lua => GAME_Pong.lua} | 9 +- ...shittyTetris.lua => GAME_ShittyTetris.lua} | 97 +++++++++++++++---- src/GameWinMain.cpp | 2 +- 4 files changed, 89 insertions(+), 24 deletions(-) rename lua/{Pong.lua => GAME_Pong.lua} (97%) rename lua/{shittyTetris.lua => GAME_ShittyTetris.lua} (85%) diff --git a/CmakeLists.txt b/CmakeLists.txt index efe5314..98f8e59 100644 --- a/CmakeLists.txt +++ b/CmakeLists.txt @@ -79,6 +79,9 @@ add_custom_command(TARGET CopyLuaScripts POST_BUILD ${CMAKE_CURRENT_SOURCE_DIR}/lua $/) +# 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 $/resources) +add_dependencies(${PROJECT_NAME} CopyResources) + diff --git a/lua/Pong.lua b/lua/GAME_Pong.lua similarity index 97% rename from lua/Pong.lua rename to lua/GAME_Pong.lua index 88a3fba..470709d 100644 --- a/lua/Pong.lua +++ b/lua/GAME_Pong.lua @@ -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 \ No newline at end of file diff --git a/lua/shittyTetris.lua b/lua/GAME_ShittyTetris.lua similarity index 85% rename from lua/shittyTetris.lua rename to lua/GAME_ShittyTetris.lua index bc0a049..b94abad 100644 --- a/lua/shittyTetris.lua +++ b/lua/GAME_ShittyTetris.lua @@ -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 diff --git a/src/GameWinMain.cpp b/src/GameWinMain.cpp index 556f6e4..45aa264 100644 --- a/src/GameWinMain.cpp +++ b/src/GameWinMain.cpp @@ -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 }