class Level: """Start up a level""" def __init__(self): self.level = 3 # game level self.win = False # flag for winning the game self.left_paddle = pygame.sprite.Sprite() self.right_paddle = pygame.sprite.Sprite() def setup(self, scene, ball): """Setup a level with a certain level number""" scene.qubit_num = self.level self.circuit_grid_model = CircuitGridModel(scene.qubit_num, CIRCUIT_DEPTH) # the game crashes if the circuit is empty # initialize circuit with identity gate at the end of each line to prevent crash # identity gate are displayed by completely transparent PNG for i in range(scene.qubit_num): self.circuit_grid_model.set_node(i, CIRCUIT_DEPTH - 1, CircuitGridNode(node_types.IDEN)) self.circuit = self.circuit_grid_model.compute_circuit() self.statevector_grid = StatevectorGrid(self.circuit, scene.qubit_num, 100) self.right_statevector = VBox(WIDTH_UNIT * 90, WIDTH_UNIT * 0, self.statevector_grid) self.circuit_grid = CircuitGrid(0, ball.screenheight, self.circuit_grid_model) # computer paddle self.left_paddle.image = pygame.Surface( [WIDTH_UNIT, int(round(ball.screenheight / 2**scene.qubit_num))]) self.left_paddle.image.fill((255, 255, 255)) self.left_paddle.image.set_alpha(255) self.left_paddle.rect = self.left_paddle.image.get_rect() self.left_paddle.rect.x = 9 * WIDTH_UNIT # player paddle for detection of collision. It is invisible on the screen self.right_paddle.image = pygame.Surface( [WIDTH_UNIT, int(round(ball.screenheight / 2**scene.qubit_num))]) self.right_paddle.image.fill((255, 0, 255)) self.right_paddle.image.set_alpha(0) self.right_paddle.rect = self.right_paddle.image.get_rect() self.right_paddle.rect.x = self.right_statevector.xpos def levelup(self): """Increase level by 1""" if self.level <= 3: self.level += self.level self.setup() else: self.win = True # win the game if level is higher than 3
def circuit_from_string(circuit_dimension, gate_string): gate_array = gate_string.split(',') row_max = int(circuit_dimension.split(',')[0]) column_max = int(circuit_dimension.split(',')[1]) circuit_grid_model = CircuitGridModel(row_max, column_max) for i in range(row_max): for j in range(column_max): index = i * column_max + j node = CircuitGridNode(node_types.IDEN) if gate_array[index] == 'X': node = CircuitGridNode(node_types.X) elif gate_array[index] == 'Y': node = CircuitGridNode(node_types.Y) elif gate_array[index] == 'Z': node = CircuitGridNode(node_types.Z) elif gate_array[index] == 'H': node = CircuitGridNode(node_types.H) circuit_grid_model.set_node(i, j, node) circuit = circuit_grid_model.compute_circuit() return circuit
def __init__(self): # It seems, in Linux buffersize=512 is not enough, use 4096 to prevent: # ALSA lib pcm.c:7963:(snd_pcm_recover) underrun occurred mixer.pre_init(44100, -16, 1, 4096) init() self.clock = time.Clock() self.caption = display.set_caption('Space Invaders') self.screen = SCREEN self.background = image.load(IMAGE_PATH + 'qiskit.png').convert() self.startGame = False self.mainScreen = True self.gameOver = False # Counter for enemy starting position (increased each new round) self.enemyPosition = ENEMY_DEFAULT_POSITION self.titleText = Text(FONT, 50, 'Space Invaders', WHITE, 164, 155) self.titleText2 = Text(FONT, 25, 'Press any key to continue', WHITE, 201, 225) self.pauseText = Text(FONT, 42, 'Quantum Circuit Composer', WHITE, 50, 155) self.pauseText2 = Text(FONT, 25, 'Press ENTER key to continue', WHITE, 164, 225) self.gameOverText = Text(FONT, 50, 'Game Over', WHITE, 250, 270) self.nextRoundText = Text(FONT, 50, 'Next Round', WHITE, 240, 270) self.enemy1Text = Text(FONT, 25, ' = 10 pts', GREEN, 368, 270) self.enemy2Text = Text(FONT, 25, ' = 20 pts', BLUE, 368, 320) self.enemy3Text = Text(FONT, 25, ' = 30 pts', PURPLE, 368, 370) self.enemy4Text = Text(FONT, 25, ' = ?????', RED, 368, 420) self.scoreText = Text(FONT, 20, 'Score', WHITE, 5, 5) self.livesText = Text(FONT, 20, 'Lives ', WHITE, 640, 5) self.life1 = Life(715, 3) self.life2 = Life(742, 3) self.life3 = Life(769, 3) self.livesGroup = sprite.Group(self.life1, self.life2, self.life3) self.shipPosition = 4 self.circuit_grid_model = CircuitGridModel(3, 10) self.circuit_grid = CircuitGrid(0, SCREEN_HEIGHT, self.circuit_grid_model) self.paused = False #self.pause_bar = 0 #self.pause_ready = False #self.pause_increment_time = 50 self.labels = Labels()
def __init__(self, qubit_num, position, player_type, game_mode): self._position = position self._player_type = player_type self._score = 0 self._qubit_num = qubit_num self._paddle = pg.sprite.Sprite() self._circuit = None self._circuit_grid_model = None self._statevector_grid = None self._statevector = None self._circuit_depth = (CIRCUIT_DEPTH // 2 if game_mode == MODES["TWO_PLAYER"] else CIRCUIT_DEPTH) self._circuit_grid_model = CircuitGridModel(qubit_num, self._circuit_depth) for i in range(qubit_num): self._circuit_grid_model.set_node(i, self._circuit_depth - 1, CircuitGridNode(node_types.IDEN)) self._circuit = self._circuit_grid_model.compute_circuit() if POSITIONS["LEFT"] == position: self._circuit_grid = CircuitGrid( 1.45 * WIDTH_UNIT, round(WINDOW_HEIGHT * 0.7), self._circuit_grid_model, game_mode, ) self._statevector_grid = StatevectorGrid(self._circuit, position, qubit_num, 100) self._statevector = VBox(0, WIDTH_UNIT * 0, self._statevector_grid) else: self._circuit_grid = CircuitGrid( WINDOW_WIDTH // 2 if game_mode == MODES["TWO_PLAYER"] else 0, round(WINDOW_HEIGHT * 0.7), self._circuit_grid_model, game_mode, ) self._statevector_grid = StatevectorGrid(self._circuit, position, qubit_num, 100) self._statevector = VBox(WIDTH_UNIT * 90, WIDTH_UNIT * 0, self._statevector_grid) # player paddle for detection of collision. It is invisible on the screen self._paddle.image = pg.Surface( [WIDTH_UNIT, int(round(WINDOW_HEIGHT * 0.7) / 2**qubit_num)]) self._paddle.image.fill(( 255, 0 if self._player_type in (QUANTUM_COMPUTER_1P, QUANTUM_COMPUTER_2P) else 255, 255, )) #self._paddle.image.set_alpha( # 0 # if self._player_type in (QUANTUM_COMPUTER_1P, QUANTUM_COMPUTER_2P) # else 255 #) self._paddle.rect = self._paddle.image.get_rect() if self._player_type in (QUANTUM_COMPUTER_1P, QUANTUM_COMPUTER_2P): if POSITIONS["LEFT"] == position: self._paddle.rect.x = 9.5 * WIDTH_UNIT self._statevector.arrange() else: self._paddle.rect.x = self._statevector.x else: self._paddle.rect.x = 9 * WIDTH_UNIT
class Player(object): """A class to present a quantum player.""" def __init__(self, qubit_num, position, player_type, game_mode): self._position = position self._player_type = player_type self._score = 0 self._qubit_num = qubit_num self._paddle = pg.sprite.Sprite() self._circuit = None self._circuit_grid_model = None self._statevector_grid = None self._statevector = None self._circuit_depth = (CIRCUIT_DEPTH // 2 if game_mode == MODES["TWO_PLAYER"] else CIRCUIT_DEPTH) self._circuit_grid_model = CircuitGridModel(qubit_num, self._circuit_depth) for i in range(qubit_num): self._circuit_grid_model.set_node(i, self._circuit_depth - 1, CircuitGridNode(node_types.IDEN)) self._circuit = self._circuit_grid_model.compute_circuit() if POSITIONS["LEFT"] == position: self._circuit_grid = CircuitGrid( 1.45 * WIDTH_UNIT, round(WINDOW_HEIGHT * 0.7), self._circuit_grid_model, game_mode, ) self._statevector_grid = StatevectorGrid(self._circuit, position, qubit_num, 100) self._statevector = VBox(0, WIDTH_UNIT * 0, self._statevector_grid) else: self._circuit_grid = CircuitGrid( WINDOW_WIDTH // 2 if game_mode == MODES["TWO_PLAYER"] else 0, round(WINDOW_HEIGHT * 0.7), self._circuit_grid_model, game_mode, ) self._statevector_grid = StatevectorGrid(self._circuit, position, qubit_num, 100) self._statevector = VBox(WIDTH_UNIT * 90, WIDTH_UNIT * 0, self._statevector_grid) # player paddle for detection of collision. It is invisible on the screen self._paddle.image = pg.Surface( [WIDTH_UNIT, int(round(WINDOW_HEIGHT * 0.7) / 2**qubit_num)]) self._paddle.image.fill(( 255, 0 if self._player_type in (QUANTUM_COMPUTER_1P, QUANTUM_COMPUTER_2P) else 255, 255, )) #self._paddle.image.set_alpha( # 0 # if self._player_type in (QUANTUM_COMPUTER_1P, QUANTUM_COMPUTER_2P) # else 255 #) self._paddle.rect = self._paddle.image.get_rect() if self._player_type in (QUANTUM_COMPUTER_1P, QUANTUM_COMPUTER_2P): if POSITIONS["LEFT"] == position: self._paddle.rect.x = 9.5 * WIDTH_UNIT self._statevector.arrange() else: self._paddle.rect.x = self._statevector.x else: self._paddle.rect.x = 9 * WIDTH_UNIT @property def qubit_num(self): return self._qubit_num @qubit_num.setter def qubit_num(self, value): self._qubit_num = value @property def position(self): return self._position @position.setter def position(self, value): self._position = value @property def player_type(self): return self._player_type @player_type.setter def player_type(self, value): self._player_type = value @property def score(self): return self._score @score.setter def score(self, value): self._score = value @property def paddle(self): return self._paddle @paddle.setter def paddle(self, value): self._paddle = value @property def circuit(self): return self._circuit @circuit.setter def circuit(self, value): self._circuit = value @property def circuit_grid(self): return self._circuit_grid @circuit_grid.setter def circuit_grid(self, value): self._circuit_grid = value @property def circuit_grid_model(self): return self._circuit_grid_model @circuit_grid_model.setter def circuit_grid_model(self, value): self._circuit_grid_model = value @property def statevector(self): return self._statevector @statevector.setter def statevector(self, value): self._statevector = value @property def statevector_grid(self): return self._statevector_grid @statevector_grid.setter def statevector_grid(self, value): self._statevector_grid = value def reset_score(self): self._score = 0 def update_paddle(self, screen): # Update visualizations # TODO: Refactor following code into methods, etc. circuit_grid_model = self._circuit_grid_model statevector = self._statevector circuit_grid = self._circuit_grid statevector_grid = self._statevector_grid circuit = circuit_grid_model.compute_circuit() statevector_grid.paddle_before_measurement(self._position, circuit, self._qubit_num, 100) statevector.arrange() circuit_grid.draw(screen) def move_update_circuit_grid_display(self, screen, direction): self._circuit_grid.move_to_adjacent_node(direction) self._circuit_grid.draw(screen)