python Blackjack图形游戏 - 我的卡片图片不会导入

时间:2016-04-12 01:51:57

标签: python blackjack

我正在制作一个二十一点游戏,但仍处于早期阶段。当我运行我的程序时,我收到错误 im = Image(Point(350, 250), "playingcards/" + suit + str(rank) + ".gif") NameError: name 'suit' is not defined 我很确定这是因为im = ___在我的def main()中:我的函数的一部分,即使在我的代码中定义了套装。

基本上似乎我的程序忽略了我的所有代码并直接进入def main(),这可能是我的一个排序错误,但有人可以解释它应该去哪里以便它读取所有我的主函数之前的类函数?

这是我的代码:

from random import *
from graphics import *

class PlayingCard:

    def __init__(self, rank, suit):
       self.rank = rank
       self.suit = suit

    def getRank (self):
        return self.rank

    def getSuit(self):
        return self.suit

    def BJValue (self):
        if self.rank == 'Ace':
            return 1
        elif self.rank == 'Jack' or self.rank == 'Queen' or self.rank == 'King':
            return 10
        else:
            return int(self.rank)

    def __str__(self):
        suit_name= {'S':'Spades', 'C':'Clubs', 'D':'Diamonds', 'H':'Hearts'}
        rank_name=[None, 'Ace','2','3','4','5','6','7','8','9','10','Jack','Queen','King']

        return '%s of %s' % (Card.rank_names[self.rank], Card.suit_names[self.suit])


from random import *
from playingcards import *

class Deck:

    def __init__(self):
        self.cards = []
        x = eval(input("How many cards shall be dealt?"))
        for suit in range(x):
            for rank in range(1, 14):
                card = Card(suit, rank)
                self.cards.append(card)

    def shuffle(self):
        random.shuffle(self.cards)


    def DealCard(self): 
        return self.deck.pop(0) 

    def CardsLeft(self): 
        return len(self.deck)

class Blackjack:

    def __init__(self, dHand=[], pHand=[]):
        self.dHand = PlayingCard
        self.pHand = PlayingCard
        self.deck = Deck()
        Deck.shuffle(self.Blackjack)

    def initDeal(self,gwin,xposD,yposD,xposP,yposP):
        self.DealCard.dHand = [2]
        self.DealCard.pHand = [2]
      #  PlayingCard.draw(win)

        dealtCard = self.deck.DealCard()

        suit = dealtCard.getSuit()
        rank = dealtCard.getRank()
        #import(file=image_



    def hit(self, gwin, xPos, yPos):
        print('hi')

    def evaluateHand(self, hand):
        print('hi')

    def dealerPlays(self, gwin, xPos, yPos):
        print('hi')



def main():
    win = GraphWin("BlackJack", 800, 800)
    im = Image(Point(350, 250), "playingcards/" + suit + str(rank) + ".gif")
    im.draw(win)

if __name__ == "__main__":
    main()

0 个答案:

没有答案