Let’s Talk 💬


    LinkedinDribbbleTwitter

    Nelly

    Download the Revolver image HERE>

    Download the bullet image HERE>

    Download Gunshot audio HERE>

    The code:

    import pygame
    import random
    import sys
    
    pygame.init()
    
    WIDTH = 800
    HEIGHT = 600
    window = pygame.display.set_mode((WIDTH, HEIGHT))
    pygame.display.set_caption("Russian Roulette")
    
    revolver_img = pygame.image.load("revolver.jpeg")
    bullet_img = pygame.image.load("bullet.png")
    
    revolver_img = pygame.transform.scale(revolver_img, (200, 200))
    bullet_img = pygame.transform.scale(bullet_img, (40, 40))
    
    gunshot_sound = pygame.mixer.Sound("gunshot.wav")
    
    font = pygame.font.SysFont(None, 30)
    game_over_font = pygame.font.SysFont(None, 30)
    
    current_player = 1
    spinning = False
    game_over = False
    start_screen = True
    
    def render_text(text, font, x, y, center=False):
        text_surface = font.render(text, True, (255, 0, 0))
        text_rect = text_surface.get_rect()
        if center:
            text_rect.center = (x, y)
        else:
            text_rect.topleft = (x, y)
        return text_surface, text_rect
    
    def create_button(x, y, width, height, text, font):
        pygame.draw.rect(window, (200, 200, 200), (x, y, width, height))
        text_surface, text_rect = render_text(text, font, x + width / 2, y + height / 2, center=True)
        window.blit(text_surface, text_rect)
        return pygame.Rect(x, y, width, height)
    
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
    
            if event.type == pygame.MOUSEBUTTONDOWN:
                if start_screen:
                    if start_button.collidepoint(event.pos):
                        start_screen = False
                else:
                    if exit_button.collidepoint(event.pos):
                        start_screen = True
                        current_player = 1
                        spinning = False
                        game_over = False
    
            if not start_screen:
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_SPACE and not spinning and not game_over:
                        spinning = True
    
                    if event.key == pygame.K_RETURN and spinning and not game_over:
                        spinning = False
                        bullet_position = random.randint(0, 5)
    
                        if bullet_position == 0:
                            game_over = True
                            gunshot_sound.play()
                        else:
                            if current_player == 1:
                                current_player = 2
                            else:
                                current_player = 1
    
                    if event.key == pygame.K_r and game_over:
                        game_over = False
                        current_player = 1
    
        window.fill((255, 255, 255))
    
        if start_screen:
            start_button = create_button(WIDTH // 2 - 100, HEIGHT // 2 - 50, 200, 100, "Start game", game_over_font)
        else:
            exit_button = create_button(WIDTH - 100, 0, 100, 50, "Exit", font)
    
            window.blit(revolver_img, (WIDTH // 2 - 100, HEIGHT // 2 - 100))
            if spinning:
                window.blit(bullet_img, (WIDTH // 2 - 20, HEIGHT // 2 - 20))
    
            player_text = font.render("Player {}: Press SPACE to spin, ENTER to shoot".format(current_player), True, (255, 0, 0))
            window.blit(player_text, (WIDTH // 2 - player_text.get_width() // 2, HEIGHT // 2 + 150))
    
            if game_over:
                game_over_text = game_over_font.render("Player {} loses! Press 'R' to restart.".format(current_player), True, (255, 0, 0))
                window.blit(game_over_text, (WIDTH // 2 - game_over_text.get_width() // 2, HEIGHT // 2 - 200))
    
        pygame.display.update()