Advertisement
zicaentu

Часы

Oct 8th, 2018
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.96 KB | None | 0 0
  1. import pygame
  2. import math
  3.  
  4.  
  5. WIN_WIDTH  = 1240                   # Ширина экрана
  6. WIN_HEIGHT = 960                    # Высота экрана
  7. WIN_SIZE = [WIN_WIDTH, WIN_HEIGHT]  # Размеры экрана
  8. FPS = 30                            # Frame Per Second(Кадров в секунду)
  9.  
  10. center = (WIN_WIDTH // 2, WIN_HEIGHT // 2)
  11.  
  12. # Цвета в шестнадцаричной системе счисления
  13. WHITE = (0xff, 0xff, 0xff)
  14. BLACK = (0x00, 0x00, 0x00)
  15. RED = (0xff, 0x00, 0x00)
  16. GREEN = (0x00, 0xff, 0x00)
  17. BLUE = (0x00, 0x00, 0xff)
  18. GREY = (0xcc, 0xcc, 0xcc)
  19. DARK_GREY = (0x33, 0x33, 0x33)
  20. DARK_GREY_2 = (0x22, 0x22, 0x22)
  21. DARK_BLUE = (0x3e, 0x42, 0x43)
  22. LIGHT_GREY = (0x2b, 0x2c, 0x2d)
  23. WHITE_GREY = (0xd7, 0xd7, 0xd7)
  24.  
  25. # Инициализация игрового движка
  26. pygame.init()
  27.  
  28. # Создание окна
  29. screen = pygame.display.set_mode(WIN_SIZE)
  30. # Установка заголовка окна
  31. pygame.display.set_caption("My game")
  32.  
  33. # Используется для контроля fps
  34. clock = pygame.time.Clock()
  35.  
  36. # Шрифт
  37. font = pygame.font.Font("freesansbold.ttf", 12)
  38.  
  39. # Настройки
  40. radius = 221
  41. alpha = math.pi / 2
  42.  
  43. # Ремень
  44. strap_w = 230
  45. strap_h = 700
  46.  
  47. # Крепление ремня
  48. belt_face = 5
  49.  
  50. belt_mount_w = 280
  51. belt_mount_h = 500
  52.  
  53. belt_mount_w_2 = belt_mount_w - 2
  54. belt_mount_h_2 = belt_mount_h - 2
  55.  
  56. belt_mount_w_3 = belt_mount_w - 40
  57. belt_mount_h_3 = belt_mount_h - 64
  58.  
  59. # Завод
  60. charge_w = 15
  61. charge_h = 40
  62. charge_face = 5
  63.  
  64. charge_w += radius
  65.  
  66. # Рамка
  67. frame_margin = 30
  68. frame_face = 50
  69. frame_line_w = 2
  70. frame_color = BLACK
  71.  
  72. gameExit = False
  73. #**************** Основной цикл игры ************************
  74. while not gameExit:
  75.     # Обработка событий
  76.     for event in pygame.event.get():
  77.         if event.type == pygame.QUIT:
  78.             gameExit = True
  79.     # -----------------  
  80.  
  81.          
  82.     # Игровая логика
  83.     x1 = int(-1 * radius * math.cos(alpha) * 0.8) + center[0]
  84.     y1 = int(-1 * radius * math.sin(alpha) * 0.8) + center[1]
  85.  
  86.     x2 = int(-1 * radius * math.cos(alpha * 0.1) * 0.5) + center[0]
  87.     y2 = int(-1 * radius * math.sin(alpha * 0.1) * 0.5) + center[1]
  88.    
  89.     alpha += 0.01 * math.pi
  90.     if alpha == 2 * math.pi:
  91.         alpha = 0
  92.     # -----------------
  93.  
  94.  
  95.     # Рисование
  96.     screen.fill(WHITE)      # Залить экран
  97.    
  98.     # Рамка
  99.     '''pygame.draw.arc(screen, BLACK,
  100.        (frame_margin, frame_margin, frame_face*2, frame_face*2),
  101.        math.pi/2, math.pi, frame_line_w)    
  102.    pygame.draw.arc(screen, BLACK,
  103.        (WIN_WIDTH - frame_margin - 2*frame_face, frame_margin, frame_face*2, frame_face*2),
  104.        0, math.pi/2, frame_line_w)    
  105.    pygame.draw.arc(screen, BLACK,
  106.        (frame_margin, WIN_HEIGHT - frame_margin - frame_face*2, frame_face*2, frame_face*2),
  107.        math.pi, 3*math.pi/2, frame_line_w)    
  108.    pygame.draw.arc(screen, BLACK,
  109.        (WIN_WIDTH - frame_margin - 2*frame_face, WIN_HEIGHT - frame_margin - frame_face*2,
  110.            frame_face*2, frame_face*2),
  111.        3*math.pi/2, 0, frame_line_w)'''
  112.     pygame.draw.circle(screen, frame_color,
  113.         (frame_margin + frame_face, frame_margin + frame_face),
  114.         frame_face, frame_line_w)    
  115.     pygame.draw.circle(screen, frame_color,
  116.         (WIN_WIDTH - frame_margin - frame_face, frame_margin + frame_face),
  117.         frame_face, frame_line_w)    
  118.     pygame.draw.circle(screen, frame_color,
  119.         (frame_margin + frame_face, WIN_HEIGHT - frame_margin - frame_face),
  120.         frame_face, frame_line_w)    
  121.     pygame.draw.circle(screen, frame_color,
  122.         (WIN_WIDTH - frame_margin - frame_face, WIN_HEIGHT - frame_margin - frame_face),
  123.         frame_face, frame_line_w)
  124.    
  125.     pygame.draw.rect(screen, WHITE,
  126.         (frame_margin + frame_face, frame_margin,
  127.             WIN_WIDTH - 2*frame_margin - 2*frame_face, WIN_HEIGHT - 2*frame_margin))
  128.     pygame.draw.rect(screen, WHITE,
  129.         (frame_margin, frame_margin + frame_face,
  130.             WIN_WIDTH - 2*frame_margin, WIN_HEIGHT - 2*frame_margin - 2*frame_face))
  131.    
  132.     pygame.draw.line(screen, frame_color,
  133.         (frame_margin + frame_face, frame_margin),
  134.             (WIN_WIDTH - frame_margin - frame_face, frame_margin), frame_line_w)    
  135.     pygame.draw.line(screen, frame_color,
  136.         (frame_margin, frame_margin + frame_face),
  137.             (frame_margin, WIN_HEIGHT - frame_margin - frame_face), frame_line_w)    
  138.     pygame.draw.line(screen, frame_color,
  139.         (frame_margin + frame_face, WIN_HEIGHT - frame_margin),
  140.             (WIN_WIDTH - frame_margin - frame_face, WIN_HEIGHT - frame_margin), frame_line_w)    
  141.     pygame.draw.line(screen, frame_color,
  142.         (WIN_WIDTH - frame_margin, frame_margin + frame_face),
  143.             (WIN_WIDTH - frame_margin, WIN_HEIGHT - frame_margin - frame_face), frame_line_w)
  144.  
  145.     # Ремень
  146.     pygame.draw.rect(screen, DARK_GREY_2,
  147.         (center[0] - strap_w // 2, center[1] - strap_h // 2, strap_w, strap_h))
  148.  
  149.     # Крепление ремня
  150.     pygame.draw.circle(screen, BLACK,
  151.         (center[0] - belt_mount_w//2 + belt_face, center[1] - belt_mount_h//2 + belt_face),
  152.         belt_face)      
  153.     pygame.draw.circle(screen, BLACK,
  154.         (center[0] + belt_mount_w//2 - belt_face, center[1] - belt_mount_h//2 + belt_face),
  155.         belt_face)      
  156.     pygame.draw.circle(screen, BLACK,
  157.         (center[0] - belt_mount_w//2 + belt_face, center[1] + belt_mount_h//2 - belt_face),
  158.         belt_face)    
  159.     pygame.draw.circle(screen, BLACK,
  160.         (center[0] + belt_mount_w//2 - belt_face, center[1] + belt_mount_h//2 - belt_face),
  161.         belt_face)
  162.     pygame.draw.rect(screen, BLACK,
  163.         (center[0] - belt_mount_w//2, center[1] - belt_mount_h//2 + belt_face,
  164.             belt_mount_w, belt_mount_h - 2 * belt_face))    
  165.     pygame.draw.rect(screen, BLACK,
  166.         (center[0] - belt_mount_w//2 + belt_face, center[1] - belt_mount_h//2,
  167.             belt_mount_w - 2 * belt_face, belt_mount_h))
  168.  
  169.     pygame.draw.circle(screen, DARK_GREY,
  170.         (center[0] - belt_mount_w_2//2 + belt_face, center[1] - belt_mount_h_2//2 + belt_face),
  171.         belt_face)      
  172.     pygame.draw.circle(screen, DARK_GREY,
  173.         (center[0] + belt_mount_w_2//2 - belt_face, center[1] - belt_mount_h_2//2 + belt_face),
  174.         belt_face)      
  175.     pygame.draw.circle(screen, DARK_GREY,
  176.         (center[0] - belt_mount_w_2//2 + belt_face, center[1] + belt_mount_h_2//2 - belt_face),
  177.         belt_face)    
  178.     pygame.draw.circle(screen, DARK_GREY,
  179.         (center[0] + belt_mount_w_2//2 - belt_face, center[1] + belt_mount_h_2//2 - belt_face),
  180.         belt_face)
  181.     pygame.draw.rect(screen, DARK_GREY,
  182.         (center[0] - belt_mount_w_2//2, center[1] - belt_mount_h_2//2 + belt_face,
  183.             belt_mount_w_2, belt_mount_h_2 - 2 * belt_face))    
  184.     pygame.draw.rect(screen, DARK_GREY,
  185.         (center[0] - belt_mount_w_2//2 + belt_face, center[1] - belt_mount_h_2//2,
  186.             belt_mount_w_2 - 2 * belt_face, belt_mount_h_2))    
  187.  
  188.     pygame.draw.circle(screen, WHITE,
  189.         (center[0] - belt_mount_w_3//2 + belt_face, center[1] - belt_mount_h_3//2 + belt_face),
  190.         belt_face)      
  191.     pygame.draw.circle(screen, WHITE,
  192.         (center[0] + belt_mount_w_3//2 - belt_face, center[1] - belt_mount_h_3//2 + belt_face),
  193.         belt_face)      
  194.     pygame.draw.circle(screen, WHITE,
  195.         (center[0] - belt_mount_w_3//2 + belt_face, center[1] + belt_mount_h_3//2 - belt_face),
  196.         belt_face)    
  197.     pygame.draw.circle(screen, WHITE,
  198.         (center[0] + belt_mount_w_3//2 - belt_face, center[1] + belt_mount_h_3//2 - belt_face),
  199.         belt_face)
  200.     pygame.draw.rect(screen, WHITE,
  201.         (center[0] - belt_mount_w_3//2, center[1] - belt_mount_h_3//2 + belt_face,
  202.             belt_mount_w_3, belt_mount_h_3 - 2 * belt_face))    
  203.     pygame.draw.rect(screen, WHITE,
  204.         (center[0] - belt_mount_w_3//2 + belt_face, center[1] - belt_mount_h_3//2,
  205.             belt_mount_w_3 - 2 * belt_face, belt_mount_h_3))
  206.  
  207.  
  208.     # Завод
  209.     pygame.draw.circle(screen, DARK_GREY,
  210.         (center[0] + charge_w - charge_face, center[1] - charge_h//2 + charge_face),
  211.         charge_face)    
  212.     pygame.draw.circle(screen, DARK_GREY,
  213.         (center[0] + charge_w - charge_face, center[1] + charge_h//2 - charge_face),
  214.         charge_face)  
  215.     pygame.draw.rect(screen, DARK_GREY,
  216.         (center[0], center[1] - charge_h//2, charge_w - charge_face, charge_h))  
  217.     pygame.draw.rect(screen, DARK_GREY,
  218.         (center[0], center[1] - charge_h//2 + charge_face,
  219.             charge_w, charge_h - charge_face*2))
  220.  
  221.     # Обод часов
  222.     pygame.draw.circle(screen, BLACK, center, radius)
  223.     pygame.draw.circle(screen, DARK_GREY, center, radius - 1)
  224.     pygame.draw.circle(screen, DARK_BLUE, center, radius - 16)
  225.     pygame.draw.circle(screen, BLACK, center, radius - 21)
  226.     pygame.draw.circle(screen, LIGHT_GREY, center, radius - 23)
  227.  
  228.     # Засечки
  229.     for i in range(60):
  230.         angle = i * math.pi / 30
  231.         x = int((radius - 24) * math.cos(angle)) + center[0]
  232.         y = int((radius - 24) * math.sin(angle)) + center[1]
  233.         pygame.draw.line(screen, DARK_BLUE, center, (x, y), 1)
  234.     pygame.draw.circle(screen, LIGHT_GREY, center, radius - 35)
  235.  
  236.     for i in range(12):
  237.         angle = i * math.pi / 6
  238.         x = int((radius - 40) * math.cos(angle)) + center[0]
  239.         y = int((radius - 40) * math.sin(angle)) + center[1]
  240.         pygame.draw.line(screen, WHITE_GREY, center, (x, y), 3)
  241.     pygame.draw.circle(screen, LIGHT_GREY, center, radius - 80)
  242.    
  243.     # Надпись
  244.     text = font.render("TISSOT", True, RED)
  245.     screen.blit(text, [center[0] - text.get_width()//2, center[1] + 100])
  246.    
  247.     # Стрелки
  248.     pygame.draw.circle(screen, WHITE_GREY, center, 5)
  249.     pygame.draw.line(screen, WHITE_GREY, center, (x1, y1), 2)
  250.     pygame.draw.line(screen, WHITE_GREY, center, (x2, y2), 4)
  251.  
  252.  
  253.     pygame.display.flip()   # Обновить экран, выведя то, что в буфере
  254.     # -----------------
  255.    
  256.     # Ограничение fps
  257.     clock.tick(FPS)
  258.  
  259. # "Правильное" завершение программы в pygame
  260. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement