Python Project – Candy Crush

What is Python?

Python is a high-level, interpreted programming language that is widely used for a variety of purposes, including web development, data analysis, artificial intelligence, scientific computing, and more. It was first released in 1991 by Guido van Rossum and has since become one of the most popular programming languages in the world.

Python is known for its simplicity, readability, and ease of use. Its syntax is designed to be easily readable and intuitive, which makes it a great choice for beginners who are just starting to learn programming. However, it is also a powerful language that can be used for complex tasks and projects.

Python is an open-source language, which means that anyone can download and use it for free. It has a large and active community of developers who contribute to its ongoing development, creating new libraries and tools that make it even more versatile and useful.

Python Project - Candy Crush

Python project – candy crush complete code is given below. you can simply intsall the software by following given methods

Candy Crush - Working Method

  1. Install TKinter in your device>> use cmd>> type- pip install tk
  2. Save the square shape picture(you can create these pictures in paint- 1. red, 2, yellow, 3. Green, 4. blue etc) in same folder where you saved python project’s file.

Code:

import pygame
import random

 

# Initialize Pygame
pygame.init()

 

# Set the screen size and caption
screen_width, screen_height = 600, 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption(“Candy Crush”)

 

# Define some colors
white = (255, 255, 255)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)

 

# Define the grid size and candy size
grid_size = 8
candy_size = 50

 

# Define the candy types and images
candy_types = [“blue”, “green”, “orange”, “purple”, “red”, “yellow”]
candy_images = {}
for candy_type in candy_types:
    candy_images[candy_type] = pygame.image.load(f”{candy_type}.png”).convert_alpha()

 

# Define the Candy class
class Candy:
    def __init__(self, x, y, candy_type):
        self.x = x
        self.y = y
        self.candy_type = candy_type
        self.image = candy_images[candy_type]
        self.selected = False
   
    def draw(self):
        screen.blit(self.image, (self.x, self.y))
       
    def select(self):
        self.selected = True
       
    def deselect(self):
        self.selected = False
       
# Define the grid of candies
candies = []
for i in range(grid_size):
    row = []
    for j in range(grid_size):
        candy_type = random.choice(candy_types)
        x = j * candy_size + (screen_width – grid_size * candy_size) / 2
        y = i * candy_size + (screen_height – grid_size * candy_size) / 2
        candy = Candy(x, y, candy_type)
        row.append(candy)
    candies.append(row)

 

# Define the game loop
running = True
while running:
    # Handle events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEBUTTONDOWN:
            # Get the position of the mouse click
            x, y = pygame.mouse.get_pos()
           
            # Find the candy that was clicked
            for i in range(grid_size):
                for j in range(grid_size):
                    candy = candies[i][j]
                    if candy.x <= x < candy.x + candy_size and candy.y <= y < candy.y + candy_size:
                        if not candy.selected:
                            candy.select()
                        else:
                            candy.deselect()
                           
    # Update the screen
    screen.fill(white)
    for i in range(grid_size):
        for j in range(grid_size):
            candy = candies[i][j]
            candy.draw()
            if candy.selected:
                pygame.draw.rect(screen, blue, (candy.x, candy.y, candy_size, candy_size), 5)
    pygame.display.update()

 

# Quit Pygame
pygame.quit()

Result:

python project - candy crush

Leave a Reply

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