Coder Social home page Coder Social logo

python-projects's People

Contributors

katrina0406 avatar utsav507 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

python-projects's Issues

Black Jack Game doesnt run but no errors also

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()

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.