在 pygame 中的相同 "level" 上画东西

Draw things on the same "level" in pygame

我正在尝试在 pygame 上玩单人纸牌游戏,我想抽取所有位于其列底部的牌,以便在每隔一张牌之后抽取。有没有办法改变绘制图像的顺序?我的意思是像一种改变我的 draw_window 函数

中的顺序的方法

import pygame
import os
#Fixa så att man flyttar flera kort när man flyttar högsta
#Fixa så att korten inte hamnar på varandra
#fixa så att man kan lägga kungen på en tom yta
#fixa blandad kortlek
#fixa fixa kort som ligger med baksidan upp



def draw_window():
    WIN.blit(RESIZED_BACKGROUND, (0, 0))
    WIN.blit(RESIZED_AXEL, (CARD3.xpos, CARD3.ypos))
    WIN.blit(RESIZED_HDAM, (CARD1.xpos, CARD1.ypos))
    WIN.blit(RESIZED_RKUNG, (CARD2.xpos, CARD2.ypos))
    pygame.display.update()


def main():
    clock = pygame.time.Clock()
    run = True
    while run:
        clock.tick(fps)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
            if event.type == pygame.MOUSEBUTTONUP:
                mousepos = pygame.mouse.get_pos()
                clicked_card = clicked_on_card(mousepos)    #Kollar om kortet är klickbart
                if clicked_card is not None and is_moveable(clicked_card, LIST_OF_CARDS) is not None:
                    move_card(clicked_card, parent_card)

        draw_window()
    pygame.quit()


main()

有很多方法可以做到这一点。该代码通过将卡片作为单独的项目进行管理变得困难。

最好把它们放到一个列表中,然后绘制列表。如果您想要特定的顺序,sort() 列表。 re-orders 下方的示例代码显示了按下 space 时显示的卡片。它通过对卡片列表进行排序来实现这一点,而不是单独对卡片进行操作。

下面我做一个简单的class来创建一个Card类型。然后创建一些卡片并将其放入列表中。要在列中绘制卡片,for() 循环遍历卡片列表,设置它们的位置,然后将它们绘制到屏幕上。

可能您想要游戏中每一列纸牌的列表。

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()