-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.py
236 lines (204 loc) · 9.05 KB
/
Main.py
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import sys
import pygame
from lib.Settings import Settings
from lib.Cards import Cards
from lib.Pictures import Pictures
class BlackJack:
def __init__(self):
# Initialize the game and settings from Settings class
pygame.init()
# Initialize the game screen in fullscreen mode
self.screen = pygame.display.set_mode ((0, 0), pygame.FULLSCREEN)
# Creates instances of te other classes
self.settings = Settings(self)
self.playerscreen = Cards (self, "Player")
self.computerscreen = Cards (self, "Dealer")
self.pictures = Pictures(self)
# Deal first hand to each player
self.computerscreen.dealHand()
self.playerscreen.dealHand()
def playGame(self):
'''Start the main loop of the game'''
while True:
self._updateScreen()
self._checkEvents()
self._checkGameStatus()
def _checkGameStatus(self):
if self.playerscreen.cardPoints > 21:
self.pictures.play_stage = False
self.pictures.end_stage = self.pictures.over_21 = True
def _checkEvents(self):
# Respond to keypresses and mouse events
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_r: self._redeal_hand()
elif event.key == pygame.K_q: sys.exit()
# Buttons in INTRO menu
if (
event.type == pygame.MOUSEBUTTONDOWN and
event.button == 1 and
self.pictures.intro
):
mouse=pygame.mouse.get_pos()
# Press PLAY button
if (
mouse[0] in range(
self.pictures.intro_play_x,
self.pictures.intro_play_x + self.pictures.intro_play.get_rect()[2]
) and
mouse[1] in range(
self.pictures.intro_play_y,
self.pictures.intro_play_y + self.pictures.intro_play.get_rect()[3]
)
):
self.pictures.bet_stage = True
self.pictures.intro = False
# Press QUIT button
if (mouse[0] in range(
self.pictures.intro_quit_x,
self.pictures.intro_quit_x + self.pictures.intro_quit.get_rect()[2]
) and
mouse[1]in range(
self.pictures.intro_quit_y,
self.pictures.intro_quit_y + self.pictures.intro_quit.get_rect()[3]
)
):
sys.exit()
# Buttons in BET STAGE menu
if (
event.type == pygame.MOUSEBUTTONDOWN and
event.button == 1 and
self.pictures.bet_stage == True
):
mouse = pygame.mouse.get_pos()
if (
mouse[0] in range(
self.pictures.bet_done_x,
self.pictures.bet_done_x + self.pictures.bet_done.get_rect()[2]
) and
mouse[1] in range(
self.pictures.bet_done_y,
self.pictures.bet_done_y + self.pictures.bet_done.get_rect()[3]
) and self.pictures.current_bet > 0
):
self.pictures.play_stage = True
self.pictures.bet_stage = False
if (
mouse[0] in range(
self.pictures.bet_100_x
,self.pictures.bet_100_x + self.pictures.bet_100.get_rect()[2]
) and
mouse[1] in range(self.pictures.bet_100_y,
self.pictures.bet_100_y + self.pictures.bet_100.get_rect()[3]
)
):
self.pictures.current_bet += 100
if self.pictures.current_bet > self.pictures.bankroll:
self.pictures.current_bet = self.pictures.bankroll
if (
mouse[0] in range(
self.pictures.bet_200_x,
self.pictures.bet_200_x + self.pictures.bet_done.get_rect()[2]
) and
mouse[1] in range(self.pictures.bet_200_y,
self.pictures.bet_200_y + self.pictures.bet_200.get_rect()[3]
)
):
self.pictures.current_bet += 200
if self.pictures.current_bet > self.pictures.bankroll:
self.pictures.current_bet = self.pictures.bankroll
if (
mouse[0] in range(
self.pictures.bet_500_x,
self.pictures.bet_500_x + self.pictures.bet_500.get_rect()[2]
) and
mouse[1] in range(
self.pictures.bet_500_y,
self.pictures.bet_500_y + self.pictures.bet_500.get_rect()[3]
)
):
self.pictures.current_bet += 500
if self.pictures.current_bet > self.pictures.bankroll:
self.pictures.current_bet = self.pictures.bankroll
if (
mouse[0] in range(
self.pictures.bet_reset_x,
self.pictures.bet_reset_x
+ self.pictures.bet_reset.get_rect()[2]
) and
mouse[1] in range(
self.pictures.bet_reset_y,
self.pictures.bet_reset_y
+ self.pictures.bet_reset.get_rect()[3]
)
):
self.pictures.current_bet = 0
if (
event.type == pygame.MOUSEBUTTONDOWN and
event.button == 1 and
self.pictures.play_stage
):
mouse = pygame.mouse.get_pos()
if (
mouse[0] in range(
self.pictures.play_hit_x,
self.pictures.play_hit_x
+ self.pictures.play_hit.get_rect()[2]
) and
mouse[1] in range(
self.pictures.play_hit_y,
self.pictures.play_hit_y
+ self.pictures.play_hit.get_rect()[3]
)
):
self.pictures.player_has_hit = True
self.playerscreen.hit_card()
self.pictures.player_hit = True
if (
mouse[0] in range(
self.pictures.play_double_x,
self.pictures.play_double_x
+ self.pictures.play_double.get_rect()[2]
) and
mouse[1] in range(
self.pictures.play_double_y,
self.pictures.play_double_y
+ self.pictures.play_double.get_rect()[3]
) and not self.pictures.player_hit
and self.pictures.bankroll >= 2 * self.pictures.current_bet
):
self.playerscreen.hit_card()
self.computerscreen.playerTurn = False
self.pictures.current_bet *= 2
if (
mouse[0] in range(
self.pictures.play_stand_x,
self.pictures.play_stand_x
+ self.pictures.play_stand.get_rect()[2]
) and
mouse[1] in range(
self.pictures.play_stand_y,
self.pictures.play_stand_y
+ self.pictures.play_stand.get_rect()[3]
)
):
self.computerscreen.playerTurn = False
def _updateScreen(self):
'''Update images on the screen, and flip to new screen'''
# Redraw the screen during each pass through the loop
self.pictures.blitme(self)
# Make the most recently drawn screen visible
pygame.display.flip()
def _redeal_hand(self):
self.pictures.bet_stage = True
self.pictures.end_stage = self.pictures.over_21 = False
self.computerscreen.dealHand()
self.playerscreen.dealHand()
self.computerscreen.playerTurn = False
self.pictures.current_bet = 0
self.pictures.player_hit = self.pictures.update = False
if self.pictures.bankroll <= 0: self.pictures.bankroll = 1000
if __name__ == '__main__':
jack = BlackJack()
jack.playGame()