#π Betting system in blackjack - just need help modifying a few lines
140 messages Β· Page 1 of 1 (latest)
@topaz gyro
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Can you share your code?
Will do
true
wow that long
Issue i have is when i win or dealer busts and i win my bet money, the balance doesnt update to register it
I'm not really familiar with pygame, but the first thing I notice is that it's very odd having your Play class nested inside your main function
I did that because I made a menu
I can't think of a single reason why you'd need to nest a class
can you show about what line is the balance adding code is
Im not sure, i did it and it all works so haha
until it doesn't
game_finish("You Won.", 450, 30, white)
self.balance += (self.bet * 2) # Add the win amount to the balance
game_balance(f"Balance: {self.balance}", 100, 330, white)
self.bet = 0 # current bet
self.current_bet = 0 # current bet display
time.sleep(3)
self.play_or_exit()```
TextSurf, TextRect = text_objects(text, textfont)
TextSurf = font.render(text, True, color)
TextRect.center = (x, y)
gameDisplay.fill(background_color, pygame.Rect(x-100, y-30, 200, 50)) # Clear the old text
gameDisplay.blit(TextSurf, TextRect)```
game_texts(f"Current Bet: {play_blackjack.current_bet}", 100, 370, white)
# Display the current bet and balance
game_texts(f"Current Bet: {play_blackjack.current_bet}", 100, 370, white)
game_balance(f"Balance: {play_blackjack.current_balance}", 100, 330, white)```
which line do you think is modifying the balance?
self.balance += (self.bet * 2)
and then it reprints
game_balance(f"Balance: {self.balance}", 100, 330, white)
and then you call self.play_or_exit()
Yeah
def play_or_exit(self):
time.sleep(1)
self.reset_balance()
and there you self.reset_balance()
self.balance = self.balance
game_balance(f"Balance: {self.balance}", 100, 330, white)```
am to dumb for this
Yeah, this is quite a big undertaking if you're new
had problems with displaying cards and stuff
true
but i fix
using class when you new
now it just balance
can you show that?
from constants import *
class Deck:
def __init__(self):
self.cards = []
self.build()
def build(self):
for value in RANKS:
for suit in SUITS:
self.cards.append((value, suit))
def shuffle(self):
random.shuffle(self.cards)
def deal(self):
if len(self.cards) > 1:
return self.cards.pop()
class Hand(Deck):
def __init__(self):
self.cards = []
self.card_img = []
self.value = 0
def add_card(self, card):
self.cards.append(card)
def calc_hand(self): #grouping cards + calculating values
self.value = 0
first_card_index = [a_card[0] for a_card in self.cards]
non_aces = [c for c in first_card_index if c != 'A']
aces = [c for c in first_card_index if c == 'A']
for card in non_aces:
if card in 'JQK':
self.value += 10 #picture cards JQK worth 10
else:
self.value += int(card) #other cards have face value
for card in aces: #ace is worth 11/1 depending on hand value
if self.value <= 10:
self.value += 11
else:
self.value += 1
#displaying cards on screen
def display_cards(self):
for card in self.cards:
cards = "".join((card[0], card[1]))
if cards not in self.card_img:
self.card_img.append(cards)
ahh ok so it's separate logic from the balance
Yeah
more about the card values
and deck shuffling building
all the balance is just in the main file
hmm nothing else really seems out of place
there could be a disconnect from the actual variable data and how you're updating it in pygame
I'd try and use some print statements and print out the balance to confirm its value
alr
<function main.<locals>.game_balance at 0x0000024BFB2E84C0>
it said that
when i tried printing the balance after it updating
can you show how you tried to do that?
there are so much repeated code in here?
self.balance holds your balance, you're printing the wrong thing
:(
please be constructive if you're going to chime in
did u like coppy the code from tut and forgot?
no..
i am very sorry sir Fashoomp
i dont think anythings repeated in the main():?
oh i did too
sorry i did that at like 1 htis morning
i was tryna sort out the diff menu screens
also i cant see where did you use that func
i suppose that not related to your queston tho
Hi silly! π you still grinding on this issue?
Yessir
all ive got left is tryna fix the balance/betting
could you help please?
if you scrollup youll see the code
whats happening is that when im winning, the balance isnt updating with the earnt money
What did the testing with print reveal?
Okay its weird
I bet 100 and had a balance of 550 remaining
I won and it listed my balance as 850 in the print (not updated on screen)
then it glitched and said i Tied and reset my balance + bet to 550 and 0
And i just clicked -50 to subtract from my already 0 current bet and it allowed me to add it back to my account...
meaning i earnt 300..
is it the print showing you this, or the game balance text in the game?
in game display
use print
only listen right now to what print says
because it's possible you simply aren't updating your game display correctly
Okay
i bet 100 and have balance of 900
Tied
printed my new balance at 1200
but display reads
and then when i click +50 to current bet i get this:
completely ignore your display
we're just focusing on the truth of the value
once you know it's working 100% correctly with printing
then you can focus on making sure the game display updates along with it
The true value is working
More or less
I bet 100, have a balance of 900 remainder and it sets my total at 1200
meaning it tripled my bet although the code asks it to double
game_finish("You Won!", 500, 40, white)
self.balance += self.bet * 2 # Add the win amount to the balance
print(self.balance )
game_balance(f"Balance: {self.balance}", 100, 330, white)
self.bet = 0 # current bet
self.current_bet = 0 # current bet display
time.sleep(3)
self.play_or_exit()```
what's the difference between self.bet and self.current_bet?
bet is the adding subtracting from current bet
which is like the total
displayed
if self.bet + 50 > self.balance:
print("Insufficient balance")
return
self.bet += 50
self.current_bet = self.bet
self.current_balance = self.balance - self.bet
def sub_bet(self):
if self.bet == 0:
return # Do nothing if the current bet is already at 0
if self.bet - 50 < 0:
print("Minimum bet is 0")
self.bet -= 50
self.current_bet = self.bet
self.current_balance = self.balance - self.bet
return```
you seem to just always be setting current_bet to bet
I don't understand why you need both
I probably dont
try and eliminate any redundancy. If something isn't doing anything, you can probably remove it
the less stuff you have to keep track of, the easier it is to debug (and write new code)
Deleted it
same error
the print text is true
the balance text is old
but when i click +50/-50 it makes it updated?
what i was saying here
keep using prints to track down when things go awry
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.