κ²μ λ°©λ²:
solider.png
μ΄λ―Έμ§)λ₯Ό μ‘°μνμ¬ μμμ λ¨μ΄μ§λ νν (bomb.jpg
μ΄λ―Έμ§)λ€μ νΌν΄μΌ ν©λλ€.import pygame
import random
import sys
import time
# Initialize Pygame
pygame.init()
# Screen dimensions
screen_width = 600
screen_height = 400
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Bomb Dodge Game')
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
# Fonts
font = pygame.font.SysFont(None, 30)
large_font = pygame.font.SysFont(None, 70)
# Game variables (can change based on difficulty)
player_width = 40
player_height = 35
player_speed = 5
bomb_width = 30
bomb_height = 30
# Load images
player_image = pygame.image.load('solider.png').convert_alpha()
player_image = pygame.transform.scale(player_image, (player_width, player_height))
bomb_image = pygame.image.load('bomb.jpg').convert()
bomb_image = pygame.transform.scale(bomb_image, (bomb_width, bomb_height))
# Function to play background music
def play_background_music(file_path):
pygame.mixer.init()
try:
pygame.mixer.music.load(file_path)
pygame.mixer.music.play(-1)
print(f"Playing background music: '{file_path}'")
except pygame.error as e:
print(f"Audio error: {e}")
except FileNotFoundError:
print(f"Background music file not found: '{file_path}'")
# Function to stop background music
def stop_background_music():
pygame.mixer.music.stop()
pygame.mixer.quit()
# Function for the Bomb Dodge Game menu (difficulty selection)
def bomb_dodge_game_menu():
difficulty = "easy" # Default difficulty
while True:
screen.fill(WHITE)
title_text = large_font.render("Bomb Dodge Game", True, BLACK)
easy_text = font.render("1. Easy", True, BLACK)
normal_text = font.render("2. Normal", True, BLACK)
difficult_text = font.render("3. Difficult", True, BLACK)
instruction_text = font.render("Press Enter to select", True, BLACK)
title_rect = title_text.get_rect(center=(screen_width // 2, screen_height // 4))
easy_rect = easy_text.get_rect(center=(screen_width // 2, screen_height // 2 - 30))
normal_rect = normal_text.get_rect(center=(screen_width // 2, screen_height // 2))
difficult_rect = difficult_text.get_rect(center=(screen_width // 2, screen_height // 2 + 30))
instruction_rect = instruction_text.get_rect(center=(screen_width // 2, screen_height * 3 // 4))
screen.blit(title_text, title_rect)
screen.blit(easy_text, easy_rect)
screen.blit(normal_text, normal_rect)
screen.blit(difficult_text, difficult_rect)
screen.blit(instruction_text, instruction_rect)
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_1:
return "easy"
elif event.key == pygame.K_2:
return "normal"
elif event.key == pygame.K_3:
return "difficult"
# Function to reset game based on difficulty
def reset_game(difficulty):
global player_x, player_y, score, game_over, falling_speed, start_ticks, bomb_list, bomb_spawn_rate
player_x = screen_width // 2 - player_width // 2
player_y = screen_height - player_height - 10
score = 0
game_over = False
start_ticks = pygame.time.get_ticks()
bomb_list = []
if difficulty == "easy":
global player_speed
player_speed = 5
falling_speed = 3
bomb_spawn_rate = 60 # Lower value means more frequent bombs
elif difficulty == "normal":
player_speed = 7
falling_speed = 5
bomb_spawn_rate = 45
elif difficulty == "difficult":
player_speed = 9
falling_speed = 7
bomb_spawn_rate = 30
bomb_spawn_counter = 0
game_over = False
clock = pygame.time.Clock()
# Background music file path
background_music_file = 'music.mp3'
play_background_music(background_music_file)
# Show the Bomb Dodge Game menu (difficulty selection)
selected_difficulty = bomb_dodge_game_menu()
# Start the game
reset_game(selected_difficulty)
while True:
if game_over:
screen.fill(WHITE)
game_over_text = large_font.render("Game Over!", True, RED)
restart_text = font.render("Press 'R' to Restart or 'Q' to Quit", True, BLACK)
score_text = font.render(f"Score: {score}", True, BLACK)
game_over_rect = game_over_text.get_rect(center=(screen_width // 2, screen_height // 3))
score_rect = score_text.get_rect(center=(screen_width // 2, screen_height // 2))
restart_rect = restart_text.get_rect(center=(screen_width // 2, screen_height * 2 // 3))
screen.blit(game_over_text, game_over_rect)
screen.blit(score_text, score_rect)
screen.blit(restart_text, restart_rect)
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
stop_background_music()
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_r:
reset_game(selected_difficulty)
game_over = False
elif event.key == pygame.K_q:
stop_background_music()
pygame.quit()
sys.exit()
else:
for event in pygame.event.get():
if event.type == pygame.QUIT:
stop_background_music()
pygame.quit()
sys.exit()
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and player_x > 0:
player_x -= player_speed
if keys[pygame.K_RIGHT] and player_x < screen_width - player_width:
player_x += player_speed
elapsed_time = (pygame.time.get_ticks() - start_ticks) / 1000
speed_increase_interval = 10 - (selected_difficulty == "easy" and 7 or selected_difficulty == "normal" and 5 or 3)
if elapsed_time >= speed_increase_interval and falling_speed < 15:
speed_multiplier = selected_difficulty == "easy" and 0.5 or selected_difficulty == "normal" and 1 or 1.5
falling_speed += 0.5 * speed_multiplier
start_ticks = pygame.time.get_ticks()
bomb_spawn_counter += 1
if bomb_spawn_counter >= bomb_spawn_rate:
new_bomb = {'x': random.randint(0, screen_width - bomb_width), 'y': -bomb_height}
bomb_list.append(new_bomb)
bomb_spawn_counter = 0
for bomb in bomb_list[:]:
bomb['y'] += falling_speed
if bomb['y'] > screen_height:
bomb_list.remove(bomb)
score += 1
if player_x < bomb['x'] + bomb_width and player_x + player_width > bomb['x']:
if player_y < bomb['y'] + bomb_height and player_y + player_height > bomb['y']:
game_over = True
screen.fill(WHITE)
screen.blit(player_image, (player_x, player_y))
for bomb in bomb_list:
screen.blit(bomb_image, (bomb['x'], bomb['y']))
score_text = font.render(f"Score: {score}", True, BLACK)
screen.blit(score_text, (10, 10))
pygame.display.flip()
clock.tick(60)
stop_background_music()
pygame.quit()
sys.exit()
import pygame
import sys
# Pygame μ΄κΈ°ν (μ΄λ―Έ λμ΄ μλ€λ©΄ μλ΅ κ°λ₯)
pygame.init()
# νλ©΄ ν¬κΈ° (κΈ°μ‘΄ μ½λμ λμΌνκ² μ€μ )
screen_width = 600
screen_height = 400
screen = pygame.display.set_mode((screen_width, screen_height))
# μμ (κΈ°μ‘΄ μ½λμ λμΌνκ² μ€μ )
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
# ν°νΈ (κΈ°μ‘΄ μ½λμ λμΌνκ² μ€μ )
font = pygame.font.SysFont(None, 30)
large_font = pygame.font.SysFont(None, 70)
def bomb_dodge_game_menu():
while True:
screen.fill(WHITE)
title_text = large_font.render("Bomb Dodge Game", True, BLACK)
easy_button_text = font.render("Easy", True, BLACK)
normal_button_text = font.render("Normal", True, BLACK)
difficult_button_text = font.render("Difficult", True, BLACK)
title_rect = title_text.get_rect(center=(screen_width // 2, screen_height // 4))
easy_button_rect = easy_button_text.get_rect(center=(screen_width // 2, screen_height // 2 - 30))
normal_button_rect = normal_button_text.get_rect(center=(screen_width // 2, screen_height // 2))
difficult_button_rect = difficult_button_text.get_rect(center=(screen_width // 2, screen_height // 2 + 30))
screen.blit(title_text, title_rect)
pygame.draw.rect(screen, BLACK, easy_button_rect, 2)
screen.blit(easy_button_text, easy_button_rect)
pygame.draw.rect(screen, BLACK, normal_button_rect, 2)
screen.blit(normal_button_text, normal_button_rect)
pygame.draw.rect(screen, BLACK, difficult_button_rect, 2)
screen.blit(difficult_button_text, difficult_button_rect)
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
mouse_pos = pygame.mouse.get_pos()
if easy_button_rect.collidepoint(mouse_pos):
return "easy"
elif normal_button_rect.collidepoint(mouse_pos):
return "normal"
elif difficult_button_rect.collidepoint(mouse_pos):
return "difficult"
if __name__ == '__main__':
difficulty = bomb_dodge_game_menu()
print(f"μ νλ λμ΄λ: {difficulty}")
# μ¬κΈ°μ μ νλ λμ΄λλ₯Ό μ¬μ©νμ¬ κ²μμ μμνλ λ‘μ§μ μΆκ°νμΈμ.
pygame.quit()
sys.exit()