There's lots of ways to do this. The code is making it difficult by managing the cards as separate items.
It would work well to put them into a list, and then draw the list. If you want a specific ordering, sort() the list. The example code below re-orders the displayed cards when space is pressed. It does this by sorting the list of cards, rather than operating on the cards individually.
Below I make a simple class to create a Card type. A few cards are then created & put into a list. To draw the cards in a column, a for() loop iterates over the list of cards, setting their position and then drawing them to the screen.
Probably you want a list for each column of cards in your game.
import pygame
WIDTH = 500
HEIGHT= 500
CARD_WIDTH = 80
CARD_HEIGHT= 160
BLACK = ( 0, 0, 0)
RED = (255, 0, 0)
WHITE = (255, 255, 255)
GREEN = ( 20, 200, 20)
pygame.init()
screen = pygame.display.set_mode( ( WIDTH, HEIGHT ) )
pygame.display.set_caption( "Card Game" )
font = pygame.font.Font( None, 22 ) # font used to make card faces
class Card:
""" Simple sprite-like object representing a playing card """
def __init__(self, number, suit ):
super().__init__()
self.image = pygame.Surface( ( CARD_WIDTH, CARD_HEIGHT ) )
self.rect = self.image.get_rect()
self.suit = suit
self.number = number
# create a card image until we have nice bitmaps ready
self.image.fill( WHITE )
# coloured border
if ( suit == 'spades' or suit == 'clubs' ):
self.colour = BLACK
else: # hearts, diamonds
self.colour = RED
width = self.rect.width
height = self.rect.height
pygame.draw.rect( self.image, self.colour, [0, 0, width, height], 3 )
# centred text for number & suit
text = font.render( str( number ).capitalize(), True, self.colour )
self.image.blit( text, [ 5, 5, text.get_rect().width, text.get_rect().height ] )
self.image.blit( text, [ CARD_WIDTH-5-text.get_rect().width, CARD_HEIGHT-5-text.get_rect().height, text.get_rect().width, text.get_rect().height ] )
text = font.render( suit.capitalize(), True, self.colour )
text_centre_rect = text.get_rect( center = self.image.get_rect().center )
self.image.blit( text, text_centre_rect )
def moveTo( self, x, y ):
""" Reposition the card """
self.rect.x = x
self.rect.y = y
def draw( self, surface ):
""" Paint the card on the given surface """
surface.blit( self.image, self.rect )
### Create a list of cards to play with
cards = []
cards.append( Card( "ace", "spades" ) )
cards.append( Card( "10", "diamonds" ) )
cards.append( Card( "queen", "hearts" ) )
last_change_time = 0 # used to slow down the sort UI
### MAIN
while True:
clock = pygame.time.get_ticks() # time now
# Handle events
keys = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
if ( keys[pygame.K_SPACE] ):
# When space is pushed re-order the cards, but not more than every 300ms
if ( clock > last_change_time + 300 ):
cards.reverse()
last_change_time = clock
# paint the screen
screen.fill( GREEN ) # paint background
# Paint the list of cards in a column
for i,card in enumerate( cards ):
card.moveTo( 200, 50 + ( i * CARD_HEIGHT//4 ) ) # spread out in a single column
card.draw( screen );
pygame.display.flip()