Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. '''
  2. MatchingGame.py
  3. 10/10/19
  4. Recreate the matching game/memory using the graphics.py library
  5. Mr. Sharick
  6. '''
  7.  
  8. import math, random, time
  9. from graphics import *
  10.  
  11. '''
  12. Randomly genrate an NxN grid filled with card values.
  13. Setup the graphics window.
  14. '''
  15. def setup():
  16. global win, grid, NUM_OF_PAIRS, SQ_SIZE, numbers
  17. NUM_OF_PAIRS = 6
  18. dimension = math.ceil(math.sqrt(NUM_OF_PAIRS*2))
  19. windowSize = 600
  20. SQ_SIZE = windowSize // dimension #// - integer division (truncates answer)
  21. #same as int(SQ_SIZE)
  22. #1.) Make a list of numbers from 1 to NUM_OF_PAIRS with 2 of each value.
  23. numbers = []
  24. for i in range(NUM_OF_PAIRS): #[0, 1, 2, ... Pairs -1]
  25. numbers.append(i+1)
  26. numbers.append(i+1)
  27. #numbers.extend([i+1, i+1])
  28. #2.) Shuffle that list
  29. random.shuffle(numbers)
  30. #3.) Deal the list of numbers into the grid
  31. grid = []
  32. for r in range(dimension): #for each row
  33. grid.append([]) #add empty row
  34. for c in range(dimension): #each col in that row
  35. if len(numbers) != 0: #still cards to deal
  36. grid[r].append(numbers.pop(0)) #deals one card
  37. else:
  38. grid[r].append(0) #zero is not a card
  39. win = GraphWin("Matching Game", windowSize, windowSize, autoflush=False)
  40. win.setBackground("yellow")
  41. drawBoard()
  42. '''
  43. Draw the NxN grid using the graphics library. A positive number is hidden, a negative number is revealed. Zero draws nothing.
  44. '''
  45. def drawBoard():
  46. #draw rectangles that are SQ_SIZE x SQ_SIZE for each non-zero spot in the grid
  47. for r in range(len(grid)): #rows
  48. for c in range(len(grid[0])): #columns
  49. value = grid[r][c]
  50. if value != 0:
  51. p1 = Point(c*SQ_SIZE, r*SQ_SIZE)
  52. p2 = Point((c+1)*SQ_SIZE, (r+1)*SQ_SIZE)
  53. rect = Rectangle(p1, p2)
  54. rect.setFill("white")
  55. rect.draw(win)
  56. if value < 0: #revealed card
  57. p3 = Point((c+0.5)*SQ_SIZE, (r+0.5)*SQ_SIZE)
  58. text = Text(p3, str(-value))
  59. text.setTextColor("blue")
  60. text.setSize(20)
  61. text.draw(win)
  62.  
  63. '''
  64. Basic game logic
  65. '''
  66. def main():
  67. setup()
  68. gameOver = False
  69. while not gameOver:
  70. drawBoard()
  71. #wait for user to click mouse
  72. mousePoint = win.getMouse()
  73. mouseX = mousePoint.getX()
  74. mouseY = mousePoint.getY()
  75. #how to get row and col from this mouseX and mouseY
  76. col = int((mouseX // SQ_SIZE))
  77. row = int((mouseY // SQ_SIZE))
  78. firstPickedRow = row
  79. firstPickedCol = col
  80. grid[firstPickedRow][firstPickedCol] *= -1
  81. firstPickedNumber = grid[firstPickedRow][firstPickedCol] #This number is already negative
  82.  
  83. mousePoint = win.getMouse()
  84. mouseX = mousePoint.getX()
  85. mouseY = mousePoint.getY()
  86. col = int((mouseX // SQ_SIZE))
  87. row = int((mouseY // SQ_SIZE))
  88. secondPickedRow = row
  89. secondPickedCol = col
  90. grid[secondPickedRow][secondPickedCol] *= -1
  91. drawBoard()
  92. secondPickedNumber = grid[secondPickedRow][secondPickedCol]
  93. if firstPickedNumber != secondPickedNumber:
  94. grid[firstPickedRow][firstPickedCol] *= -1
  95. grid[secondPickedRow][secondPickedCol] *= -1
  96.  
  97.  
  98. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement