forked from Raptorrr59/githubWorkshopEpitech
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcardgame.py
More file actions
217 lines (173 loc) · 6.51 KB
/
Copy pathcardgame.py
File metadata and controls
217 lines (173 loc) · 6.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import random
import pygame
from enum import Enum
class Suit(Enum):
"""Enum for card suits."""
HEARTS = "♥"
DIAMONDS = "♦"
CLUBS = "♣"
SPADES = "♠"
class Rank(Enum):
"""Enum for card ranks."""
ACE = "A"
TWO = "2"
THREE = "3"
FOUR = "4"
FIVE = "5"
SIX = "6"
SEVEN = "7"
EIGHT = "8"
NINE = "9"
TEN = "10"
JACK = "J"
QUEEN = "Q"
KING = "K"
class Card:
"""Represents a single playing card."""
def __init__(self, suit: Suit, rank: Rank):
self.suit = suit
self.rank = rank
def __repr__(self):
return f"{self.rank.value}{self.suit.value}"
class Deck:
"""Represents a deck of playing cards."""
def __init__(self):
"""Initialize a standard 52-card deck."""
self.cards = []
self._initialize_deck()
def _initialize_deck(self):
"""Create all 52 cards and shuffle the deck."""
for suit in Suit:
for rank in Rank:
self.cards.append(Card(suit, rank))
random.shuffle(self.cards)
def draw_card(self):
"""Draw and return the top card from the deck.
Returns:
Card: The drawn card, or None if deck is empty.
"""
if self.cards:
return self.cards.pop()
return None
def cards_remaining(self):
"""Return the number of cards remaining in the deck."""
return len(self.cards)
def reset(self):
"""Reset and reshuffle the deck."""
self.cards = []
self._initialize_deck()
# Example usage
if __name__ == "__main__":
pygame.init()
# Screen setup
SCREEN_WIDTH = 1000
SCREEN_HEIGHT = 700
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Card Game")
clock = pygame.time.Clock()
# Colors
GREEN = (34, 139, 34)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (220, 20, 60)
GRAY = (200, 200, 200)
# Fonts - use system font for Unicode support
# Try multiple fonts that are likely to support Unicode card symbols
font_large = pygame.font.SysFont('dejavusans,freesans,liberationsans,arial', 48)
font_small = pygame.font.SysFont('dejavusans,freesans,liberationsans,arial', 24)
font_card = pygame.font.SysFont('dejavusans,freesans,liberationsans,arial', 32, bold=True)
# Game state
deck = Deck()
player_cards = []
# Button class
class Button:
def __init__(self, x, y, width, height, text):
self.rect = pygame.Rect(x, y, width, height)
self.text = text
self.hovered = False
def draw(self, surface):
color = (100, 150, 100) if self.hovered else (70, 130, 70)
pygame.draw.rect(surface, color, self.rect)
pygame.draw.rect(surface, WHITE, self.rect, 2)
text_surface = font_small.render(self.text, True, WHITE)
text_rect = text_surface.get_rect(center=self.rect.center)
surface.blit(text_surface, text_rect)
def is_clicked(self, pos):
return self.rect.collidepoint(pos)
def update_hover(self, pos):
self.hovered = self.rect.collidepoint(pos)
# Create buttons
draw_button = Button(50, 600, 150, 60, "Draw Card")
reshuffle_button = Button(250, 600, 150, 60, "Reshuffle")
quit_button = Button(800, 600, 150, 60, "Quit")
# Main game loop
running = True
message = "Welcome to Card Game! Click 'Draw Card' to begin."
message_timer = 0
while running:
clock.tick(60)
mouse_pos = pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
if draw_button.is_clicked(mouse_pos):
card = deck.draw_card()
if card:
player_cards.append(card)
message = f"You drew: {card}"
else:
message = "No cards left in deck!"
message_timer = 120
elif reshuffle_button.is_clicked(mouse_pos):
deck.reset()
player_cards = []
message = "Deck reshuffled! Cards returned to deck."
message_timer = 120
elif quit_button.is_clicked(mouse_pos):
running = False
# Update button hover states
draw_button.update_hover(mouse_pos)
reshuffle_button.update_hover(mouse_pos)
quit_button.update_hover(mouse_pos)
# Decrease message timer
if message_timer > 0:
message_timer -= 1
# Draw everything
screen.fill(GREEN)
# Title
title = font_large.render("Card Game", True, WHITE)
screen.blit(title, (SCREEN_WIDTH // 2 - title.get_width() // 2, 20))
# Deck info
deck_text = font_small.render(f"Cards in deck: {deck.cards_remaining()}", True, WHITE)
screen.blit(deck_text, (50, 100))
# Player cards
player_text = font_small.render(f"Your cards ({len(player_cards)}):", True, WHITE)
screen.blit(player_text, (50, 150))
# Display player cards in a row
card_x = 50
for i, card in enumerate(player_cards):
# Draw card background
card_rect = pygame.Rect(card_x, 200, 60, 90)
pygame.draw.rect(screen, WHITE, card_rect)
pygame.draw.rect(screen, BLACK, card_rect, 2)
# Determine card color based on suit
if card.suit in [Suit.HEARTS, Suit.DIAMONDS]:
card_color = RED
else:
card_color = BLACK
# Draw card text with Unicode support and appropriate color
card_text = font_card.render(str(card), True, card_color)
text_rect = card_text.get_rect(center=card_rect.center)
screen.blit(card_text, text_rect)
card_x += 70
# Message display
if message_timer > 0:
msg_surface = font_small.render(message, True, WHITE)
screen.blit(msg_surface, (50, 350))
# Draw buttons
draw_button.draw(screen)
reshuffle_button.draw(screen)
quit_button.draw(screen)
pygame.display.flip()
pygame.quit()