微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在Pygame的一类纸牌中添加一堆png?

如何解决如何在Pygame的一类纸牌中添加一堆png?

因此,我通过所学课程学习了如何制作具有各种功能的一副纸牌。现在,我想使用我制作的类和我的png文件创建一个可以在pygame窗口中使用的平台。当我运行各种功能时e.x. deal_card(),它会以“ 4 of Clubs”的格式返回一张卡,因此我知道它可以使用。如何获得那些能真正代表我所拥有的卡片图像的png

import pygame
import os
from random import shuffle

# Initialize the pygame
pygame.init()


# Setting up the screen and background
screen = pygame.display.set_mode((800,600))


# Title and Icon of window
pygame.display.set_caption("Blackjack")

icon = pygame.image.load('card-game.png')
pygame.display.set_icon(icon)


class Card:
    def __init__(self,value,suit):
        self.value = value
        self.suit = suit

    def __repr__(self):
        # return "{} of {}".format(self.value,self.suit)
        return f"{self.value} of {self.suit}"



class Deck:
    def __init__(self):
        suits = ["Clubs","Diamonds","Hearts","Spades"]
        values = ['A','2','3','4','5','6','7','8','9','10','J','Q','K']
        self.cards = [Card(value,suit) for suit in suits for value in values]

    def __repr__(self):
        return f"Deck of {self.count()} cards"

    def count(self):
        return len(self.cards)

    def _deal(self,num):
        count = self.count()
        actual = min([count,num])
        if count == 0:
            raise ValueError("All cards have been dealt")
        cards = self.cards[-actual:]
        self.cards = self.cards[:-actual]
        return cards

    def deal_card(self):
        return self._deal(1)[0]

    def deal_hand(self,hand_size):
        return self._deal(hand_size)

    def shuffle(self):
        if self.count() < 52:
            raise ValueError("Only full decks can be shuffled")
        shuffle(self.cards)
        return self



# Game Loop that ensures window is always running.
# We make sure we do our stuff in this loop
running = True
while running:

    # RGB
    screen.fill((50,0))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    
    # RGB
    screen.fill((50,0))

解决方法

PNG 的名称为:2 of Clubs、K of Hearts 等。

卡的文件名对应卡的“官方”字符串表示(__repr__)。通过文件扩展名 (.png) 扩展名称并使用 pygame.image.load 加载卡片图像:

class Card:
    def __init__(self,value,suit):
        self.value = value
        self.suit = suit

        self.image = pygame.image.load(str(self) + '.png')

    def __repr__(self):
        # return "{} of {}".format(self.value,self.suit)
        return f"{self.value} of {self.suit}"

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?