我正在尝试用pygame制作带卡片图像的二十一点游戏,但它只会显示1张图片。有什么方法可以显示所有这些吗?我尝试了不同的方法,但无济于事。我错过了一个脚本或一行,还是还有其他我需要做的事情?
import pygame
pygame.init()
screen_width = 900
screen_height = 600
screen = pygame.display.set_mode((screen_width,screen_height))
font = pygame.font.Font(None, 36)
text_screen_title = font.render("Version 6 of BlackJack",1,(0,0,0))
class CardRecord():
x_coord = 0
y_coord = 0
image_number = 0
face_up = False
card_image = []
card_image.append(pygame.image.load('cards/clubace.gif'))
card_image.append(pygame.image.load('cards/club2.gif'))
card_image.append(pygame.image.load('cards/club3.gif'))
card_image.append(pygame.image.load('cards/club4.gif'))
card_image.append(pygame.image.load('cards/club5.gif'))
card_image.append(pygame.image.load('cards/club6.gif'))
card_image.append(pygame.image.load('cards/club7.gif'))
card_image.append(pygame.image.load('cards/club8.gif'))
card_image.append(pygame.image.load('cards/club8.gif'))
card_image.append(pygame.image.load('cards/club9.gif'))
card_image.append(pygame.image.load('cards/club10.gif'))
card_image.append(pygame.image.load('cards/clubjack.gif'))
card_image.append(pygame.image.load('cards/clubking.gif'))
card_image.append(pygame.image.load('cards/clubqueen.gif'))
card_image.append(pygame.image.load('cards/spadeace.gif'))
card_image.append(pygame.image.load('cards/spade2.gif'))
card_image.append(pygame.image.load('cards/spade3.gif'))
card_image.append(pygame.image.load('cards/spade4.gif'))
card_image.append(pygame.image.load('cards/spade5.gif'))
card_image.append(pygame.image.load('cards/spade6.gif'))
card_image.append(pygame.image.load('cards/spade7.gif'))
card_image.append(pygame.image.load('cards/spade8.gif'))
card_image.append(pygame.image.load('cards/spade9.gif'))
card_image.append(pygame.image.load('cards/spade10.gif'))
card_image.append(pygame.image.load('cards/spadejack.gif'))
card_image.append(pygame.image.load('cards/spadeking.gif'))
card_image.append(pygame.image.load('cards/spadequeen.gif'))
card_image.append(pygame.image.load("cards\heartace.gif"))
card_image.append(pygame.image.load("cards\heart3.gif"))
card_image.append(pygame.image.load("cards\heart4.gif"))
card_image.append(pygame.image.load("cards\heart5.gif"))
card_image.append(pygame.image.load("cards\heart6.gif"))
card_image.append(pygame.image.load("cards\heart7.gif"))
card_image.append(pygame.image.load("cards\heart8.gif"))
card_image.append(pygame.image.load("cards\heart9.gif"))
card_image.append(pygame.image.load("cards\heart10.gif"))
card_image.append(pygame.image.load("cards\heartjack.gif"))
card_image.append(pygame.image.load("cards\heartqueen.gif"))
card_image.append(pygame.image.load("cards\heartking.gif"))
card_image.append(pygame.image.load("cards\diamondace.gif"))
card_image.append(pygame.image.load("cards\diamond2.gif"))
card_image.append(pygame.image.load("cards\diamond3.gif"))
card_image.append(pygame.image.load("cards\diamond4.gif"))
card_image.append(pygame.image.load("cards\diamond5.gif"))
card_image.append(pygame.image.load("cards\diamond6.gif"))
card_image.append(pygame.image.load("cards\diamond7.gif"))
card_image.append(pygame.image.load("cards\diamond8.gif"))
card_image.append(pygame.image.load("cards\diamond9.gif"))
card_image.append(pygame.image.load("cards\diamond10.gif"))
card_image.append(pygame.image.load("cards\diamondjack.gif"))
card_image.append(pygame.image.load("cards\diamondqueen.gif"))
card_image.append(pygame.image.load("cards\diamondking.gif"))
#Append: add an object to the list
clock = pygame.time.Clock()
#Initialise the array of records
card_list = []
#Append: add an object to the list
clock = pygame.time.Clock()
#Initialise the array of records
card_list = []
temp_X = 50
temp_Y = 50
i = 0
while i < 52:
temp_card = CardRecord()
temp_card.image_number = i
card_list.append(temp_card)
i = i + 1
player = []
dealer= []
i = 0
while i < 10:
player.append(card_list[i])
i = i + 1
dealer.append(card_list[i])
i = i + 1
running = True
while running == True:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN and event.key == pygame.ESCAPE:
running = False
screen.fill((30,200,50))
screen.blit(text_screen_title, (100,10))
screen.blit(dealer[0].card_image[dealer[0].image_number],(dealer[0].x_coord, dealer[0].y_coord))
pygame.display.flip()
pygame.display.quit()
我还需要了解如何为每张卡设置x和y坐标。任何帮助,将不胜感激。感谢
答案 0 :(得分:0)
嗯,我很确定它只抽一张卡的原因是因为你告诉程序只画了一张卡,如下图所示。
screen.blit(dealer[0].card_image[dealer[0].image_number].image_number, (dealer[0].x_coords, dealer[0].y_coords)
这只绘制了经销商名单的第一个索引。
为了绘制dealer
列表中的所有卡片,我们可以使用for
循环。
for dealercards in range(len(dealer)):
screen.blit(dealer[dealercards].card_image[dealer[dealercards].image_number, (dealer[dealercards].x_coords, dealer[dealercards].y_coords))
由于image_number
,我必须使用修改过的for循环另外,你的代码很奇怪。你使用while循环forrad,无效地使用一个类,你有一些错误如果你修复你的卡类,那么做x和y位置会更容易。