Skip to content

Commit 9cdb4c0

Browse files
authored
Update main.py (#30)
Significantly reduced the code by consolidating all of the various run functions into one function with a variable passed through from the buttons this allows the project to dramatically shrink in code and therefore increase the speed at which it will be run.
2 parents 7b49fd0 + 0a1c4da commit 9cdb4c0

File tree

1 file changed

+21
-80
lines changed

1 file changed

+21
-80
lines changed

main.py

Lines changed: 21 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,14 @@ def draw(self, screen):
8282
pygame.draw.rect(screen, self.colour, self.rect)
8383
draw_text(self.text, font, white, screen, self.rect.x + 10, self.rect.y + 10)
8484

85-
def click(self):
86-
if self.action:
87-
self.action()
85+
def is_clicked(self, pos):
86+
return self.rect.collidepoint(pos)
87+
88+
def handle_event(self, event):
89+
if event.type == pygame.MOUSEBUTTONDOWN:
90+
if self.is_clicked(event.pos):
91+
if self.action:
92+
self.action()
8893

8994
# Actions for the various buttons, to be implemented as part of the class later on
9095

@@ -128,82 +133,19 @@ def start_innings():
128133
def end_game():
129134
pygame.quit()
130135

131-
def run_1():
132-
global runs
133-
global bye_status
134-
global extras
135-
history.append((overs, runs, wickets, extras, noball_status, bye_status)) # Saves the current state of play before changing
136-
if bye_status == True:
137-
extras += 1
138-
runs += 1
139-
bye_status = False
140-
else:
141-
runs += 1
142-
add_ball()
143-
144-
def run_2():
145-
global runs
146-
global bye_status
147-
global extras
148-
history.append((overs, runs, wickets, extras, noball_status, bye_status)) # Saves the current state of play before changing
149-
if bye_status == True:
150-
extras += 2
151-
runs += 2
152-
bye_status = False
153-
else:
154-
runs += 2
155-
add_ball()
156-
157-
def run_3():
158-
global runs
159-
global bye_status
160-
global extras
161-
history.append((overs, runs, wickets, extras, noball_status, bye_status)) # Saves the current state of play before changing
162-
if bye_status == True:
163-
extras += 3
164-
runs += 3
165-
bye_status = False
166-
else:
167-
runs += 3
168-
add_ball()
169-
170-
def run_4():
136+
def scoring(ballscore):
137+
print("Button clicked")
171138
global runs
172139
global bye_status
173140
global extras
141+
int(ballscore)
174142
history.append((overs, runs, wickets, extras, noball_status, bye_status)) # Saves the current state of play before changing
175143
if bye_status == True:
176-
extras += 4
177-
runs +=4
178-
bye_status = False
179-
else:
180-
runs += 4
181-
add_ball()
182-
183-
def run_5():
184-
global runs
185-
global bye_status
186-
global extras
187-
history.append((overs, runs, wickets, extras, noball_status, bye_status)) # Saves the current state of play before changing
188-
if bye_status == True:
189-
extras += 5
190-
runs +=5
191-
bye_status = False
192-
else:
193-
runs += 5
194-
add_ball()
195-
196-
def run_6():
197-
global runs
198-
global extras
199-
global bye_status
200-
history.append((overs, runs, wickets, extras, noball_status, bye_status)) # Saves the current state of play before changing
201-
if bye_status == True:
202-
extras += 6
203-
runs += 6
144+
extras += ballscore
145+
runs += ballscore
204146
bye_status = False
205147
else:
206-
runs += 6
148+
runs += ballscore
207149
add_ball()
208150

209151
def add_wicket():
@@ -313,12 +255,12 @@ def toggle_bold(): # Function to toggle between regular and bold font
313255
# Create buttons for the various actions using the Button class created earlier
314256

315257
buttons = [ # (self, text, x, y, w, h, colour, action=None)
316-
Button("1", 50, 200, 40, 50, homecolour, run_1),
317-
Button("2", 100, 200, 40, 50, homecolour, run_2),
318-
Button("3", 150, 200, 40, 50, homecolour, run_3),
319-
Button("4", 200, 200, 40, 50, homecolour, run_4),
320-
Button("5", 250, 200, 40, 50, homecolour, run_5),
321-
Button("6", 300, 200, 40, 50, homecolour, run_6),
258+
Button("1", 50, 200, 40, 50, homecolour, lambda: scoring(1)),
259+
Button("2", 100, 200, 40, 50, homecolour, lambda: scoring(2)),
260+
Button("3", 150, 200, 40, 50, homecolour, lambda: scoring(3)),
261+
Button("4", 200, 200, 40, 50, homecolour, lambda: scoring(4)),
262+
Button("5", 250, 200, 40, 50, homecolour, lambda: scoring(5)),
263+
Button("6", 300, 200, 40, 50, homecolour, lambda: scoring(6)),
322264
Button("0", 350, 200, 40, 50, homecolour, add_ball),
323265
Button("Wicket", 200, 300, 100, 50, awaycolour, add_wicket),
324266
Button("N.B.",310, 300, 75, 50, awaycolour, noball),
@@ -397,8 +339,7 @@ def toggle_bold(): # Function to toggle between regular and bold font
397339
noball_status = False
398340
# Check if buttons are clicked
399341
for button in buttons:
400-
if button.rect.collidepoint(event.pos):
401-
button.click()
342+
button.handle_event(event)
402343

403344
# Draw the score display
404345
draw_text(f"Runs: {runs}", font, black, screen, 50, 0)

0 commit comments

Comments
 (0)