-
Notifications
You must be signed in to change notification settings - Fork 0
/
hand_cricket.py
137 lines (129 loc) · 4.32 KB
/
hand_cricket.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
import pygame, sys, time, random
checkErrors = pygame.init()
if checkErrors[1] > 0:
print("(!) Had {0} initializing errors,exiting.....".format(checkErrors[1]))
sys.exit(-1)
else:
print("(+) pygame successfully initialized")
# game logic
def game():
"""
Hand cricket...press keys from 1 to 6 and if the bowler(computer) chooses the same
number...You will be declared OUT!
"""
playsurface = pygame.display.set_mode((700,390))
pygame.display.set_caption('WELCOME TO HAND CRICKET GAME!!!')
# colors
red = pygame.Color(255, 0, 0) # if batsman out
black = pygame.Color(0, 0, 0) # score
white = pygame.Color(255, 255, 255)
# fps controller
fpsController = pygame.time.Clock()
# background image
bg = pygame.image.load("cp.png")
bgRect = bg.get_rect()
runs = 0
def out(r):
myFont = pygame.font.SysFont('monaco', 72)
Osurf = myFont.render('OUT', True, red)
Orect = Osurf.get_rect()
Orect.midtop = (350, 15)
playsurface.blit(Osurf, Orect)
showscore(runs)
pygame.display.flip()
time.sleep(5)
game()
def showscore(r):
sFont = pygame.font.SysFont('monaco', 40)
ssurf = sFont.render('Runs: {0}'.format(r), True, black)
srect = ssurf.get_rect()
srect.midtop = (350,250)
playsurface.blit(ssurf, srect)
def batsmen(r):
sFont = pygame.font.SysFont('monaco', 70)
ssurf = sFont.render('Batsman: {0}'.format(r), True, black)
srect = ssurf.get_rect()
srect.midtop = (150,50)
playsurface.blit(ssurf, srect)
def bowler(r):
sFont = pygame.font.SysFont('monaco', 70)
ssurf = sFont.render('Bowler: {0}'.format(r), True, black)
srect = ssurf.get_rect()
srect.midtop = (550,50)
playsurface.blit(ssurf, srect)
bowl = 0
k = 0
s = 0
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
slist=[1,2,3,4,5,6]
bowl = random.choice(slist)
if event.key == pygame.K_1:
if bowl == 1:
out(runs)
else:
runs = runs + 1
k = 1
batsmen(1)
bowler(bowl)
showscore(runs)
if event.key == pygame.K_2:
if bowl == 2:
out(runs)
else:
runs += 2
k = 2
batsmen(2)
bowler(bowl)
showscore(runs)
if event.key == pygame.K_3:
if bowl == 3:
out(runs)
else:
runs += 3
k = 3
batsmen(3)
bowler(bowl)
showscore(runs)
if event.key == pygame.K_4:
if bowl == 4:
out(runs)
else:
runs += 4
k = 4
batsmen(4)
bowler(bowl)
showscore(runs)
if event.key == pygame.K_5:
if bowl == 5:
out(runs)
else:
runs += 5
k = 5
batsmen(5)
bowler(bowl)
showscore(runs)
if event.key == pygame.K_6:
if bowl == 6:
out(runs)
else:
runs += 6
k = 6
batsmen(6)
bowler(bowl)
showscore(runs)
if event.key == pygame.K_ESCAPE:
pygame.event.post(pygame.event.Event(pygame.QUIT))
# drawing
playsurface.fill(white)
playsurface.blit(bg,bgRect)
batsmen(k)
bowler(bowl)
showscore(runs)
pygame.display.flip()
fpsController.tick(23)
game()