Game Development 3 min read

Create a Candy Crush‑Style Game with Python and Pygame

This tutorial explains how to build a simple Candy Crush‑like game on Windows using Python 3.6 and the pygame library, covering required setup, basic configuration, and providing the complete source code with detailed comments.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Create a Candy Crush‑Style Game with Python and Pygame

The article demonstrates how to develop a basic Candy Crush‑style game using Python and the cross‑platform pygame module, which is designed for game creation and built on SDL.

Basic configuration : The required environment includes Windows, Python 3.6, and pygame, which can be installed via pip install pygame. The tutorial notes that pygame is a familiar tool for many developers.

Full code :

import pygame
from pygame.locals import *
import sys
import manager

'''
brick : 218*218
   animal : 40*40
   bg : 850*600
'''

# print(dir())   # 已导入的包
pygame.init()  # 初始化
pygame.mixer.init()

tree = manager.ManagerTree()
m = manager.Manager(0, 0)
sound_sign = 0
world_bgm = pygame.mixer.Sound(manager.SoundPlay.world_bgm)
game_bgm = pygame.mixer.Sound(manager.SoundPlay.game_bgm)
while True:
    if m.level == 0:
        if sound_sign == 0:
            game_bgm.stop()
            world_bgm.play(-1)
            sound_sign = 1
    else:
        if sound_sign == 1:
            world_bgm.stop()
            game_bgm.play(-1)
            sound_sign = 0
    if m.level == 0:
        tree.draw_tree(m.energy_num, m.money)
    else:
        m.set_level_mode(m.level)
        sprite_group = m.draw()
        if m.type == 0:
            m.eliminate_animal()
            m.death_map()
            m.exchange(sprite_group)
        m.judge_level()

    for event in pygame.event.get():
        if event.type == KEYDOWN:
            if event.key == pygame.K_q or event.key == pygame.K_ESCAPE:
                exit()
        if event.type == QUIT:
            sys.exit()
        m.level, m.energy_num, m.money = tree.mouse_select(event, m.level, m.energy_num, m.money)
        m.mouse_select(event)

    m.mouse_image()
    pygame.display.flip()

if __name__ == "main":
    main()

The code initializes pygame, sets up background music, manages game levels and states, handles user input for quitting or selecting tiles, updates the game state, and renders the graphics each frame.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Game DevelopmentTutorialPygameCandy Crush
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.