我仍在努力,慢慢地,创建我自己的简单纸牌游戏。我有以下代码(工作正常):
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)
无论我在屏幕的哪个位置单击,都会打印出“错误”。