Reputation: 65
I am coding a game called "Memory" in pygame. Everything seems right for me but it cannot blit images on the surface. I want to have two pairs of 9 images in random orders appear on the surface. However, it is all black instead of having two pairs of 9 images in random orders. Someone, please help me. Thank you very much!
import pygame
import random
import time
screen = pygame.display.set_mode((525, 435))
def main():
pygame.init()
pygame.display.set_mode((535, 435))
pygame.display.set_caption('Memory')
w_surface = pygame.display.get_surface()
game = Game(w_surface)
game.play()
pygame.quit()
class Game:
def __init__(self, surface):
self.surface = surface
self.bg_color = pygame.Color('black')
self.FPS = 10
self.game_Clock = pygame.time.Clock()
self.close_clicked = False
self.continue_game = True
Tile.set_surface(self.surface)
self.grid_size = 4
self.grid = []
imgnames = ['./images/' + str(i) + '.jpg' for i in range(1,9)]
self.images = [pygame.image.load(name) for name in imgnames]
self.shuffle = self.images + self.images
random.shuffle(self.shuffle)
def create_grid(self, grid_size):
for row_num in range(grid_size):
new_row = self.create_row(row_num, grid_size)
self.grid.append(new_row)
def create_row(self, row_num, size):
tile_height = self.surface.get_height() // size
tile_width = self.surface.get_width() // (size+1)
one_row = [ ]
for col_num in range(size):
y = row_num * tile_height + 5
x = col_num * tile_width + 5
for i in range (self.grid_size):
one_tile = Tile(x, y, tile_width, tile_height, self.shuffle[i], self.surface)
i = col_num*size + row_num
one_row.append(one_tile)
return one_row
def play(self):
while not self.close_clicked:
self.handle_events()
self.draw()
if self.continue_game:
self.update()
self.decide_continue()
self.game_Clock.tick(self.FPS)
def handle_events(self):
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
self.close_clicked = True
def draw(self):
self.surface.fill(self.bg_color)
for row in self.grid:
for tile in row:
tile.draw()
pygame.display.update()
def update(self):
pass
def decide_continue(self):
pass
class Tile:
def __init__(self, pos_x, pos_y, tile_numx, tile_numy, image, surface):
self.pos_x = pos_x
self.pos_y = pos_y
self.numx = tile_numx
self.numy = tile_numy
self.image = image
self.surface = surface
@classmethod
def set_surface(cls, surface):
cls.surface = surface
def draw(self):
x = 0
y = 0
screen.blit(self.image,(self.pos_x,self.pos_y))
main()
Upvotes: 1
Views: 131
Reputation: 210878
The call of create_grid
is missing in the constructor of Game
:
class Game:
def __init__(self, surface):
# [...]
self.create_grid(self.grid_size)
There is on loop too much in create_grid
/create_row
. create_grid
has one loop, to create the rows. So create_row
needs one loop to create the columns in a row:
class Game:
# [...]
def create_grid(self, grid_size):
for row_num in range(grid_size):
new_row = self.create_row(row_num, grid_size)
self.grid.append(new_row)
def create_row(self, row_num, size):
tile_height = self.surface.get_height() // size
tile_width = self.surface.get_width() // (size+1)
one_row = [ ]
for col_num in range(size):
y = row_num * tile_height + 5
x = col_num * tile_width + 5
i = col_num*size + row_num
one_tile = Tile(x, y, tile_width, tile_height, self.shuffle[i], self.surface)
one_row.append(one_tile)
return one_row
Upvotes: 1