def game_loop(self): """Game loop.""" surface = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) surface.fill(0xC4CFA1) # Same colour as splash screen playfield = Playfield(PLAYFIELD_WIDTH, PLAYFIELD_HEIGHT, self.tileset) pygame.time.set_timer(pygame.USEREVENT, START_SPEED) clock = pygame.time.Clock() trimino = self.spawn_trimino() game_over = False while not game_over: event = pygame.event.poll() if event.type == pygame.QUIT: self.exit_game() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: break if event.key == pygame.K_j: if not self.legal_move(playfield, trimino.rotate_left()): trimino.rotate_right() # Revert rotation if event.key == pygame.K_k: if not self.legal_move(playfield, trimino.rotate_right()): trimino.rotate_left() # Revert rotation if event.key == pygame.K_SPACE: # Hard drop while self.legal_move(playfield, trimino.move_down()): pass if playfield.place_trimino(trimino.move_up()): trimino = self.spawn_trimino() self.process_lines(playfield) else: game_over = True elif event.type == pygame.USEREVENT: if not self.legal_move(playfield, trimino.move_down()): if playfield.place_trimino(trimino.move_up()): trimino = self.spawn_trimino() self.process_lines(playfield) else: game_over = True pressed = pygame.key.get_pressed() if pressed[pygame.K_LEFT]: if not self.legal_move(playfield, trimino.move_left()): trimino.move_right() # Revert move if pressed[pygame.K_RIGHT]: if not self.legal_move(playfield, trimino.move_right()): trimino.move_left() # Revert move if pressed[pygame.K_DOWN]: if not self.legal_move(playfield, trimino.move_down()): trimino.move_up() # Revert move playfield.draw() trimino.draw() self.font.write(0, 1, "SCORE: %d" % self.player.score) self.font.write(0, 10, "LEVEL: %d" % self.level) pygame.display.flip() clock.tick(30) pygame.time.set_timer(pygame.USEREVENT, 0) # Disable timer
class GameScreen(object): dt = 0.05 def __init__(self, game, player, font, zoom, push, pop): self._game = game self._playfield = Playfield(self._game, player, font, zoom, self._ignoremotion) self._info = InfoView(self._playfield, font, zoom, push, pop) self._ignore = None self.scale(font) def scale(self, font): self._font = font self._playfield.scale(self._font) self._info.scale(self._font) def resize(self, size): self._fieldwidth = max(0, size[0] - self._info.width) self._playfield.resize((self._fieldwidth, size[1])) self._info.resize((size[0] - self._fieldwidth, size[1])) def _ignoremotion(self, rel): self._ignore = rel def handle(self, e): if e.type == MOUSEMOTION and e.rel != self._ignore: mouse.set_visible(True) self._ignore = None if self._playfield.handle(e): return True if 'pos' in e.dict: e.dict['pos'] = (e.pos[0] - self._fieldwidth, e.pos[1]) return self._info.handle(e) def _playinfosurfaces(self, surface): size = surface.get_size() return (surface.subsurface(Rect((0,0),(self._fieldwidth,size[1]))), surface.subsurface(Rect((self._fieldwidth,0), (size[0]-self._fieldwidth, size[1])))) def draw(self, surface): play, info = self._playinfosurfaces(surface) self._playfield.draw(play) self._info.draw(info)