How to Make Vicks Store In Pygame

import pygame
import sys

# Initialize Pygame
pygame.init()

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

# Set up the display
width, height = 800, 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Vicks Modern Store")

# Define fonts
font_large = pygame.font.Font(None, 36)
font_small = pygame.font.Font(None, 24)

# Define products
products = [
    {"name": "Vicks Cough Drops", "price": 5.99},
    {"name": "Vaporub", "price": 10.99},
    {"name": "Inhaler", "price": 7.99},
    {"name": "BabyRub", "price": 8.99},
    {"name": "Vapostick", "price": 6.99},
    {"name": "Vapopatch", "price": 9.99},
]

# Main game loop
def main():
    cart = []

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1:  # Left mouse button
                    handle_click(event.pos, cart)

        screen.fill(WHITE)

        draw_products()

        draw_cart(cart)

        pygame.display.flip()

# Function to handle mouse clicks
def handle_click(pos, cart):
    for i, product in enumerate(products):
        if product["rect"].collidepoint(pos):
            cart.append(product)
            print(f"Added {product['name']} to the cart")

# Function to draw products on the screen
def draw_products():
    for i, product in enumerate(products):
        text = font_large.render(product["name"] + f" - ${product['price']:.2f}", True, BLACK)
        rect = text.get_rect(center=(width // 2, 100 + i * 50))
        product["rect"] = rect
        screen.blit(text, rect)

# Function to draw the shopping cart
def draw_cart(cart):
    total_price = sum(product["price"] for product in cart)
    text = font_small.render(f"Total: ${total_price:.2f}", True, BLACK)
    screen.blit(text, (10, height - 50))

    for i, product in enumerate(cart):
        text = font_small.render(product["name"], True, BLACK)
        rect = text.get_rect(topleft=(10, height - 20 - (i + 1) * 20))
        screen.blit(text, rect)

if __name__ == "__main__":
    main()
Previous article
Next article
Taiyeb Hasan
Taiyeb Hasanhttps://www.kidsmodo.com
Inspiring young minds through engaging coding lessons. Dedicated tutor fostering creativity and logic in kids' tech journeys

Related Articles

Latest Articles