Ejemplo n.º 1
0
class Pong(Node2D):
    game_finished = signal()

    def _ready(self):
        self.score_left = 0
        self.score_right = 0
        # let each paddle know which one is left, too
        p1 = self.get_node("player1")
        p2 = self.get_node("player2")
        p1.left = True
        p2.left = False
        p1.action_prefix = 'p1'
        p2.action_prefix = 'p2'

    def update_score(self, add_to_left):
        if add_to_left:
            self.score_left += 1
            self.get_node("score_left").set_text(str(self.score_left))
        else:
            self.score_right += 1
            self.get_node("score_right").set_text(str(self.score_right))

        game_ended = False
        if self.score_left == SCORE_TO_WIN:
            self.get_node("winner_left").show()
            game_ended = True
        elif self.score_right == SCORE_TO_WIN:
            self.get_node("winner_right").show()
            game_ended = True

        if game_ended:
            self.get_node("ball").stop()
            self.get_node("player1").can_move = False
            self.get_node("player2").can_move = False
class Player(Area2D):
    speed = export(int, default=420)
    screen_size = None
    hit = signal()

    def _ready(self):
        self.screen_size = self.get_viewport_rect().size
        self.hide()

    def _process(self, delta):
        velocity = Vector2()

        if Input.is_action_pressed("ui_right"):
            velocity.x += 1
        if Input.is_action_pressed("ui_left"):
            velocity.x -= 1
        if Input.is_action_pressed("ui_down"):
            velocity.y += 1
        if Input.is_action_pressed("ui_up"):
            velocity.y -= 1

        animated_sprite = self.get_node("AnimatedSprite")
        if velocity.length() > 0:
            velocity = velocity.normalized() * self.speed
            animated_sprite.play()
        else:
            animated_sprite.stop()

        self.position += velocity * delta

        # NOTE: It seems you can't set the self.position's x and y values
        # so you'll need to assign a new vector instead.
        self.position = Vector2(
            clamp(self.position.x, 0, self.screen_size.x),
            clamp(self.position.y, 0, self.screen_size.y),
        )

        if velocity.x != 0:
            animated_sprite.animation = "walk"
            animated_sprite.flip_v = False
            animated_sprite.flip_h = velocity.x < 0
        elif velocity.y != 0:
            animated_sprite.animation = "up"
            animated_sprite.flip_v = velocity.y > 0

    def _on_Player_body_entered(self, body):
        self.hide()  # Player disappears after being hit.

        self.call('emit_signal', 'hit')

        # Ensures that the collission shape is not disabled while
        # the Godot is still in the middle of collision processing.
        self.get_node("CollisionShape2D").set_deferred("disabled", True)

    def start(self, pos):
        self.position = pos
        self.show()
        self.get_node("CollisionShape2D").disabled = False
Ejemplo n.º 3
0
class Hud(CanvasLayer):
    start_game = signal()
    is_game_over = False
    show_start_button = False

    def show_message(self, text):
        message = self.get_node("Message")
        message.text = text
        message.show()
        self.get_node("MessageTimer").start()

    def show_game_over(self):
        self.show_message("Game Over")
        self.is_game_over = True

    def show_title_message(self):
        message = self.get_node("Message")
        message.text = "Dodge the\nCreeps!"
        message.show()
        self.get_node("DelayTimer").start()
        self.show_start_button = True

    def update_score(self, score):
        self.get_node("ScoreLabel").text = str(score)

    def _on_StartButton_pressed(self):
        self.get_node("StartButton").hide()
        self.call('emit_signal', 'start_game')

    def _on_MessageTimer_timeout(self):
        self.get_node("Message").hide()

        if self.is_game_over:
            self.is_game_over = False
            self.show_title_message()

    def _on_DelayTimer_timeout(self):
        if self.show_start_button:
            self.show_start_button = False
            self.get_node("StartButton").show()
Ejemplo n.º 4
0
class Board(TileMap):
    is_measuring = export(bool, False)
    is_cleared = export(bool, False)

    game_finished = signal()
    circuit_finished = signal()
    job_finished = signal()

    gate_start_rock = None
    current_gate = None

    job = None
    backend = sim_backend

    def get_rock(self, x, y):
        return self.rocks_array[TILE_COUNT * y + x]

    def _unhandled_input(self, event):
        if not self.is_measuring and not self.is_cleared:
            if event.is_action_pressed('mouse_left_button'):
                v = self.get_local_mouse_position()
                x = int(v.x // TILE_SIZE)
                y = int(v.y // TILE_SIZE)
                cell = self.get_cell(x, y)
                if cell != -1:
                    rock = self.get_rock(x, y)
                    if rock.value == EMPTY:
                        if self.gameplay.is_quantum:
                            rock.value = UNKNOWN

                            num = len(self.quantum_rocks)
                            self.quantum_rocks[rock.get_name()] = num
                            self.quantum_rocks_inv.append(rock)
                            if not self.gameplay.is_white:
                                self.quantum_operations.append(('x', num))
                            self.quantum_operations.append(('h', num))
                        elif self.gameplay.is_white:
                            rock.value = WHITE
                            self.check_cleared([(x, y)])
                        else:
                            rock.value = BLACK
                            self.check_cleared([(x, y)])
                        self.gameplay.tick()

            elif event.is_action_pressed(
                    'mouse_right_button') and self.gameplay.is_quantum:
                v = self.get_local_mouse_position()
                x = int(v.x // TILE_SIZE)
                y = int(v.y // TILE_SIZE)
                cell = self.get_cell(x, y)
                if cell != -1:
                    rock = self.get_rock(x, y)
                    if rock.value == UNKNOWN:
                        self.gate_start_rock = rock
                        gate = GateScene.instance()
                        gate.position = self.gate_start_rock.position
                        self.gates_node.add_child(gate)
                        self.current_gate = gate

            elif event.is_action_released(
                    'mouse_right_button') and self.current_gate != None:
                diff = self.get_local_mouse_position(
                ) - self.gate_start_rock.get_position()

                cx, cy = self.gate_start_rock.cell_x, self.gate_start_rock.cell_y
                nx, ny = cx, cy

                if diff.x >= 0:
                    if diff.y >= 0:
                        if diff.x >= diff.y:
                            nx += 1
                        else:
                            ny += 1
                    else:
                        if diff.x >= -diff.y:
                            nx += 1
                        else:
                            ny -= 1
                else:
                    if diff.y >= 0:
                        if -diff.x >= diff.y:
                            nx -= 1
                        else:
                            ny += 1
                    else:
                        if -diff.x >= -diff.y:
                            nx -= 1
                        else:
                            ny -= 1

                if nx >= 0 and nx < TILE_COUNT and ny >= 0 and ny < TILE_COUNT:
                    next_rock = self.get_rock(nx, ny)
                    if next_rock.value != EMPTY:
                        a_num = self.quantum_rocks[
                            self.gate_start_rock.get_name()]
                        b_num = 0
                        if next_rock.value != UNKNOWN:
                            b_num = len(self.quantum_rocks)
                            self.quantum_rocks[next_rock.get_name()] = b_num
                            self.quantum_rocks_inv.append(next_rock)
                            if next_rock.value == BLACK:
                                self.quantum_operations.append(('x', b_num))
                            next_rock.value = UNKNOWN
                        else:
                            b_num = self.quantum_rocks[next_rock.get_name()]

                        self.quantum_operations.append(('gate', a_num, b_num))
                        self.quantum_gates.append(self.current_gate)
                        self.gameplay.tick()
                    else:
                        self.current_gate.queue_free()
                else:
                    self.current_gate.queue_free()

                self.gate_start_rock = None
                self.current_gate = None

    def collapse(self):
        register_size = len(self.quantum_rocks)
        circuit_builder = CircuitBuilder(register_size)
        for op in self.quantum_operations:
            circuit_builder.add_operation(op, self.backend)
        qr, cr, circuit = circuit_builder.build()
        circuit_text = circuit.draw(output='text', line_length=75)
        self.emit_signal('circuit_finished', circuit_text.single_string())
        qobj = assemble(transpile(circuit, self.backend, optimization_level=2),
                        self.backend,
                        memory=True,
                        shots=32)
        self.job = self.backend.run(qobj)
        self.is_measuring = True

    def check_job(self):
        if self.job != None:
            status = self.job.status()
            if status == JobStatus.DONE:
                self.cleanup()
                self.emit_signal('job_finished')
            elif status == JobStatus.ERROR or status == JobStatus.CANCELLED:
                raise QiskitError

    def cleanup(self):
        result = self.job.result()

        memory_list = result.get_memory()
        # counts = result.get_counts()
        # memory_list = []
        # for k,v in counts.items():
        #     for _ in range(v):
        #         memory_list.append(k)
        pick = memory_list[0]
        pick = pick[::-1]

        arr = []
        for i, v in enumerate(pick):
            rock = self.quantum_rocks_inv[i]
            if v == '0':
                rock.value = WHITE
            else:
                rock.value = BLACK
            arr.append((rock.cell_x, rock.cell_y))

        self.quantum_rocks.clear()
        self.quantum_rocks_inv.clear()
        self.quantum_operations.clear()
        for gate in self.quantum_gates:
            gate.queue_free()
        self.quantum_gates.clear()

        self.job = None
        self.is_measuring = False

        self.check_cleared(arr)

    def check_cleared(self, arr):
        for v in arr:
            x, y = v[0], v[1]
            value = self.get_rock(x, y).value

            count = 0
            for i in range(-4, 5):
                cx = x + i
                if cx >= 0 and cx < TILE_COUNT and self.get_rock(
                        cx, y).value == value:
                    count += 1
                    if count == 5:
                        return self.cleared(value)
                else:
                    count = 0

            count = 0
            for i in range(-4, 5):
                cy = y + i
                if cy >= 0 and cy < TILE_COUNT and self.get_rock(
                        x, cy).value == value:
                    count += 1
                    if count == 5:
                        return self.cleared(value)
                else:
                    count = 0

            count = 0
            for i in range(-4, 5):
                cx, cy = x + i, y + i
                if cx >= 0 and cx < TILE_COUNT and cy >= 0 and cy < TILE_COUNT and self.get_rock(
                        cx, cy).value == value:
                    count += 1
                    if count == 5:
                        return self.cleared(value)
                else:
                    count = 0

            count = 0
            for i in range(-4, 5):
                cx, cy = x + i, y - i
                if cx >= 0 and cx < TILE_COUNT and cy >= 0 and cy < TILE_COUNT and self.get_rock(
                        cx, cy).value == value:
                    count += 1
                    if count == 5:
                        return self.cleared(value)
                else:
                    count = 0

    def cleared(self, value):
        self.is_cleared = True
        winner = ''
        if value == WHITE:
            winner = 'White'
        else:
            winner = 'Black'
        self.emit_signal('game_finished', winner)

    def quantum_toggle(self, button_pressed):
        if button_pressed:
            self.backend = real_backend
        else:
            self.backend = sim_backend

    def _process(self, dt):
        if self.current_gate != None:
            dv = self.current_gate.position - self.get_local_mouse_position()
            if dv.x >= 0:
                if dv.y >= 0:
                    if dv.x >= dv.y:
                        self.current_gate.rotation_degrees = 180.0
                    else:
                        self.current_gate.rotation_degrees = 270.0
                else:
                    if dv.x >= -dv.y:
                        self.current_gate.rotation_degrees = 180.0
                    else:
                        self.current_gate.rotation_degrees = 90.0
            else:
                if dv.y >= 0:
                    if -dv.x >= dv.y:
                        self.current_gate.rotation_degrees = 0.0
                    else:
                        self.current_gate.rotation_degrees = 270.0
                else:
                    if -dv.x >= -dv.y:
                        self.current_gate.rotation_degrees = 0.0
                    else:
                        self.current_gate.rotation_degrees = 90.0

    def _ready(self):
        self.gameplay = self.get_parent()
        self.rocks_node = self.get_node('Rocks')
        self.gates_node = self.get_node('Gates')

        self.rocks_array = []
        for iy in range(TILE_COUNT):
            for ix in range(TILE_COUNT):
                y = (iy + 0.5) * TILE_SIZE
                x = (ix + 0.5) * TILE_SIZE
                rock = RockScene.instance()
                self.rocks_array.append(rock)
                self.rocks_node.add_child(rock)
                rock.position = Vector2(x, y)
                rock.cell_x, rock.cell_y = ix, iy
        self.quantum_rocks = {}
        self.quantum_rocks_inv = []
        self.quantum_gates = []
        self.quantum_operations = []

        self.set_process(True)