drawing works sometimes

This commit is contained in:
Nico
2025-08-08 13:57:05 +02:00
parent 686f6d8962
commit 1c2e6e5ca2
3 changed files with 104 additions and 77 deletions

View File

@@ -1,53 +1,86 @@
local love = require "love" local love = require("love")
require "karten" require("karten")
spielerHand = {} PlayerHand = {}
DealerHand = {} DealerHand = {}
Deck = {}
function LoadBlackjack() function LoadBlackjack()
Deck = Karten:createDeck() DealerHandPosition = {
Karten:shuffleDeck(Deck) x = love.graphics.getWidth() / 2,
DealInitialCards() y = love.graphics.getHeight() / 4,
Cards = love.graphics.newImage("/cards_asset_pack/CuteCards.png") }
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/CuteCards.png")
end end
function UpdateBlackjack(dt) function UpdateBlackjack(dt)
-- Hier können zukünftige Updates für Blackjack hinzugefügt werden dt = dt * 1
end end
function DrawBlackjack() function DrawBlackjack()
print(love.graphics:getWidth()) for i, card in ipairs(PlayerHand) do
love.graphics.draw(Cards, love.graphics.getWidth() / 2 - 90 , 50, 0, 0.2) local suits = { ["Clubs"] = 0, ["Diamonds"] = 1, ["Spades"] = 2, ["Hearts"] = 3 }
-- love.graphics.print("Spieler Hand: " .. Karten:handValue(spielerHand), 50, 50) local tileWidth = Cards:getWidth() / 15
-- love.graphics.print("Dealer Hand: " .. Karten:handValue(DealerHand), 50, 100) local tileHeight = Cards:getHeight() / 4
-- love.graphics.print("Drücke 'H' für Hit oder 'S' für Stand", 50, 150) local tileX = suits[card.suit]
local tileY = math.min(card.value - 2, 9)
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
for i, card in ipairs(DealerHand) do
local suits = { ["Clubs"] = 0, ["Diamonds"] = 1, ["Spades"] = 2, ["Hearts"] = 3 }
local tileWidth = Cards:getWidth() / 15
local tileHeight = Cards:getHeight() / 4
local tileX = suits[card.suit]
local tileY = math.min(card.value - 2, 9)
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
end end
function HandleBlackjackInput(key) function HandleBlackjackInput(key)
if key == 'h' then if key == "h" then
Karten:hit(spielerHand) Karten:hit(PlayerHand)
if Karten:handValue(spielerHand) > 21 then if Karten:handValue(PlayerHand) > 21 then
love.graphics.print("Spieler hat verloren!", 50, 200) love.graphics.print("Spieler hat verloren!", 50, 200)
end end
elseif key == 's' then elseif key == "s" then
while Karten:handValue(DealerHand) < 17 do -- while Karten:handValue(DealerHand) < 17 do
Karten:hit(DealerHand) Karten:hit(DealerHand)
end -- end
if Karten:handValue(DealerHand) > 21 then -- if Karten:handValue(DealerHand) > 21 then
love.graphics.print("Dealer hat verloren!", 50, 200) -- print("Dealer hat verloren!", 50, 200)
elseif Karten:handValue(spielerHand) > Karten:handValue(DealerHand) then -- elseif Karten:handValue(PlayerHand) > Karten:handValue(DealerHand) then
love.graphics.print("Spieler gewinnt!", 50, 200) -- print("Spieler gewinnt!", 50, 200)
else -- else
love.graphics.print("Dealer gewinnt!", 50, 200) -- print("Dealer gewinnt!", 50, 200)
end -- end
end end
end end
function DealInitialCards() function DealInitialCards()
for i = 1, 2 do for i = 1, 2 do
Karten:hit(spielerHand) Karten:hit(PlayerHand)
Karten:hit(DealerHand) Karten:hit(DealerHand)
end end
end end

Binary file not shown.

After

Width:  |  Height:  |  Size: 40 KiB

View File

@@ -1,56 +1,50 @@
local love = require "love" local love = require("love")
Karten = {} Karten = {}
function Karten:createDeck() function Karten:createDeck()
local deck = {} local deck = {}
local suits = {"Clubs", "Diamonds", "Spades", "Hearts"} local suits = { "Clubs", "Diamonds", "Spades", "Hearts" }
local values = {2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11} -- 10, J, Q, K sind alle 10, A ist 11 local values = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11 } -- 10, J, Q, K sind alle 10, A ist 11
for _, suit in ipairs(suits) do for _, suit in ipairs(suits) do
for _, value in ipairs(values) do for _, value in ipairs(values) do
table.insert(deck, {suit = suit, value = value}) table.insert(deck, { suit = suit, value = value })
end end
end end
TileMap = love.graphics.newImage("/cards_asset_pack/CuteCards.png") TileMap = love.graphics.newImage("/cards_asset_pack/JustNormalCards.png")
return deck return deck
end end
function Karten:shuffleDeck(deck) function Karten:shuffleDeck(deck)
for i = #deck, 2, -1 do for i = #deck, 2, -1 do
local j = love.math.random(i) local j = love.math.random(i)
deck[i], deck[j] = deck[j], deck[i] deck[i], deck[j] = deck[j], deck[i]
end end
end end
function Karten:handValue(hand) function Karten:handValue(hand)
local value = 0 local value = 0
local aces = 0 local aces = 0
for _, card in ipairs(hand) do for _, card in ipairs(hand) do
value = value + card.value value = value + card.value
if card.value == 11 then if card.value == 11 then
aces = aces + 1 aces = aces + 1
end end
end end
while value > 21 and aces > 0 do while value > 21 and aces > 0 do
value = value - 10 value = value - 10
aces = aces - 1 aces = aces - 1
end end
return value return value
end end
function Karten:hit(x, y, hand) function Karten:hit(hand)
if #Deck > 0 then if #Deck > 0 then
local card = table.remove(Deck) local card = table.remove(Deck)
table.insert(hand, card) table.insert(hand, card)
local suits = {["Clubs"] = 0, ["Diamonds"] = 1, ["Spades"] = 2, ["Hearts"] = 3} return card
local tileWidth = TileMap:getWidth() / 4 end
local tileHeight = TileMap:getHeight() / 13
local tileX = suits[card.suit]
local tileY = math.min(card.value - 2, 9)
love.graphics.draw(TileMap, love.graphics.newQuad(tileX * tileWidth, tileY * tileHeight, tileWidth, tileHeight, TileMap:getDimensions()), x, y)
end
end end
return Karten return Karten