Files
LuckyShroom/blackjack.lua
2025-08-12 23:30:06 +02:00

119 lines
2.8 KiB
Lua

local love = require("love")
require("karten")
PlayerHand = {}
DealerHand = {}
function LoadBlackjack()
DealerHandPosition = {
x = love.graphics.getWidth() / 2,
y = love.graphics.getHeight() / 4,
}
PlayerHandPosition = {
x = love.graphics.getWidth() / 2,
y = love.graphics.getHeight() / 4 * 2.5,
}
Deck = Karten:createDeck()
Karten:shuffleDeck(Deck)
DealInitialCards()
Cards = love.graphics.newImage("/cards_asset_pack/JustNormalCards.png")
end
function UpdateBlackjack(dt)
dt = dt * 1
end
function DrawBlackjack()
local suits = { ["Clubs"] = 0, ["Diamonds"] = 1, ["Spades"] = 2, ["Hearts"] = 3 }
local tileWidth = Cards:getWidth() / 13
local tileHeight = Cards:getHeight() / 4
for _, card in pairs(PlayerHand) do
local tileX = card.value - 2
local tileY = suits[card.suit]
love.graphics.draw(
Cards,
love.graphics.newQuad(tileX * tileWidth, tileY * tileHeight, tileWidth, tileHeight, Cards:getDimensions()),
PlayerHandPosition.x - tileWidth / 4,
PlayerHandPosition.y,
0,
0.5
)
end
love.graphics.print(GetHandValue(PlayerHand), PlayerHandPosition.x, PlayerHandPosition.y + tileHeight + 5)
for _, card in pairs(DealerHand) do
suits = { ["Clubs"] = 0, ["Diamonds"] = 1, ["Spades"] = 2, ["Hearts"] = 3 }
tileWidth = Cards:getWidth() / 13
tileHeight = Cards:getHeight() / 4
local tileX = math.min(card.value - 2, 9)
local tileY = suits[card.suit]
love.graphics.draw(
Cards,
love.graphics.newQuad(tileX * tileWidth, tileY * tileHeight, tileWidth, tileHeight, Cards:getDimensions()),
DealerHandPosition.x - tileWidth / 4,
DealerHandPosition.y,
0,
0.5
)
end
love.graphics.print(GetHandValue(DealerHand), DealerHandPosition.x, DealerHandPosition.y + tileHeight + 5)
end
function GetHandValue(hand)
local count = 0
local aces = 0
for _, card in pairs(hand) do
if card.value < 10 then
count = count + card.value
elseif card.value < 14 then
count = count + 10
else
count = count + 11
aces = aces + 1
end
end
::aceloop::
if count > 21 and aces > 0 then
for _ = 1, aces do
count = count - 10
aces = aces - 1
goto aceloop
end
end
return count
end
function HandleBlackjackInput(key)
if key == "h" then
Karten:hit(PlayerHand)
if Karten:handValue(PlayerHand) > 21 then
love.graphics.print("Spieler hat verloren!", 50, 200)
end
elseif key == "s" then
-- while Karten:handValue(DealerHand) < 17 do
Karten:hit(DealerHand)
-- end
-- if Karten:handValue(DealerHand) > 21 then
-- print("Dealer hat verloren!", 50, 200)
-- elseif Karten:handValue(PlayerHand) > Karten:handValue(DealerHand) then
-- print("Spieler gewinnt!", 50, 200)
-- else
-- print("Dealer gewinnt!", 50, 200)
-- end
end
end
function DealInitialCards()
for i = 1, 2 do
Karten:hit(PlayerHand)
Karten:hit(DealerHand)
end
end