I am a beginner in pygame and I am not a English native speaker.
My assignment is coding a game called 'Memory'. This game contains 8 pairs pictures and an cover exists on each pictures. This week, our assignment is draw covers and if you click an cover, this cover will disappear. However, although I draw pictures successfully. I still cannot draw covers properly. Actually, I don't know the approaches that to draw an cover and to click an cover and make it disappear. I searched a lot however, maybe because I am not a native speaker, I cannot really understand how it works. Therefore, I hope someone could help me here. I will provide my code and pictures below. Thanks everyone!
# This is version 1 for 'Memory'.
# This version contains complete tile grid, but not the score.
# All 8 pairs of two tiles should be exposed when the game starts.
# Each time the game is played, the tiles must be in random locations in the grid.
# Player actions must be ignored.import pygame, sys, time, random
from pygame.locals import *
pygame.init()# User-defined Classclass Tile:bgColor=pygame.Color('black')BorderWidth= 3def __init__(self, x, y, image, surface):self.x = xself.y = yself.surface = surfaceself.image= imagedef DrawTile(self):self.surface.blit( self.image , (self.x, self.y))class Memory:boardWidthSize=4boardHeightSize=4 def __init__(self, surface):self.surface = surfaceself.board = []self.cover = []self.board2= []def createImages(self):#load images from file self.cover=[]self.imageNames = ['image1.bmp','image2.bmp','image3.bmp','image4.bmp','image5.bmp','image6.bmp','image7.bmp','image8.bmp','image1.bmp','image2.bmp','image3.bmp','image4.bmp','image5.bmp','image6.bmp','image7.bmp','image8.bmp']self.images=[]for name in self.imageNames :pic = pygame.image.load(name)self.images.append(pic) random.shuffle(self.images)self.cover.append(pygame.image.load('image0.bmp'))def createTile(self):board =[]board2=[]#loop through the loaded images and create tile objectsfor rowIndex in range(0,Memory.boardWidthSize):row = []row2=[]for columnIndex in range(0,Memory.boardHeightSize):width = 100x = columnIndex*widthheight = 100y = rowIndex*heighttile = Tile(x, y, self.images[rowIndex*4+columnIndex], self.surface)cover= Tile(x, y, self.cover, self.surface)row.append(tile)row2.append(cover)self.board.append(row)self.board2.append(row2)def GetScore(self):position=(400,0)FontSize=50FontColor=pygame.Color('White')String='Score : 'font=pygame.font.SysFont(None, FontSize, True)surface1=font.render(str(pygame.time.get_ticks()/1000), True, FontColor,0)self.surface.blit(surface1,position) def draw(self):for row in self.board:for tile in row:tile.DrawTile()def update(self):if False:return Trueelse:self.createTile()return Falsedef main():surfaceSize = (500, 400) windowTitle = 'Memory'# No frame delay since no moving objectsgameOver = False# Create the windowsurface = pygame.display.set_mode(surfaceSize, 0, 0)pygame.display.set_caption(windowTitle)memory = Memory(surface)memory.createImages()memory.createTile()pygame.display.update()while True:for event in pygame.event.get():if event.type == QUIT:pygame.quit()sys.exit()memory.draw()memory.GetScore()gameOver = memory.update()pygame.display.update()main()
I upload these pictures in my Google Drive:
https://drive.google.com/folderview?id=0Bx-bEVazt-TWUnRSZlJVRmhfQm8&usp=sharing