python - 如何使图片从列表中出现并使其消失,同时在pygame中为其设置变量?
问题描述
我想知道如何让图像出现在列表中,然后在单击后使其消失。一旦点击它,就会分配一个变量。
当我在 pygame 中运行它时,我打印了一堆所有这些图片,并且它们通过得非常快。
def game():
screen = pygame.display.set_mode((1400, 750))
pygame.display.set_caption("Goofspiel")
screenExit = False
while not screenExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
screenExit = True
keys = pygame.key.get_pressed()
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
screen.fill(lightgray)
fontname = pygame.font.SysFont("Denmark", 150)
font = pygame.font.SysFont("Denmark", 40)
font1 = pygame.font.SysFont("Denmark", 75)
name = fontname.render("Goofspeil", True, (black))
score = font.render("Player 1", True, (blue))
score1 = font.render("Player 2", True, (red))
player = font1.render("Player 1", True, (black))
que = font.render("Who's turn is it?", True, (black))
screen.blit(name, (490, 0))
#Score board
pygame.draw.rect(screen, sun, [0,20,375,80])
pygame.draw.rect(screen, black, [0,20,375,5])
pygame.draw.rect(screen, black, [0, 100.5, 375, 5])
pygame.draw.rect(screen, black, [375, 20, 5, 85])
pygame.draw.rect(screen, black, [305, 20, 5, 85])
pygame.draw.rect(screen, black, [180, 20, 5, 85])
pygame.draw.rect(screen, black, [110, 20, 5, 85])
screen.blit(score, (0, 50))
screen.blit(score1, (190, 50))
screen.blit(que, (1100, 20))
screen.blit(player, (1120, 70))
#Displaying the cards
screen.blit(DK, (5, 450))
screen.blit(DQ, (100, 450))
screen.blit(DJ, (200, 450))
screen.blit(D10, (300, 450))
screen.blit(D9, (400, 450))
screen.blit(D8, (500, 450))
screen.blit(D7, (600, 450))
screen.blit(D6, (700, 450))
screen.blit(D5, (800, 450))
screen.blit(D4, (900, 450))
screen.blit(D3, (1000, 450))
screen.blit(D2, (1100, 450))
screen.blit(D1, (1200, 450))
#Add a random picture
list = []
list.append(HK)
list.append(HQ)
list.append(HJ)
list.append(H10)
list.append(H9)
list.append(H8)
list.append(H7)
list.append(H6)
list.append(H5)
list.append(H4)
list.append(H3)
list.append(H2)
list.append(H1)
#rand = random.randrange(0, len(list))
random.shuffle(list)
screen.blit(list[0], (700,150))
if 200 > mouse[0] > 100 and 700 > mouse[1] > 450:
pygame.draw.rect(screen, lightblue2, [80, 705, 200, -60])
if click[0] == 1:
x = 12
score2 = font.render(str(x), True, (black))
screen.blit(score2, (135, 50))
pygame.display.update()
game()
我想要做的是,我希望从这个列表中随机显示一张图片,然后从列表中“删除”,然后让图片从屏幕上消失。因为这些图片是卡片,所以我想要如果一个人点击国王,x
价值=13等等。
有人可以帮忙吗,我被这个问题困住了。我正在尝试制作Goofspiel游戏,但我做不到!
解决方案
这是我将如何进行的点形式:
- 将您的 H1、H2 ... 图像转换为PyGame Sprites。
这将图像与矩形融合在一起。它使您可以轻松地重新定位和绘制所有卡片。该代码还使用鼠标单击事件来轻松确定单击了哪些卡片(如果有)。
- 将必要的卡片详细信息也添加到 Sprite 类中
这可以让你的卡片“知道”它的花色、号码以及它是否正面朝上。
- 将您的卡片精灵放入 Pygame SpriteGroup
这有助于轻松绘图和鼠标单击碰撞检测。
- 一旦您知道点击了哪张卡片,就很容易将其从屏幕上移除,将其翻转过来,因为它的状态(位置、套装、编号、正面朝上等)都保存在 sprite 类中。它不再成为从列表中删除“#8”的问题。
像这样的东西:
class Card( pygame.sprite.Sprite ):
def __init__( self, front_image, back_image, suit, number, facing_up=True ):
pygame.sprite.Sprite.__init__(self)
self.front = pygame.image.load( front_image ).convert()
self.back = pygame.image.load( back_image ).convert()
self.rect = self.front.get_rect()
self.suit = suit
self.number = number
self.face_up = not facing_up
self.flip() # re-draw
def flip( self ):
self.face_up = not self.face_up
if ( self.face_up ):
self.image = self.front
else:
self.image = self.back
def moveTo( self, x, y ):
self.rect.x = x
self.rect.y = y
def isFaceUp( self ):
return self.face_up
# ... etc.
# make the cards
HK = Card( 'hearts_king.png', 'card_back.png', 'hearts', 13 )
HQ = Card( 'hearts_queen.png', 'card_back.png', 'hearts', 12 )
HJ = Card( 'hearts_jack.png', 'card_back.png', 'hearts', 11 )
H10 = Card( 'hearts_10.png', 'card_back.png', 'hearts', 10 )
# ... etc
all_hearts = [ HK, HQ, HJ, H10, H9, ... H1 ]
# TODO: call Card.moveTo() to position each card
cards_on_table = pygame.sprite.Group()
for card in all_hearts:
cards_on_table.add( card )
# Main Loop:
while not done:
# Handle user-input
for event in pygame.event.get():
if ( event.type == pygame.QUIT ):
done = True
elif ( event.type == pygame.MOUSEBUTTONUP ):
# On mouse-click
mouse_pos = pygame.mouse.get_pos()
# Did we click on a card?
for card in cards_on_table:
if ( card.rect.collidepoint( mouse_pos ) ):
print( "Card [%s, %d] was clicked" % ( card.suit, card.number ) )
cards_on_table.remove( card ) # remove card from group
break
# Re-draw the window
window.fill( DARK_GREEN )
cards_on_table.draw( window )
pygame.display.flip()
# ... etc
推荐阅读
- node.js - Mongodb:聚合/管道基于嵌套文档连接两个模式的数据
- entity-framework - EF Core:急切加载 (.Include) 子类别(自引用)
- python - 使用 Python tensorflow 进行预测和预测
- python - 如何使用 while 循环迭代器修改 tf 变量
- graphdb - GraphDB free 停止工作
- c# - C# 调用自定义 COM+ 错误:80070005 访问被拒绝
- javascript - 小图像不使用 ElevateZoom 插件缩放
- c# - 在 C++/CLI 中实现 C# 接口函数返回数组
- http - 多个 POST 操作的 Httpt 批量调用
- transform - 在 HEVC 参考软件 16.18 中提取 TU 分区信息