Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

Python implementacija shtihead kartaške igre

[es] :: Python :: Python implementacija shtihead kartaške igre

[ Pregleda: 2956 | Odgovora: 0 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

cickozg

Član broj: 298871
Poruke: 3
*.dsl.iskon.hr.



Profil

icon Python implementacija shtihead kartaške igre28.08.2013. u 17:03 - pre 128 meseci
Pozdrav ljudi, radim na vlastitoj implementaciji shithead igre (http://en.wikipedia.org/wiki/Shithead_(card_game)). Radim preko tkinter modula, ako netko ima bolji prijedlog rado ću poslušati savjet. Imam nekoliko pitanja koje me muče za sada. Pa krenimo. Za početak me zanima jeli bolje da za svaku igračevu ruku kreiram novu klasu ili ih sve pod jednu utrpam (kao što sam napravio)? Postavio sam u kodu da se nacrta i dek karata ukoliko ima karata u njemu, no to se ne događa, zašto?

Ako netko ima iskustva u ovakvim igrama, zanima me i kako je najbolje riješiti problematiku klikanja karti ?


Ovo je moj kod za sada, budući da sam početnik vjerovatno će vam biti dosta nepregledan.

Code:
from Tkinter import *
from PIL import Image, ImageTk
import random

#Velicina ekrana
HEIGHT = 960
WIDTH = 1280


#Dimenzije karata
CARD_SIZE = (73, 98)
CARD_BACK_SIZE = (71, 96)

#Koordinate pozicija
PLAYING_POS = (WIDTH/2,HEIGHT/2)
DECK_POS = (WIDTH-100,HEIGHT/2)

PLAYER_POS = (WIDTH/4,HEIGHT-150)
KOMP_POS = (WIDTH/4,200)
KOMP2_POS = (130,HEIGHT/4)
KOMP3_POS = (WIDTH-130,HEIGHT/4)

PLAYER_POS2 = (WIDTH/2,HEIGHT-100)
KOMP_POS2 = (WIDTH/4,100)
KOMP2_POS2 = (80,HEIGHT/4)
KOMP3_POS2 = (WIDTH-80,HEIGHT/4)

PLAYER_POS3 = (WIDTH/2,HEIGHT-100)
KOMP_POS3 = (WIDTH/4,100)
KOMP2_POS3 = (80,HEIGHT/4)
KOMP3_POS3 = (WIDTH-80,HEIGHT/4)

#Karte
SUITS = ['C', 'S', 'H', 'D']
RANKS = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
VALUES = {'2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, 'J':10, 'Q':11, 'K':12, 'A':13, '10':14}

#Ostale varijable
klik=0
counter=4
thrown_cards=[]


class Cards:
    def __init__(self,suit,rank):
        self.suit = suit
        self.rank = rank
        self.karta = None
  
    def get_suit(self):
        return self.suit
    
    def get_rank(self):
        return self.rank

    def __str__(self):
        return self.suit + self.rank
    
    def draw(self,position):
        CARD = Image.open ("cards.png")
        box = [RANKS.index(self.rank) * CARD_SIZE[0], SUITS.index(self.suit) * CARD_SIZE[1], CARD_SIZE[0] * (RANKS.index(self.rank)+1) , CARD_SIZE[1] * (SUITS.index(self.suit)+1)]
        cropped = CARD.crop(box)
        self.karta = ImageTk.PhotoImage(cropped)
        canvas.create_image(position, image=self.karta)

    def draw_back(self,position):    
        CB=Image.open("card_back.png")
        self.karta = ImageTk.PhotoImage(CB)
        canvas.create_image(position, image=self.karta)
        

class Deck:
    def __init__(self):
        self.deck_list = []
        # standard 52 card deck
        global SUITS
        global RANKS
        for s in SUITS:
            for r in RANKS:
                self.deck_list.append(Cards(s, r))            

    def shuffle(self):
        return random.shuffle(self.deck_list)

    def deal_card(self):
        return self.deck_list.pop(0)
    
    def draw(self):
        for c in self.deck_list:
            c.draw_back(DECK_POS)
 
    

class Hand:
    def __init__(self ):
        self.card_list=[]
        self.card_list2=[]
        self.card_list3=[]
    
    def add_card(self,card):
        self.card_list.append(card)
        game_deck.draw()
    def add_card2(self,card):
        self.card_list2.append(card)
    def add_card3(self,card):    
        self.card_list3.append(card)

    def choose_card(self):
        global VALUES,izbor
        self.choices=[]
        if len(self.card_list) > 0:
            for c in self.card_list:
                if ( VALUES.get(c.get_rank()) >= thrown_cards[-1] ) or c.get_rank()== "2":
                    self.choices.append(c)
            izbor = random.choice(self.choices)
        
        elif len(self.card_list) == 0 and len(self.card_list2) > 0:
            for c in self.card_list:
                if VALUES.get(c.get_rank()) >= thrown_cards[-1] or c.get_rank()== "2":
                    self.choices.append(c)
            izbor = random.choice(choices)
      
        elif len(self.card_list) == 0 and len(self.card_list2) == 0 and len(self.card_list3) > 0:
            izbor = random.choice(self.card_list3)
            self.choices.append(izbor)

     

    def throw_card(self):
        global thrown_cards
        if len(self.choices) == 0 :
            self.card_list.append(thrown_cards)
            thrown_cards=[]
            
        elif len(self.choices) == 1 and ( choices[0].get_rank() < thrown_cards[-1] or choices[0].get_rank() != 2):
            thrown_cards.append(izbor)
            self.card_list.append(thrown_cards)
            thrown_cards=[]
            
        else:
            is_valid(izbor)
            if self.card_list > 0:
                self.card_list.pop(self.card_list.index(izbor))
            elif self.card_list == 0 and self.card_list2 >0:
                self.card_list2.pop(self.card_list2.index(izbor))
            else:
                self.card_list3.pop(self.card_list3.index(izbor))
    
    def draw(self,position):
        pos = list(position)
        for c in self.card_list:
                c.draw(pos)
                pos[0] += CARD_SIZE[0]/3
                
    def draw2(self,position):
        pos = list(position)
        for c in self.card_list3:
                c.draw(pos)
                pos[0] += CARD_SIZE[0]/3

    def draw3(self,position):
        pos = list(position)
        for c in self.card_list2:
                c.draw3(pos)
                pos[0] += CARD_SIZE[0]/3
                

    

 
def click(event):
    global klik,counter
    klik = (event.x-PLAYER_POS[0]-36) //(CARD_SIZE[0]//3)
    if (len(player_hand.card_list) == 0) and (len(player_hand.card_list2) == 0) and (HEIGHT-148 < event.y < HEIGHT-52):
        is_valid(player_hand.card_list3[klik])
        if valid:
            player_hand.card_list3.pop(klik)
            player_hand.draw2(PLAYER_POS3)

    elif (len(player_hand.card_list) == 0) and (len(player_hand.card_list2) > 0) and (HEIGHT-148 < event.y < HEIGHT-52):
        is_valid(player_hand.card_list2[klik])
        if valid:
            player_hand.card_list2.pop(klik)
            player_hand.draw3(PLAYER_POS2)

    elif (HEIGHT-198 < event.y < HEIGHT-102):
        is_valid(player_hand.card_list[klik])
        if valid:
            player_hand.card_list.pop(klik)
            player_hand.draw(PLAYER_POS)

    if len(player_hand.card_list) < 3 and len(game_deck.deck_list)>0:
        player_hand.add_card(game_deck.deal_card())
        player_hand.draw(PLAYER_POS)
    counter +=1
    root.after(1500,turn)    
    

def start():
    global game_deck,player_hand,komp_hand,komp2_hand,komp3_hand,card_list,thrown_cards
    canvas.delete(ALL)
    #Shuffle the deck
    game_deck = Deck()
    game_deck.shuffle()
    
    #Create players hands
    player_hand = Hand()
    komp_hand = Hand()
    komp2_hand = Hand()
    komp3_hand = Hand()
    thrown_cards=[]
        
    #add three cards to each hand
    player_hand.add_card(game_deck.deal_card())
    player_hand.add_card(game_deck.deal_card())
    player_hand.add_card(game_deck.deal_card())

    player_hand.add_card2(game_deck.deal_card())
    player_hand.add_card2(game_deck.deal_card())
    player_hand.add_card2(game_deck.deal_card())

    player_hand.add_card3(game_deck.deal_card())
    player_hand.add_card3(game_deck.deal_card())
    player_hand.add_card3(game_deck.deal_card())
    
    komp_hand.add_card(game_deck.deal_card())
    komp_hand.add_card(game_deck.deal_card())
    komp_hand.add_card(game_deck.deal_card())

    komp_hand.add_card2(game_deck.deal_card())
    komp_hand.add_card2(game_deck.deal_card())
    komp_hand.add_card2(game_deck.deal_card())    

    komp_hand.add_card3(game_deck.deal_card())
    komp_hand.add_card3(game_deck.deal_card())
    komp_hand.add_card3(game_deck.deal_card())
    
    komp2_hand.add_card(game_deck.deal_card())
    komp2_hand.add_card(game_deck.deal_card())
    komp2_hand.add_card(game_deck.deal_card())

    komp2_hand.add_card2(game_deck.deal_card())
    komp2_hand.add_card2(game_deck.deal_card())
    komp2_hand.add_card2(game_deck.deal_card())    

    komp2_hand.add_card3(game_deck.deal_card())
    komp2_hand.add_card3(game_deck.deal_card())
    komp2_hand.add_card3(game_deck.deal_card())    
    
    komp3_hand.add_card(game_deck.deal_card())
    komp3_hand.add_card(game_deck.deal_card())
    komp3_hand.add_card(game_deck.deal_card())
    
    komp3_hand.add_card2(game_deck.deal_card())
    komp3_hand.add_card2(game_deck.deal_card())
    komp3_hand.add_card2(game_deck.deal_card())    

    komp3_hand.add_card3(game_deck.deal_card())
    komp3_hand.add_card3(game_deck.deal_card())
    komp3_hand.add_card3(game_deck.deal_card())    
    
    #Draw players card
    player_hand.draw(PLAYER_POS)
    player_hand.draw2(PLAYER_POS2)
    komp_hand.draw(KOMP_POS2) 
    komp2_hand.draw(KOMP2_POS2) 
    komp3_hand.draw(KOMP3_POS2)

    
def turn():
    global counter
    canvas.delete(ALL)
    if counter % 4 == 1:
        komp_hand.choose_card()
        komp_hand.throw_card()
        if len(game_deck.deck_list) > 0:
            komp_hand.add_card(game_deck.deal_card())
        counter += 1
        
    elif counter % 4 == 2:
         komp2_hand.choose_card()
         komp2_hand.throw_card()
         if len(game_deck.deck_list) > 0:
             komp2_hand.add_card(game_deck.deal_card())
         counter += 1
        
    elif counter %4 == 3:
         komp3_hand.choose_card()
         komp3_hand.throw_card()
         if len(game_deck.deck_list) > 0:
             komp3_hand.add_card(game_deck.deal_card())
         counter += 1
    root.after(1500,turn)

def is_valid(card):
    global VALUES,thrown_cards,valid
    if len(thrown_cards)>0:
        if VALUES.get(card.get_rank()) >= thrown_cards[-1] or card.get_rank()== 2:
            card.draw([640,480])
            thrown_cards.append(card)
            valid = True
        else:
            message = "Not a valid card !!"
            valid = False
    else:
        card.draw([640,480])
        thrown_cards.append(card)
        valid = True      

root=Tk()
root.bind("<Button-1>",click)

canvas=Canvas(root,height=HEIGHT,width=WIDTH)
canvas.pack()

menubar=Menu(root)
filemenu=Menu(menubar,tearoff=0)
filemenu.add_command(label="New Game", command = start)
menubar.add_cascade(label="File",menu=filemenu)
    
root.config(menu=menubar)
kajla = Game
root.mainloop()
 
Odgovor na temu

[es] :: Python :: Python implementacija shtihead kartaške igre

[ Pregleda: 2956 | Odgovora: 0 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.