Используй следующий код как контекст: import pygame import random import time import…
- User
Используй следующий код как контекст:
import pygame import random import time import math
Инициализация Pygame
pygame.init()
Константы
WIDTH, HEIGHT = 380, 600 FPS = 60
Цвета
GREEN = (0, 100, 0) WHITE = (255, 255, 255) BLACK = (0, 0, 0) BLUE = (0, 0, 255) GRAY = (128, 128, 128) YELLOW = (255, 255, 0) RED = (255, 0, 0) ORANGE = (255, 165, 0)
Настройки игры
#GAME_DURATION = 30 GAME_DURATION = 120 FREEZE_TIME = 4 CELL_SIZE = 40
Классы для элементов
class FallingObject: def init(self, shape, color, size, score_value, speed, is_bomb=False): self.shape = shape self.color = color self.size = size self.score_value = score_value self.is_bomb = is_bomb self.x = random.randint(0 + size, WIDTH - size) self.y = 0 self.speed = speed
def fall(self): self.y += self.speed def draw(self, surface): draw_functions = { "snowflake": self.draw_snowflake, "crystal": self.draw_crystal, "bomb": self.draw_bomb, } draw_functions[self.shape](surface) def draw_snowflake(self, surface): center = (self.x, self.y) for i in range(6): angle = i * (360 / 6) x_offset = self.size * 0.5 * math.cos(math.radians(angle)) y_offset = self.size * 0.5 * math.sin(math.radians(angle)) pygame.draw.line(surface, WHITE, center, (center[0] + x_offset, center[1] + y_offset), 2) pygame.draw.line(surface, WHITE, (center[0] + x_offset, center[1] + y_offset), (center[0] + x_offset * 0.5, center[1] + y_offset * 0.5), 1) def draw_crystal(self, surface): points = [ (self.x, self.y), (self.x + self.size // 2, self.y - self.size // 1.5), (self.x + self.size, self.y), (self.x + self.size // 1.5, self.y + self.size // 2), (self.x + self.size // 2, self.y + self.size), (self.x, self.y) ] pygame.draw.polygon(surface, self.color, points) pygame.draw.polygon(surface, WHITE, points, 2) def draw_bomb(self, surface): pygame.draw.circle(surface, GRAY, (self.x, self.y), self.size // 3) pygame.draw.line(surface, WHITE, (self.x, self.y - self.size // 3), (self.x, self.y - self.size // 3 - 10), 2) pygame.draw.circle(surface, YELLOW, (self.x, self.y - self.size // 3 - 10), 5)class Button: def init(self, text, x, y, width, height, font_size=30): self.rect = pygame.Rect(x, y, width, height) self.text = text self.font = pygame.font.Font(None, font_size)
def draw(self, surface): pygame.draw.rect(surface, WHITE, self.rect) text_surface = self.font.render(self.text, True, BLACK) text_rect = text_surface.get_rect(center=self.rect.center) surface.blit(text_surface, text_rect) def is_hovered(self, pos): return self.rect.collidepoint(pos)def display_message(screen, message, size, color, y_offset=0): font = pygame.font.Font(None, size) text_surface = font.render(message, True, color) text_rect = text_surface.get_rect(center=(WIDTH // 2, HEIGHT // 2 + y_offset)) screen.blit(text_surface, text_rect)
- Assistant
Ваш код представляет собой простую игру на Pygame, в которой игрок управляет падением объектов и получает очки, кликая по ним. В игре присутствуют снежинки, кристаллы и бомбы, которые игрок должен избегать. Опишу структуру программы и несколько деталей, если вы хотите расширить функционал или улучшить игровой процесс.
- User
Можешь ли для эффекта взрыва реализовать функцию небольшой тряски экрана (не затрагивать элементы)?
- Assistant
- User
Что-то пошло не так и теперь при нажатии на кнопка старта игры - просто чёрный экран
- Assistant
- User
всё хорошо, но почему ты постоянно в функции main удаляешь код для логики заморозки и эффекта подбора снежинок и бомб
- Assistant
- User
отредактируй тряску так, чтобы больше было смещения в сторону
- Assistant