Answered step by step
Verified Expert Solution
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 actually 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 and I have been staring at the code for a while now and it's driving me crazy, because every feedback I get and adjust still ends up in a black screen
:
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 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 # 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.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:
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
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