-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
187 lines (137 loc) · 5.63 KB
/
main.py
File metadata and controls
187 lines (137 loc) · 5.63 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
######### CODING PLAYER THEN NPC #########
# Main game looping
# Moduels
import pygame
import sys
import random
# Initialize Pygame
pygame.init()
WIDTH, HEIGHT = 1000, 800
# Set up the display
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("James Bond!")
# Set up colors
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
# Card width and heights
small_card_width, small_card_height = 100, 134 # 145, 196
big_card_width, big_card_height = 100, 134
# Load images
card_back_image = pygame.transform.scale(pygame.image.load("back_card.png"), (big_card_width, big_card_height))
BACKGROUND_IMAGE = pygame.transform.scale(pygame.image.load("background.png"), (WIDTH, HEIGHT))
# 3 variables needed to render and use the cards
ranks = ["clubs", "diamonds", "hearts", "spades"]
numbers = range(2, 11)
faces = ["ace", "jack", "king", "queen"]
# Lists for participants
cpu_card_list = [[] for _ in range(6)] # holds instance of classes
player_card_list = [[] for _ in range(6)] # holds instance of classes
cpu_image = [] # holds instances of Card_Flipped class
player_image = [] # holds instances of Card_Flipped class
# Coordinants that separate the cards
coords_cpu_x = list(range(10, 2000, 165))
coords_player_x = list(range(10, 2000, 165))
stack_contents_x = list(range(272, 2000, 123))
deck_cards_x = list(range(250, 2000, 123))
player_card_y = 595
cpu_card_y = 10
# Sets a counter that prevents more than 6 cards from being displayed, because math is hard
cpu_stack_counter = 0
player_stack_counter = 0
# Variables used to track how many times a loop has been repeated
# Tracking the players cards
stack_clicked = False
mid_card_y = 200
deck_clicked_on = False
stack_complete = [False, False, False, False, False, False]
# Classes
class Card():
def __init__(self, img, card_value, card):
self.img = img
self.card_value = card_value
self.card = card
def blit_cards(self, coord_lst, ycord):
for i in range(len(coord_lst)):
WIN.blit(self.img, (coord_lst[i], ycord))
def blit_deck_cards(self, deck_card_coord):
WIN.blit(self.img, (deck_card_coord, mid_card_y))
def card_clicked(self, y_pos, image_index, x_coord_list):
mouse_x, mouse_y = pygame.mouse.get_pos()
image_x = x_coord_list[image_index]
return image_x <= mouse_x <= image_x + big_card_width and y_pos <= mouse_y <= y_pos + big_card_height
# Functions
def stack_check(stck):
return all(x.card_value == stck[0].card_value for x in stck)
# Create the deck
deck = [] # stores the 4 cards that will be displayed in the middle
for rank in ranks:
for number in numbers:
card_temp = Card(pygame.transform.scale(pygame.image.load
(f"Cards/{number}_of_{rank}.png"), (small_card_width, small_card_height)), number, f"{number}_of_{rank}")
deck.append(card_temp)
for face in faces:
face_temp = Card(pygame.transform.scale(pygame.image.load
(f"Cards/{face}_of_{rank}.png"), (small_card_width, small_card_height)), face, f"{face}_of_{rank}")
deck.append(face_temp)
# Creates the list for the cpu's deck and the player's deck
random.shuffle(deck)
for i in range(len(cpu_card_list)):
for j in range(4):
cpu_card_list[i].append(deck.pop(0))
for i in range(len(player_card_list)):
for j in range(4):
player_card_list[i].append(deck.pop(0))
# Creates each image for the stack (back of the card)
for stack in range(len(cpu_card_list)):
clipped = Card(card_back_image, "", "")
cpu_image.append(clipped)
for stack in range(len(player_card_list)):
clipped = Card(card_back_image, "", "")
player_image.append(clipped)
# Game loop
while True:
# Initialize selected_stack to a default value
selected_stack = 0 # or -1, depending on your logic
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
# Check if the stack is clicked on
for image in player_image:
if image.card_clicked(player_card_y, player_image.index(image), coords_player_x):
stack_clicked = not stack_clicked
selected_stack = player_image.index(image) if stack_clicked else selected_stack
# Ensure selected_stack is valid before using it
if selected_stack >= 0 and selected_stack < len(player_card_list):
for picture in deck:
if picture.card_clicked(mid_card_y, deck.index(picture), deck_cards_x) and not deck_clicked_on:
player_card_list[selected_stack].append(deck.pop(deck.index(picture)))
deck_clicked_on = True
if deck_clicked_on:
for obj in player_card_list[selected_stack]:
if obj.card_clicked(470, player_card_list[selected_stack].index(obj), stack_contents_x):
deck.append(player_card_list[selected_stack].pop(player_card_list[selected_stack].index(obj)))
deck_clicked_on = False
if stack_check(player_card_list[selected_stack]):
print("Stack complete!")
# Fill the screen with white color
WIN.blit(BACKGROUND_IMAGE, (0, 0))
# Blit CPU and player cards
for image in cpu_image:
image.blit_cards(coords_cpu_x, cpu_card_y)
for image in player_image:
image.blit_cards(coords_player_x, player_card_y)
if stack_clicked:
displayed_cardon = 0
for item in player_card_list[selected_stack]:
if displayed_cardon < 4:
card_x = coords_player_x[selected_stack]
WIN.blit(item.img, (stack_contents_x[displayed_cardon], 470))
displayed_cardon += 1
# Blit the deck in the middle
for deck_counter, card in enumerate(deck):
card.blit_deck_cards(deck_cards_x[deck_counter])
# Update the display
pygame.display.update()