utsav507 / python-projects Goto Github PK
View Code? Open in Web Editor NEWIntermediate-level Projects in Python
Intermediate-level Projects in Python
import pygame
import random
pygame.init()
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 128, 0)
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Blackjack")
font = pygame.font.Font(None, 36)
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}
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()
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.