Code
Code
import pygame
# Initialize pygame
pygame.init()
# Screen dimensions
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Pin Ball Game")
# Colors
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
SKY_BLUE = (135, 206, 250)
LIGHT_PINK = (255, 182, 193)
# Font settings
font = pygame.font.Font(None, 100) # Larger font
small_font = pygame.font.Font(None, 50)
custom_font = pygame.font.Font(None, 40) # Smaller custom font
# Difficulty selection
difficulties = ["Easy", "Medium", "Hard"]
difficulty_selected = 0
difficulty_text = font.render("New Game", True, BLUE)
difficulty_rect = difficulty_text.get_rect(center=(WIDTH // 2, HEIGHT // 4))
difficulty_boxes = [
pygame.Rect(WIDTH // 2 - 80, HEIGHT // 2 + i * 80, 160, 60)
for i in range(3)
]
# Back button
back_button = pygame.Rect(50, HEIGHT - 70, 100, 50)
back_text = custom_font.render("Back", True, WHITE)
game_started = False
difficulty_screen = False
game_screen = False
running = True
menu_selected = 0 # 0 for Start, 1 for Quit
# Ball settings
ball_radius = 70 # Bigger ball
ball_x, ball_y = WIDTH // 2, HEIGHT // 2
while running:
screen.fill(LIGHT_PINK if not game_started else SKY_BLUE if game_screen else
WHITE)
if not game_started:
pygame.draw.circle(screen, RED, (ball_x, ball_y), ball_radius) # Display big ball
at center
screen.blit(welcome_text, welcome_rect)
screen.blit(title_text, title_rect)
# Highlight selection
highlight_rect = start_rect if menu_selected == 0 else quit_rect
pygame.draw.rect(screen, YELLOW, highlight_rect, 3)
elif difficulty_screen:
screen.fill(WHITE) # Keep background white
screen.blit(difficulty_text, difficulty_rect)
for i, text in enumerate(difficulties):
pygame.draw.rect(screen, BLUE, difficulty_boxes[i]) # Blue boxes
text_render = small_font.render(text, True, WHITE)
text_rect = text_render.get_rect(center=difficulty_boxes[i].center)
screen.blit(text_render, text_rect)
if i == difficulty_selected:
pygame.draw.rect(screen, YELLOW, difficulty_boxes[i], 3) # Highlight
selected difficulty
pygame.display.flip()
pygame.time.delay(30)
pygame.quit()
2nd code: