-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoinflip.py
145 lines (107 loc) · 4.42 KB
/
coinflip.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import sys, pygame, random
heads = 0
total = 0
def game_coin_flip():
pygame.init()
size = width, height = 900, 700
speed = [0, 2]
screen = pygame.display.set_mode(size)
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((177,149,160))
font = pygame.font.Font(None, 36)
text = font.render("How many flips will you need to get three heads?", 1, (10, 10, 10))
textpos = text.get_rect()
textpos = (120, 20)
background.blit(text, textpos)
font = pygame.font.Font(None, 36)
text1 = font.render("No of heads:"+str(heads), 1, (10, 10, 10))
textpos1 = text.get_rect()
textpos1 = (10, 180)
background.blit(text1, textpos1)
font = pygame.font.Font(None, 36)
text2 = font.render("Total no of throws:"+ str(total), 1, (10, 10, 10))
textpos2 = text.get_rect()
textpos2 = (10, 210)
background.blit(text2, textpos2)
quit_button = pygame.draw.rect(background, (200,2,2), (5, height-50, 80, 40))
quit_buttonrect = quit_button.move(background.get_rect().centerx, 60)
pygame.display.update()
font = pygame.font.Font(None, 36)
quit = font.render("Quit?", 1, (10, 10, 10))
quitpos = quit.get_rect(center =(45, height-30))
background.blit(quit, quitpos)
flip_button = pygame.draw.rect(background, (200,2,2), (435, 50, 70, 45))
flip_buttonrect = flip_button.move(background.get_rect().centerx, 60)
pygame.display.update()
font = pygame.font.Font(None, 36)
flip = font.render("Flip", 1, (10, 10, 10))
flippos = (background.get_rect().centerx, 60)
background.blit(flip, flippos)
image = pygame.image.load("pictures/coin.png")
coin = pygame.transform.scale(image, (300, 200))
coin.set_colorkey((255,255,255))
coinrect = coin.get_rect(center =(width/2, height*3/4))
background.blit(coin, coinrect)
screen.blit(background,(0,0))
pygame.display.update()
def flip_coin(screen, coinrect, coin, image):
pygame.draw.rect(background, (177,149,160), (330, 400, 250, 250))
r = random.randint(0,1)
for x in range (200):
coinrect = coinrect.move(0,-2)
coin = pygame.transform.scale(image, (300, 200))
screen.blit(pygame.transform.rotate(coin,x), coinrect)
pygame.display.update()
pygame.time.delay(8)
screen.blit(background,(0,0))
pygame.display.flip()
for x in range (200):
coinrect = coinrect.move(0,2)
coin = pygame.transform.scale(image, (300, 200))
screen.blit(pygame.transform.rotate(coin,200+x), coinrect)
pygame.display.update()
pygame.time.delay(8)
screen.blit(background,(0,0))
pygame.display.flip()
if r==1:
result = "head"
global heads
heads += 1
else:
result = "tail"
global total
total += 1
pygame.draw.rect(background, (177,149,160), (157, 150, 50, 50))
pygame.draw.rect(background, (177,149,160), (230, 210, 50, 50))
font = pygame.font.Font(None, 36)
text1 = font.render("No of heads: "+str(heads), 1, (10, 10, 10))
textpos1 = text.get_rect()
textpos1 = (10, 180)
background.blit(text1, textpos1)
font = pygame.font.Font(None, 36)
text2 = font.render("Total no of throws: "+ str(total), 1, (10, 10, 10))
textpos2 = text.get_rect()
textpos2 = (10, 210)
background.blit(text2, textpos2)
font = pygame.font.Font(None, 36)
result = font.render(result, 1, (10, 10, 10))
resultpos = result.get_rect(center =(width/2, height*7/8))
background.blit(result, resultpos)
screen.blit(background,(0,0))
coinrect = coin.get_rect(center =(width/2, height*3/4))
background.blit(coin, coinrect)
screen.blit(background,(0,0))
pygame.display.update()
pygame.time.delay(1200)
while(1):
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
pygame.time.delay(10)
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if 435+70 > mouse[0] > 435 and 50+45 > mouse[1] > 50 and click[0] == 1:
flip_coin(screen, coinrect, coin, image)
if 5+80 > mouse[0] > 5 and height-50+40 > mouse[1] > height-50 and click[0] == 1:
return
game_coin_flip()