-
Notifications
You must be signed in to change notification settings - Fork 0
/
flip-coin.py
73 lines (58 loc) · 2.4 KB
/
flip-coin.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
72
73
import pygame
import random
pygame.init()
# Set up the screen
screen_width, screen_height = 400, 400
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Flip a Coin')
# Load the coin image
coin_img = pygame.image.load('coin.png')
coin_img = pygame.transform.scale(coin_img, (100, 100))
# Coin properties
coin_center = (screen_width // 2, screen_height // 2)
# Function to draw the coin on the screen
def draw_coin():
screen.blit(coin_img, (coin_center[0] - coin_img.get_width() // 2, coin_center[1] - coin_img.get_height() // 2))
font = pygame.font.SysFont(None, 30)
text = font.render("Press 'F' to flip the coin", True, (0, 0, 0))
screen.blit(text, (screen_width // 2 - 130, screen_height - 50))
# Function to flip the coin
def flip_coin():
return random.choice(['Heads', 'Tails'])
def main():
clock = pygame.time.Clock()
coin_result = None
flip_animation = False
flip_frame = 0
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_f and not flip_animation:
coin_result = flip_coin()
flip_animation = True
flip_frame = 0
# Clear the screen
screen.fill((255, 255, 255))
if flip_animation:
flip_frame += 1
if flip_frame >= 20: # Change animation speed by adjusting the value here
flip_animation = False
# Draw the coin or the flipped coin
if flip_animation:
rotated_coin = pygame.transform.rotate(coin_img, flip_frame * 18) # Adjust the rotation speed here
screen.blit(rotated_coin, (coin_center[0] - rotated_coin.get_width() // 2, coin_center[1] - rotated_coin.get_height() // 2))
else:
draw_coin()
# Display the coin result
if coin_result is not None and not flip_animation:
font = pygame.font.SysFont(None, 50)
text = font.render(coin_result, True, (0, 0, 0))
screen.blit(text, (screen_width // 2 - 50, screen_height // 2 + coin_img.get_height() // 2 + 20))
pygame.display.update()
clock.tick(30)
pygame.quit()
if __name__ == "__main__":
main()