As with my previous posts, (sorry if im filling up the list too much) I'm working on solitaire and other card games to get a toolkit made for when I use pygame. I am trying to figure out the best way to load sprites, and I wanna get into using the built in Sprite class in pygame, but I am unsure of an effective way on how to get everything organized. I am working with a CMV model. All my card sprites are 2 character strings for the face up stuff, and My last iteration of the code used a dict just for the upfacing stuff and just single vars for the extra stuff. I feel like at least having pygame's sprite class figured out will streamline my work on this and other games I plan to figure out how to make
#🔒 Working out best options for loading sprites and handling rects in solitaire.
12 messages · Page 1 of 1 (latest)
@slender pine
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Closes after a period of inactivity, or when you send !close.
Click here to see this code in our pastebin.
here's the view code so far
import pygame
import os
import solitaireModel as model
# Base resolution
baseWidth = 1100
baseHeight = 800
# Layout constants
tableauYOffset = 25
tableauXOffset = 105
bgColor = (0, 90, 0)
stockPos = [25, 25]
wastePos = [135, 25] # fanning for 3 cards adds 2 * 25 or 50 px
tableauStartX = 345
tableauY = 25
foundationPosList = [
[25, 470], # Hearts
[25, 635], # Diamonds
[150, 470], # Spades
[150, 635] # Clubs
]
images = []
# Setup
pygame.init()
game = model.Game()
screen = pygame.display.set_mode((baseWidth, baseHeight), pygame.RESIZABLE)
pygame.display.set_caption("Solitaire")
clock = pygame.time.Clock()
# Sprite loader
currentDir = os.path.dirname(os.path.abspath(__file__))
baseDir = os.path.dirname(currentDir) # Games file
os.chdir(baseDir)
spritesDir = os.path.join(baseDir, "Card Sprites")
print(baseDir)
print(spritesDir)
def createDeck(): # Sprite booter, scales sprites down for ease of use with smaller window
for filename in os.listdir(spritesDir):
sprite = pygame.image.load(os.path.join(spritesDir, filename))
sprite = pygame.transform.smoothscale_by(sprite, 0.8) # 80% scale
images.append(sprite)
createDeck()
running = True
while running:
screen.fill([0, 90, 0])
screen.blit(images[4], [25, 25])
screen.blit(images[4], [135, 25])
screen.blit(images[4], [160, 25])
screen.blit(images[4], [185, 25])
screen.blit(images[61], [25, 175])
screen.blit(images[4], [25, 485])
screen.blit(images[4], [25, 635])
screen.blit(images[4], [135, 485])
screen.blit(images[4], [135, 635])
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
I had some responses talking about like hashing to link the sprites to the objects but I'm trying to make a good starting OOP program and I'm not too familiar with straight hashing stuff
had to backtrack to an older version to make sure I'm maintaining scale stuff
Basically how can I use pygame’s sprite class effectively? I know it has like functions to get the rects of stuff but I don’t know how to check for intersecting rects how I need to.
The images in the running chunk were put by me to test spacing
.
@slender pine
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.