i have this program this is basically the "concentration" game. i added a function that…
- User
i have this program this is basically the "concentration" game. i added a function that loops through, revealing each card in the game briefly, but i cannot play the game now. i'd like to keep this functionality where the program briefly revels each card but be able to click on any card to reveal it and make matches (while keeping any matches i make as permanently revealed)
import pygame import random import os
Initialize Pygame
pygame.init()
Constants
WIDTH, HEIGHT = 900, 750 GRID_SIZE = (8, 13) CARD_WIDTH, CARD_HEIGHT = WIDTH // GRID_SIZE[1], HEIGHT // GRID_SIZE[0] last_flip_time = 0 # To keep track of the time of the last flip
Load images and create a deck
card_images = {} for i in range(1, 53): image_path = os.path.join('/home/mike/Valut/Vault/STEM/Coding/Python/concentration/cards', f'card_{i}.png') img = pygame.image.load(image_path) img = pygame.transform.scale(img, (CARD_WIDTH, CARD_HEIGHT)) card_images[i] = img
Create the game board
def create_board(): board = [] card_values = list(range(1, 53)) random.shuffle(card_values) selected_cards = random.sample(card_values, 52) * 2 # Select 9 unique cards and duplicate to create pairs random.shuffle(selected_cards)
for row in range(GRID_SIZE[0]): board.append([]) for col in range(GRID_SIZE[1]): board[row].append(selected_cards.pop()) return boardDraw the cards on the screen
def draw_board(screen, board, revealed): for row in range(GRID_SIZE[0]): for col in range(GRID_SIZE[1]): x, y = col * CARD_WIDTH, row * CARD_HEIGHT card_value = board[row][col] if revealed[row][col]: screen.blit(card_images[card_value], (x, y)) else: # Add a border or background color to unrevealed cards pygame.draw.rect(screen, (100, 100, 100), (x, y, CARD_WIDTH, CARD_HEIGHT)) pygame.draw.rect(screen, (0, 0, 0), (x, y, CARD_WIDTH, CARD_HEIGHT), 2) # Border
def auto_play(screen, board, revealed): for row in range(GRID_SIZE[0]): for col in range(GRID_SIZE[1]): revealed[row][col] = True draw_board(screen, board, revealed) pygame.display.flip() pygame.time.wait(500) # Adjust this delay as needed revealed[row][col] = False draw_board(screen, board, revealed) pygame.display.flip() pygame.time.wait(500) # Adjust this delay as needed
Main game loop
def main(): screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption('Concentration Game')
clock = pygame.time.Clock() running = True board = create_board() revealed = [[False] * GRID_SIZE[1] for _ in range(GRID_SIZE[0])] selected_cards = [] selected_values = [] # To keep track of the selected card values reset_game = False last_flip_time = 0 while running: screen.fill((0, 0, 0)) auto_play(screen, board, revealed) # Call the auto_play function for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.MOUSEBUTTONDOWN: row = event.pos[1] // CARD_HEIGHT col = event.pos[0] // CARD_WIDTH if not revealed[row][col]: revealed[row][col] = True selected_cards.append((row, col)) selected_values.append(board[row][col]) if len(selected_cards) == 2: row1, col1 = selected_cards[0] row2, col2 = selected_cards[1] # Display the second card briefly before checking for the match draw_board(screen, board, revealed) pygame.display.flip() pygame.time.wait(500) # Adjust this delay as needed if selected_values[0] == selected_values[1]: # Matched, keep cards revealed selected_cards = [] selected_values = [] else: # Not matched, flip back unmatched cards after a short delay revealed[row1][col1] = False revealed[row2][col2] = False selected_cards = [] selected_values = [] # Update the last flip time to avoid immediate flipping last_flip_time = pygame.time.get_ticks() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_r: # Reset the game if "r" key is pressed board = create_board() revealed = [[False] * GRID_SIZE[1] for _ in range(GRID_SIZE[0])] selected_cards = [] selected_values = [] reset_game = True if not reset_game: # Only continue the game logic if the game is not being reset for row in range(GRID_SIZE[0]): for col in range(GRID_SIZE[1]): x, y = col * CARD_WIDTH, row * CARD_HEIGHT card_value = board[row][col] if revealed[row][col]: screen.blit(card_images[card_value], (x, y)) else: pygame.draw.rect(screen, (100, 100, 100), (x, y, CARD_WIDTH, CARD_HEIGHT)) pygame.draw.rect(screen, (0, 0, 0), (x, y, CARD_WIDTH, CARD_HEIGHT), 2) # Border auto_play(screen, board, revealed) # Call the auto_play function draw_board(screen, board, revealed) pygame.display.flip() clock.tick(60) else: # If the game is being reset, display a reset message or perform any other reset logic reset_game = False pygame.quit() - Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.