Skip to content

Commit 25d9ba5

Browse files
authored
Create Stopwatch.py
1 parent 5fbcdd9 commit 25d9ba5

File tree

1 file changed

+149
-0
lines changed

1 file changed

+149
-0
lines changed

Diff for: python/Stopwatch.py

+149
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
import pygame
2+
3+
import sys
4+
5+
import time
6+
7+
# Initializing of Pygame
8+
9+
pygame.init()
10+
11+
width = 200
12+
13+
height = 100
14+
15+
display = pygame.display.set_mode((width, height))
16+
17+
pygame.display.set_caption(" ")
18+
19+
clock = pygame.time.Clock()
20+
21+
dark_gray = (23, 32, 42)
22+
23+
white = (230, 230, 230)
24+
25+
seconds = 0
26+
27+
pause = False
28+
29+
# Font and Size
30+
31+
font = pygame.font.SysFont("Times New Roman", 24)
32+
33+
# Close the Window
34+
35+
def close():
36+
37+
pygame.quit()
38+
39+
sys.exit()
40+
41+
# Blit time and text to Pygame Window
42+
43+
def showTime():
44+
45+
hours = seconds/3600
46+
47+
minutes = (seconds/60)%60
48+
49+
sec = seconds%60
50+
51+
52+
53+
text = font.render("HH MM SS", True, white)
54+
55+
time = font.render(str(hours).zfill(2) + " " + str(minutes).zfill(2) + " " + str(sec).zfill(2), True, white)
56+
57+
display.blit(text, (10, 10))
58+
59+
display.blit(time, (13, 40))
60+
61+
# Pause the Stopwatch
62+
63+
def Pause():
64+
65+
while pause:
66+
67+
for event in pygame.event.get():
68+
69+
if event.type == pygame.QUIT:
70+
71+
close()
72+
73+
if event.type == pygame.KEYDOWN:
74+
75+
if event.key == pygame.K_SPACE or pygame.key == pygame.K_p:
76+
77+
stopWatch()
78+
79+
if event.key == pygame.K_r:
80+
81+
reset()
82+
83+
if event.key == pygame.K_q:
84+
85+
close()
86+
87+
pauseText = font.render("Paused!", True, white)
88+
89+
display.blit(pauseText, (10, height - 35))
90+
91+
92+
93+
pygame.display.update()
94+
95+
clock.tick(60)
96+
97+
# Reset StopWatch
98+
99+
def reset():
100+
101+
global seconds
102+
103+
seconds = 0
104+
105+
# StopWatch
106+
107+
def stopWatch():
108+
109+
tick = True
110+
111+
global seconds, pause
112+
113+
pause = False
114+
115+
while tick:
116+
117+
for event in pygame.event.get():
118+
119+
if event.type == pygame.QUIT:
120+
121+
close()
122+
123+
if event.type == pygame.KEYDOWN:
124+
125+
if event.key == pygame.K_SPACE or event.key == pygame.K_p:
126+
127+
pause = True
128+
129+
Pause()
130+
131+
if event.key == pygame.K_r:
132+
133+
reset()
134+
135+
if event.key == pygame.K_q:
136+
137+
close()
138+
139+
display.fill(dark_gray)
140+
141+
showTime()
142+
143+
seconds += 1
144+
145+
pygame.display.update()
146+
147+
clock.tick(1)
148+
149+
stopWatch()

0 commit comments

Comments
 (0)