我还在努力,慢慢地,创造我自己的简单纸牌游戏。我有以下代码(工作正常):
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
代码成功让我点击屏幕上的一张“卡片”图像,并在控制台中打印出相应的“名称”。
我今天早上已经工作了一个小时,试图让我的选择被清除(或通过blit)并向屏幕中间“向上移动”(或重新布线)(想想类似于选择一个Windows上的Hearts中的卡片 - 我希望选择卡片在屏幕上显示可见的响应。“
无论我似乎尝试过什么,我都无法选择将blit移到另一个区域,而我无法让表面显示原始选择。
有人可以帮助我了解这个blitting过程,以便我可以清除一张卡并让它重新出现在屏幕的另一个区域吗?
以下是我已实施的“卡片”课程:
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
当我实施建议的更改时,我现在收到以下错误:
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'
当我运行代码时:
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'。
答案 0 :(得分:2)
Blitting基本上是将一个图像(或表面)A一次一个像素地绘制到第二个图像B中的过程。
实现这一目标的最简单方法是清除屏幕,在各自的位置绘制所有元素(称为精灵)。
查看你的代码,我发现你没有使用item(x,y)坐标来绘制你的牌,而是使用他们的索引n。基本上,它总是会将您的卡片放在同一个位置而不考虑它们的坐标。
首先,我不是将项目存储在一个列表中,而是将图像存储在第二个列表中,而是将项目存储在第三个列表中,而是使用单个类: 来自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
这应该这样做。