Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have the following code for a set card game, it works but whenever I make a change the pygame screen goes black, I think

I have the following code for a set card game, it works but whenever I make a change the pygame screen goes black, I think it could be because of the image paths and the way they're hard coded, I want the code to be accessible to other devices as well, how do i make the image paths in a way that they only go to the map "cards" instead of going so extremely specific of the location, I am trying to make the game look more visually nice but this prevents me from making any changes, please if able rewrite it, I have seen that the image paths need to be in the same directory as the code, but I cant figure it out for the life of me
?:
import sys
import random
from enum import Enum
import time
import os
import pygame
# Enums for card properties
class Number(Enum):
ONE =1
TWO =2
THREE =3
class Symbol(Enum):
DIAMOND = 'diamond'
SQUIGGLE = 'squiggle'
OVAL = 'oval'
class Color(Enum):
RED = 'red'
GREEN = 'green'
PURPLE = 'purple'
class Shading(Enum):
FILLED = 'filled'
SHADED = 'shaded'
EMPTY = 'empty'
# Class representing a SET card
class SetCard:
def __init__(self, number, symbol, color, shading):
self.number = number
self.symbol = symbol
self.color = color
self.shading = shading
self.highlighted = False
def __repr__(self):
return f'{self.number.value}{self.symbol.value}{self.color.value}{self.shading.value}'
@staticmethod
def is_set(card1, card2, card3):
properties =['symbol', 'color', 'shading', 'number']
for prop in properties:
if len(set([getattr(card1, prop), getattr(card2, prop), getattr(card3, prop)]))!=2:
return False
return True
@staticmethod
def find_all_sets(cards):
n = len(cards)
sets =[]
for i in range(n -2):
for j in range(i +1, n -1):
for k in range(j +1, n):
if SetCard.is_set(cards[i], cards[j], cards[k]):
sets.append((cards[i], cards[j], cards[k]))
return sets
# Pygame initialization function
def init_pygame():
pygame.init()
screen = pygame.display.set_mode((800,600))
pygame.display.set_caption("SET Game")
return screen
# Function to draw text on the screen
def draw_text(screen, text, position, font_size=36, color=(255,255,255)):
font = pygame.font.Font(None, font_size)
text_surface = font.render(text, True, color)
screen.blit(text_surface, position)
# Function to draw confetti on the screen
def draw_confetti(screen):
for _ in range(100):
x = random.randint(0,800)
y = random.randint(0,600)
pygame.draw.circle(screen,(255,255,0),(x, y),5)
# Function to draw cards on the screen
def draw_cards(screen, table_cards, selected_cards):
screen.fill((50,50,50)) # Set background color
for i, card in enumerate(table_cards):
x =50+(i %3)*150
y =50+(i //3)*200
if card in selected_cards:
pygame.draw.rect(screen,(0,0,255),(x -5, y -5,110,160),0) # Selected card border
if card.highlighted:
y -=20 # Adjust the y-coordinate to bring the card forward
card_color =(255,255,255) if card.highlighted else (200,200,200) # Card background color
pygame.draw.rect(screen, card_color, (x, y,100,150),0) # Card background
image_path = os.path.join("C:\\Users\\Gebruiker\\OneDrive - HvA\\Desktop\\cards", f"{card.color.value}{card.symbol.value}{card.shading.value}{card.number.value}.gif")
card_image = pygame.image.load(image_path)
card_image = pygame.transform.scale(card_image, (100,150))
screen.blit(card_image, (x, y))
pygame.display.flip()
# Function to draw the play button on the screen
def draw_play_button(screen):
pygame.draw.rect(screen,(0,200,0),(350,250,100,50), border_radius=5)
draw_text(screen, "Play", (370,265), font_size=20, color=(255,255,255))
# Function to draw the number of cards left on the screen
def draw_cards_left(screen, deck):
draw_text(screen, f"Cards Left: {len(deck)}",(350,550), font_size=20, color=(255,255,255))
# Function to draw the turn indicator on the screen
def draw_turn_indicator(screen, turn, timer):
box_rect = pygame.Rect(580,90,200,60) # Define a rectangle for the box
pygame.draw.rect(screen,(200,200,200), box_rect) # Draw the box
draw_text(screen, f"Turn: {turn}",(600,100), font_size=20, color=(0,0,255))
draw_text(screen, f"Timer: {timer:.1f}",(600,130), font_size=20, color=(0,0,255))
def draw_message(screen, message, font_size=30, color=(255,255,255)):
font = pygame.font.Font(None, font_size)
text_surface = font.render(message, True, color)
text_rect = text_surface.get_rect(center=(screen.get_width()//2, screen.get_height()//2))
screen.blit(text_surface, text_rect.topleft)
pygame.display.flip()
pygame.time.delay(2000) # Display the message for 2000 milliseconds (2 seconds)
# Function to reset the timer
def reset_timer():
return pygame.time.get_ticks()
# Function to check if the play button is clicked
def is_play_button_clicked(x, y):
return 350<= x <=450 and 250<= y <=300
# Function to check if a card is clicked
def is_card_clicked(x, y, card_index):
card_x =50+(card_index %3)*150
card_y =50+(card_index //3)*200
return card_x <= x <= card_x +100 and card_y <= y <= card_y +150
# Function to play the SET game
def play_set_game():
SCREEN_WIDTH, SCREEN_HEIGHT =800,600
CARD_SPACING_X, CARD_SPACING_Y =150,200
deck_size =81
table_size =12
inactivity_timeout =10000
computer_thinking_time

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning PostgreSQL On The Cloud Simplifying Database As A Service On Cloud Platforms

Authors: Baji Shaik ,Avinash Vallarapu

1st Edition

1484234464, 978-1484234464

More Books

Students also viewed these Databases questions

Question

What is the preferred personality?

Answered: 1 week ago