python - I would like to shuffle these variables then combine them in another function to print images to the screen -


i'm following tutorial , own code different, thought easier if i'd show original code in english. original code uses 7 colors , 5 shapes, have 7 colors , 5 shapes, have them pictures though original code drew shape , colored it. made variables shape or color name example square = "square" red = "red". uppercase variables equal string name of color or shape.

the problem nothing happening , window not responding when run game.

def getrandomizedboard():     # list of every possible shape in every possible color.     icons = []     c in (punane, roheline, sinine, must, kollane, cyan, lilla):         s in (nurk, viisnurk, ring, ruut, syda):             icons.append( (s, c) )      # decide how many icons use, shuffle list , truncate it.     random.shuffle(icons)     numiconsused = int(cols * rows / 2)     icons = icons[:numiconsused] * 2 # going need pairs of icons      # create board data structure.     board = []     x in range(cols):         columns = []         y in range(rows):             randomindex = random.randint(0, len(icons) - 1)             columns.append(icons[randomindex])             del icons[randomindex]         board.append(columns)     return board 

function draw shape changed blit images.

def drawshape(shape, color, boxx, boxy):     left, top = lefttopofbox(boxx, boxy)     if shape == nurk:         ekraan.blit(color+nurk+".png")     elif shape == viisnurk:         ekraan.blit(color+viisnurk+".png")     elif shape == ring:         ekraan.blit(color+ring+".png")     elif shape == ruut:         ekraan.blit(color+ruut+".png")     elif shape == syda:         ekraan.blit(color+syda+".png") 

here's original:

# memory # http://inventwithpython.com # al sweigart al@inventwithpython.com  import random import time import pygame import sys pygame.locals import *  fps = 30 windowwidth = 640 windowheight = 480 revealspeed = 8 cols = 10 rows = 6 boxsize = 40 gapsize = 10  darkgray = (60, 60, 60) white = (255, 255, 255) red = (255, 0, 0) green = (0, 255, 0) blue = (0, 0, 255) yellow = (255, 255, 0) orange = (255, 128, 0) purple = (255, 0, 255) cyan = (0, 255, 255)  bgcolor = darkgray boxcolor = white  donut = 1 square = 2 diamond = 3 lines = 4 oval = 5  def main():     global mainclock, mainsurf     pygame.init()     mainclock = pygame.time.clock()     mainsurf = pygame.display.set_mode((windowwidth, windowheight))      mousex = 0     mousey = 0     pygame.display.set_caption('memory')      mainboard = getrandomizedboard()     revealedboxes = generaterevealedboxesdata(false)      firststep = true     firstselection = none      mainsurf.fill(bgcolor)     startgameanimation(mainboard)      # main game loop:     while true:         clicked = false          # draw board.         mainsurf.fill(bgcolor)         drawboard(mainboard, revealedboxes)          # handle events.         event in pygame.event.get():             if event.type == quit or (event.type == keyup , event.key == k_escape):                 pygame.quit()                 sys.exit()             if event.type == mousemotion:                 mousex, mousey = event.pos             if event.type == mousebuttonup:                 mousex, mousey = event.pos                 clicked = true           boxx, boxy = isoverbox(mousex, mousey)         if boxx != none , boxy != none:             # mouse on box.              highlightbox(boxx, boxy)              if clicked , not revealedboxes[boxx][boxy]:                 revealboxesanimation(mainboard, [(boxx, boxy)], revealspeed)                 #unrevealboxesanimation(mainboard, [(boxx, boxy)], revealspeed)                 revealedboxes[boxx][boxy] = true                  if firststep:                     firstselection = (boxx, boxy)                     firststep = false                 else:                     # check if there match.                     shape1, color1 = getshapeandcolor(mainboard, firstselection[0], firstselection[1])                     shape2, color2 = getshapeandcolor(mainboard, boxx, boxy)                      if shape1 != shape2 or color1 != color2:                         # icons don't match. unreveal both selections.                         time.sleep(1)                         unrevealboxesanimation(mainboard, [(firstselection[0], firstselection[1]), (boxx, boxy)], revealspeed)                         revealedboxes[firstselection[0]][firstselection[1]] = false                         revealedboxes[boxx][boxy] = false                     elif haswon(revealedboxes):                         gamewonanimation(mainboard)                         time.sleep(2)                          # reset board                         mainboard = getrandomizedboard()                         revealedboxes = generaterevealedboxesdata(false)                          # show unrevealed board second.                         drawboard(mainboard, revealedboxes)                         pygame.display.update()                         time.sleep(1)                          # replay start game animation.                         startgameanimation(mainboard)                     firststep = true          # redraw screen , wait clock tick.         pygame.display.update()         mainclock.tick(fps)  def generaterevealedboxesdata(val):     datastruct = []     c in range(cols):         datastruct.append([val] * rows)     return datastruct   def splitintogroupsof(groupsize, thelist):     result = []     in range(0, len(thelist), groupsize):         result.append(thelist[i:i+groupsize])     return result   def startgameanimation(board):     fakerevealedboxes = generaterevealedboxesdata(false)     boxes = []     x in range(cols):         y in range(rows):             boxes.append( (x, y) )     random.shuffle(boxes)      groups = splitintogroupsof(8, boxes)      g in groups:         drawboard(board, fakerevealedboxes)         revealboxesanimation(board, g, revealspeed)         unrevealboxesanimation(board, g, revealspeed)   def gamewonanimation(board):     global bgcolor, boxcolor     fakerevealedboxes = generaterevealedboxesdata(true)      in range(14):         bgcolor, boxcolor = boxcolor, bgcolor          mainsurf.fill(bgcolor)         drawboard(board, fakerevealedboxes)         pygame.display.update()         time.sleep(0.3)   def haswon(revealed):     in revealed:         if false in i:             return false     return true   def getshapeandcolor(board, boxx, boxy):     return board[boxx][boxy][0], board[boxx][boxy][1]   def revealboxesanimation(board, boxes, speed):     # "box reveal" animation.     in range(boxsize, -speed - 1, -speed):         b in boxes:             drawboxcover(board, b, i)         pygame.display.update()         mainclock.tick(fps)   def unrevealboxesanimation(board, boxes, speed):     # "box cover" animation.     in range(0, boxsize, speed):         b in boxes:             drawboxcover(board, b, i)         pygame.display.update()         mainclock.tick(fps)  def drawboxcover(board, b, coverage):     """both revealboxesanimation() , unrevealboxesanimation() exact same thing inside nested loops, instead of copying , pasting code twice, put code in own function , call function twice. getting rid of duplicated code way idea, because if want change code later (say, if find bug in it), have change in 1 place instead of multiple places. makes our program shorter , easier read."""     left, top = lefttopofbox(b[0], b[1])     pygame.draw.rect(mainsurf, bgcolor, (left, top, boxsize, boxsize))     shape, color = getshapeandcolor(board, b[0], b[1])     drawshape(shape, color, b[0], b[1])     if coverage > 0:         pygame.draw.rect(mainsurf, boxcolor, (left, top, coverage, boxsize))  def getrandomizedboard():  # memory # http://inventwithpython.com # al sweigart al@inventwithpython.com  import random import time import pygame import sys pygame.locals import *  fps = 30 windowwidth = 640 windowheight = 480 revealspeed = 8 cols = 10 rows = 6 boxsize = 40 gapsize = 10  darkgray = (60, 60, 60) white = (255, 255, 255) red = (255, 0, 0) green = (0, 255, 0) blue = (0, 0, 255) yellow = (255, 255, 0) orange = (255, 128, 0) purple = (255, 0, 255) cyan = (0, 255, 255)  bgcolor = darkgray boxcolor = white  donut = 1 square = 2 diamond = 3 lines = 4 oval = 5  def main():     global mainclock, mainsurf     pygame.init()     mainclock = pygame.time.clock()     mainsurf = pygame.display.set_mode((windowwidth, windowheight))      mousex = 0     mousey = 0     pygame.display.set_caption('memory')      mainboard = getrandomizedboard()     revealedboxes = generaterevealedboxesdata(false)      firststep = true     firstselection = none      mainsurf.fill(bgcolor)     startgameanimation(mainboard)      # main game loop:     while true:         clicked = false          # draw board.         mainsurf.fill(bgcolor)         drawboard(mainboard, revealedboxes)          # handle events.         event in pygame.event.get():             if event.type == quit or (event.type == keyup , event.key == k_escape):                 pygame.quit()                 sys.exit()             if event.type == mousemotion:                 mousex, mousey = event.pos             if event.type == mousebuttonup:                 mousex, mousey = event.pos                 clicked = true           boxx, boxy = isoverbox(mousex, mousey)         if boxx != none , boxy != none:             # mouse on box.              highlightbox(boxx, boxy)              if clicked , not revealedboxes[boxx][boxy]:                 revealboxesanimation(mainboard, [(boxx, boxy)], revealspeed)                 #unrevealboxesanimation(mainboard, [(boxx, boxy)], revealspeed)                 revealedboxes[boxx][boxy] = true                  if firststep:                     firstselection = (boxx, boxy)                     firststep = false                 else:                     # check if there match.                     shape1, color1 = getshapeandcolor(mainboard, firstselection[0], firstselection[1])                     shape2, color2 = getshapeandcolor(mainboard, boxx, boxy)                      if shape1 != shape2 or color1 != color2:                         # icons don't match. unreveal both selections.                         time.sleep(1)                         unrevealboxesanimation(mainboard, [(firstselection[0], firstselection[1]), (boxx, boxy)], revealspeed)                         revealedboxes[firstselection[0]][firstselection[1]] = false                         revealedboxes[boxx][boxy] = false                     elif haswon(revealedboxes):                         gamewonanimation(mainboard)                         time.sleep(2)                          # reset board                         mainboard = getrandomizedboard()                         revealedboxes = generaterevealedboxesdata(false)                          # show unrevealed board second.                         drawboard(mainboard, revealedboxes)                         pygame.display.update()                         time.sleep(1)                          # replay start game animation.                         startgameanimation(mainboard)                     firststep = true          # redraw screen , wait clock tick.         pygame.display.update()         mainclock.tick(fps)  def generaterevealedboxesdata(val):     datastruct = []     c in range(cols):         datastruct.append([val] * rows)     return datastruct   def splitintogroupsof(groupsize, thelist):     result = []     in range(0, len(thelist), groupsize):         result.append(thelist[i:i+groupsize])     return result   def startgameanimation(board):     fakerevealedboxes = generaterevealedboxesdata(false)     boxes = []     x in range(cols):         y in range(rows):             boxes.append( (x, y) )     random.shuffle(boxes)      groups = splitintogroupsof(8, boxes)      g in groups:         drawboard(board, fakerevealedboxes)         revealboxesanimation(board, g, revealspeed)         unrevealboxesanimation(board, g, revealspeed)   def gamewonanimation(board):     global bgcolor, boxcolor     fakerevealedboxes = generaterevealedboxesdata(true)      in range(14):         bgcolor, boxcolor = boxcolor, bgcolor          mainsurf.fill(bgcolor)         drawboard(board, fakerevealedboxes)         pygame.display.update()         time.sleep(0.3)   def haswon(revealed):     in revealed:         if false in i:             return false     return true   def getshapeandcolor(board, boxx, boxy):     return board[boxx][boxy][0], board[boxx][boxy][1]   def revealboxesanimation(board, boxes, speed):     # "box reveal" animation.     in range(boxsize, -speed - 1, -speed):         b in boxes:             drawboxcover(board, b, i)         pygame.display.update()         mainclock.tick(fps)   def unrevealboxesanimation(board, boxes, speed):     # "box cover" animation.     in range(0, boxsize, speed):         b in boxes:             drawboxcover(board, b, i)         pygame.display.update()         mainclock.tick(fps)  def drawboxcover(board, b, coverage):     """both revealboxesanimation() , unrevealboxesanimation() exact same thing inside nested loops, instead of copying , pasting code twice, put code in own function , call function twice. getting rid of duplicated code way idea, because if want change code later (say, if find bug in it), have change in 1 place instead of multiple places. makes our program shorter , easier read."""     left, top = lefttopofbox(b[0], b[1])     pygame.draw.rect(mainsurf, bgcolor, (left, top, boxsize, boxsize))     shape, color = getshapeandcolor(board, b[0], b[1])     drawshape(shape, color, b[0], b[1])     if coverage > 0:         pygame.draw.rect(mainsurf, boxcolor, (left, top, coverage, boxsize))  def getrandomizedboard():     # list of every possible shape in every possible color.     icons = []     c in (red, green, blue, yellow, orange, purple, cyan):         s in (donut, square, diamond, lines, oval):             icons.append( (s, c) )      # decide how many icons use, shuffle list , truncate it.     random.shuffle(icons)     numiconsused = int(cols * rows / 2)     icons = icons[:numiconsused] * 2 # going need pairs of icons      # create board data structure.     board = []     x in range(cols):         columns = []         y in range(rows):             randomindex = random.randint(0, len(icons) - 1)             columns.append(icons[randomindex])             del icons[randomindex]         board.append(columns)     return board   def lefttopofbox(boxx, boxy):     # see how big margins each side.     xmargin = int((windowwidth - (cols * (boxsize + gapsize))) / 2)     ymargin = int((windowheight - (rows * (boxsize + gapsize))) / 2)     left = boxx * (boxsize + gapsize) + xmargin     top = boxy * (boxsize + gapsize) + ymargin     return (left, top)   def drawboard(board, revealed):     boxx in range(cols):         boxy in range(rows):             left, top = lefttopofbox(boxx, boxy)             if not revealed[boxx][boxy]:                 # draw covered box.                 pygame.draw.rect(mainsurf, boxcolor, (left, top, boxsize, boxsize))             else:                 # draw icon.                 shape, color = getshapeandcolor(board, boxx, boxy)                 drawshape(shape, color, boxx, boxy)   def isoverbox(x, y):     boxx in range(cols):         boxy in range(rows):             left, top = lefttopofbox(boxx, boxy)             boxrect = pygame.rect(left, top, boxsize, boxsize)             if boxrect.collidepoint(x, y):                 return (boxx, boxy)     return (none, none)   def highlightbox(boxx, boxy):     left, top = lefttopofbox(boxx, boxy)     pygame.draw.rect(mainsurf, blue, (left - 5, top - 5, boxsize + 10, boxsize + 10), 4)   def drawshape(shape, color, boxx, boxy):     quarter = int(boxsize * 0.25)     half = int(boxsize * 0.5)      left, top = lefttopofbox(boxx, boxy)     if shape == donut:         pygame.draw.circle(mainsurf, color, (left + half, top + half), half - 5)         pygame.draw.circle(mainsurf, bgcolor, (left + half, top + half), quarter - 5)     elif shape == square:         pygame.draw.rect(mainsurf, color, (left + 10, top + 10, boxsize - 20, boxsize - 20))     elif shape == diamond:         pygame.draw.polygon(mainsurf, color, ((left + half, top), (left + boxsize, top + half), (left + half, top + boxsize), (left, top + half)))     elif shape == lines:         in range(0, boxsize, 4):             pygame.draw.line(mainsurf, color, (left, top + i), (left + i, top))             pygame.draw.line(mainsurf, color, (left + i, top + boxsize), (left + boxsize, top + i))     elif shape == oval:         pygame.draw.ellipse(mainsurf, color, (left, top + quarter, boxsize, half))   if __name__ == '__main__':     main() 

i believe issue may not loading images correctly. blitting name of file name, while want send blit pygame opened image. here how that:

instead of

ekraan.blit(color+nurk+".png") 

maybe work

ekraan.blit(pygame.image.load(color+nurk+".png").convert_alpha())          #i assuming shapes have transparent regions if not case not need convert_alpha part, convert() should fine 

this may problem. alternative solution, if working these shapes lot, use sprites , renderupdates group, practical if utilizing many of features of sprite object.

another problem perhaps more issue don't see updating screen. after drawing things on screen, call:

pygame.display.update() 

you may display optional argument specific rectangle update screen in, useful speed program making update necessary regions of screen. however, if not specify arguments, alternatively use

pygame.display.flip() 

which exact same thing not accept arguments, while other 1 has optional arguments.


Comments

Popular posts from this blog

php - mySql Join with 4 tables -

css - Text drops down with smaller window -

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -