0

我仍在努力,慢慢地,创建我自己的简单纸牌游戏。我有以下代码(工作正常):

player_hand_images = []
opponent_hand_images = []
player_image_rects = []
for item in player_hand:
    player_hand_images.append(pygame.image.load(os.path.join('Images', item.name+'.png')))
for item in opponent_hand:
    opponent_hand_images.append(pygame.image.load(os.path.join('Images', item.name+'.png')))
for n, item in enumerate(player_hand_images):
    player_image_rects.append(screen.blit(item, ((n * (SCREEEN_WIDTH/5))+50, SCREEN_HEIGHT*.6)))
for n, item in enumerate(opponent_hand_images):
    screen.blit(item, ((n * (SCREEEN_WIDTH/5))+50, SCREEN_HEIGHT*.15))
# ALL CODE TO DRAW SHOULD GO ABOVE THIS COMMENT

# ALL EVENT PROCESSING SHOULD GO BELOW THIS COMMENT
for event in pygame.event.get(): # User did something
    if event.type == pygame.QUIT: # If user clicked close
        done = True # Flag that we are done so we exit this loop
    if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
        # Set the x, y postions of the mouse click
        x, y = event.pos
        #print x, y
        for n, item in enumerate(player_image_rects):
            if item.collidepoint(x, y):
                card_picked = player_hand[n]
                print card_picked.name

该代码成功地让我在屏幕上单击我的一张“卡片”图像并在控制台中打印出相应的“名称”。

今天早上我已经工作了一个小时,试图让我的选择被清除(或blitted)并向上“移动”(或重新blitted)朝向屏幕中间(想想类似于在Hearts中选择一张卡片在 Windows 上 - 我希望卡的选择在我的屏幕上有可见的响应)。

无论我似乎尝试了什么,我都无法让选区快速移动到另一个区域,也无法让表面不显示原始选区。

有人可以帮助我了解 blitting 过程,以便我可以清除一张卡片并让它重新出现在屏幕的另一个区域吗?


编辑1

这是我已经实现的“卡片”类:

class Card(object):
    def __init__(self, name="", attack=0, defense=0, magic=0, shield=0, description=""):
        self.name = name
        self.attack = int(attack)
        self.defense = int(defense)
        self.magic = int(magic)
        self.shield = int(shield)
        self.description = description

我使用此方法用上述“卡片”对象填充“甲板”类:

class Deck(object):

    def __init__(self, deck):
        self.deck = self.getDeck(deck)


    def getDeck(self, deck):
        with open(deck, "rU") as csvfile:
            deckReader = csv.DictReader(csvfile, delimiter=',')
            newDeck = []
            for row in deckReader:
                for x in range(int(row['NumberOfCards'])):
                    if row['NameOfCard'] in cards:
                         newDeck.append(cards[row['NameOfCard']])

        return newDeck

编辑2

当我实施建议的更改时,我现在收到以下错误:

Traceback (most recent call last):
  File "/.../Card Game 1/pygame_new_attempt.py", line 176, in <module>
    if item.hitpoint(x, y):
 AttributeError: 'pygame.Surface' object has no attribute 'hitpoint'

编辑3

当我运行代码时:

for event in pygame.event.get(): # User did something
    if event.type == pygame.QUIT: # If user clicked close
        done = True # Flag that we are done so we exit this loop
    if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
        # Set the x, y postions of the mouse click
        x, y = event.pos
        #print x, y
        for card in player_cards:
            print card.hittest(x, y)

无论我在屏幕的哪个位置单击,都会打印出“错误”。

4

1 回答 1

2

Blitting 基本上是一次将一个图像(或表面)A 绘制到第二个图像 B 一个像素的过程。

实现这一点的最简单方法是清除屏幕,在各自的位置绘制所有元素(称为精灵)。

查看您的代码,我发现您不使用项目 (x,y) 坐标来绘制卡片,而是使用它们的索引 n。基本上,它总是会在同一个位置绘制你的卡片,而不考虑它们的坐标。

首先,我不会将项目存储在一个列表中,将图像存储在第二个列表中,将矩形存储在第三个列表中,而是使用一个类: from rect import Rect

class Card:

    def __init__(self, name, x, y, width, height, image):
        self.name = name
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.image = image

    def hittest(x, y):
        return Rect((self.x, self.y, self.width, self.height)).collidepoint((x, y))

现在我创造了双手

player_cards = []
for n, item in enumerate(player_hand):
    image = pygame.image.load(os.path.join('Images', item.name+'.png'))
    x = (n * (SCREEEN_WIDTH/5))+50
    y = SCREEN_HEIGHT*.6
    width = 20
    height = 60
    name = item.name
    players_cards.append(Card(name, x,y,width,height, image))

Blitting 很简单:

for card in player_cards:
    screen.blit(card.image, card.x, card.y)

最后但并非最不重要的一点是,我们处理选择过程并将卡片移动到屏幕中间:

if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
        # Set the x, y postions of the mouse click
        x, y = event.pos
        #print x, y
        for card in player_cards:
            if item.hitpoint(x, y):
                card_picked = card
                print card_picked.name
                card_picked.x = (SCREEEN_WIDTH - card_picked.width) / 2
                card_picked.y = (SCREEEN_HEIGHT - card_picked.width) / 2

这应该这样做。

于 2013-06-03T16:38:47.813 回答