How to create a memory puzzle game with Python and Pygame (#004)

 In the previous tutorial, we have created a list of coordinates of all green square (be aware, coordinates are squares' center points). For we just need to draw all the green squares on the screen.

def initialize_board(square_pos):
      # draw all the squares on the screen
      for i in range(6):
            for j in range(6):
                  pygame.draw.rect(
                        screen, # surface to draw upon
                        (0,255,0), # square fill color
                        (square_pos[i][j][0]-25, square_pos[i][j][1]-25, 50, 50))# square rect

First, we create a function initialize_board() which takes the square_pos argument that we get from initialize_drawing_pos() function. 

Second, we need a nested for loop to draw 36 green squares on the screen surface.

pygame.draw.rect(
                 screen, # surface to draw upon
                 (0,255,0), # square fill color
                 (square_pos[i][j][0]-25, square_pos[i][j][1]-25, 50, 50))# square rect

As you may have notice that, 

(square_pos[i][j][0]-25, square_pos[i][j][1]-25, 50, 50)

square_pos[i][j] will return the square's center point coordinates (tuple of two elsements eg. (170, 90)), but we need the top-left corner point to draw our square, so we minus 25 from both its x-coordinate and y-coordinate.

The code we have written so for should look like:

# puzzle game created by pete
# it is free to redistribute or modify the source code

import pygame, sys, random
from pygame.locals import *

def board(patterns):
      # create the data structure for the game board
      board = []
      c = 0
      for i in range(6):
            board.append([])
            for j in range(6):
                  board[i].append(patterns[c])
                  c += 1
      return board

def initialize_drawing_pos(start_p):
      # create the drawing coordinates data structure
      # coresponding to the game board
      # the starting drawing pos is (145, 65)
      # the square is a 50*50 pixels
      # the space between each boxes is 10 pixels
      starting_pos = start_p
      step = 60
      square_pos = []
      for i in range(6):
            square_pos.append([])
            for j in range(6):
                  square_pos[i].append((starting_pos[0]+25+60*j, starting_pos[1]+25+60*i))
      return square_pos

def initialize_board(square_pos):
      # draw all the squares on the screen
      for i in range(6):
            for j in range(6):
                  pygame.draw.rect(
                        screen, # surface to draw upon
                        (0,255,0), # square fill color
                        (square_pos[i][j][0]-25, square_pos[i][j][1]-25, 50, 50))# square rect


pygame.init()

SCREEN_SIZE = (640,480)

# main screen set up
screen = pygame.display.set_mode(SCREEN_SIZE)
pygame.display.set_caption('Memory Puzzle Game')

# color setup
WHITE = (255,255,255)
screen.fill(WHITE)

patterns = [chr(i) for i in range(65,65+18)]*2
random.shuffle(patterns)
      

starting_pos = (145,65)
board = board(patterns)
square_pos = initialize_drawing_pos(starting_pos)
initialize_board(square_pos)

# set backgroud color to white
while True:
      
      for event in pygame.event.get():
            if event.type == QUIT:
                  pygame.quit()
                  sys.exit()                  

      

      pygame.display.update()

      

Just run the code, the output should look like this:

In the next tutorial, we are gonna develop the game loop logic.

Comments

Popular posts from this blog

How to write a slide puzzle game with Python and Pygame (2020 tutorial)

How to create a memory puzzle game with Python and Pygame (#005)

Introduction to multitasking with Python #001 multithreading (2020 tutorial)