For 循环不加载 pygame 中的所有图像并且不能将它们 blit 到屏幕上
For loop not loading all images in pygame and can't blit them onto screen
我正在尝试制作二十一点游戏,但在 for 循环中加载纸牌图像时遇到问题。我命名图片的方式就像 D4.png 是方块卡片 4 的图片,QD.png 是方块皇后的图片等。我的问题是只有最后一张卡片类型和正在加载数字。当我检查 cards_list 的长度时,它 returns 只有 8 张而不是将要使用的 53 张卡。
cards_list = []
cards_type_1 = ['D', 'H', 'C', 'S']
cards_type_2 = ['Q', 'K', 'A', 'J']
for card in cards_type_1:
for x in range(2, 11):
img = pygame.image.load(f'img\cards\{card}{x}.png')
cards_list.append(img)
for card in cards_type_2:
for suit in cards_tpye_1:
img = pygame.image.load(f'img\cards\{card}{suit}.png')
cards_list.append(img)
print(len(cards_list)) # This returns only 8
另外,对于我加载的牌,我希望电脑随机发牌。我正在使用我在网上找到的按钮模块来检测何时单击了按钮。所以我要做的是让玩家点击 Hit 按钮并获得一张牌。然而,卡片只出现在屏幕上半秒钟,然后就消失了。
while run:
clock.tick(60)
screen.blit(bg, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
# this is code for the main menu
if start_game is False:
if display_main_menu:
if exit_btn_1.draw(screen):
run = False
if start_btn.draw(screen):
start_game = True
# tkinter mainloop
prompt.grid(row=0, column=0, columnspan=3)
root.title('Enter Name')
e.grid(row=1, column=0, columnspan=3)
add_btn.grid(row=2, column=1)
root.mainloop()
if leaders_btn.draw(screen):
pass
if start_game is True:
display_main_menu = False
if exit_btn_2.draw(screen):
run = False
if menu_btn_2.draw(screen):
start_game = False
display_main_menu = True
if hit_btn.draw(screen): # this means if the bit button is clicked
screen.blit(random.choice(cards_list), (400, 400))
screen.blit(coin_img, (960, 40))
# draw text method created just to draw text on image
draw_text(f'Name: {name}', font1, 50, 50, BLACK)
draw_text(f'Score: {score}', font1, 50, 100, BLACK)
draw_text(f'{player.credits}', font2, 1050, 50, GOLD)
pygame.display.update()
首先,导致列表中只有 8 张卡片的问题是 cards_list.append(img)
在第二个循环之外,这导致整个循环每次都完成并覆盖 img
。 cards_list.append(img)
行的缩进应该在两个循环中增加,如下所示:
for card in cards_type_2:
for suit in cards_tpye_1:
img = pygame.image.load(f'img\cards\{card}{suit}.png')
cards_list.append(img)
至于第二个问题,我相信这是if hit_btn.draw(screen):
行造成的。似乎这个函数 returns 只有在按住“Hit”按钮时才为真,这意味着一旦停止按下按钮,卡片就会停止绘制。你可能想要的是在这个 if 语句中有一个设置为 True 的标志,然后你可以根据标志绘制卡片,这意味着当按钮不再被按下时它不会消失。
我正在尝试制作二十一点游戏,但在 for 循环中加载纸牌图像时遇到问题。我命名图片的方式就像 D4.png 是方块卡片 4 的图片,QD.png 是方块皇后的图片等。我的问题是只有最后一张卡片类型和正在加载数字。当我检查 cards_list 的长度时,它 returns 只有 8 张而不是将要使用的 53 张卡。
cards_list = []
cards_type_1 = ['D', 'H', 'C', 'S']
cards_type_2 = ['Q', 'K', 'A', 'J']
for card in cards_type_1:
for x in range(2, 11):
img = pygame.image.load(f'img\cards\{card}{x}.png')
cards_list.append(img)
for card in cards_type_2:
for suit in cards_tpye_1:
img = pygame.image.load(f'img\cards\{card}{suit}.png')
cards_list.append(img)
print(len(cards_list)) # This returns only 8
另外,对于我加载的牌,我希望电脑随机发牌。我正在使用我在网上找到的按钮模块来检测何时单击了按钮。所以我要做的是让玩家点击 Hit 按钮并获得一张牌。然而,卡片只出现在屏幕上半秒钟,然后就消失了。
while run:
clock.tick(60)
screen.blit(bg, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
# this is code for the main menu
if start_game is False:
if display_main_menu:
if exit_btn_1.draw(screen):
run = False
if start_btn.draw(screen):
start_game = True
# tkinter mainloop
prompt.grid(row=0, column=0, columnspan=3)
root.title('Enter Name')
e.grid(row=1, column=0, columnspan=3)
add_btn.grid(row=2, column=1)
root.mainloop()
if leaders_btn.draw(screen):
pass
if start_game is True:
display_main_menu = False
if exit_btn_2.draw(screen):
run = False
if menu_btn_2.draw(screen):
start_game = False
display_main_menu = True
if hit_btn.draw(screen): # this means if the bit button is clicked
screen.blit(random.choice(cards_list), (400, 400))
screen.blit(coin_img, (960, 40))
# draw text method created just to draw text on image
draw_text(f'Name: {name}', font1, 50, 50, BLACK)
draw_text(f'Score: {score}', font1, 50, 100, BLACK)
draw_text(f'{player.credits}', font2, 1050, 50, GOLD)
pygame.display.update()
首先,导致列表中只有 8 张卡片的问题是 cards_list.append(img)
在第二个循环之外,这导致整个循环每次都完成并覆盖 img
。 cards_list.append(img)
行的缩进应该在两个循环中增加,如下所示:
for card in cards_type_2:
for suit in cards_tpye_1:
img = pygame.image.load(f'img\cards\{card}{suit}.png')
cards_list.append(img)
至于第二个问题,我相信这是if hit_btn.draw(screen):
行造成的。似乎这个函数 returns 只有在按住“Hit”按钮时才为真,这意味着一旦停止按下按钮,卡片就会停止绘制。你可能想要的是在这个 if 语句中有一个设置为 True 的标志,然后你可以根据标志绘制卡片,这意味着当按钮不再被按下时它不会消失。