-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
46 changed files
with
807 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"difficulty": 3, "fullscreen": "yes", "sound": 25} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import json | ||
import pygame | ||
import os | ||
|
||
def load_config(): | ||
config_path = "config.json" | ||
if not os.path.exists(config_path): | ||
return {"difficulty": 1, "fullscreen": "yes", "sound": 50} | ||
else: | ||
with open(config_path, "r") as f: | ||
return json.load(f) | ||
|
||
def get_screen_size(): | ||
config = load_config() | ||
if config["fullscreen"] == "yes": | ||
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN) | ||
width, height = screen.get_size() | ||
else: | ||
screen = pygame.display.set_mode((0, 0)) | ||
width, height = screen.get_size() | ||
height -= 40 | ||
return width, height | ||
|
||
def get_game_parameters(): | ||
config = load_config() | ||
difficulty = config.get("difficulty", 1) | ||
|
||
if difficulty == 1: # Легкий уровень | ||
score = 200 | ||
lives = 5 | ||
fall_speed = 2 | ||
bomb_spawn_rate = 0.005 | ||
coin_spawn_rate = 0.05 | ||
elif difficulty == 2: # Средний уровень | ||
score = 100 | ||
lives = 5 | ||
fall_speed = 3 | ||
bomb_spawn_rate = 0.01 | ||
coin_spawn_rate = 0.1 | ||
elif difficulty == 3: # Сложный уровень | ||
score = 100 | ||
lives = 3 | ||
fall_speed = 4 | ||
bomb_spawn_rate = 0.01 | ||
coin_spawn_rate = 0.1 | ||
elif difficulty == 4: # Очень сложный уровень | ||
score = 100 | ||
lives = 2 | ||
fall_speed = 5 | ||
bomb_spawn_rate = 0.02 | ||
coin_spawn_rate = 0.2 | ||
elif difficulty == 5: # Экстремальный уровень | ||
score = 0 | ||
lives = 1 | ||
fall_speed = 6 | ||
bomb_spawn_rate = 0.02 | ||
coin_spawn_rate = 0.2 | ||
else: # Значение по умолчанию для неопределенного уровня сложности | ||
score = 100 | ||
lives = 3 | ||
fall_speed = 3 | ||
bomb_spawn_rate = 0.01 | ||
coin_spawn_rate = 0.1 | ||
|
||
return score, lives, fall_speed, bomb_spawn_rate, coin_spawn_rate | ||
|
||
def get_sound_level(): | ||
config = load_config() | ||
return config.get("sound", 50) | ||
|
||
# Пример использования функций | ||
if __name__ == "__main__": | ||
pygame.init() | ||
width, height = get_screen_size() | ||
print(f"Screen size: {width}x{height}") | ||
|
||
score, lives, fall_speed, bomb_spawn_rate, coin_spawn_rate = get_game_parameters() | ||
print(f"Score: {score}, Lives: {lives}, Fall Speed: {fall_speed}, Bomb Spawn Rate: {bomb_spawn_rate}, Coin Spawn Rate: {coin_spawn_rate}") | ||
|
||
sound_level = get_sound_level() | ||
print(f"Sound level: {sound_level}") | ||
pygame.quit() |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
import pygame | ||
import time | ||
import random | ||
import sys | ||
import json | ||
import os | ||
|
||
# Инициализация Pygame | ||
pygame.init() | ||
|
||
# Загрузка и масштабирование изображений | ||
def load_and_scale_image(path, scale_factor=1): | ||
image = pygame.image.load(path) | ||
return pygame.transform.scale(image, (image.get_width() // scale_factor, image.get_height() // scale_factor)) | ||
|
||
background_image = load_and_scale_image("sprits/game/fon.png", 1) | ||
|
||
# Загрузка кадров анимации бомбы | ||
bomb_frames = [load_and_scale_image(f"sprits/game/bomb/bomb-{i}.png", 2) for i in range(7)] | ||
coin_image = load_and_scale_image("sprits/game/coin.png", 2) | ||
|
||
# Загрузка шрифта | ||
font = pygame.font.Font("fonts/HomeVideo-Regular.otf", 36) | ||
|
||
# Цвета | ||
WHITE = (255, 255, 255) | ||
|
||
def generate_fall_speeds(fall_speed): | ||
""" Генерирует три скорости падения на основе текущей fall_speed """ | ||
min_speed = fall_speed * 0.8 # 20% меньше | ||
max_speed = fall_speed * 1.6 # 60% больше | ||
return [ | ||
min_speed + (max_speed - min_speed) * 0.2, # 20% от диапазона | ||
min_speed + (max_speed - min_speed) * 0.5, # 50% от диапазона | ||
min_speed + (max_speed - min_speed) * 0.8 # 80% от диапазона | ||
] | ||
|
||
def spawn_item(image_frames, spawn_rate, items_list, screen_width, fall_speed): | ||
if random.random() < spawn_rate: | ||
x = random.randint(0, screen_width - image_frames[0].get_width()) | ||
y = -image_frames[0].get_height() | ||
fall_speeds = generate_fall_speeds(fall_speed) # Генерация скоростей | ||
speed = random.choice(fall_speeds) # Выбор случайной скорости | ||
items_list.append({ | ||
'rect': pygame.Rect(x, y, image_frames[0].get_width(), image_frames[0].get_height()), | ||
'frames': image_frames, | ||
'frame_index': 0, | ||
'speed': speed, | ||
'last_update': pygame.time.get_ticks() | ||
}) | ||
|
||
def update_items(items_list, screen_height, item_type, score, lives): | ||
mouse_x, mouse_y = pygame.mouse.get_pos() | ||
mouse_pressed = pygame.mouse.get_pressed()[0] | ||
|
||
to_remove = [] | ||
|
||
for item in items_list: | ||
item['rect'].y += item['speed'] # Используем случайную скорость | ||
|
||
# Обновление текущего кадра анимации | ||
current_time = pygame.time.get_ticks() | ||
if current_time - item['last_update'] > 300: # смена кадра каждые 300 мс | ||
if item['frame_index'] < len(item['frames']) - 1: | ||
item['frame_index'] += 1 | ||
item['last_update'] = current_time | ||
|
||
if item['rect'].y > screen_height: | ||
to_remove.append(item) | ||
if item_type == 'coin': | ||
lives -= 1 | ||
if lives <= 0: | ||
return 'game_over', score, lives | ||
|
||
elif mouse_pressed and item['rect'].collidepoint(mouse_x, mouse_y): | ||
to_remove.append(item) | ||
if item_type == 'coin': | ||
score += 5 | ||
elif item_type == 'bomb': | ||
score -= 50 | ||
if score < 0: | ||
score = 0 | ||
lives -= 1 | ||
if lives <= 0: | ||
return 'game_over', score, lives | ||
|
||
for item in to_remove: | ||
items_list.remove(item) | ||
|
||
return None, score, lives | ||
|
||
def draw_items(items_list, screen): | ||
for item in items_list: | ||
frame = item['frames'][item['frame_index']] | ||
screen.blit(frame, item['rect'].topleft) | ||
|
||
def draw_text(screen, text, font, color, position): | ||
surface = font.render(text, True, color) | ||
screen.blit(surface, position) | ||
|
||
def save_game_over_data(score, elapsed_time): | ||
# Создание папки, если она не существует | ||
os.makedirs('settings/top', exist_ok=True) | ||
|
||
# Данные для сохранения | ||
data = { | ||
'max_score': score, | ||
'game_duration': int(elapsed_time) # Преобразование времени в целое число | ||
} | ||
|
||
# Запись данных в файл | ||
with open('settings/top/game_over.json', 'w') as f: | ||
json.dump(data, f, indent=4) | ||
|
||
def game(screen_width, screen_height, initial_score, initial_lives, fall_speed, bomb_spawn_rate, coin_spawn_rate): | ||
screen = pygame.display.set_mode((screen_width, screen_height), pygame.FULLSCREEN if screen_height == pygame.display.Info().current_h else 0) | ||
clock = pygame.time.Clock() | ||
|
||
score = initial_score | ||
lives = initial_lives | ||
bombs = [] | ||
coins = [] | ||
|
||
running = True | ||
start_time = pygame.time.get_ticks() | ||
last_speed_increase_time = start_time # Время последнего увеличения скорости | ||
|
||
while running: | ||
for event in pygame.event.get(): | ||
if event.type == pygame.QUIT: | ||
pygame.quit() | ||
sys.exit() | ||
|
||
spawn_item(bomb_frames, bomb_spawn_rate, bombs, screen_width, fall_speed) | ||
spawn_item([coin_image], coin_spawn_rate, coins, screen_width, fall_speed) | ||
|
||
result, score, lives = update_items(bombs, screen_height, 'bomb', score, lives) | ||
if result == 'game_over': | ||
elapsed_time = (pygame.time.get_ticks() - start_time) / 1000 | ||
save_game_over_data(score, elapsed_time) | ||
return 'game_over' | ||
|
||
result, score, lives = update_items(coins, screen_height, 'coin', score, lives) | ||
if result == 'game_over': | ||
elapsed_time = (pygame.time.get_ticks() - start_time) / 1000 | ||
save_game_over_data(score, elapsed_time) | ||
return 'game_over' | ||
|
||
current_time = pygame.time.get_ticks() / 1000 | ||
elapsed_time = current_time - (start_time / 1000) | ||
|
||
# Обновление скорости падения каждые 60 секунд | ||
if current_time - (last_speed_increase_time / 1000) > 60: | ||
fall_speed += 1 | ||
last_speed_increase_time = pygame.time.get_ticks() # Обновление времени последнего увеличения скорости | ||
|
||
screen.blit(background_image, (0, 0)) | ||
draw_items(bombs, screen) | ||
draw_items(coins, screen) | ||
|
||
draw_text(screen, f"Счет: {score}", font, WHITE, (10, 10)) | ||
draw_text(screen, f"Жизни: {lives}", font, WHITE, (10, 50)) | ||
|
||
pygame.display.flip() | ||
time.sleep(0.01) | ||
|
||
return 'quit' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
import pygame | ||
import sys | ||
import json | ||
import os | ||
import math | ||
|
||
# Инициализация Pygame | ||
pygame.init() | ||
|
||
# Установка окна на полный экран | ||
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN) | ||
pygame.display.set_caption('Game Over Screen') | ||
|
||
# Получение размеров экрана | ||
screen_width, screen_height = screen.get_size() | ||
|
||
# Загрузка ресурсов | ||
background = pygame.image.load('sprits/game_over/game_over.png') | ||
background = pygame.transform.scale(background, (screen_width, screen_height)) | ||
|
||
font_path = 'fonts/HomeVideo-Regular.otf' | ||
font_size = 55 | ||
font_color = (255, 255, 255) # Белый цвет | ||
|
||
menu_img = pygame.image.load('sprits/game_over/menu.png') | ||
menu_hover_img = pygame.image.load('sprits/game_over/menuactive.png') | ||
|
||
font = pygame.font.Font(font_path, font_size) | ||
|
||
def load_config(): | ||
with open('config.json', 'r') as f: | ||
return json.load(f) | ||
|
||
def get_settings_file_path(difficulty): | ||
return f'settings/top/top_{difficulty}.json' | ||
|
||
def load_game_data(): | ||
file_path = 'settings/top/game_over.json' | ||
if os.path.exists(file_path): | ||
with open(file_path, 'r') as f: | ||
data = json.load(f) | ||
return data.get('max_score', 0), data.get('game_duration', 0) | ||
return 0, 0 | ||
|
||
def load_game_over_data(): | ||
config = load_config() | ||
difficulty = config['difficulty'] | ||
settings_file_path = get_settings_file_path(difficulty) | ||
if os.path.exists(settings_file_path): | ||
with open(settings_file_path, 'r') as f: | ||
data = json.load(f) | ||
return data.get('max_score', 0), data.get('formatted_time', "00:00:00") | ||
return 0, "00:00:00" | ||
|
||
def save_game_over_data(max_score, formatted_time): | ||
config = load_config() | ||
difficulty = config['difficulty'] | ||
settings_file_path = get_settings_file_path(difficulty) | ||
data = { | ||
'max_score': max_score, | ||
'formatted_time': formatted_time | ||
} | ||
os.makedirs(os.path.dirname(settings_file_path), exist_ok=True) | ||
with open(settings_file_path, 'w') as f: | ||
json.dump(data, f) | ||
|
||
def format_time(seconds): | ||
hours = seconds // 3600 | ||
minutes = (seconds % 3600) // 60 | ||
seconds = seconds % 60 | ||
return f'{hours:02}:{minutes:02}:{seconds:02}' | ||
|
||
def game_over_screen(): | ||
# Загрузка текущих данных из файла game_over.json и вызов экрана | ||
current_score, game_duration = load_game_data() | ||
max_score, saved_time = load_game_over_data() | ||
new_record = current_score > max_score | ||
|
||
if new_record: | ||
max_score = current_score | ||
formatted_time = format_time(game_duration) | ||
save_game_over_data(max_score, formatted_time) | ||
|
||
running = True | ||
start_ticks = pygame.time.get_ticks() | ||
period = 1000 # период в миллисекундах для одного цикла анимации | ||
start_size = 65 | ||
end_size = 75 | ||
|
||
while running: | ||
for event in pygame.event.get(): | ||
if event.type == pygame.QUIT: | ||
pygame.quit() | ||
sys.exit() | ||
|
||
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: | ||
running = False # Переход в меню | ||
|
||
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: | ||
mouse_pos = pygame.mouse.get_pos() | ||
menu_rect = menu_img.get_rect(center=(screen_width // 2, screen_height // 2 + 250)) | ||
if menu_rect.collidepoint(mouse_pos): | ||
return "game_over" | ||
|
||
screen.blit(background, (0, 0)) | ||
|
||
formatted_time = format_time(game_duration) | ||
|
||
max_score_text = font.render(f'Счет: {max_score}', True, font_color) | ||
game_time_text = font.render(f'Время игры: {formatted_time}', True, font_color) | ||
|
||
max_score_rect = max_score_text.get_rect(center=(screen_width // 2, screen_height // 2 - 50)) | ||
game_time_rect = game_time_text.get_rect(center=(screen_width // 2, screen_height // 2 + 50)) | ||
|
||
screen.blit(max_score_text, max_score_rect) | ||
screen.blit(game_time_text, game_time_rect) | ||
|
||
if new_record: | ||
ticks = pygame.time.get_ticks() | ||
elapsed_time = ticks - start_ticks | ||
# Использование синусоидальной функции для плавной анимации | ||
scale = (math.sin(2 * math.pi * (elapsed_time % period) / period) + 1) / 2 | ||
current_size = start_size + (end_size - start_size) * scale | ||
anim_font = pygame.font.Font(font_path, int(current_size)) | ||
new_record_text = anim_font.render("Новый рекорд!", True, font_color) | ||
new_record_rect = new_record_text.get_rect(center=(screen_width // 2, screen_height // 2 - 150)) | ||
screen.blit(new_record_text, new_record_rect) | ||
|
||
menu_rect = menu_img.get_rect(center=(screen_width // 2, screen_height // 2 + 250)) | ||
|
||
mouse_pos = pygame.mouse.get_pos() | ||
|
||
if menu_rect.collidepoint(mouse_pos): | ||
screen.blit(menu_hover_img, menu_rect) | ||
else: | ||
screen.blit(menu_img, menu_rect) | ||
|
||
pygame.display.flip() |
Oops, something went wrong.