NIM游戏,不知道如何进行

时间:2020-02-09 19:54:04

标签: python pygame

因此,我尝试使用pygame制作Nim游戏(您有3行纸牌,玩家必须让对手选择最后一张纸牌),但我不是pygame的专家,所以我不知道如何使用它。我设法做到了一点,但是我不知道如何从原始纸牌堆中删除玩家选择的纸牌并重画所有纸牌

import pygame
import time


pygame.init()


#Creating the Pygame window
X = 500
Y = 500
win = pygame.display.set_mode((X, Y))
pygame.display.set_caption('Last to Pick')

width = 65
height = 80
vel = 10

#Colors to Use
cool_blue = (20, 50, 70)
black = (0,0,0)
white = (255, 255, 255)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 128) 
bright_red = (255,0,0)
bright_green = (0,255,0)


#1st Row
A1 = pygame.Rect(180, 150, width, height)
A2 = pygame.Rect(260, 150, width, height)
#2nd Row
B1 = pygame.Rect(140, 250, width, height)
B2 = pygame.Rect(220, 250, width, height)
B3 = pygame.Rect(300, 250, width, height)
#3rd Row
C1 = pygame.Rect(60, 350, width, height)
C2 = pygame.Rect(140, 350, width, height)
C3 = pygame.Rect(220, 350, width, height)
C4 = pygame.Rect(300, 350, width, height)
C5 = pygame.Rect(380, 350, width, height)

cards = [ [A1, red], [A2, red], [B1, red], [B2, red], [B3, red], [C1, red], [C2, red], [C3, red], [C4, red], [C5, red] ]

bot_turn_first = False
bot_turn = False
player_turn = False
player_turn_finsihed = False

"GENERAL GAME FUNCTIONS"

def button(mouse,msg,x,y,w,h,ic,ac,action=None):
    click = pygame.mouse.get_pressed()    
    print(click)
    if x+w > mouse[0] > x and y+h > mouse[1] > y:
        pygame.draw.rect(win, ac,(x,y,w,h))
        if (click[0] or click[1] or click[2]) == 1 and action != None:
            action()   
    else:
        pygame.draw.rect(win, ic,(x,y,w,h))

    smallText = pygame.font.Font("freesansbold.ttf",20)
    textSurf, textRect = text_objects(msg, smallText, white)
    textRect.center = ( (x+(w/2)), (y+(h/2)) )
    win.blit(textSurf, textRect)
"GENERAL GAME FUNCTIONS END"

def quit_box():
    pygame.quit()


def intro_screen():
    run = True
    while run:
        win.fill(cool_blue)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
            if event.type == pygame.MOUSEMOTION:
                mouse = pygame.mouse.get_pos()
                font = pygame.font.Font('freesansbold.ttf', 50) 
                text = font.render('The Last To Pick', True, green, cool_blue) 
                textRect = text.get_rect()  
                textRect.center = (250, 200) 
                win.blit(text, textRect)

                button(mouse,'Start', 110, 350, 90, 50, black, green, start_loop)
                button(mouse,'Options', 220, 350, 90, 50, black, blue)
                button(mouse,'Quit', 330, 350, 90, 50, black, red, quit_box)

                pygame.display.update()


def bot_playing():
    print('I get the first move')
    bot_start_loop()

def player_playing():
    print('You get the first move')
    player_start_loop()

def checking_player_turn():
    global player_turn
    if player_turn == True:
        pass#playerturn()


def start_loop():
    run = True
    while run:
        win.fill(cool_blue)  
        #when 'x' is clicked game loop will quit
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
            if event.type == pygame.MOUSEMOTION:
                mouse2 = pygame.mouse.get_pos()
                button(mouse2, 'Bot can start', 300, 0, 200, 50, black, blue, bot_playing)
                button(mouse2, 'I want to start', 300, 60, 200, 50, black, blue, player_playing)

                for rect, color in cards:
                    pygame.draw.rect(win, color, rect)

                pygame.display.update()

def bot_start_loop():
    cards_bot1 = [
    [A1, red],
    [A2, red],
    #2nd Row
    [B1, red],
    [B2, red],
    [B3, red],
    #3rd Row
    [C1, red],
    ]

    run =  True
    while run:
        win.fill(cool_blue)  
        #when 'x' is clicked game loop will quit
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
            if event.type == pygame.MOUSEMOTION:
                mouse3 = pygame.mouse.get_pos()
                button(mouse3, 'I finshed my turn', 300, 0, 200, 50, black, blue)

                for rect, color, in cards_bot1:
                    pygame.draw.rect(win, color, rect)

                pygame.display.update()

            if event.type == pygame.MOUSEBUTTONUP:
                pos = pygame.mouse.get_pos()

                for rect, color in cards:
                    if rect.collidepoint(pos):
                        print('Player has selected card' + str(rect))

selected_cards_turn1 = []

def player_start_loop():
    global selected_cards_turn1
    run =  True
    while run:
        win.fill(cool_blue)  
        #when 'x' is clicked game loop will quit
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
            if event.type == pygame.MOUSEMOTION:
                mouse3 = pygame.mouse.get_pos()
                button(mouse3, 'I finshed my turn', 300, 0, 200, 50, black, blue, card_remover)

                for rect, color in cards:
                    pygame.draw.rect(win, color, rect)

                pygame.display.update()

            if event.type == pygame.MOUSEBUTTONUP:
                pos = pygame.mouse.get_pos()
                for rect, color in cards:
                    if rect.collidepoint(pos):
                        print('Player has selected card' + str(rect))
                        nrect = str(rect)
                        selected_cards_turn1.append(nrect)



def card_remover():
    global selected_cards_turn1
    print('Selected cards' + str(selected_cards_turn1))
    for element in selected_cards_turn1:
        if element == '<rect(180, 150, 65, 80)>':#A1
            A11 = 0
            exterminate(element, 0)
        if element == '<rect(260, 150, 65, 80)>':#A2
            A12 = 1
            exterminate(element, 1)
        if element == '<rect(140, 250, 65, 80)>':#B1
            B21 = 2
            exterminate(element, B21)
        if element == '<rect(220, 250, 65, 80)>':#B2
            B22 = 3
            exterminate(element, B22)
        if element == '<rect(300, 250, 65, 80)>':#B3
            B23 = 4
            exterminate(element, B23)
        if element == '<rect(60, 350, 65, 80)>':#C1
            C31 = 5
            exterminate(element, C31)    
        if element == '<rect(140, 350, 65, 80)>':#C2
            C32 = 6
            exterminate(element, C32)   
        if element == '<rect(220, 350, 65, 80)>':#C3
            C33 = 7
            exterminate(element, C33)  
        if element == '<rect(300, 350, 65, 80)>':#C4
            C34 = 8
            exterminate(element, C34)   
        if element == '<rect(380, 350, 65, 80)>':#C5
            C35 = 9
            exterminate(element, C35) 


def new_loop(new_car_list):
    print('New_card_list:' + str(new_car_list))     
    run = True
    while run:
        win.fill(cool_blue)  
        #when 'x' is clicked game loop will quit
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
            if event.type == pygame.MOUSEMOTION:
                mouse4 = pygame.mouse.get_pos()
                button(mouse4, 'I finshed my turn', 300, 0, 200, 50, black, blue, card_remover)

                for rect, color in new_car_list:
                    pygame.draw.rect(win, color, rect)

                pygame.display.update()

def exterminate(cards1, ele):
    cards11 = str(cards1) + '(255, 0, 0)'
    print('Current cards:' + str(cards))
    print("""

    """)
    new_set_of_cards = [ments for e in cards if card.pop(ele) != ele]
    print('Drawing this cards: ' + str(cards1))
    print("""


    """)
    print('New Set of Cards' +  str(new_set_of_cards))




intro_screen()
pygame.quit()

这是我不知道该怎么做的部分,所选卡未从列表中删除:

    def card_remover():
    global selected_cards_turn1
    print('Selected cards' + str(selected_cards_turn1))
    for element in selected_cards_turn1:
        if element == '<rect(180, 150, 65, 80)>':#A1
            A11 = 0
            exterminate(element, 0)
        if element == '<rect(260, 150, 65, 80)>':#A2
            A12 = 1
            exterminate(element, 1)
        if element == '<rect(140, 250, 65, 80)>':#B1
            B21 = 2
            exterminate(element, B21)
        if element == '<rect(220, 250, 65, 80)>':#B2
            B22 = 3
            exterminate(element, B22)
        if element == '<rect(300, 250, 65, 80)>':#B3
            B23 = 4
            exterminate(element, B23)
        if element == '<rect(60, 350, 65, 80)>':#C1
            C31 = 5
            exterminate(element, C31)    
        if element == '<rect(140, 350, 65, 80)>':#C2
            C32 = 6
            exterminate(element, C32)   
        if element == '<rect(220, 350, 65, 80)>':#C3
            C33 = 7
            exterminate(element, C33)  
        if element == '<rect(300, 350, 65, 80)>':#C4
            C34 = 8
            exterminate(element, C34)   
        if element == '<rect(380, 350, 65, 80)>':#C5
            C35 = 9
            exterminate(element, C35) 

def exterminate(cards1, ele):
    cards11 = str(cards1) + '(255, 0, 0)'
    print('Current cards:' + str(cards))
    print("""

    """)
    new_set_of_cards = [ments for e in cards if card.pop(ele) != ele]
    print('Drawing this cards: ' + str(cards1))
    print("""


    """)
    print('New Set of Cards' +  str(new_set_of_cards))

我知道这段代码看起来确实很糟糕,但是如果有人可以帮助我,我将不胜感激。关于如何使其更好的任何其他反馈绝对有帮助。

谢谢!

0 个答案:

没有答案