有一個6x5的格子,需要能夠點擊兩張圖片並交換它們

2024-10-01 22:37:51 发布

您现在位置:Python中文网/ 问答频道 /正文

好吧,我有点困在这里,我试图创建一个游戏,用户可以选择一个游戏模式,并在此基础上,它把图片的资源,并把他们放到一个5x6网格,然后我必须交换图像-在一个游戏模式中-以便所有相同的图像都在一列中,而在另一个游戏模式中,所有相同颜色的图像都在一行中。如何修复此交换?我试着使用变量card1\u x,card1\u y等等。。以及定义的交换函数是什么

#List of white cards
whitecards = ["shape1.png", "shape2.png", "shape3.png", "shape4.png", "shape5.png", "shape6.png"]

#variables to keep track of where the user clicked
box_x = -1
box_y = -1

selected_x = -1
selected_y = -1

#variables to keep track of mouse clicks
first_click = False
second_click = False

def colorlessBoard():
    # one dimensional list
    icons = []
    for card in whitecards:
        icons.append(card)
        icons.append(card)
        icons.append(card)
        icons.append(card)
        icons.append(card)
    random.shuffle(icons)

    # two dimensional list
    board = []
    for x in range(grid_height):
        column = []
        for y in range(grid_width):
            column.append(icons[0])
            del icons[0]
        board.append(column)
    return board

colorlessboard = colorlessBoard()

def swap(box_x, box_y, selected_x, selected_y):
    temp = mycards[box_x * grid_width + box_y]
    print (temp)
    mycards[box_x * grid_width + box_y] = mycards[selected_x * grid_width +selected_y]
    mycards[selected_x * grid_width + selected_y] = temp

##def swap(card1_x, card1_y, card2_x, card2_y):
##    temp = whitecards[card1_x * grid_width + card1_y]
##    whitecards[card1_x * grid_width + card1_y] = whitecards[card2_x * grid_width + card2_y]
##    whitecards[card2_x * grid_width +card2_y] = temp


            rowindex = 0
            for cardlist in colorlessboard:
                colindex = 0
                for card in cardlist:
                    imgx = colindex * box_width + play_lcorner_x
                    imgy = rowindex * box_width + play_lcorner_y
                    myimg = pygame.image.load(cardlist[colindex])
                    pygame.draw.rect(dispWindow, black, (imgx,imgy,box_width, box_width),2)
                    dispWindow.blit(myimg, (imgx, imgy))
                    colindex += 1
                rowindex += 1
                pygame.display.update()
pygame.display.update()

Tags: inbox游戏forpngcardwidthtemp

热门问题