I created a simple blackjack game using Python’s Pygame package. This was an opportunity to create a program that focused on using conditional operators.
The game itself is simple to understand, the player follows the instructions on screen and clicks on buttons to continue game play.
The main challenge with this project was getting the buttons to function properly. Pygame uses the ‘mouse’ module to track the X and Y coordinates of the cursor on the screen
mouse = pygame.mouse.get_pos()
. Pygame also uses the ‘event’ module to track events in game, such as mouse clicks. I set the X and Y coordinates for each button
new_deal = (40, 405)
, as well as a variable keeping track of if that button is active for different phases of the game (i.e. new_deal_active = True
). Using these variables
I was able to build the main game loop keeping track of which buttons where pressed, and what steps to take when they are pressed.
if event.type == pygame.MOUSEBUTTONDOWN:
if new_deal[0] <= mouse[0] <= new_deal[0] + BUTTON_WIDTH and new_deal[1] <= mouse[1] <= new_deal[1] + BUTTON_HEIGHT and new_deal_active:
From there it was just a matter of programming the steps for each button click, such as drawing cards and putting them on screen, changing the active status of buttons, and changing the onscreen instructions.