Advertisement
Lillgrinn

pygame #1

Apr 14th, 2023
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.87 KB | None | 0 0
  1. import pygame
  2.  
  3. pygame.init()
  4. # Создание окна
  5. WIDTH = 1000
  6. HEIGHT = 800
  7. screen = pygame.display.set_mode((WIDTH, HEIGHT))
  8. # FPS
  9. clock = pygame.time.Clock()
  10. FPS = 30
  11. # Игровые переменные
  12. white = (255, 255, 255)
  13. dark = (0, 0, 0)
  14. red = (255, 0, 0)
  15. cuber_size = 70
  16. cuber_color = dark
  17. cuber = pygame.rect.Rect(0, 0, cuber_size, cuber_size)
  18. cuber.center = screen.get_rect().center  # Центр Кубера берём из центра экрана
  19. is_game_run = True
  20.  
  21. # Игровой цикл
  22. while is_game_run:
  23.     # Обработка событий
  24.     for event in pygame.event.get():
  25.         if event.type == pygame.QUIT:
  26.             is_game_run = False
  27.         if event.type == pygame.MOUSEBUTTONDOWN:
  28.             # Если левая кнопка и нажали внутри Кубера
  29.             if event.button == 1 and cuber.collidepoint(event.pos):
  30.                 cuber_color = red
  31.             if event.button == 2:
  32.                 cuber.center = event.pos
  33.     buttons = pygame.mouse.get_pressed()  # Посмотрим, какие кнопки нажаты
  34.  
  35.     # Игровая логика
  36.     # Сдвинемся в зависимости от того, что нажали
  37.     cuber.centerx += -10 * buttons[0] + 10 * buttons[2]
  38.     # если Кубер не полность внутри экрана
  39.     if not screen.get_rect().contains(cuber):
  40.         # Вернём на границы экрана
  41.         cuber.left = 0 if cuber.left < 0 else cuber.left
  42.         cuber.right = WIDTH if cuber.right > WIDTH else cuber.right
  43.         cuber.top = 0 if cuber.top < 0 else cuber.top
  44.         cuber.bottom = HEIGHT if cuber.bottom > HEIGHT else cuber.bottom
  45.  
  46.     # Отрисовка
  47.     screen.fill(white)
  48.     pygame.draw.rect(screen, cuber_color, cuber)
  49.     pygame.display.flip()
  50.     clock.tick(FPS)
  51.  
  52. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement