Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have the following code for a set card game, I think it works , only visually it glitches and it doesnt look very good,

I have the following code for a set card game, I think it works , only visually it glitches and it doesnt look very good, for example i want to to see which cards I click and which cards the computer clicks, I want everything to happen in the pygame screen instead of the command prompt, could you pretty please clean the code up to make it look good visually??: import pygame
import sys
import random
from enum import Enum
import time
import os
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 SetCard:
def __init__(self, number, symbol, color, shading):
self.number = number
self.symbol = symbol
self.color = color
self.shading = shading
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
def init_pygame(deck):
pygame.init()
screen = pygame.display.set_mode((800,600))
pygame.display.set_caption("SET Game")
return 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)
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)
def draw_cards(screen, table_cards):
screen.fill((200,200,200))
for i, card in enumerate(table_cards):
x =50+(i %3)*150
y =50+(i //3)*200
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)
screen.blit(card_image, (x, y,100,150))
pygame.display.flip()
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))
def draw_cards_left(screen, deck):
draw_text(screen, f"Cards Left: {len(deck)}",(350,550), font_size=20, color=(255,255,255))
def reset_timer():
return pygame.time.get_ticks()
def is_play_button_clicked(x, y):
return 350= x =450 and 250= y =300
def play_set_game():
# Constants
SCREEN_WIDTH, SCREEN_HEIGHT =800,600
CARD_WIDTH, CARD_HEIGHT =100,150
CARD_SPACING_X, CARD_SPACING_Y =150,200
PLAY_BUTTON_RECT =(350,250,100,50)
deck_size =81
table_size =12
inactivity_timeout =10000
computer_thinking_time =2
# Initialize Pygame
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("SET Game")
# Load deck and shuffle
deck =[SetCard(Number(num), Symbol(sym), Color(col), Shading(shade)) for num in Number
for sym in Symbol for col in Color for shade in Shading]
random.shuffle(deck)
# Initialize table cards
table_cards = deck[:table_size]
deck = deck[table_size:]
# Initialize scores
player_score =0
computer_score =0
# Computer details
computer_name = "Computer"
# Set initial conditions
play_button_clicked = False
last_activity_time = reset_timer()
while not play_button_hppclicked:hj
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN and event.button ==1:
(The rest of the code is in the screenshot because it wont paste, thank you in advance) I also cant get the computer to wait its turn and instead it keeps going until i interfer)
image text in transcribed

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

Databases In Networked Information Systems 6th International Workshop Dnis 2010 Aizu Wakamatsu Japan March 2010 Proceedings Lncs 5999

Authors: Shinji Kikuchi ,Shelly Sachdeva ,Subhash Bhalla

2010th Edition

3642120377, 978-3642120374

More Books

Students also viewed these Databases questions

Question

Brief the importance of span of control and its concepts.

Answered: 1 week ago

Question

What is meant by decentralisation?

Answered: 1 week ago

Question

a. How do you think these stereotypes developed?

Answered: 1 week ago

Question

7. Describe phases of multicultural identity development.

Answered: 1 week ago