def add_instance( self, instance: "GameInstance" ) -> Tuple[QGraphicsItem, Callable[[], None]]: """Add instance to manage, return a disconnect function and the canvas""" positionChanged = lambda rect: self.on_instance_moved(instance, rect) instance.positionChanged.connect(positionChanged) focusChanged = lambda focus: self.on_instance_focus_change( instance, focus) instance.focusChanged.connect(focusChanged) instance_pos = instance.get_position() gfx = QGraphicsRectItem(rect=instance_pos) gfx.setPen(self.transparent_pen) gfx.setPos(instance_pos.x(), instance_pos.y()) self.scene.addItem(gfx) self._instances[instance.wid] = gfx def disconnect(): gfx.hide() self.scene.removeItem(gfx) instance.positionChanged.disconnect(positionChanged) instance.focusChanged.disconnect(focusChanged) return gfx, disconnect
class GameWindow(QGraphicsView): def __init__(self, parent=None): super(GameWindow, self).__init__(parent=parent) self.screen = None self.scene = QGraphicsScene() self.scene.setSceneRect( QRect(left=0, top=0, width=PLAYGROUND_SIZEX * SCALE_FACTORX, height=PLAYGROUND_SIZEY * SCALE_FACTORY)) self.setFrameStyle(4) self.setScene(self.scene) self.snake = [] # define SnakeHead self.snake.append(QGraphicsRectItem()) self.snake[0].setRect(QRect(0, 0, SCALE_FACTORX, SCALE_FACTORY)) self.snake[0].setBrush(brushHead) self.scene.addItem(self.snake[0]) # define rest of the snake for i in range(1, MAX_LENGTH_SNAKE): self.snake.append(QGraphicsRectItem()) self.snake[i].setRect(QRect(0, 0, SCALE_FACTORX, SCALE_FACTORY)) self.snake[i].setBrush(brushBody) self.snake[i].setVisible(False) self.scene.addItem(self.snake[i]) # Create the graphic item for apple self.goal = QGraphicsRectItem() self.goal.setRect(QRect(0, 0, SCALE_FACTORX, SCALE_FACTORY)) self.goal.setBrush(brushGoal) self.scene.addItem(self.goal) self.show() def draw(self, state): self.goal.setPos(state.goal.x * SCALE_FACTORX, state.goal.y * SCALE_FACTORY) self.goal.setVisible(True) self.snake[0].setPos(float(state.snake[0].x * SCALE_FACTORX), float(state.snake[0].y * SCALE_FACTORY)) for i in range(1, state.snake_length): self.snake[i].setPos(state.snake[i].x * SCALE_FACTORX, state.snake[i].y * SCALE_FACTORY) self.snake[i].setVisible(True) for i in range(state.snake_length, MAX_LENGTH_SNAKE): self.snake[i].setVisible(False) def screenshot(self): self.screen = self.grab() self.screen.save("./snake_screen.png")
class TestShrinkingScene(unittest.TestCase): @classmethod def setUpClass(cls): if not QApplication.instance(): QApplication() def setUp(self): horizontal_threshold = 1.0 vertical_threshold = 3.0 self.scene = ShrinkingScene(horizontal_threshold, vertical_threshold, None) item_size = 1.0 self.fixed_item = QGraphicsRectItem(0.0, 0.0, item_size, item_size) self.item = QGraphicsRectItem(0.0, 0.0, item_size, item_size) self.item.setPos(10.0, 10.0) self.scene.addItem(self.fixed_item) self.scene.addItem(self.item) self.assertEqual(self.scene.sceneRect(), QRectF(-0.5, -0.5, 12.0, 12.0)) def test_shrink_if_needed_doesnt_shrink_within_thresholds(self): self.item.setPos(9.1, 7.1) self.scene.shrink_if_needed() self.assertEqual(self.scene.sceneRect(), QRectF(-0.5, -0.5, 12.0, 12.0)) def test_shrink_if_needed(self): self.item.setPos(8.9, 6.1) self.scene.shrink_if_needed() self.assertEqual(self.scene.sceneRect(), QRectF(-0.5, -0.5, 11.9, 11.1)) def test_shrink_if_needed_doesnt_shink_too_much(self): self.item.setPos(0.0, 0.0) self.scene.shrink_if_needed() self.assertEqual(self.scene.sceneRect(), QRectF(-0.5, -0.5, 3.0, 5.0))