Advertisement
Orleon

Untitled

Nov 27th, 2021
927
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.18 KB | None | 0 0
  1. import pygame
  2.  
  3. pygame.init()
  4.  
  5.  
  6. def draw_text(screen, text, size, x, y, color):
  7.     font_name = pygame.font.match_font('arial')    # Выбираем тип шрифта для текста
  8.     font = pygame.font.Font(font_name, size)       # Шрифт выбранного типа и размера
  9.     text_image = font.render(text, True, color)    # Превращаем текст в картинку
  10.     text_rect = text_image.get_rect()              # Задаем рамку картинки с текстом
  11.     text_rect.center = (x, y)                       # Переносим текст в координаты
  12.     screen.blit(text_image, text_rect)             # Рисуем текст на экране
  13.  
  14. width = 1200
  15. height = 700
  16. fps = 60
  17. name = "Arkanoid"
  18.  
  19. lives = 3
  20. screen = pygame.display.set_mode((width, height))
  21. pygame.display.set_caption(name)
  22.  
  23. ball = pygame.image.load("smile.png")    # Спрайт
  24. ball = pygame.transform.scale(ball, (50, 50))
  25. ball_rect = ball.get_rect()              # Рамка спрайта
  26.  
  27. stick = pygame.image.load("racket.png")
  28. stick_rect = stick.get_rect()              # Рамка спрайта
  29. stick_rect.x = width / 2 - 80
  30. stick_rect.y = height - 50
  31.  
  32. clock = pygame.time.Clock()         # Часы для управления скоростью
  33. speedx = 5                         # Горизонтальная скорость
  34. speedy = 5                         # Вертикальная скорость
  35.  
  36. run = True
  37. while run:
  38.     clock.tick(fps)                 # Тик игровых часов
  39.  
  40.     for i in pygame.event.get():    # Перебираем игровые события
  41.         if i.type == pygame.QUIT:   # Если тип события выход из игры
  42.             run = False             # Выходим из игры
  43.  
  44.     ball_rect.x += speedx
  45.     ball_rect.y += speedy
  46.  
  47.  
  48.     key = pygame.key.get_pressed()   # Получаем нажатие клавиши
  49.     if key[pygame.K_RIGHT]:          # Если нажата стрелка вправо
  50.         stick_rect.x += 5            # Двигаем платформу вправо
  51.  
  52.     if key[pygame.K_LEFT]:          # Если нажата стрелка вправо
  53.         stick_rect.x -= 5            # Двигаем платформу вправо
  54.  
  55.  
  56.     if ball_rect.top <= 0:  # Отскок от верхней границы
  57.         speedy = -speedy
  58.  
  59.  
  60.     if ball_rect.right >= width:  # Отскок от правой границы
  61.         speedx = -speedx
  62.  
  63.  
  64.     if ball_rect.left <= 0:  # отскок от левой границы экрана
  65.         speedx = -speedx
  66.  
  67.     if ball_rect.top >= height:  # отскок от нижней границы экрана
  68.         lives -= 1
  69.  
  70.  
  71.     if ball_rect.colliderect(stick_rect):
  72.         speedy = -speedy
  73.  
  74.  
  75.     screen.fill("#E0FFFF")
  76.     draw_text(screen, "Lives: " + str(lives), 40, 70, 50, "#00008B")
  77.     screen.blit(ball, ball_rect)       # Выводим спрайт на экран
  78.     screen.blit(stick, stick_rect)     # Выводим ракетку на экран
  79.     pygame.display.update()            # Обновляем экран
  80.  
  81.  
  82. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement