|
| 1 | +# ------------------------------------------------------------------------------------------- |
| 2 | +# |
| 3 | +# Jatin Kumar Mandav |
| 4 | +# |
| 5 | +# Stopwatch Using Pygame |
| 6 | +# Pause or Unpause : Space Bar or 'p' |
| 7 | +# Reset : 'r' |
| 8 | +# Quit : 'q' |
| 9 | +# |
| 10 | +# Website : https://jatinmandav.wordpress.com |
| 11 | +# YouTube : https://www.youtube.com/channel/UCdpf6Lz3V357cIZomPwjuFQ?view_as=subscriber |
| 12 | +# |
| 13 | +# Facebook : facebook.com/jatinmandav |
| 14 | +# Twitter : @jatinmandav |
| 15 | + |
| 16 | +# |
| 17 | +# ------------------------------------------------------------------------------------------- |
| 18 | + |
| 19 | +import pygame |
| 20 | +import sys |
| 21 | +import time |
| 22 | + |
| 23 | +# Initializing of Pygame |
| 24 | +pygame.init() |
| 25 | + |
| 26 | +width = 200 |
| 27 | +height = 100 |
| 28 | +display = pygame.display.set_mode((width, height)) |
| 29 | +pygame.display.set_caption(" ") |
| 30 | +clock = pygame.time.Clock() |
| 31 | + |
| 32 | +dark_gray = (23, 32, 42) |
| 33 | +white = (230, 230, 230) |
| 34 | + |
| 35 | +seconds = 0 |
| 36 | +pause = False |
| 37 | + |
| 38 | +# Font and Size |
| 39 | +font = pygame.font.SysFont("Times New Roman", 24) |
| 40 | + |
| 41 | +# Close the Window |
| 42 | +def close(): |
| 43 | + pygame.quit() |
| 44 | + sys.exit() |
| 45 | + |
| 46 | +# Blit time and text to Pygame Window |
| 47 | +def showTime(): |
| 48 | + hours = seconds/3600 |
| 49 | + minutes = (seconds/60)%60 |
| 50 | + sec = seconds%60 |
| 51 | + |
| 52 | + text = font.render("HH MM SS", True, white) |
| 53 | + time = font.render(str(hours).zfill(2) + " " + str(minutes).zfill(2) + " " + str(sec).zfill(2), True, white) |
| 54 | + display.blit(text, (10, 10)) |
| 55 | + display.blit(time, (13, 40)) |
| 56 | + |
| 57 | +# Pause the Stopwatch |
| 58 | +def Pause(): |
| 59 | + while pause: |
| 60 | + for event in pygame.event.get(): |
| 61 | + if event.type == pygame.QUIT: |
| 62 | + close() |
| 63 | + if event.type == pygame.KEYDOWN: |
| 64 | + if event.key == pygame.K_SPACE or pygame.key == pygame.K_p: |
| 65 | + stopWatch() |
| 66 | + if event.key == pygame.K_r: |
| 67 | + reset() |
| 68 | + if event.key == pygame.K_q: |
| 69 | + close() |
| 70 | + |
| 71 | + pauseText = font.render("Paused!", True, white) |
| 72 | + display.blit(pauseText, (10, height - 35)) |
| 73 | + |
| 74 | + pygame.display.update() |
| 75 | + clock.tick(60) |
| 76 | + |
| 77 | +# Reset StopWatch |
| 78 | +def reset(): |
| 79 | + global seconds |
| 80 | + seconds = 0 |
| 81 | + |
| 82 | +# StopWatch |
| 83 | +def stopWatch(): |
| 84 | + tick = True |
| 85 | + global seconds, pause |
| 86 | + pause = False |
| 87 | + while tick: |
| 88 | + for event in pygame.event.get(): |
| 89 | + if event.type == pygame.QUIT: |
| 90 | + close() |
| 91 | + if event.type == pygame.KEYDOWN: |
| 92 | + if event.key == pygame.K_SPACE or event.key == pygame.K_p: |
| 93 | + pause = True |
| 94 | + Pause() |
| 95 | + if event.key == pygame.K_r: |
| 96 | + reset() |
| 97 | + if event.key == pygame.K_q: |
| 98 | + close() |
| 99 | + |
| 100 | + display.fill(dark_gray) |
| 101 | + showTime() |
| 102 | + seconds += 1 |
| 103 | + |
| 104 | + pygame.display.update() |
| 105 | + clock.tick(1) |
| 106 | + |
| 107 | +stopWatch() |
| 108 | + |
0 commit comments