Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- # Simple boiler plate
- class Scene:
- def __init__(self, control):
- self.control = control
- def on_draw(self, surface):
- pass
- def on_event(self, event):
- pass
- def on_update(self, delta):
- pass
- class SceneHandler:
- def __init__(self):
- self.current = None
- self.next_ = None
- def push(self, scene):
- self.next_ = scene
- def update(self):
- if self.next_:
- self.current = self.next_
- self.next_ = None
- return self.current
- class SceneControl:
- def __init__(self, caption, width, height, flags=0, fps=60):
- pygame.display.set_caption(caption)
- self.display = pygame.display.set_mode((width, height), flags)
- self.rect = self.display.get_rect()
- self.clock = pygame.time.Clock()
- self.running = True
- self.delta = 0
- self.fps = fps
- self.scene = SceneHandler()
- def loop(self):
- if self.scene.current:
- while self.running:
- scene = self.scene.update()
- for event in pygame.event.get():
- if event.type != pygame.QUIT:
- scene.on_event(event)
- else:
- self.running = False
- scene.on_update(self.delta)
- scene.on_draw(self.display)
- pygame.display.flip()
- self.delta = self.clock.tick(self.fps)/1000
- def run(self, scene):
- self.scene.current = scene
- self.loop()
- class Grid:
- def __init__(self, rect, cut, gap=(0, 0)):
- self.rect = rect
- self.cut = cut
- self.gap = gap
- self.slice = cut[0] + gap[0], cut[1] + gap[1]
- self.blocks = self.get_blocks()
- def get_blocks(self):
- blocks = []
- for y in range(self.rect.y, self.rect.h, self.slice[1]):
- xblock = []
- for x in range(self.rect.x, self.rect.w, self.slice[0]):
- xblock.append(pygame.Rect((x, y), self.cut))
- blocks.append(xblock)
- return blocks
- def get_cells(self):
- return len(self.blocks[0]), len(self.blocks)
- def get_empty_blocks(self):
- w, h = self.get_cells()
- return [[None for _ in range(w)] for _ in range(h)]
- def collide(self, mpos):
- x, y = self.get_coords(mpos)
- if self.blocks[y][x].collidepoint(mpos):
- return self.blocks[y][x].copy()
- def collision_point(self, mpos):
- x, y = self.get_coords(mpos)
- if self.blocks[y][x].collidepoint(mpos):
- return x, y
- def get_coords(self, mpos):
- x = (mpos[0] - self.rect.x) // self.slice[0]
- y = (mpos[1] - self.rect.y) // self.slice[1]
- return x, y
- def draw_outline(self, surface):
- for yblock in self.blocks:
- for rect in yblock:
- pygame.draw.rect(surface, 'lawngreen', rect, 1)
- class Game(Scene):
- def __init__(self, control):
- super().__init__(control)
- self.grid = Grid(self.control.rect.inflate(-20, -20), (20, 20))
- self.hover = None
- self.blocks = self.grid.get_empty_blocks()
- self.toggle_grid = False
- def on_draw(self, surface):
- surface.fill('black')
- if self.toggle_grid:
- self.grid.draw_outline(surface)
- for blocks in self.blocks:
- for rect in blocks:
- if rect:
- surface.fill('dodgerblue', rect)
- def on_event(self, event):
- if event.type == pygame.MOUSEBUTTONDOWN:
- if event.button == 1:
- point = self.grid.collision_point(event.pos)
- if point:
- x, y = point
- if self.blocks[y][x]:
- self.blocks[y][x] = None
- else:
- self.blocks[y][x] = self.grid.collide(event.pos)
- elif event.type == pygame.KEYDOWN:
- if event.key == pygame.K_SPACE:
- self.toggle_grid = not self.toggle_grid
- if __name__ == "__main__":
- pygame.init()
- control = SceneControl("Grid Example", 800, 600)
- control.run(Game(control))
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment