我昨天发布了这个,但我没有提供足够的背景供任何人做任何正确的分析。我正在尝试制作一个纸牌游戏,我在这个网站上发布的代码(感谢所有帮助过的人)。我已编码到点按结束按钮启动由drawPhase()函数定义的绘制阶段并在游戏循环代码结束时启动的点。我已经做到这一点,每个玩家都会抽两张牌,但由于某种原因,它试图抽出四倍的金额,因为卡片没有索引而造成崩溃。这是代码:
import pygame
import sys
import random
import os
from pygame.locals import*
WINWIDTH = 1200
WINHEIGHT = 800
CARDTHUMBWIDTH = 50
CARDTHUMBHEIGHT = 80
FPS = 30
FPSCLOCK = pygame.time.Clock()
DISPLAYSURF = pygame.display.set_mode((WINWIDTH,WINHEIGHT))
pygame.display.set_caption('NBA Card Game')
pygame.init()
lakersDeck = ['Lakers_01.png', 'Lakers_02.png', 'Lakers_03.png', 'Lakers_04.png', 'Lakers_05.png',
'Lakers_06.png', 'Lakers_07.png', 'Lakers_08.png', 'Lakers_09.png', 'Lakers_10.png',
'Lakers_11.png', 'Lakers_12.png', 'Lakers_13.png', 'Lakers_14.png', 'Lakers_15.png',
'Lakers_16.png', 'Lakers_17.png', 'Lakers_18.png', 'Lakers_19.png', 'Lakers_20.png']
#shuffling deck, getting first hand, deleting drawn cards from deck
shuffleDeck = random.sample(lakersDeck, len(lakersDeck))
playerDeck = shuffleDeck
playerHand = []
playerHand.append(playerDeck[0])
playerHand.append(playerDeck[1])
playerHand.append(playerDeck[2])
playerHand.append(playerDeck[3])
for cards in range(4):
del playerDeck[0]
oppShuffleDeck = random.sample(lakersDeck, len(lakersDeck))
oppGetDeck = oppShuffleDeck
oppGetHand = []
oppGetHand.append(oppGetDeck[0])
oppGetHand.append(oppGetDeck[1])
oppGetHand.append(oppGetDeck[2])
oppGetHand.append(oppGetDeck[3])
for cards in range(4):
del oppGetDeck[0]
#display playmat etc
textPromptFont = pygame.font.SysFont("Lato-Regular", 30)
textPromptRect = "20, 730"
playMat = pygame.image.load('playmat.png')
endButton = pygame.image.load('End Button.png')
detailBox = pygame.image.load('detailBox.png')
DISPLAYSURF.blit(playMat, (0,0))
DISPLAYSURF.blit(endButton, (775, 675))
#defining images and rects
HAND_IMAGES = {
0: pygame.image.load(playerHand[0]),
1: pygame.image.load(playerHand[1]),
2: pygame.image.load(playerHand[2]),
3: pygame.image.load(playerHand[3])}
OPP_HAND_IMAGES = {
0: pygame.image.load(oppGetHand[0]),
1: pygame.image.load(oppGetHand[1]),
2: pygame.image.load(oppGetHand[2]),
3: pygame.image.load(oppGetHand[3])}
HAND_RECTS = [pygame.Rect(17, 635, 50, 80), pygame.Rect(75, 635, 50, 80),
pygame.Rect(133, 635, 50, 80), pygame.Rect(191, 635, 50, 80),
pygame.Rect(249, 635, 50, 80), pygame.Rect(307, 635, 50, 80),
pygame.Rect(365, 635, 50, 80), pygame.Rect(423, 635, 50, 80),
pygame.Rect(481, 635, 50, 80), pygame.Rect(539, 635, 50, 80)]
OPP_HAND_RECTS = [pygame.Rect(17, 80, 50, 80), pygame.Rect(75, 80, 50, 80),
pygame.Rect(133, 80, 50, 80), pygame.Rect(191, 80, 50, 80),
pygame.Rect(249, 80, 50, 80), pygame.Rect(307, 80, 50, 80),
pygame.Rect(365, 80, 50, 80), pygame.Rect(423, 80, 50, 80),
pygame.Rect(481, 80, 50, 80), pygame.Rect(539, 80, 50, 80)]
END_RECT = [pygame.Rect(775,675, 110, 110)]
oppHandPos = [(17, 80), (75, 80), (133, 80), (191, 80), (249, 80), (307, 80), (365, 80), (423, 80), (481, 80), (539, 80)]
playerHandPos = [(17, 635), (75, 635), (133, 635), (191, 635), (249, 635), (307, 635), (365, 635), (423, 635), (481, 635), (539, 635)]
defStep = [1]
##drawText("Choose a card for your bench.", textPromptFont, DISPLAYSURF, 20, 725)
##pygame.display.update()
class Text:
def __init__(self, text, font, surface, x, y):
self.text = text
self.font = font
self.surface = surface
self.x = x
self.y = y
def draw_text(self):
text_obj = textPromptFont.render(self.text, 1, (0,0,0))
text_rect = text_obj.get_rect()
text_rect.topleft = (self.x, self.y)
self.surface.blit(text_obj, text_rect)
text_prompt_enter_draw_phase = Text("Click the End Button to enter the Draw Phase.", textPromptFont, DISPLAYSURF,20,725)
text_prompt_choose_for_bench = Text("Choose a card for your bench.", textPromptFont, DISPLAYSURF, 20, 725)
def terminate():
pygame.quit()
sys.exit()
def drawPlayMat():
DISPLAYSURF.blit(playMat, (0,0))
DISPLAYSURF.blit(endButton, (775, 675))
def displayHand(hand, oppHand):
handCount = len(hand)
for card in range(handCount):
cardThumb = pygame.image.load(hand[card])
cardThumb = pygame.transform.scale(cardThumb, (50,80))
DISPLAYSURF.blit(cardThumb, playerHandPos[card])
handCount = len(hand)
oppHandCount = len(oppHand)
for card in range(oppHandCount):
cardThumb = pygame.image.load(oppHand[card])
cardThumb = pygame.transform.scale(cardThumb, (50,80))
DISPLAYSURF.blit(cardThumb, oppHandPos[card])
pygame.display.update()
def updateHandImages():
tempHand = hand
finTempHand = {}
newTempHand = [pygame.image.load(t) for t in tempHand]
for i in range(0,len(newTempHand)):
finTempHand[i] = newTempHand[i]
return finTempHand
def drawPhase():
drawHand = hand
drawDeck = deck
oppDrawHand = oppHand
oppDrawDeck = oppDeck
drawHand.append(drawDeck[0])
drawHand.append(drawDeck[1])
oppDrawHand.append(oppDrawDeck[0])
oppDrawHand.append(oppDrawDeck[1])
for cards in range(2):
del drawDeck[0]
del oppDrawDeck[0]
return drawHand, drawDeck, oppDrawHand, oppDrawDeck
##def benchPhase():
## benchHand = hand
## benchDeck = deck
## benchBench = []
##
## .append(deck[0])
## for cards in range(2):
## del drawDeck[0]
## return drawHand, drawDeck
#game loop
while True:
# defining image indexes
currentHandImageIndex = None
endButtonIndex = None
mouse_pos = pygame.mouse.get_pos()
#game loop variables
hand = playerHand
oppHand = oppGetHand
deck = playerDeck
oppDeck = oppGetDeck
benchHand = []
oppBench = []
postHand = []
oppPost = []
perimeterHand = []
oppPerimet = []
showHand = displayHand(hand, oppHand)
handImages = updateHandImages()
#stage variables
step = defStep
isDrawPhase = False
isBenchPhase = False
isRecruitPhase = False
isAttackPhase = False
#text_prompt_choose_for_bench.draw_text()
endButtonPressed = False
mouseClick = False
#text drawing
if len(step) == 1:
isDrawPhase = True
text_prompt_enter_draw_phase.draw_text()
if len(step) == 2:
isBenchPhase = True
text_prompt_choose_for_bench.draw_text()
#defining exit protocol and mouse click
for event in pygame.event.get():
if event.type == QUIT:
terminate()
if event.type == KEYUP:
if event.key == K_ESCAPE:
terminate()
if event.type == MOUSEBUTTONUP:
mouseClick = True
#getting card indexes for image loading
for i, rect in enumerate(HAND_RECTS):
if rect.collidepoint(mouse_pos):
currentHandImageIndex = i
break
for i, rect in enumerate(END_RECT):
if rect.collidepoint(mouse_pos):
endButtonIndex = i
break
#defining what happens on mouse click for images and defining when end button is clicked
if mouseClick == True:
if endButtonIndex is not None:
endButtonPressed = True
if currentHandImageIndex is not None:
if currentHandImageIndex <= len(hand)-1:
DISPLAYSURF.blit(handImages[currentHandImageIndex], (925,200))
#Draw Phase protocol
if isDrawPhase == True:
if endButtonPressed == True:
hand, deck, oppHand, oppDeck = drawPhase()[0], drawPhase()[1], drawPhase()[2], drawPhase()[3]
endButtonPressed = False
isDrawPhase = False
step.append(1)
drawPlayMat()
FPSCLOCK.tick(FPS)
pygame.display.update()
pygame.display.update()
先谢谢你,真的很感激。
答案 0 :(得分:0)
问题似乎出现在drawPhase()
函数的这一行:
hand, deck, oppHand, oppDeck = drawPhase()[0], drawPhase()[1], drawPhase()[2], drawPhase()[3]
如果你以这种方式索引,每次索引时都会再次调用该函数,导致2张牌被附加到牌组4次,而不是追加1次。
如果你想要存储在hand, deck, oppHand oppDeck
中的函数的四个输出值,你可以用这一行替换上面的代码行:
hand, deck, oppHand, oppDeck = drawPhase()
此行将所有返回值存储在变量hand, deck, oppHand, oppDeck
中,这样函数只调用一次。