好吧,我有点卡在这里,我正在尝试创建一个用户可以选择游戏模式的游戏,并在此基础上将图片的资源拉出并将它们放入5x6网格,然后我必须交换图像 - 在一种游戏模式中 - 使所有相同的图像在一列中,而在另一种游戏模式中,所有相同颜色的图像都在一行中。我该如何修复此交换?我尝试使用变量card1_x,card1_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()