TypeError:只能将元组(不是“int”)连接到元组以及如何修复它

问题描述:

我每次运行使用Pygame在Python中编写的游戏时都会收到此错误消息。该错误信息是:TypeError:只能将元组(不是“int”)连接到元组以及如何修复它

Traceback (most recent call last): 
    File "/home/pi/Memory Puzzle_20171029.py", line 248, in <module> 
    main() 
    File "/home/pi/Memory Puzzle_20171029.py", line 58, in main 
    startGameAnimation(mainBoard) 
    File "/home/pi/Memory Puzzle_20171029.py", line 226, in startGameAnimation 
    revealBoxesAnimation(board, boxGroups) 
    File "/home/pi/Memory Puzzle_20171029.py", line 192, in revealBoxesAnimation 
    drawBoxCovers(board, boxesToReveal, coverage) 
    File "/home/pi/Memory Puzzle_20171029.py", line 182, in drawBoxCovers 
    left, top = leftTopCoordsOfBox(box[0], box[1]) 
    File "/home/pi/Memory Puzzle_20171029.py", line 144, in leftTopCoordsOfBox 
    left = boxx*(BOXSIZE + GAPSIZE) + XMARGIN 
TypeError: can only concatenate tuple (not "int") to tuple 

请帮帮忙,我在编程初学者,不知道在Python中的所有错误。如果你能提供你的知识并帮助我解决这个问题,我将不胜感激 谢谢!

这里是我的剧本,我的比赛

import random, pygame, sys 
from pygame.locals import * 

FPS = 30 
WINDOWWIDTH = 640 
WINDOWHEIGHT = 480 
REVEALSPEED = 8 
BOXSIZE = 40 
GAPSIZE = 10 
BOARDWIDTH = 10 
BOARDHEIGHT = 7 
assert (BOARDWIDTH * BOARDHEIGHT) % 2 == 0,'Board needs to have an even number of boxes for pairs of matches.' 
XMARGIN = int((WINDOWWIDTH - (BOARDWIDTH*(BOXSIZE + GAPSIZE)))/2) 
YMARGIN = int((WINDOWWIDTH - (BOARDHEIGHT*(BOXSIZE + GAPSIZE)))/2) 

GRAY = (100, 100, 100) 
NAVYBLUE = (60, 60, 100) 
WHITE = (255, 255, 255) 
RED = (255, 0, 0) 
GREEN = (0, 255, 0) 
BLUE = (0, 0,255) 
YELLOW = (255, 255, 0) 
ORANGE = (255, 255, 0) 
PURPLE = (255, 0, 255) 
CYAN = (0, 255, 255) 

BGCOLOR = NAVYBLUE 
LIGHTBGCOLOR = GRAY 
BOXCOLOR = WHITE 
HIGHLIGHTCOLOR = BLUE 

DONUT = 'donut' 
SQUARE = 'square' 
DIAMOND = 'diamond' 
LINES = 'lines' 
OVAL = 'oval' 

ALLCOLORS = (RED, GREEN, BLUE, YELLOW, ORANGE, PURPLE, CYAN) 
ALLSHAPES = (DONUT, SQUARE, DIAMOND, LINES, OVAL) 
assert len(ALLCOLORS) * len(ALLSHAPES)*2 >= BOARDWIDTH*BOARDHEIGHT, 'Board is too big for the number of shapes/colors defined.' 

