Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 

Repository files navigation

Pygame-checkers

Checkers (Python)

A simple checkers game written in Python using pygame. The program opens a window with a checkers board where two players can move pieces with the mouse.

Requirements

  • Python 3
  • pygame-ce

Setup

Create and activate a virtual environment:

python3 -m venv venv
source venv/bin/activate

Install pygame:

pip install pygame-ce

Run

python3 checkers.py

Controls

Click a piece to select it, then click a square to move it.

Script

import pygame
import sys

pygame.init()

WIDTH, HEIGHT = 640, 640
ROWS, COLS = 8, 8
SQUARE_SIZE = WIDTH // COLS

WHITE = (255, 255, 255)
RED = (200, 0, 0)
BLACK = (0, 0, 0)
GRAY = (100, 100, 100)

screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Checkers")

class Piece:
    def __init__(self, row, col, color):
        self.row = row
        self.col = col
        self.color = color
        self.king = False

    def draw(self):
        x = self.col * SQUARE_SIZE + SQUARE_SIZE // 2
        y = self.row * SQUARE_SIZE + SQUARE_SIZE // 2
        pygame.draw.circle(screen, self.color, (x, y), SQUARE_SIZE // 2 - 10)
        if self.king:
            pygame.draw.circle(screen, GRAY, (x, y), 10)

    def move(self, row, col):
        self.row = row
        self.col = col

    def make_king(self):
        self.king = True

pieces = []

for row in range(3):
    for col in range(COLS):
        if (row + col) % 2 == 1:
            pieces.append(Piece(row, col, RED))

for row in range(5, 8):
    for col in range(COLS):
        if (row + col) % 2 == 1:
            pieces.append(Piece(row, col, WHITE))

selected_piece = None

def draw_board():
    screen.fill(BLACK)
    for row in range(ROWS):
        for col in range(COLS):
            if (row + col) % 2 == 0:
                pygame.draw.rect(screen, WHITE, (col*SQUARE_SIZE, row*SQUARE_SIZE, SQUARE_SIZE, SQUARE_SIZE))

def get_piece(row, col):
    for p in pieces:
        if p.row == row and p.col == col:
            return p
    return None

clock = pygame.time.Clock()

while True:
    clock.tick(60)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

        if event.type == pygame.MOUSEBUTTONDOWN:
            x, y = pygame.mouse.get_pos()
            col = x // SQUARE_SIZE
            row = y // SQUARE_SIZE

            clicked_piece = get_piece(row, col)

            if selected_piece:
                selected_piece.move(row, col)

                if selected_piece.color == WHITE and row == ROWS - 1:
                    selected_piece.make_king()

                if selected_piece.color == RED and row == 0:
                    selected_piece.make_king()

                selected_piece = None
            else:
                selected_piece = clicked_piece

    draw_board()

    for p in pieces:
        p.draw()

    pygame.display.update()

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors