我可以以某种方式将其导入另一个文件并与pygame结合吗?

时间:2019-06-27 21:00:29

标签: pygame python-3.7

我已经在python 3中编写了一个二十一点脚本,我认为id在进入GUI之前已经弄清了游戏逻辑。 但是对GUI和Pygame的经验很少,我似乎无法使其正常工作。 在这个阶段是否有办法将Pygame纳入我的项目,还是我应该从头开始考虑Pygame?

    from random import shuffle

# define the card ranks, and suits
ranks = [i for i in range(2, 11)] + ['JACK', 'QUEEN', 'KING', 'ACE']
suits = ['SPADE', 'HEART', 'DIAMOND', 'CLUB']
## card class makes cards and assigns suits and ranks to them
def how_many_decks():
    print(' how many decks would you like the new shoe to be?')
    num_of_decks = input('please type a digit betwee 1 and 6!')
    while True:
        if not num_of_decks.isdigit():
            num_of_decks = input('please type a digit betwee 1 and 6!')
        elif int(num_of_decks) == 0:
            print ('You cannot play with no cards!')
            num_of_decks = input('please type a digit betwee 1 and 6!')
        elif int(num_of_decks) > 6:
            print("you can onkly jhave a maximum of 6 decks!")
            num_of_decks = input('please type a digit betwee 1 and 6: ')
        else:
            return int(num_of_decks)

class card (object):

    def __init__(self,suit,rank):
        self.suit = suit
        self.rank = rank
    def show(self):
        print('{} of {}'.format(self.rank, self.suit))
## child class of card, also assigns a blackjack score to each card        
class bjcard(card):

    def __init__(self,suit,rank):
        card.__init__(self,suit,rank)
        if type(self.rank) == int:
            self.value = rank
        elif self.rank == 'ACE':
            self.value = 11
        else:
            self.value = 10
    def showval(self):
        card.show(self)
        print('card value is: ', self.value)


#creates a deck of 52 cards from card class
class deck:

    def __init__(self):
        self.cards = []
        for i in suits:
            for j in ranks:
                self.cards.append(card(i,j))
    def show(self):
        for card in self.cards:
            card.show()
    def shuffle(self):
        shuffle(self.cards)
#creates a deck of 52 cards from bjcard class   

class bjdeck():

    def __init__(self,numberofdecks=1):
        self.cards = []
        for h in range(numberofdecks):
            for i in suits:
                for j in ranks:
                    self.cards.append(bjcard(i,j))
        self.shuffle()
    def show(self):
        for card in self.cards:
            card.showval()

    def __len__(self):
        return len(self.cards)
    def shuffle(self):
        shuffle(self.cards)
    def reshuffle(self):
        print('Reshuffling the deck!')

        self.__init__(int(num_of_decks))


class hand:
    def __init__(self,bet,name=None):
        self.bet = bet
        self.bust = False
        self.stood = False
        self.has_black_jack = False
        print('your cards are:')
        self.contents = []
        self.deal(2)
        if self.total==21:
            self.has_black_jack = True
    def deal(self,x):
        global bjdd
        if len(bjdd) < x:
            bjdd.reshuffle()
        for j in range (x):
            self.contents.append(bjdd.cards.pop(0))
            self.calctotalscore()
            self.total = sum(i.value for i in self.contents)
        self.show()
    def show(self):
        for card in self.contents:
            card.showval()

        print ('your hand total is: ' + str(sum(i.value for i in self.contents)))

    def calctotalscore(self):
        listofaces=[]
        for i in self.contents:
                if i.value == 11:
                    listofaces.append(self.contents.index(i))
        for ace in listofaces:
            if 32 > sum(i.value for i in self.contents) > 21:
                self.contents[ace].value = 1

    def totalscore(self):             
        return sum(i.value for i in self.contents) 

    def makedecision(self):
        hitorstand = ''
        while hitorstand not in ('h' , 'H' , 's' , 'S'):
            self.show()
            hitorstand = input('enter "H" to hit or "S" to stand: ')
        return hitorstand


    def playhand(self):
        if self.totalscore() == 21:
            return 'well done! you got BlackJack!'
        while self.totalscore() < 21:
            decision = self.makedecision()
            if decision in('h', 'H') :
                self.deal(1)
                if self.totalscore() == 21:
                    print('well done! you hit 21!')
                    return
                elif self.totalscore() > 21:
                    self.bust=True
                    return str('bad luck!, your hand is bust as your score of /n' + str(self.totalscore())+ 'is above 21, you lose!')

            elif decision in ('s', 'S'):
                self.stood = True
                return ('your final hand score is: ' + str(self.totalscore())+ ' good luck!')
    def newhand(self,x):
        self.__init__(x)  

class dealerhand(hand):

    def __init__(self):

        self.has_black_jack = False
        self.bust = False
        self.name = 'Dealer'
        self.contents = []
        self.deal(2)
        print("The Dealer's first card is: ") 
        self.show_one_card()
        print('\n')
    def deal(self,x):
        global bjdd
        if len(bjdd) < x:
            bjdd.reshuffle()

        for j in range (x):
            self.contents.append(bjdd.cards.pop(0)) 
            self.calctotalscore()
            self.total = sum(i.value for i in self.contents)   
    def show_one_card(self):
        self.contents[0].showval()

    def show(self):
        for card in self.contents:
            card.showval()

        print ("Dealer's total is: " + str(sum(i.value for i in self.contents)))


    def calctotalscore(self):
        listofaces=[]
        for i in self.contents:
                if i.value == 11:
                    listofaces.append(self.contents.index(i))
        for ace in listofaces:
            if 32 > sum(i.value for i in self.contents) > 21:
                self.contents[ace].value = 1

    def totalscore(self): 
        self.calctotalscore() 
        self.total = sum(i.value for i in self.contents)             
        return sum(i.value for i in self.contents)         


    def dealer_plays(self):
        if self.total==21:
            self.has_black_jack = True
        self.show()
        while 1 < self.total<17:
            self.deal(1)
            self.show()
        if 17 > self.total<22:
            print('dealers score is: ',self.total)
        elif self.total >21:
            print('Dealer Busts!')
            self.bust = True


    def payout_time(self):
        pass

## default playernames
defualtnames=['Player 0','Player 1','Player 2','Player 3','Player 4','Player 5','Player 6','Player 7','Player 8','Player 9']    
class player:
    def __init__(self,bankroll,name=None):
        if name == None:
            if len (defualtnames)>0:
                self.name = defualtnames.pop(0)
        else:
            self.name=name
        self.bankroll = bankroll
        print('Hello',self.name, ', your starting balance is:',self.bankroll)

    def isin(self):
        bet = input(str(self.name)+', Please enter your bet amount in digits: ')
        while True:
            if not bet.isdigit():
                bet = input(str(self.name)+'Please enter your bet in digits!')
            elif int(bet) ==0:
                self.bet = 0
                print ('OK, you are sitting this hand out')
                return
            elif int(bet) > self.bankroll:
                print("your account only has $"+str(self.bankroll))
                bet = input('\n please enter a bet amount lower than, or equal to your account: ')
            else:

                self.bet = int(bet)
                self.bankroll -= int(bet)
                self.hand = hand(int(bet))
                return

##to workout and show the player(s) with highest score
def gethiscorers(x):
    hiscore = max(x.values())
    hiscorers = []
    for i in x:
        if x[i] == hiscore:
            hiscorers.append(i)
    if len(hiscorers)>1:
        print('the players with the highest score are: ')
        print(str( hiscorers).strip('[').strip(']').strip("'").strip("'"))
        print('with a score of: ', hiscore)
    else:
        print('the player with the highest score is: ',hiscorers[0])
        print('with a score of: ', hiscore)
bjdd = bjdeck()
#deals 2 cards to dealer and shows the first 1 to the player(s)!
## start a hand with a bet of 200 and deal 2 cards to the player!
#player_name = player(1000,'Troy')
#player_name.isin()
#D_hand = dealerhand()

第二个文件是:

print('Welcome To My Blackjack Game!')
from BLACKJACK import *
def howmanycardsleft():
    print(len(bjdd))
def set_players():
    global a
    # start six hands and a dealer
    a = player(1000,'Troy')
    #b = player(1000,'Troy B')
    #c = player(1000,'BABBY!')
    #d = player(1000,'Troy c')
    #e= player(1000,'Troy d')
    #f= player(1000,'Troy e')
    #g= player(1000,'Troy f')
    #h= player(1000,'Troy g')
    #i= player(1000,'Troy h')



#print('\n')
#gethiscorers(playerscores)
def begin_game():
    global players_in 
    global dealer
    global players_left
    players_left = []
    players_in = [] 
    for player in players_playing:
        print(player.name,' your account has $',player.bankroll,':')
        player.isin()
        if player.bet > 0:
            players_in.append(player)
            players_left.append(player)
    print('\n')
    if len(players_in) == 0:
        return 'Please start game with at least 1 player!'
    dealer = dealerhand()
    for playing_player in players_in:
        print(playing_player.name,"'s turn")
        playing_player.hand.playhand()

        if playing_player.hand.has_black_jack:
            if not dealer.has_black_jack:
                print(playing_player.name, 'BLACKJACK! You Win!')
                playing_player.bankroll += int(playing_player.bet * 2.5)    
                players_left.remove(playing_player)
        elif playing_player.hand.bust:
            print(playing_player.name, 'You Lose')
            players_left.remove(playing_player)
    if len(players_left) > 0:
        print('\n')
        dealer.dealer_plays()
    else:
        end_game()
    end_game()



def end_game():
    print('\n') 

    if dealer.has_black_jack:
        for player in players_left:
            if player.hand.has_black_jack:
                print(player.name,'dealer also has blackjack!, you draw!')
                player.bankroll += player.bet
                players_left.remove(player)

            else:
                print('Dealer has  Bad luck',player.name)
                players_left.remove(player)

    for player in players_left:
        if dealer.bust:
            print('DEALER BUSTS!')
            print(player.name, 'You Win')
            player.bankroll+= player.bet * 2
        elif player.hand.total < dealer.total:
            print(player.name, 'You Lose')
        elif player.hand.total > dealer.total:
            print(player.name, 'You Win')
            player.bankroll += player.bet * 2
        elif player.hand.total == dealer.total:
            print(player.name, 'You Draw')
            player.bankroll += player.bet

    for broke_players in players_playing:
        if broke_players.bankroll == 0:
               players_playing.remove(broke_players)


    if sum([players.bankroll for players in players_playing]) >0:
        begin_game()
    else:
        print('Thanks for playing!')
        input('press any button to exit game!')
        return


##make list of player scores
#playerscores = {}
#for i in players_playing:
#    playerscores[i.name] = (i.hand.total

if __name__ == '__main__':
    #make list of hands for players in play
    num_of_decks = how_many_decks()
    set_players()
    players_playing = [a]

    begin_game()

0 个答案:

没有答案