def init(self): Entity.init(self) for row in range(self._rows): for col in range(self._cols): cellType = self._cellMatrix[row][col] cellEntity = EntitySpawner.spawnEntity(CellEntity, self, Vector2(row, col), cellType) cellEntity.getTransform().position = Vector2(col * CELL_SIZE.x, row * CELL_SIZE.y) self._cells.append(cellEntity)
def init(self): Entity.init(self) self._renderComponent = self.addComponent( SnakeRenderComponent(Screen.getSize(), CELL_SIZE, CELL_BORDER_WIDTH, Color.RED, Color.BLACK)) self._inputComponent = self.addComponent(InputComponent()) self._inputComponent.bindAction("left", InputEvent.EVENT_TYPE_PRESSED, lambda: self.changeDirection(DIRECTION_LEFT)) self._inputComponent.bindAction("right", InputEvent.EVENT_TYPE_PRESSED, lambda: self.changeDirection(DIRECTION_RIGHT)) self._inputComponent.bindAction("up", InputEvent.EVENT_TYPE_PRESSED, lambda: self.changeDirection(DIRECTION_UP)) self._inputComponent.bindAction("down", InputEvent.EVENT_TYPE_PRESSED, lambda: self.changeDirection(DIRECTION_DOWN))
def init(self): Entity.init(self) self._inputComponent = self.addComponent(InputComponent()) self._inputComponent.bindAction("submit", InputEvent.EVENT_TYPE_PRESSED, self.restart) self._inputComponent.bindAction("cancel", InputEvent.EVENT_TYPE_PRESSED, self.quit) self._inputComponent.bindAction("pause", InputEvent.EVENT_TYPE_PRESSED, self.togglePaused) self._backgroundEntity = EntitySpawner.spawnEntity(Entity) self._backgroundEntity.addComponent(RectRenderComponent(Screen.getSize(), Screen.getSize(), Color.BLACK)) self._boardEntity = EntitySpawner.spawnEntity(BoardEntity, CELL_MATRIX) self._snakeEntity = EntitySpawner.spawnEntity(SnakeEntity, self._boardEntity, 5, 3, Vector2(3, 3), DIRECTION_RIGHT) self._snakeEntity.onFoodEaten += lambda: self.spawnFood() self._snakeEntity.onFoodEaten += lambda: self.setScore(self._score + 1) self._snakeEntity.onFoodEaten += lambda: self.increaseSnakeSpeed() self._snakeEntity.onDeath += lambda: self.setGameOver(True) self.spawnFood() self._pausedTextEntity = self.createTextEntity("PAUSED") pausedTextRectSize = self._pausedTextEntity.getComponent(TextRenderComponent).getRectSize() boardRectSize = Vector2(self._boardEntity.getCols() * CELL_SIZE.x, self._boardEntity.getRows() * CELL_SIZE.y) self._pausedTextEntity.getTransform().position = Vector2((boardRectSize.x - pausedTextRectSize.x) // 2, (boardRectSize.y - pausedTextRectSize.y) // 2) self._gameOverTextEntity = self.createTextEntity("GAME OVER") gameOverTextRectSize = self._gameOverTextEntity.getComponent(TextRenderComponent).getRectSize() self._gameOverTextEntity.getTransform().position = Vector2((boardRectSize.x - gameOverTextRectSize.x) // 2, (boardRectSize.y - gameOverTextRectSize.y) // 2) self._scoreTextEntity = self.createTextEntity("SCORE:0") self._scoreTextEntity.getTransform().position = Vector2(0, boardRectSize.y) self._score = 0 self._gameOver = False self.setGameOver(self._gameOver) self._paused = True self.setPaused(self._paused)
def init(self): Entity.init(self) if (self._type == CELL_TYPE_EMPTY): self.addComponent(CellRenderComponent(CELL_SIZE, CELL_SIZE, CELL_BORDER_WIDTH, Color(25, 25, 25), Color.BLACK)) if (self._type == CELL_TYPE_BLOCK): self.addComponent(CellRenderComponent(CELL_SIZE, CELL_SIZE, CELL_BORDER_WIDTH, Color.BLUE, Color.BLACK))
def init(self): Entity.init(self) self._renderComponent = self.addComponent( CellRenderComponent(CELL_SIZE, CELL_SIZE, CELL_BORDER_WIDTH, Color.GREEN, Color.BLACK))