Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Black Jack Game doesnt run but no errors also #4

Open
malteserssss opened this issue May 15, 2023 · 0 comments
Open

Black Jack Game doesnt run but no errors also #4

malteserssss opened this issue May 15, 2023 · 0 comments

Comments

@malteserssss
Copy link

import pygame
import random

Initialize Pygame

pygame.init()

Define colors

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 128, 0)

Set the screen dimensions

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600

Create the screen

screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Blackjack")

Set the font

font = pygame.font.Font(None, 36)

Define the ranks, suits, and values of the cards

ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
suits = ['Hearts', 'Diamonds', 'Clubs', 'Spades']
values = {'A': 11, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '10': 10, 'J': 10, 'Q': 10, 'K': 10}

Load card images

card_images = {}
for suit in suits:
for rank in ranks:
card_name = rank + ' of ' + suit
image_path = f"images/{card_name}.png"
card_images[card_name] = pygame.image.load(image_path)

class Card:
def init(self, rank, suit):
self.rank = rank
self.suit = suit

def __str__(self):
    return self.rank + ' of ' + self.suit

class Deck:
def init(self):
self.deck = []
for suit in suits:
for rank in ranks:
self.deck.append(Card(rank, suit))

def shuffle(self):
    random.shuffle(self.deck)

def deal(self):
    return self.deck.pop()

class Hand:
def init(self):
self.cards = []
self.value = 0
self.aces = 0

def add_card(self, card):
    self.cards.append(card)
    self.value += values[card.rank]
    if card.rank == 'A':
        self.aces += 1

def adjust_for_ace(self):
    while self.value > 21 and self.aces > 0:
        self.value -= 10
        self.aces -= 1

class Chips:
def init(self):
self.total = 100
self.bet = 0

def win_bet(self):
    self.total += self.bet

def lose_bet(self):
    self.total -= self.bet

class BlackjackGame:
def main(self):
pass

class BlackjackGame:
def draw_text(self, text, color, x, y):
text_surface = font.render(text, True, color)
screen.blit(text_surface, (x, y))

def draw_card(self, card, x, y):
    card_name = str(card)
    card_image = card_images[card_name]
    screen.blit(card_image, (x, y))

def draw_table(self):
    screen.fill(GREEN)

    # Draw player
    x = 50
    y = 200
    for card in self.dealer_hand.cards:
        self.draw_card(card, x, y)
        x += 100

    # Draw player's chips total
    self.draw_text("Chips: " + str(self.chips.total), WHITE, 50, 50)

    # Update the display
    pygame.display.flip()

def main(self):
    print("Welcome to Blackjack!")

    while True:
        self.deck.shuffle()

        self.player_hand = Hand()
        self.dealer_hand = Hand()

        self.player_hand.add_card(self.deck.deal())
        self.player_hand.add_card(self.deck.deal())
        self.dealer_hand.add_card(self.deck.deal())
        self.dealer_hand.add_card(self.deck.deal())

        # Take bets
        self.take_bet()

        # Show initial cards (one dealer card hidden)
        self.show_some()

        # Start the game
        self.playing = True

        # Player's turn
        self.hit_or_stand()

        # Show all cards (including dealer's)
        self.show_all()

        # Check if player busts
        if self.player_hand.value > 21:
            self.player_busts()
            if not self.play_again():
                break
            else:
                continue

        # Dealer's turn (automated)
        while self.dealer_hand.value < 17:
            self.hit(self.dealer_hand)

        # Show all cards
        self.show_all()

        # Check if dealer busts
        if self.dealer_hand.value > 21:
            self.dealer_busts()
        elif self.dealer_hand.value > self.player_hand.value:
            self.dealer_wins()
        elif self.dealer_hand.value < self.player_hand.value:
            self.player_wins()
        else:
            self.push()

        # Play again?
        if not self.play_again():
            break

# Create the game object
game = BlackjackGame()

# Start the game
game.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant