- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我每次运行使用Pygame用Python编写的游戏时,都会不断收到此错误消息。错误消息是:
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
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()
最佳答案
您正在尝试将元组添加/连接为整数。
left = boxx*(BOXSIZE + GAPSIZE) + XMARGIN
XMARGIN
添加到元组
boxx*(BOXSIZE + GAPSIZE)
中的两个元素中,则可以通过这种方式
boxx*(BOXSIZE + GAPSIZE) + (XMARGIN, XMARGIN)
关于python - TypeError : can only concatenate tuple (not “int” ) to tuple and how to fix it,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47107665/
在OOCalc中,我想使用CONCATENATE函数为A列中的每个字符串添加引号。 所以在单元格 B1 中我想做: =CONCATENATE("\"",A1,"\"") OOCalc 不喜欢这样,或者
我对此进行了编码,当我运行它时,它向我显示一条消息,变量“Number”与其余串联“大于5”之间的“+”中存在问题 这是代码 fun main(args: Array) { print("En
我正在尝试连接多个 dask 数据帧,但这会导致我的所有 RAM 都用完并使我的环境 (Google Colab) 崩溃。 我曾尝试与 Dask 连接,因为我听说 Dask 会对文件进行分区,以便更容
常规语言在串联下是封闭的 - 这可以通过使一种语言的接受状态以 epsilon 过渡到下一种语言的开始状态来证明。 如果我们考虑语言 L = {a^n | n >=0},这种语言是正则的(就是一个*)
有什么方法可以跨多行使用 | 字符串运算符? 使用经典的 CONCATENATE 标记,您可以进行如下分配: CONCATENATE 'A rubber duck is a toy shaped li
我有一个表单,其中包含许多格式为 name="field-1" name="field-2" name="field-3" name="field-4" 等等.... 在表单操作页面上,我希望能够使用
我们如何连接动态工作区的字段?这个想法在下面的代码中: LOOP AT lt_final INTO DATA(ls_final). CONCATENATE ls_final-field1
我需要将符号从 4 位数字扩展到 32 位数字。 我尝试像这样重复 MSB 28 次: assign x={28'b{a[3]},a[3:0]}; 但是,我得到一个错误: Syntax error n
我的数据如下所示: ColumnName PrimaryKey 1 ID Y 2 JOB_NAME N 3 JOB_DES
我试图用 Perl6 连接一个字符串,因此: my $cmd = "databricks jobs --job-id 37 --notebook-params '\{"; put $cmd; $cmd
我是 verilog 的初学者。 几乎所有的连接示例如下。 wire [3:0] result; reg a, b, c, d; result = {a, b, c, d}; 以下也可以吗? wir
我正在尝试在 VHDL 上实现它: a<=(b+c)/16; 这个我试过了,但是synthesis不接受。 signal b,c : std_logic_vector(7 downto 0); s
我很难理解 Verilog 中的以下语法: input [15:0] a; // 16-bit input output [31:0] result; // 32-bit output a
假设我有一个占位符 ph_input = tf.placeholder(dtype=tf.int32, [无, 1]) 和一个向量 h = tf.zeros([1,2], dtype=tf.int32
假设我有 ceylon 字符串列表。 (不一定是 List ;它可以是可迭代对象、序列、数组等)将所有这些字符串连接成一个字符串的最佳方法是什么? 最佳答案 最有效的解决方案是使用静态方法String
这个问题已经有答案了: Why does Java's concat() method not do anything? (6 个回答) 已关闭 8 年前。 我找到了这段代码 public class
似乎预处理器在连接有符号数字的 token 时添加了一个空格。 我试过这个: #define DECL_FL(IE) 1e##IE##f float val[] = { DECL_FL(12)
我正在实现一个基于作者的图书搜索功能。我应该返回一个查询结果,其中包含查询作者撰写的所有书籍。但是,对某些作者姓名的查询可能会返回多个结果(例如,查询“Smith, W”可能会匹配“Smith, We
我正在尝试将多个单元格添加在一起,并且在它们之间,我需要一个 IF 公式。我基本上是根据我拥有的列表制作 HTML 输出,我需要检查列中是否有“事件”或“终止”,并根据该列表选择要使用的 html 类
通常,当您链接到另一个单元格时,您会返回该单元格的内容。 我想做的是这样的: =HYPERLINK(CONCATENATE("=C:/documents/'[",B15,".xls]Sheet 1'!
我是一名优秀的程序员,十分优秀!