Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I have been trying to get the images of this set card game to not be hardcoded, and I cant get it to work, My
I have been trying to get the images of this set card game to not be hardcoded, and I cant get it to work, My original code works but I cant make any adjustments because the images are hardcoded, and this code gives me a black screen on pygame, please help I have no idea what to do with it:
import sys
import random
from enum import Enum
import time
import os
import pygame
# Enums for card properties
class NumberEnum:
ONE
TWO
THREE
class SymbolEnum:
DIAMOND 'diamond'
SQUIGGLE 'squiggle'
OVAL 'oval'
class ColorEnum:
RED 'red'
GREEN 'green'
PURPLE 'purple'
class ShadingEnum:
FILLED 'filled'
SHADED 'shaded'
EMPTY 'empty'
# Class representing a SET card
class SetCard:
def initself number, symbol, color, shading:
self.number number
self.symbol symbol
self.color color
self.shading shading
self.highlighted False
def reprself:
return fselfnumber.valueselfsymbol.valueselfcolor.valueselfshading.value
@staticmethod
def issetcard card card:
properties symbol 'color', 'shading', 'number'
for prop in properties:
if lensetgetattrcard prop getattrcard prop getattrcard prop:
return False
return True
@staticmethod
def findallsetscards:
n lencards
sets
for i in rangen :
for j in rangei n :
for k in rangej n:
if SetCard.issetcardsi cardsj cardsk:
sets.appendcardsi cardsj cardsk
return sets
# Pygame initialization function
def initpygame:
pygame.init
screen pygame.display.setmode
pygame.display.setcaptionSET Game"
return screen
# Function to draw cards on the screen
def drawcardsscreen tablecards, selectedcards, currentdir:
screen.fill # Set background color
for i card in enumeratetablecards:
x i
y i
if card in selectedcards:
pygame.draw.rectscreenx y # Selected card border
if card.highlighted:
y # Adjust the ycoordinate to bring the card forward
cardcolor if card.highlighted else # Card background color
pygame.draw.rectscreen cardcolor, x y # Card background
# Construct relative path to the image directory
imagedir ospath.joincurrentdir, "cards"
# Load the card image using the relative path
imagepath ospath.joinimagedir, fcardcolor.valuecardsymbol.valuecardshading.valuecardnumber.valuegif"
cardimage pygame.image.loadimagepath
cardimage pygame.transform.scalecardimage,
screen.blitcardimage, x y
pygame.display.flip
# Function to draw the play button on the screen
def drawplaybuttonscreen:
pygame.draw.rectscreen borderradius
drawtextscreen "Play", fontsize color
# Function to draw the number of cards left on the screen
def drawcardsleftscreen deck:
drawtextscreen fCards Left: lendeck fontsize color
# Function to draw the turn indicator on the screen
def drawturnindicatorscreen turn, timer:
boxrect pygame.Rect # Define a rectangle for the box
pygame.draw.rectscreen boxrect # Draw the box
drawtextscreen fTurn: turn fontsize color
drawtextscreen fTimer: timer:f fontsize color
# Function to draw message on the screen
def drawmessagescreen message, fontsize color:
font pygame.font.FontNone fontsize
textsurface font.rendermessage True, color
textrect textsurface.getrectcenterscreengetwidth screen.getheight
screen.blittextsurface, textrect.topleft
pygame.display.flip
pygame.time.delay # Display the message for milliseconds seconds
# Function to reset the timer
def resettimer:
return pygame.time.getticks
# Function to check if the play button is clicked
def isplaybuttonclickedx y:
return x and y
# Function to check if a card is clicked
def iscardclickedx y cardindex:
cardx cardindex
cardy cardindex
return cardx x cardx and cardy y cardy
# Function to play the SET game
def playsetgame:
SCREENWIDTH, SCREENHEIGHT
CARDSPACINGX CARDSPACINGY
decksize
tablesize
inactivitytimeout
computerthinkingtime
# Initialize Pygame
screen initpygame
# Get the directory of the current script
currentdir ospath.dirnamefile
# Load deck and shuffle
deck SetCardNumbernum Symbolsym Colorcol Shadingshade for num in Number
for sym in Symbol for col in Color for shade in Shading
random.shuffledeck
# Initialize table cards
tablecards deck:tablesize
deck decktablesize:
# Initialize scores
playerscore
computerscore
# Computer details
computername "Computer"
# Set initial conditions
playbuttonclicked False
lastactivitytime reset
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started