import pygame BLOCKSIZE = 20 width = 800 height = 600 SIZE = (width, height) BLACK = (0, 0, 0) YELLOW = (255, 255, 0) RED = (255, 0, 0) class Block: def __init__(self, grid_pos, visibility): self.grid_pos = grid_pos self.visibility = visibility def set_visible(self): self.visibility = True def unset_visible(self): self.visibility = False def draw(self,screen): block_shape = (self.grid_pos[0], self.grid_pos[1], BLOCKSIZE, BLOCKSIZE) pygame.draw.rect(screen, YELLOW, block_shape) pygame.draw.rect(screen, RED, block_shape, 1) def main(): global blocks blocks = {} screen = init_game() create_grid(screen) game_loop(screen) exit_game() def init_game(): pygame.init() screen = pygame.display.set_mode(SIZE) return screen def game_loop(screen): while True: if not event_handling(): break if not update_game(): break event = event_handling() update_game() draw_game(screen) def event_handling(): for event in pygame.event.get(): if event.type == pygame.QUIT: return False if event.type == pygame.MOUSEBUTTONUP: pos = pygame.mouse.get_pos() x = pos[0] // BLOCKSIZE * BLOCKSIZE y = pos[1] // BLOCKSIZE * BLOCKSIZE blocks[(x,y)].set_visible() if event.type == pygame.KEYDOWN: if event.key == pygame.K_LSHIFT: pos = pygame.mouse.get_pos() x = pos[0] // BLOCKSIZE * BLOCKSIZE y = pos[1] // BLOCKSIZE * BLOCKSIZE blocks[(x,y)].unset_visible() return True def update_game(): return True def draw_game(screen): screen.fill(BLACK) for x,y in blocks: if blocks[(x,y)].visibility: blocks[(x,y)].draw(screen) pygame.display.flip() def create_grid(screen): y = 0 for _ in range(SIZE[1]//BLOCKSIZE*BLOCKSIZE): x = 0 for _ in range(SIZE[0]//BLOCKSIZE*BLOCKSIZE): blocks[(x,y)]= (Block ((x, y), False)) x += BLOCKSIZE y += BLOCKSIZE def exit_game(): pygame.quit() main()