Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- pygame.init()
- def draw_text(screen, text, size, x, y, color):
- font_name = pygame.font.match_font('arial') # Выбираем тип шрифта для текста
- font = pygame.font.Font(font_name, size) # Шрифт выбранного типа и размера
- text_image = font.render(text, True, color) # Превращаем текст в картинку
- text_rect = text_image.get_rect() # Задаем рамку картинки с текстом
- text_rect.center = (x, y) # Переносим текст в координаты
- screen.blit(text_image, text_rect) # Рисуем текст на экране
- width = 1200
- height = 700
- fps = 60
- name = "Arkanoid"
- lives = 3
- screen = pygame.display.set_mode((width, height))
- pygame.display.set_caption(name)
- ball = pygame.image.load("smile.png") # Спрайт
- ball = pygame.transform.scale(ball, (50, 50))
- ball_rect = ball.get_rect() # Рамка спрайта
- stick = pygame.image.load("racket.png")
- stick_rect = stick.get_rect() # Рамка спрайта
- stick_rect.x = width / 2 - 80
- stick_rect.y = height - 50
- clock = pygame.time.Clock() # Часы для управления скоростью
- speedx = 5 # Горизонтальная скорость
- speedy = 5 # Вертикальная скорость
- run = True
- while run:
- clock.tick(fps) # Тик игровых часов
- for i in pygame.event.get(): # Перебираем игровые события
- if i.type == pygame.QUIT: # Если тип события выход из игры
- run = False # Выходим из игры
- ball_rect.x += speedx
- ball_rect.y += speedy
- key = pygame.key.get_pressed() # Получаем нажатие клавиши
- if key[pygame.K_RIGHT]: # Если нажата стрелка вправо
- stick_rect.x += 5 # Двигаем платформу вправо
- if key[pygame.K_LEFT]: # Если нажата стрелка вправо
- stick_rect.x -= 5 # Двигаем платформу вправо
- if ball_rect.top <= 0: # Отскок от верхней границы
- speedy = -speedy
- if ball_rect.right >= width: # Отскок от правой границы
- speedx = -speedx
- if ball_rect.left <= 0: # отскок от левой границы экрана
- speedx = -speedx
- if ball_rect.top >= height: # отскок от нижней границы экрана
- lives -= 1
- if ball_rect.colliderect(stick_rect):
- speedy = -speedy
- screen.fill("#E0FFFF")
- draw_text(screen, "Lives: " + str(lives), 40, 70, 50, "#00008B")
- screen.blit(ball, ball_rect) # Выводим спрайт на экран
- screen.blit(stick, stick_rect) # Выводим ракетку на экран
- pygame.display.update() # Обновляем экран
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement