Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I have the following code for a set card game, now I had the image paths to a folder in my laptop, I managed to
I have the following code for a set card game, now I had the image paths to a folder in my laptop, I managed to get the images in the same directory as the script, now Im confused because the code still wont work and I thought I had changed all the paths, could you please take a look and rewrite itAlso if able please make it look more visually nice and that the game runs smoothly when you start the pygame thank you in advance:
import pygame
import sys
import random
from enum import Enum
import time
import os
# 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 text on the screen
def drawtextscreen text, position, fontsize color:
font pygame.font.FontNone fontsize
textsurface font.rendertext True, color
screen.blittextsurface, position
# Function to draw confetti on the screen
def drawconfettiscreen:
for in range:
x random.randint
y random.randint
pygame.draw.circlescreenx y
# Function to draw cards on the screen
# previous code
# Function to draw cards on the screen
def drawcardsscreen tablecards, selectedcards:
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
imagepath ospath.joincards fcardcolor.valuecardsymbol.valuecardshading.valuecardnumber.valuegif"
cardimage pygame.image.loadimagepath
cardimage pygame.transform.scalecardimage,
screen.blitcardimage, x y
pygame.display.flip
# remaining code
# 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
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 init
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started