def main(): 
    global FPSCLOCK, DISPLAYSURF 
    pygame.init() 
    FPSCLOCK = pygame.time.Clock() 
    DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT)) 

    mousex = 0 
    mousey = 0 
    pygame.display.set_caption('Memory Game') 

    mainBoard = getRandomizedBoard() 
    revealedBoxes = generateRevealedBoxesData(False) 

    firstSelection = None 

    DISPLAYSURF.fill(BGCOLOR) 
    startGameAnimation(mainBoard) 

    while True: 
     mouseClicked = False 

     DISPLAYSURF.fill(BGCOLOR) 
     drawBoard(mainBoard, revealedBoxes) 

     for event in pygame.event.get(): 
      if event.type == QUIT or (event.type == KEYUP and event.key == K_ESCAPE): 
       pygame.quit() 
       sys.exit() 
      elif event.type == MOUSEMOTION: 
       mousex, mousey = event.pos 
      elif event.type == MOUSEBUTTONUP: 
       mousex, mousey = event.pos 
       mouseClicked = True 

     boxx, boxy = getBoxAtPixel(mousex, mousey) 
     if boxx != None and boxy != None: 
      if not revealedBoxes[boxx][boxy]: 
       drawHighlightBox(boxx, boxy) 
      if not revealedBoxes[boxx, boxy] and mouseClicked: 
       revealBoxesAnimation(mainBoard, [(boxx, boxy)]) 
       revealedBoxes [boxx, boxy] = True 
       if fisrtSelection == None: 
        fisrtSelection = (boxx, boxy) 
       else: 
        icon1shape, icon1color = getShapeAndColor(mainBoard, firstSelection[0], firstselection[1]) 
        icon2shape, icon2color = getShapeAndColor(mainBoard, boxx, boxy) 

        if icon1shape != icon2shape or icon1color: 
         pygame.time.wait(1000) 
         coverBoxesAnimation(mainBoard, [(firstSelection[0], firstSelection[1]), (boxx, boxy)]) 
         revealedBoxes[firstSelection[0]][firstSelection[1]] = False 
         revealedBoxes[boxx][boxy] = False 
        elif hasWon(revealedBoxes): 
         gameWonAnimation(mainBoard) 
         pygame.time.wait(2000) 

         mainBoard = getRandomizedBoard() 
         revealedBoxes = generateRevealedBoxesData(False) 

         drawBoard(mainBoard, revealedBoxes) 
         pygame.display.update() 
         pygame.time.wait(1000) 

         startGameAnimation(mainBoard) 
        firstSelection = None 


       pygame.display.update() 
       FPSCLOCK.tick(FPS) 
def generateRevealedBoxesData(val): 
    revealedBoxes =[] 
    for i in range(BOARDWIDTH): 
     revealedBoxes.append([val] * BOARDHEIGHT) 
    return revealedBoxes 

def getRandomizedBoard(): 
    icons = [1] 
    for color in ALLCOLORS: 
     for shape in ALLSHAPES: 
      icons.append((shape, color)) 

    random.shuffle(icons) 
    numIconsUsed = int(BOARDWIDTH * BOARDHEIGHT/2) 
    icons = icons[:numIconsUsed] * 2 
    random.shuffle(icons) 

    board = [0] 
    for x in range(BOARDWIDTH): 
     column = [] 
     for y in range(BOARDHEIGHT): 
      column.append(icons[0]) 
      del icons[0] 
      board.append(column) 
      return board 

def splitIntoGroupsOf(groupSize, theList): 
    result = [] 
    for i in range (0, len(theList), groupSize): 
     result.append(theList[i:i +groupSize]) 
    return result 

def leftTopCoordsOfBox(boxx, boxy): 
    left = boxx*(BOXSIZE + GAPSIZE) + XMARGIN 
    top = boxy * (BOXSIZE + GAPSIZE) + YMARGIN 
    return (left, top) 
    return(left, top) 

def getBoxAtPixel(x, y): 
    for boxx in range(BOARDWIDTH): 
     for boxy in range(BOARDHEIGHT): 
      left,top = leftTopCoordsOfBox(boxx, boxy) 
      boxRect = pygame.Rect(left, top, BOXSIZE, BOXSIZE) 
      if boxRect.collidepoint(x, y): 
       return (boxx, boxy) 
    return(None, None) 

def drawIcon(shape, color, boxx, boxy): 
    quarter = int(BOXSIZE * 0.25) 
    half = int(BOXSIZE * 0.5) 

    left, top = leftTopCoordsOfBox(boxx, boxy) 
    if shape == DONUT: 
     pygame.draw.circle(DISPLAYSURF, color, (left + half, top + half), half - 5) 
     pygame.draw.circle(DISPLAYSURF, BGCOLOR, (left + half, top + half), quarter - 5) 
    elif shape == SQUARE: 
     pygame.draw.rect(DISPLAYSURF, color,(left + quarter, top + quarter, BOXSIZE - half, BOXSIZE - half)) 
    elif shape == DIAMOND: 
     pygame.draw.polygon(DISPLAYSURF, color,((left + half, top), (left + BOXSIZE - 1, top + half), (left + half, top + BOXSIZE - 1), (left, top + half))) 
    elif shape == LINES: 
     for i in range(0, BOXSIZE, 4): 
      pygame.draw.line(DISPLAYSURF, color, (left, top + i), (left + i, top)) 
      pygame.draw.line(DISPLAYSURF, color, (left + i, top + BOXSIZE - 1), (left + BOXSIZE - 1, top + i)) 
    elif shape == OVAL: 
     pygame.draw.ellipse(DISPLAYSURF, color, (left, top + quarter, BOXSIZE, half)) 

def getShapeAndColor(board, boxx, boxy): 
    return board[boxx][boxy][0], board[boxx][boxy][1] 

def drawBoxCovers(board, boxes, coverage): 
    for box in boxes: 
     left, top = leftTopCoordsOfBox(box[0], box[1]) 
     pygame.draw.rect(DISPLAYSURF, BGCOLOR, (left, top, BOXSIZE, BOXSIZE)) 
     shape, color = getShapeAndColor(board, box[0], box[1]) 
     drawIcon(shape, color, box[0], box[1]) 
     if coverage > 0: 
      pygame.draw.rect(DISPLAYSURF, BOXCOLOR, (left, top, coverage, BOXSIZE)) 
      pygame.display.update() 
      FPSCLOCK.tick(FPS) 
def revealBoxesAnimation(board, boxesToReveal): 
    for coverage in range(BOXSIZE, (-REVEALSPEED) - 1, - REVEALSPEED): 
     drawBoxCovers(board, boxesToReveal, coverage) 

def coverBoxesAnimation(board, boxesToReveal): 
    for coverage in range(BOXSIZE, (-REVEALSPEED) - 1, -REVEALSPEED): 
     drawBoxCovers(board, boxesToReveal, coverage) 


def drawBoard(board, revealed): 
    for boxx in range(BOARDWIDTH): 
     for boxy in range(BOARDHEIGHT): 
      left, top = leftTopCoordsOfBox(boxx, boxy) 
      if not revealed[boxx][boxy]: 
       pygame.draw.rect(DISPLAYSURF, BOXCOLOR, (left, top, BOXSIZE, BOXSIZE)) 
      else: 
        shape, color = getShapeAndColor(board, boxx, boxy) 
        drawIcon(shape, color, boxx, boxy) 


def drawHighlightBox(boxx, boxy): 
    left, top = leftTopCoordsOfBox(boxx, boxy) 
    pygame.draw.rect(DISPLAYSURF, HIGHLIGHTCOLOR, (left - 5, top - 5, BOXSIZE + 10, BOXSIZE + 10), 4) 


def startGameAnimation(board): 
    coveredBoxes = generateRevealedBoxesData(False) 
    boxes = [0] 
    for x in range(BOARDHEIGHT): 
     for y in range(BOARDHEIGHT): 
      boxes.append((x,y)) 
    random.shuffle(boxes) 
    boxGroups = splitIntoGroupsOf(8, boxes) 

    drawBoard(board, coveredBoxes) 
    for boxGroup in boxGroups: 
     revealBoxesAnimation(board, boxGroups) 
     coverBoxesAnimation(board, boxGroups) 

def gameWonAnimation(board): 
    coveredBoxes = generateRevealedBoxesData(True) 
    color1 = LIGHTBGCOLOR 
    color2 = BGCOLOR 

    for i in range(13): 
     color1, color2 = color2, color1 
     DISPLAYSURF.fill(color1) 
     drawBoard(board, coveredBoxes) 
     pygame.display.update() 
     pygame.time.wait(300) 

def hasWon(revealedBoxes): 
    for i in revealedboxes: 
     if False in i: 
      return False 
     return True 

if __name__ == '__main__': 
    main() 
+2

欢迎SO请到[这里](https://*.com/help/mcve)学习如何编写一个最小的问题。 – gommb

+1

这是一个非常复杂的程序,尝试作为初学者运行。当你有这样一个特定的错误时,也会发布太多的代码。正如gommb已经提到的那样,如果您将工作放在创建最小,完整和可验证示例的工作中,您将帮助自己和其他找到答案的人寻找答案。话虽如此:在'startGameAnimation()'中,在'boxGroups'内使用'boxGroup'而不是'boxGroups' for-loop - 这就是问题所在。 –

+1

请不要使用Python的“代码段”功能。它适用于将在浏览器中运行的HTML/CSS/JavaScript。 –

您尝试添加/串连一个元组的整数。

left = boxx*(BOXSIZE + GAPSIZE) + XMARGIN 

如果你想添加XMARGIN双方在元组boxx*(BOXSIZE + GAPSIZE)的元素,然后再去做这样boxx*(BOXSIZE + GAPSIZE) + (XMARGIN, XMARGIN)