Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- MatchingGame.py
- 10/10/19
- Recreate the matching game/memory using the graphics.py library
- Mr. Sharick
- '''
- import math, random, time
- from graphics import *
- '''
- Randomly genrate an NxN grid filled with card values.
- Setup the graphics window.
- '''
- def setup():
- global win, grid, NUM_OF_PAIRS, SQ_SIZE, numbers
- NUM_OF_PAIRS = 6
- dimension = math.ceil(math.sqrt(NUM_OF_PAIRS*2))
- windowSize = 600
- SQ_SIZE = windowSize // dimension #// - integer division (truncates answer)
- #same as int(SQ_SIZE)
- #1.) Make a list of numbers from 1 to NUM_OF_PAIRS with 2 of each value.
- numbers = []
- for i in range(NUM_OF_PAIRS): #[0, 1, 2, ... Pairs -1]
- numbers.append(i+1)
- numbers.append(i+1)
- #numbers.extend([i+1, i+1])
- #2.) Shuffle that list
- random.shuffle(numbers)
- #3.) Deal the list of numbers into the grid
- grid = []
- for r in range(dimension): #for each row
- grid.append([]) #add empty row
- for c in range(dimension): #each col in that row
- if len(numbers) != 0: #still cards to deal
- grid[r].append(numbers.pop(0)) #deals one card
- else:
- grid[r].append(0) #zero is not a card
- win = GraphWin("Matching Game", windowSize, windowSize, autoflush=False)
- win.setBackground("yellow")
- drawBoard()
- '''
- Draw the NxN grid using the graphics library. A positive number is hidden, a negative number is revealed. Zero draws nothing.
- '''
- def drawBoard():
- #draw rectangles that are SQ_SIZE x SQ_SIZE for each non-zero spot in the grid
- for r in range(len(grid)): #rows
- for c in range(len(grid[0])): #columns
- value = grid[r][c]
- if value != 0:
- p1 = Point(c*SQ_SIZE, r*SQ_SIZE)
- p2 = Point((c+1)*SQ_SIZE, (r+1)*SQ_SIZE)
- rect = Rectangle(p1, p2)
- rect.setFill("white")
- rect.draw(win)
- if value < 0: #revealed card
- p3 = Point((c+0.5)*SQ_SIZE, (r+0.5)*SQ_SIZE)
- text = Text(p3, str(-value))
- text.setTextColor("blue")
- text.setSize(20)
- text.draw(win)
- '''
- Basic game logic
- '''
- def main():
- setup()
- gameOver = False
- while not gameOver:
- drawBoard()
- #wait for user to click mouse
- mousePoint = win.getMouse()
- mouseX = mousePoint.getX()
- mouseY = mousePoint.getY()
- #how to get row and col from this mouseX and mouseY
- col = int((mouseX // SQ_SIZE))
- row = int((mouseY // SQ_SIZE))
- firstPickedRow = row
- firstPickedCol = col
- grid[firstPickedRow][firstPickedCol] *= -1
- firstPickedNumber = grid[firstPickedRow][firstPickedCol] #This number is already negative
- mousePoint = win.getMouse()
- mouseX = mousePoint.getX()
- mouseY = mousePoint.getY()
- col = int((mouseX // SQ_SIZE))
- row = int((mouseY // SQ_SIZE))
- secondPickedRow = row
- secondPickedCol = col
- grid[secondPickedRow][secondPickedCol] *= -1
- drawBoard()
- secondPickedNumber = grid[secondPickedRow][secondPickedCol]
- if firstPickedNumber != secondPickedNumber:
- grid[firstPickedRow][firstPickedCol] *= -1
- grid[secondPickedRow][secondPickedCol] *= -1
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement