|
| 1 | +import pygame |
| 2 | +import sys |
| 3 | +from math import * |
| 4 | + |
| 5 | +pygame.init() |
| 6 | + |
| 7 | +WIDTH = 500 |
| 8 | +HEIGHT = 500 |
| 9 | + |
| 10 | +screen = pygame.display.set_mode((WIDTH, HEIGHT)) |
| 11 | +clock = pygame.time.Clock() |
| 12 | +pygame.display.set_caption("Fidget Spinner") |
| 13 | + |
| 14 | +# Colors |
| 15 | +background = (0,191,173) |
| 16 | +body_color = (0,96,101) |
| 17 | +white = (240, 240, 240) |
| 18 | +black = (0 , 0 , 0) |
| 19 | +red = (176, 58, 46) |
| 20 | +yellow = (250,212,2) |
| 21 | +dark_yellow = (190, 152, 8) |
| 22 | + |
| 23 | +LINE_WIDTH = 60 |
| 24 | + |
| 25 | +# Drawing of Fidget Spinner on Pygame Window |
| 26 | +def show_spinner(angle): |
| 27 | + d = 80 |
| 28 | + innerd = 50 |
| 29 | + x = WIDTH/2 - d/2 |
| 30 | + y = HEIGHT/2 |
| 31 | + l = 200 |
| 32 | + r = l/(3**0.5) |
| 33 | + w = 10 |
| 34 | + |
| 35 | + # A little math for calculation the coordinates after rotation by some 'angle' |
| 36 | + # x = originx + r*cos(angle) |
| 37 | + # y = originy + r*sin(angle) |
| 38 | + |
| 39 | + centre = [x, y, d, d] |
| 40 | + centre_inner = [x + d/2 - innerd/2, y + d/2 - innerd/2, innerd, innerd] |
| 41 | + |
| 42 | + top = [x, y - l/(3)**0.5, d, d] |
| 43 | + top_inner = [x, y - l/(3)**0.5, innerd, innerd] |
| 44 | + |
| 45 | + top[0] = x + r*cos(radians(angle)) |
| 46 | + top[1] = y + r*sin(radians(angle)) |
| 47 | + top_inner[0] = x + d/2 - innerd/2 + r*cos(radians(angle)) |
| 48 | + top_inner[1] = y + d/2 - innerd/2 + r*sin(radians(angle)) |
| 49 | + |
| 50 | + left = [x - l/2, y + l/(2*(3)**0.5), d, d] |
| 51 | + left_inner = [x, y - l/(3)**0.5, innerd, innerd] |
| 52 | + |
| 53 | + left[0] = x + r*cos(radians(angle - 120)) |
| 54 | + left[1] = y + r*sin(radians(angle - 120)) |
| 55 | + left_inner[0] = x + d/2 - innerd/2 + r*cos(radians(angle - 120)) |
| 56 | + left_inner[1] = y + d/2 - innerd/2 + r*sin(radians(angle - 120)) |
| 57 | + |
| 58 | + |
| 59 | + right = [x + l/2, y + l/(2*(3)**0.5), d, d] |
| 60 | + right_inner = [x, y - l/(3)**0.5, innerd, innerd] |
| 61 | + |
| 62 | + right[0] = x + r*cos(radians(angle + 120)) |
| 63 | + right[1] = y + r*sin(radians(angle + 120)) |
| 64 | + right_inner[0] = x + d/2 - innerd/2 + r*cos(radians(angle + 120)) |
| 65 | + right_inner[1] = y + d/2 - innerd/2 + r*sin(radians(angle + 120)) |
| 66 | + |
| 67 | + # Drawing shapes on Pygame Window |
| 68 | + pygame.draw.line(screen, body_color, (top[0] + d/2, top[1] + d/2), (centre[0] + d/2, centre[1] + d/2), LINE_WIDTH) |
| 69 | + pygame.draw.line(screen, body_color, (left[0] + d/2, left[1] + d/2), (centre[0] + d/2, centre[1] + d/2), LINE_WIDTH) |
| 70 | + pygame.draw.line(screen, body_color, (right[0] + d/2, right[1] + d/2), (centre[0] + d/2, centre[1] + d/2), LINE_WIDTH) |
| 71 | + pygame.draw.ellipse(screen, yellow, tuple(centre)) |
| 72 | + pygame.draw.ellipse(screen, dark_yellow, tuple(centre_inner)) |
| 73 | + pygame.draw.ellipse(screen, body_color, tuple(top)) |
| 74 | + pygame.draw.ellipse(screen, background, tuple(top_inner), 10) |
| 75 | + pygame.draw.ellipse(screen, body_color, tuple(left)) |
| 76 | + pygame.draw.ellipse(screen, background, tuple(left_inner), 10) |
| 77 | + pygame.draw.ellipse(screen, body_color, tuple(right)) |
| 78 | + pygame.draw.ellipse(screen, background, tuple(right_inner), 10) |
| 79 | + |
| 80 | + |
| 81 | +# Displaying Information on Pygame Window |
| 82 | +def show_info(friction, speed): |
| 83 | + font = pygame.font.SysFont("Times New Roman", 22) |
| 84 | + speedText = font.render("Click ←Left or Right→ to Spin ", True, black) |
| 85 | + screen.blit(speedText, (15, 15)) |
| 86 | + tempText = font.render("Press 'Q' to exit", True, black) |
| 87 | + screen.blit(tempText, (15, 45)) |
| 88 | + |
| 89 | +# The Main Function |
| 90 | +def main(): |
| 91 | + spin = True |
| 92 | + angle = 0 |
| 93 | + speed = 0.0 |
| 94 | + friction = 0.03 |
| 95 | + rightPressed = False |
| 96 | + leftPressed = False |
| 97 | + direction = 1 |
| 98 | + |
| 99 | + while spin: |
| 100 | + for event in pygame.event.get(): |
| 101 | + if event.type == pygame.QUIT: |
| 102 | + sys.exit() |
| 103 | + if event.type == pygame.KEYDOWN: |
| 104 | + if event.key == pygame.K_q: |
| 105 | + sys.exit() |
| 106 | + if event.key == pygame.K_RIGHT: |
| 107 | + rightPressed = True |
| 108 | + direction = 1 |
| 109 | + if event.key == pygame.K_LEFT: |
| 110 | + leftPressed = True |
| 111 | + direction = -1 |
| 112 | + if event.type == pygame.KEYUP: |
| 113 | + leftPressed = False |
| 114 | + rightPressed = False |
| 115 | + |
| 116 | + # Changing the Angle of rotation |
| 117 | + if direction == 1: |
| 118 | + if rightPressed: |
| 119 | + speed += 0.3 |
| 120 | + else: |
| 121 | + speed -= friction |
| 122 | + if speed < 0: |
| 123 | + speed = 0.0 |
| 124 | + else: |
| 125 | + if leftPressed: |
| 126 | + speed -= 0.3 |
| 127 | + else: |
| 128 | + speed += friction |
| 129 | + if speed > 0: |
| 130 | + speed = 0.0 |
| 131 | + |
| 132 | + screen.fill(background) |
| 133 | + |
| 134 | + angle += speed |
| 135 | + |
| 136 | + # Displaying Information and the Fidget Spinner |
| 137 | + show_spinner(angle) |
| 138 | + show_info(friction, speed) |
| 139 | + |
| 140 | + pygame.display.update() |
| 141 | + clock.tick(90) |
| 142 | + |
| 143 | +main() |
0 commit comments