0% found this document useful (0 votes)
3 views

Code

The document contains a Python script for a simple Pin Ball Game using the Pygame library. It initializes the game window, sets up colors, fonts, and handles user input for navigating through the main menu, difficulty selection, and gameplay. The game features a bouncing ball and allows users to select difficulty levels before starting the game.

Uploaded by

binnayomaurinep
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Code

The document contains a Python script for a simple Pin Ball Game using the Pygame library. It initializes the game window, sets up colors, fonts, and handles user input for navigating through the main menu, difficulty selection, and gameplay. The game features a bouncing ball and allows users to select difficulty levels before starting the game.

Uploaded by

binnayomaurinep
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

First 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

title_text = font.render("Pin Ball Game", True, BLUE)


title_rect = title_text.get_rect(center=(WIDTH // 2, HEIGHT // 3))
welcome_text = font.render("Welcome", True, BLUE)
welcome_rect = welcome_text.get_rect(center=(WIDTH // 2, HEIGHT // 5))
start_text = small_font.render("Start", True, WHITE)
quit_text = small_font.render("Quit", True, WHITE)

# 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)

# Back button for third background


game_back_button = pygame.Rect(50, HEIGHT - 70, 100, 50)
game_back_text = custom_font.render("Back", True, WHITE) # Smaller font, no
arrow

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

# Ball for game screen


small_ball_radius = 20
small_ball_x, small_ball_y = WIDTH // 2, HEIGHT // 2
ball_speed_x, ball_speed_y = 4, 4

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)

start_rect = pygame.Rect(WIDTH // 2 - 80, HEIGHT // 2 + 100, 160, 60)


quit_rect = pygame.Rect(WIDTH // 2 - 80, HEIGHT // 2 + 170, 160, 60)
pygame.draw.rect(screen, BLUE, start_rect) # Blue box for Start
pygame.draw.rect(screen, BLUE, quit_rect) # Blue box for Quit
screen.blit(start_text, start_text.get_rect(center=start_rect.center))
screen.blit(quit_text, quit_text.get_rect(center=quit_rect.center))

# 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

# Draw back button


pygame.draw.rect(screen, BLUE, back_button)
screen.blit(back_text, back_text.get_rect(center=back_button.center))
elif game_screen:
pygame.draw.circle(screen, RED, (small_ball_x, small_ball_y), small_ball_radius)
small_ball_x += ball_speed_x
small_ball_y += ball_speed_y
if small_ball_x - small_ball_radius <= 0 or small_ball_x + small_ball_radius >=
WIDTH:
ball_speed_x = -ball_speed_x
if small_ball_y - small_ball_radius <= 0 or small_ball_y + small_ball_radius >=
HEIGHT:
ball_speed_y = -ball_speed_y

# Draw back button in third background


pygame.draw.rect(screen, BLUE, game_back_button)
screen.blit(game_back_text,
game_back_text.get_rect(center=game_back_button.center))

for event in pygame.event.get():


if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if not game_started:
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
menu_selected = 1 - menu_selected # Toggle between Start and Quit
if event.key == pygame.K_RETURN:
if menu_selected == 0:
game_started = True
difficulty_screen = True
else:
running = False
elif difficulty_screen:
if event.key == pygame.K_UP:
difficulty_selected = (difficulty_selected - 1) % 3
if event.key == pygame.K_DOWN:
difficulty_selected = (difficulty_selected + 1) % 3
if event.key == pygame.K_RETURN:
print(f"{difficulties[difficulty_selected]} mode selected!")
difficulty_screen = False # Proceed to game screen
game_screen = True
if event.key == pygame.K_LEFT:
game_started = False # Go back to the main menu
difficulty_screen = False
elif game_screen:
if event.key == pygame.K_LEFT:
difficulty_screen = True # Return to difficulty screen
game_screen = False

pygame.display.flip()
pygame.time.delay(30)

pygame.quit()

2nd code:

You might also like