Skip to content

Commit 972c9fa

Browse files
committed
fidget game
1 parent bc3c4c5 commit 972c9fa

6 files changed

+176
-0
lines changed
Loading
Loading

Fidget Game/README.md

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
**FIDGET GAME**
2+
3+
**GOAL**
4+
The Goal of this game is to simulate a Fidget Spinner, based on Google Theme.
5+
6+
**DESCRIPTION**
7+
A fidget spinner is a toy that consists of a ball bearing in the center of a multi-lobed (typically two or three) flat structure made from metal or plastic designed to spin along its axis with pressure. Fidget spinner become popular among kids and adults, as a satisfying toy.
8+
Controls : It has the following controls:
9+
1) Left and Right arrows to rotate the Spinner in direction.
10+
2) The longer you hold down the arrow key, the higher its speed.
11+
12+
### LIBRARIES NEEDED
13+
14+
1. PyGame: Pygame is a cross-platform set of Python modules which is used to create video games. Make sure to install PyGame library before playing the game.
15+
2. Sys: The sys module in Python provides various functions and variables that are used to manipulate different parts of the Python runtime environment.
16+
3. Math: Provides useful math function for simplifying equations and calculations.
17+
18+
**WHAT I HAD DONE**
19+
1) Import Libraries - pygame and initialize it.
20+
2) Created Necessary Variables like background color, line width, etc. Also generated screen.
21+
3) Created show_spinner, which contains rotation logic, using trig methods from math package to determine positions of arms based on angle provided.
22+
4) Drawn various Shapes for printing Fidget Spinner on screen, using pygame.draw
23+
5) Created a main loop to integrate all functions together, declared important parameters such as speed, friction. Defined controls to change direction of spinner and regulated respective parameters to make sure spinner work as prescibed.
24+
25+
### DEMONSTRATION
26+
https://drive.google.com/drive/folders/1qiM64AFmTvwf7YsCwb1x6F6UallacPvw?usp=share_link
27+
28+
### Sarthak Joleya ###

Fidget Game/fidget.py

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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()

Fidget Game/requirements.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
The required library is:
2+
3+
pygame - 2.1.2

Fidget Game/tempCodeRunnerFile.py

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
orange = (230, 126, 34)
2+
dark_orange = (126, 81, 9)

0 commit comments

Comments
 (0)