在相同的“级别”上绘制东西。在Pygame中

发布于 2025-01-27 13:14:00 字数 1436 浏览 3 评论 0 原文

我正在尝试在Pygame上制作纸牌,我想绘制所有在其他卡后绘制的圆柱底部的卡片。有没有办法更改绘制图像的顺序?我的意思是像一种方法更改我的draw_window函数中的顺序

。 noreferrer“> ”

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

I am trying to make solitaire on pygame and I want to draw all cards that are at the bottom in their column to be drawn after every other card. Is there a way to change the order in which the images are drawn? I mean like a method that changes the order in my draw_window function

I want the game to look like this every time

And not like this

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仅冇旳回忆 2025-02-03 13:14:00

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

将它们列入列表,然后绘制列表将很好地工作。如果您想要特定的订购, sort()列表。按下 space 时,下面的示例代码将显示的卡重新订购。它通过对卡列表进行排序,而不是单独操作卡片。

在下面,我将简单的类创建 card 类型。然后创建一些卡片&放入清单。要在列中绘制卡片,()循环在卡片列表上迭代,设置其位置,然后将其绘制到屏幕上。

可能您需要游戏中每一张卡片的列表。

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

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文