-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreaktionsklicker.py
71 lines (64 loc) · 2.56 KB
/
reaktionsklicker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import pygame
import random
import time
import os
pygame.init()
width = 800
height = 600
window = pygame.display.set_mode((width, height), pygame.RESIZABLE)
pygame.display.set_caption("Reaktionsklicker")
font = pygame.font.Font("D:/timot/Programmieren/Python/Jonas/reaktionsklicker/Mina-Regular.ttf",30)
text = font.render("", True, (255,255,255))
text_rect = text.get_rect(center=(width//2, height//0.2))
window.fill((0, 180, 255))
x = int(random.randint(1,350))
y = int(random.randint(1,350))
rect = pygame.Rect(y, x, 64, 64)
zeit = 0
highscore = 256
clock = pygame.time.Clock()
if not os.path.exists("highscore.txt"):
with open("highscore.txt", "w") as file:
file.write(str(highscore))
with open("highscore.txt", "r") as rfile:
highscore = int(rfile.read())
if not highscore == 256:
pygame.display.set_caption(f"Reaktionsklicker (Highscore: {highscore/100:.2f} Sekunden)")
running = True
while running:
text_rect = text.get_rect(center=(width//2, height//2))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
mouse_pos = event.pos
if rect.collidepoint(mouse_pos):
x = int(random.randint(1,width - 64))
y = int(random.randint(1,height - 64))
rect = pygame.Rect(x, y, 64, 64)
if zeit < highscore:
highscore = zeit
with open("highscore.txt", "w") as file:
file.write(str(highscore))
pygame.display.set_caption(f"Reaktionsklicker (Highscore: {highscore/100:.2f} Sekunden)")
textzeit = f"Deine Reaktionszeit: {zeit/100:.2f} Sekunden"
text = font.render(textzeit, True, (255,255,255))
zeit = 0
elif not rect.collidepoint(mouse_pos):
running = False
window.fill((255, 0, 0))
text = font.render("Verloren", True, (255,255,255))
text_rect = text.get_rect(center=(width//2, height//2))
window.blit(text, text_rect)
pygame.display.update()
time.sleep(2)
pygame.quit()
window.fill((0, 180, 255))
clock.tick(100)
zeit += 1
window.blit(text, text_rect)
pygame.draw.rect(window, "red" , rect)
pygame.display.update()
pygame.quit()