Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I have the following set card game code, it works but its very messy and it glitches, it doesnt really play smoothly, could you please
I have the following set card game code, it works but its very messy and it glitches, it doesnt really play smoothly, could you please make it look more visually nice, also I want the code to smoothly transition but its very glitchy: 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
def drawcardsscreen tablecards, selectedcards:
screen.fill
for i card in enumeratetablecards:
x i
y i
if card in selectedcards:
pygame.draw.rectscreenx y
if card.highlighted:
y # Adjust the ycoordinate to bring the card forward
imagepath ospath.joinC:UsersGebruikerOneDrive HvADesktopcards 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:
drawtextscreen fTurn: turn fontsize color
drawtextscreen fTimer: timer:f fontsize color
# 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
# 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 resettimer
playerturn True
turnstarttime resettimer
turnduration # seconds
selectedcards
while not playbuttonclicked:
for event in pygame.event.get:
if event.type pygame.QUIT:
pygame.quit
sysexit
elif event.type pygame.MOUSEBUTTONDOWN and event.button :
x y event.pos
if isplaybuttonclickedx y:
playbuttonclicked True
drawcardsscreen tablecards, selectedcards
drawplaybuttonscreen
pygame.display.flip
lastactivitytime resettimer
running True
while running and deck:
drawcardsscreen table
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started