Untitled

 avatar
unknown
plain_text
a month ago
5.1 kB
4
Indexable
import pygame
pygame.init()

MONSTER_WIDTH = 70
PLATFORM_WIDTH = 90
BALL_SIZE = 50
SPACING = 5

back = (200, 255, 255)
mw = pygame.display.set_mode((500, 500))
mw.fill(back)

background = pygame.image.load("Vi.png")
background = pygame.transform.scale(background, (500,500))

class Area():
    def __init__(self, x=0, y=0, width=10, height=10, color=None):
        self.rect = pygame.Rect(x, y, width, height)
        self.fill_color = back
        if color:
            self.fill_color = color

    def color(self, new_color):
        self.fill_color = new_color

    def fill(self):
        pygame.draw.rect(mw, self.fill_color, self.rect)

    def collidepoint(self, x, y):
        return self.rect.collidepoint(x, y)

    def is_collide(self, game_object):
        return self.rect.colliderect(game_object.rect)

class Picture(Area):
    def __init__(self, x=0, y=0, width=10, height=10, color=None, filename = ""):
        super().__init__(x, y, width, height, color)
        self.image = pygame.image.load(filename)

    def draw(self):
        mw.blit(self.image, (self.rect.x, self.rect.y))

class Label(Area):
    def set_text(self, text, fsize=12, text_color=(0, 0, 0)):
        self.image = pygame.font.SysFont('verdana', fsize).render(text, True, text_color)
    def draw(self, shift_x=0, shift_y=0):
        self.fill()
        mw.blit(self.image, (self.rect.x + shift_x, self.rect.y + shift_y))

ball = Picture(200,300,50,50, filename="ball.png")
platform = Picture(200,400, PLATFORM_WIDTH,20, filename= "platform.png")
monsters = []

start_x = SPACING
start_y = SPACING

count = 7
for j in range(4):
    y = start_y + ((MONSTER_WIDTH) * j )
    x = start_x + ((MONSTER_WIDTH / 2) * j)
    for i in range(count):
        m = Picture(x, y, MONSTER_WIDTH, MONSTER_WIDTH, filename = "enemy.png")
        monsters.append(m)
        x = x + MONSTER_WIDTH + SPACING
    count = count - 1

clock = pygame.time.Clock()

move_right = False
move_left = False

DEFAULT_SPEED = 3

speed_x = DEFAULT_SPEED
speed_y = DEFAULT_SPEED

platform_speed = 6

game_over = False

# Функція для встановлення складності
def set_difficulty(level):
    global speed_x, speed_y, platform_speed, DEFAULT_SPEED
    if level == 1:   # Легко
        DEFAULT_SPEED = 1
        platform_speed = 1
    elif level == 2: # Середньо
        DEFAULT_SPEED = 3
        platform_speed = 5
    elif level == 3: # Важко
        DEFAULT_SPEED = 5
        platform_speed = 4


# Початково середній рівень
set_difficulty(2)

while not game_over:
    mw.fill(back)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_over = True

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_a:
                move_left = True
            if event.key == pygame.K_d:
                move_right = True
            if event.key == pygame.K_1:
                set_difficulty(1)
            if event.key == pygame.K_2:
                set_difficulty(2)
            if event.key == pygame.K_3:
                set_difficulty(3)
    

        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_a:
                move_left = False
            if event.key == pygame.K_d:
                move_right = False

    mw.blit(background,(0,0))

    if move_left and platform.rect.x > 0:
        platform.rect.x -= platform_speed
    if move_right and platform.rect.x + PLATFORM_WIDTH < 500:
        platform.rect.x += platform_speed

    if ball.rect.y < 1:
        speed_y = -abs(speed_y)

    if ball.rect.x > 500 - BALL_SIZE:
        speed_x = -abs(speed_x)
    if ball.rect.x < 0:
        speed_x = abs(speed_x)

    if ball.is_collide(platform):
        speed_y = abs(speed_y)
        hit_pos = (ball.rect.x + BALL_SIZE / 2) - platform.rect.x
        third = PLATFORM_WIDTH / 3
        if hit_pos < third:
            speed_x = -abs(DEFAULT_SPEED)
        elif hit_pos < 2 * third:
            speed_x = 0
        else:
            speed_x = abs(DEFAULT_SPEED)

    if ball.rect.y > (platform.rect.y + 20):
        time_text = Label(150,250,50,50, back)
        time_text.set_text('YOU LOSE',60, (255,0,0))
        time_text.draw(10,10)
        game_over = True

    ball.rect.x += speed_x
    ball.rect.y -= speed_y

    ball.draw()
    platform.draw()

    for m in monsters[:]:
        if m.is_collide(ball):
            monsters.remove(m)
           
            speed_y = -speed_y
            break

    if len(monsters) == 0:
        game_over = True
        l = Label(0,0, 600,600, back)
        l.set_text("YOU WIN", 72, (10,230,30))
        l.draw(167,220)

    for m in monsters:
        m.draw()

    hint = Label(5, 470, 200, 20, back)
    hint.set_text("Press 1=Easy  2=Medium  3=Hard", 16, (0,0,0))
    hint.draw(0,0)

    pygame.display.update()
    clock.tick(40)

pygame.quit()
Editor is loading...
Leave a Comment