我如何在pygame中将我的选择blit到屏幕上?



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

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

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

我今天早上已经工作了一个小时,试图让我的选择清除(或位格)和"移动"(或重新位格)向上向屏幕的中间(想想类似于在Windows上选择红心卡-我希望卡的选择在我的屏幕上有一个可见的响应)。

无论我如何尝试,我都无法让选区移到另一个区域,也无法让表面不显示原来的选区。

有人能帮我理解比特过程,这样我就可以清除一张卡,并让它重新出现在屏幕的另一个区域?


EDIT1

这是我已经实现的'Card'类:

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

我使用这个方法用上面的'Card'对象填充'Deck'类:

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

EDIT2

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

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'

EDIT3

当我运行代码时:

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)

我得到'False'打印出来,无论我点击我的屏幕。

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

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

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

首先,不是将项目存储在一个列表中,将图像存储在第二个列表中,将rects存储在第三个列表中,而是使用单个类:

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

比特化就是直接了当的:

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

这个就可以了

最新更新