Speedmatch Code
Speedmatch Code
import sys
import random
pygame.init()
pygame.display.set_caption('speed match')
# Game settings
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
FPS = 60
BACKGROUND_COLOR = (0, 0, 0)
SHAPE_COLORS = [(255,255,255)]
SHAPES = ['circle', 'square', 'triangle', 'star']
SCORE_POS = (20, 20)
MESSAGE_POS = (SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2)
FONT_SIZE = 30
FONT_COLOR = (255, 255, 255)
wrong_choices=0
# Button settings
BUTTON_WIDTH = 100
BUTTON_HEIGHT = 50
BUTTON_PADDING = 20
BUTTON_COLOR = (255, 255, 255)
BUTTON_HOVER_COLOR = (0, 200, 0)
BUTTON_FONT_SIZE = 24
BUTTON_FONT_COLOR = (0, 0, 0)
CHANCE_POS=(600,20)
class Shape:
def __init__(self, shape_type, color, position):
self.shape_type = shape_type
self.color = color
self.position = position
def draw(self):
if self.shape_type == 'circle':
pygame.draw.circle(screen, 'white', self.position, 50)
pygame.draw.circle(screen,self.color,self.position,40)
elif self.shape_type == 'square':
pygame.draw.rect(screen, self.color,
pygame.Rect(self.position[0] - 10, self.position[1] - 10, 10, 10))
pygame.draw.rect(screen, 'white',
pygame.Rect(self.position[0] - 40, self.position[1] - 40, 80, 80))
class Button:
def __init__(self, text, position, action):
self.text = text
self.position = position
self.action = action
self.rect = pygame.Rect(position[0], position[1], BUTTON_WIDTH,
BUTTON_HEIGHT)
def draw(self):
pygame.draw.rect(screen, BUTTON_COLOR, self.rect)
font = pygame.font.Font(None, BUTTON_FONT_SIZE)
text = font.render(self.text, True, BUTTON_FONT_COLOR)
text_rect = text.get_rect(center=self.rect.center)
screen.blit(text, text_rect)
def display_score(score):
font = pygame.font.Font(None, FONT_SIZE)
text = font.render(f"Score: {score}", True, FONT_COLOR)
screen.blit(text, SCORE_POS)
def display_chances(wrong_choices):
font = pygame.font.Font(None,FONT_SIZE)
text=font.render(f"Chances Left: {(3-
wrong_choices)}",True,FONT_COLOR)
screen.blit(text,CHANCE_POS)
def display_message(message):
font = pygame.font.Font(None, FONT_SIZE)
text = font.render(message, True, FONT_COLOR)
text_rect = text.get_rect(center=MESSAGE_POS)
screen.blit(text, text_rect)
def StartScreen():
screen.fill(BACKGROUND_COLOR)
display_message('>Click To Start SpeedMatch')
pygame.display.flip()
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
main_game()
def main_game():
score = 0
wrong_choices = 0
current_shape = generate_shape()
previous_shape = current_shape
buttons = [
Button("Yes", (BUTTON_PADDING, SCREEN_HEIGHT - BUTTON_HEIGHT -
BUTTON_PADDING), "yes"),
Button("No", (BUTTON_PADDING * 2 + BUTTON_WIDTH, SCREEN_HEIGHT
- BUTTON_HEIGHT - BUTTON_PADDING), "no")
]
running = True
while running:
screen.fill(BACKGROUND_COLOR)
current_shape.draw()
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN and event.button
== 1:
mouse_pos = pygame.mouse.get_pos()
if button.check_collision(mouse_pos):
if button.action == "yes":
if current_shape.shape_type ==
previous_shape.shape_type:
score += 1
wrong_choices += 0
previous_shape=current_shape
current_shape=generate_shape()
else:
wrong_choices += 1
previous_shape=current_shape
current_shape=generate_shape()
elif button.action == "no":
if current_shape.shape_type !=
previous_shape.shape_type:
score += 1
wrong_choices += 0
previous_shape=current_shape
current_shape=generate_shape()
else:
wrong_choices += 1
previous_shape = current_shape
current_shape=generate_shape()
display_score(score)
display_chances(wrong_choices)
for button in buttons:
button.draw()
if wrong_choices >= 3:
running = False
pygame.display.flip()
clock.tick(FPS)
screen.fill(BACKGROUND_COLOR)
display_message("Game Over")
pygame.display.flip()
pygame.time.wait(2000)
while True:
StartScreen()