A Beginner’s Guide to Creating a Simple Whack-a-Mole Game in Python

Welcome to a tutorial on creating a simple Whack-a-Mole game in Python! This guide is designed for beginners who want to learn the basics of game development using Python. By the end of this tutorial, you’ll have a functional Whack-a-Mole game that you can play and even customize to your liking.

Introduction

Whack-a-Mole is a classic arcade game where players must hit moles as they pop up from holes. The objective of this tutorial is to teach you how to create a basic version of the game using Python’s pygame library for graphics and input handling.

Prerequisites

Before you start, make sure you have Python installed on your computer. You’ll also need to install the pygame library, which you can do using pip:

bashpip install pygame

Step 1: Setting Up the Game Environment

First, create a new Python file and import the necessary modules from pygame:

pythonimport pygame
import sys
import random

# Initialize pygame
pygame.init()

# Set the screen size
screen_width, screen_height = 800, 600
screen = pygame.display.set_mode((screen_width, screen_height))

# Set the game title
pygame.display.set_caption("Simple Whack-a-Mole Game")

# Define colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

Step 2: Drawing the Game Board

Next, let’s draw the game board with holes where the moles will pop up:

python# Define the number of holes
num_holes = 9
hole_width, hole_height = 80, 80
hole_spacing = (screen_width - (num_holes * hole_width)) // (num_holes + 1)

# Function to draw the game board
def draw_game_board():
screen.fill(BLACK)
for i in range(num_holes):
x = i * (hole_width + hole_spacing) + hole_spacing
pygame.draw.rect(screen, WHITE, (x, screen_height - hole_height, hole_width, hole_height))

pygame.display.flip()

Step 3: Implementing Mole Behavior

Now, let’s create a class to represent the moles and implement their behavior:

pythonclass Mole:
def __init__(self, x, y):
self.x = x
self.y = y
self.visible = False

def update(self):
# Randomly make the mole visible for a short period
if not self.visible and random.random() < 0.05:
self.visible = True
pygame.time.set_timer(pygame.USEREVENT, 500) # Make the mole invisible after 500ms

def draw(self):
if self.visible:
pygame.draw.ellipse(screen, (0, 255, 0), (self.x, self.y, 60, 60))

# Create a list of moles
moles = [Mole(x, screen_height - hole_height - 30) for i in range(num_holes) for x in [i * (hole_width + hole_spacing) + hole_spacing + 25]]

Step 4: Handling Player Input

We’ll use mouse clicks to detect when the player hits a mole:

python# Function to handle mouse clicks
def handle_mouse_click(event, moles):
if event.type
== pygame.MOUSEBUTTONDOWN:
mouse_x, mouse_y = event.pos
for mole in moles:
if mole.visible and (mouse_x - mole.x) ** 2 + (mouse_y - mole.y) ** 2 < 30 ** 2:
mole.visible
= False # Hit the mole!
print("Mole hit!")

# Add an event handler for mouse clicks
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *