lỗi ở dòng gần cuối ấy :D, ko dịch được thì google dịch nhá
mấy bro chỉ giúp em để em sửa cho chương trình chạy với
def main(): global FPSCLOCK,DISPLAYSURF pygame.init() FPSCLOCK = pygame.time.Clock() DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH,WINDOWHEIGHT)) mousex = 0 moussy = 0 pygame.display.set_caption(‘Memory Game’) mainBoard = getRandomizedBoard() revealedBoxes = generaterevealedBoxesData(False) firstSelection = None DISPLAYSURF.fill(BGCOLOR) startGameAnimation(mainBoard) while True: mouseClicked = False DISPLAYSUF.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,mosey)
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)]) reavealedBoxes[boxx][boxy] = True if firstSelection == None : firstSelection = (boxx, boxy) else : icon1shape,icon2color = getShapeAndColor(mainBoard,firstSelection[0], firstSelection[1]) icon2shape, icon2color = getShapeAndColor(mainBoard,boxx, boxy) if icon1shape !=icon2shape or icon1clor != icon2colr: 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) startGaameAnimation(mainBoard) firstSelection = None pygame.display.update() FPSCLOCK.tick(FPS) def generateRevealedBoxesData(val) : revealedBoxes = [] for i in range(BOARDWITH): revealedBoxes.append([val] * BOARDHEIGHT) return revealedBoxes def getRandomizedBoard(): icons = [] for color in ALLCOLORS : for shape in ALLSHAPERS :
icons.append(shape,color) random.shuffle(icons) numIconsUsed = int(BOARDWIDTH * BOARDHEIGHT / 2) icons = icons[:numIconsUsed] *2 random.shuffle(icons) board = [] for x in range (BOARDWIDTH) : column = [] for y in range(BOARDHEIGHT): column.append(icons[0]) del incons[0] board.append(column) return board def splitIntoGroupsof(groupSiza, 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) def getBoxAtPixel(x, y) : for boxx in range(BOARDWIDTH) : for boxy in range(BOARDHEIGHT) : left, top = leftTopcoordOfBox(boxx, boxy) boxRect = pygame.Rect(left, top ,BOXSIZE,BOXSIZE) if boxRet.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,color,(left + quarter,top + half),quarter - 5) elif shape == SQUARE: pygame.draw.rect(DISPLAYSURF,color,((left +quater,top + quater,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 + 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, top + quaater, BOXSIZE ,half)) def getShapeAndColor(board,boxx,boxy) : return board[boxx][boxy][0],board[boxx][boxy][1] def drawBoxcovers(board, boxes,coverrage) : for box in boxes : left , top = leftTopCoordOfBox(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(DISPLAUSURF, 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 , boxesToCover) : for coverafe in range(0, BOXSIZE + REVEALSPEED , REVEALSPEED) : drawBoxCovers(board, boxesToCover, 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) pygaem.draw.rect(DISPLAYSURF, HIGHLIGHTCOLOR,(left - 5 , top - 5 , BOXSIZE +10 , BOXSIZE + 10),4) def startGameAnimation(board): coveredBoxes = generateRevealedboxesDate(False) boxes = [] for x in range(BOARDWIDTH): 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, boxGroup) coverBoxesAnimation(board, boxGroup) def gameWonAnimation(board): coveredBoxes = generateRevealedBoxesData(True) color1 = LIGHTBGCOLOR color2 = BGCOLOR for i in range(13): color1, color2 = color2,color1 DISPLAYSURF.fill(color1) drawBoard(booard, 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()
mainBoard = getRandomizedBoard()
khai báo hàm getRandomizedBoard() trước khi gọi
Hữu Hải
0 chủ đề
0 bài viết
lỗi ở dòng gần cuối ấy :D, ko dịch được thì google dịch nhá
mấy bro chỉ giúp em để em sửa cho chương trình chạy với
def main():
global FPSCLOCK,DISPLAYSURF
pygame.init()
FPSCLOCK = pygame.time.Clock()
DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH,WINDOWHEIGHT))
mousex = 0
moussy = 0
pygame.display.set_caption(‘Memory Game’)
mainBoard = getRandomizedBoard()
revealedBoxes = generaterevealedBoxesData(False)
firstSelection = None
DISPLAYSURF.fill(BGCOLOR)
startGameAnimation(mainBoard)
while True:
mouseClicked = False
DISPLAYSUF.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
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)])
reavealedBoxes[boxx][boxy] = True
if firstSelection == None :
firstSelection = (boxx, boxy)
else :
icon1shape,icon2color = getShapeAndColor(mainBoard,firstSelection[0], firstSelection[1])
icon2shape, icon2color = getShapeAndColor(mainBoard,boxx, boxy)
if icon1shape !=icon2shape or icon1clor != icon2colr:
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)
startGaameAnimation(mainBoard)
firstSelection = None
pygame.display.update()
FPSCLOCK.tick(FPS)
def generateRevealedBoxesData(val) :
revealedBoxes = []
for i in range(BOARDWITH):
revealedBoxes.append([val] * BOARDHEIGHT)
return revealedBoxes
def getRandomizedBoard():
icons = []
for color in ALLCOLORS :
for shape in ALLSHAPERS :
def revealBoxesAnimation(board,boxesToReveal) :
for coverage in range(BOXSIZE,(-REVEALSPEED) - 1 , - REVEALSPEED) :
drawBoxCovers(board, boxesToReveal,coverage)
def coverBoxesAnimation(board , boxesToCover) :
for coverafe in range(0, BOXSIZE + REVEALSPEED , REVEALSPEED) :
drawBoxCovers(board, boxesToCover, 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)
pygaem.draw.rect(DISPLAYSURF, HIGHLIGHTCOLOR,(left - 5 , top - 5 , BOXSIZE +10 , BOXSIZE + 10),4)
def startGameAnimation(board):
coveredBoxes = generateRevealedboxesDate(False)
boxes = []
for x in range(BOARDWIDTH):
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, boxGroup)
coverBoxesAnimation(board, boxGroup)
def gameWonAnimation(board):
coveredBoxes = generateRevealedBoxesData(True)
color1 = LIGHTBGCOLOR
color2 = BGCOLOR
for i in range(13):
color1, color2 = color2,color1
DISPLAYSURF.fill(color1)
drawBoard(booard, coveredBoxes)
pygame.display.update()
pygame.time.wait(300)
def hasWon(revealedBoxes):
if name == ‘main’:
main()
mainBoard = getRandomizedBoard()
khai báo hàm getRandomizedBoard() trước khi gọi