Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
File renamed without changes.
72 changes: 72 additions & 0 deletions DeveshAustineAtharv/DeveshAtharvAustine/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
"""
Quantum version of the classic Pong game
"""

import random

import pygame
from pygame import DOUBLEBUF, HWSURFACE, FULLSCREEN

from qpong.utils.input import Input
from qpong.utils.level import Level
from qpong.utils.scene import Scene
from qpong.viz.statevector_grid import StatevectorGrid
from qpong.utils.parameters import (
WINDOW_SIZE,
CLASSICAL_COMPUTER,
QUANTUM_COMPUTER,
WIN_SCORE,
WIDTH_UNIT,
MEASURE_RIGHT,
)


def main():
"""
Main game loop
"""

if not pygame.get_init():
print("Warning, fonts disabled")
pygame.init()

if not pygame.font.get_init():
print("Warning, fonts disabled")
pygame.font.init()

if not pygame.mixer.get_init():
print("Warning, sound disabled")
pygame.mixer.init()

# hardware acceleration to reduce flickering. Works only in full screen
flags = DOUBLEBUF | HWSURFACE | FULLSCREEN
screen = pygame.display.set_mode(WINDOW_SIZE, flags)


# clock for timing
clock = pygame.time.Clock()
old_clock = pygame.time.get_ticks()

# initialize scene, level and input Classes
scene = Scene()
level = Level()
input = Input()

# update the screen
pygame.display.flip()

# Main Loop
while input.running:
# set maximum frame rate
clock.tick(60)
# refill whole screen with black color at each frame
screen.fill((0, 0, 0))
# handle input events
input.handle_input(level, screen, scene)

level.statevector_grid.print_statevector(5)
pygame.quit()


if __name__ == "__main__":
main()
File renamed without changes.
File renamed without changes.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,3 @@ def arrange(self):
next_xpos = self.xpos
next_ypos = self.ypos
sprite_list = self.sprites()

for sprite in sprite_list:
sprite.rect.left = next_xpos
sprite.rect.top = next_ypos
next_ypos += sprite.rect.height
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,32 @@ def __init__(self, xpos, ypos, circuit_grid_model):
self.selected_column = 0
self.circuit_grid_background = CircuitGridBackground(circuit_grid_model)
self.circuit_grid_cursor = CircuitGridCursor()
self.music_grid_cursor = MusicGridCursor()
self.gate_tiles = np.empty(
(circuit_grid_model.max_wires, circuit_grid_model.max_columns),
dtype=CircuitGridGate,
)
self.music_tiles = np.empty(
(circuit_grid_model.max_wires, circuit_grid_model.max_columns),
dtype=CircuitGridGate,
)

for row_idx in range(self.circuit_grid_model.max_wires):
for col_idx in range(self.circuit_grid_model.max_columns):
self.gate_tiles[row_idx][col_idx] = CircuitGridGate(
circuit_grid_model, row_idx, col_idx
)
self.music_tiles[row_idx][col_idx] = CircuitGridGate(
circuit_grid_model, row_idx, col_idx
)

pygame.sprite.RenderPlain.__init__(
self,
self.circuit_grid_background,
self.gate_tiles,
self.music_tiles,
self.circuit_grid_cursor,
self.music_grid_cursor
)
self.update()

Expand All @@ -87,7 +97,14 @@ def update(self):
].rect.centerx = self.xpos + GRID_WIDTH * (col_idx + 1.5)
self.gate_tiles[row_idx][
col_idx
].rect.centery = self.ypos + GRID_HEIGHT * (row_idx + 1.0)
].rect.centery = self.ypos + GRID_HEIGHT * ((row_idx + 1) * 0.65)

self.music_tiles[row_idx][
col_idx
].rect.centerx = self.xpos + GRID_WIDTH * (col_idx + 1.5)
self.music_tiles[row_idx][
col_idx
].rect.centery = self.ypos + GRID_HEIGHT * ((row_idx + 1) * 0.65 - 200)

self.highlight_selected_node(self.selected_wire, self.selected_column)

Expand All @@ -107,9 +124,21 @@ def highlight_selected_node(self, wire_num, column_num):
+ GRID_WIDTH * (self.selected_column + 1)
+ round(0.375 * WIDTH_UNIT)
)
self.music_grid_cursor.rect.left = (
(self.xpos)
+ GRID_WIDTH * (self.selected_column + 1)
+ round(0.375 * WIDTH_UNIT)
)
self.circuit_grid_cursor.rect.top = (
self.ypos
+ GRID_HEIGHT * (self.selected_wire + 0.5)
+ 10
+ GRID_HEIGHT * (self.selected_wire * 0.65)
+ round(0.375 * WIDTH_UNIT)
)
self.music_grid_cursor.rect.top = (
(self.ypos + 250)
+ 10
+ GRID_HEIGHT * (self.selected_wire * 0.65)
+ round(0.375 * WIDTH_UNIT)
)

Expand All @@ -135,11 +164,11 @@ def move_to_adjacent_node(self, direction):
and self.selected_column < self.circuit_grid_model.max_columns - 1
):
self.selected_column += 1
elif direction == MOVE_UP and self.selected_wire > 0:
elif direction == MOVE_UP and self.selected_wire >= 0:
self.selected_wire -= 1
elif (
direction == MOVE_DOWN
and self.selected_wire < self.circuit_grid_model.max_wires - 1
and self.selected_wire < 4
):
self.selected_wire += 1

Expand Down Expand Up @@ -478,21 +507,29 @@ class CircuitGridBackground(pygame.sprite.Sprite):
def __init__(self, circuit_grid_model):
pygame.sprite.Sprite.__init__(self)

self.image = pygame.Surface([GRID_WIDTH * (18 + 2), GRID_HEIGHT * (3 + 1)])
self.image = pygame.Surface([GRID_WIDTH * (18 + 2), GRID_HEIGHT * (9 + 1)])
self.image.convert()
self.image.fill(WHITE)
self.rect = self.image.get_rect()
pygame.draw.rect(self.image, BLACK, self.rect, LINE_WIDTH)

for wire_num in range(circuit_grid_model.max_wires):
for wire_num in range(5):
pygame.draw.line(
self.image,
BLACK,
(GRID_WIDTH * 0.5, (wire_num + 1) * GRID_HEIGHT),
(self.rect.width - (GRID_WIDTH * 0.5), (wire_num + 1) * GRID_HEIGHT),
MAGENTA,
(GRID_WIDTH * 0.5, (wire_num * 0.65) * GRID_HEIGHT),
(self.rect.width - (GRID_WIDTH * 0.5), (wire_num * 0.65) * GRID_HEIGHT),
LINE_WIDTH,
)

for wire_num in range(5):
pygame.draw.line(
self.image,
BLACK,
(GRID_WIDTH * 0.5, 250 + (wire_num * 0.65) * GRID_HEIGHT),
(self.rect.width - (GRID_WIDTH * 0.5), 250 + (wire_num * 0.65) * GRID_HEIGHT),
LINE_WIDTH,
)

class CircuitGridGate(pygame.sprite.Sprite):
"""
Expand Down Expand Up @@ -618,3 +655,13 @@ def __init__(self):
"cursor_images/circuit-grid-cursor-medium.png", -1
)
self.image.convert_alpha()

class MusicGridCursor(pygame.sprite.Sprite):
"""Cursor to highlight current music node"""

def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image(
"cursor_images/circuit-grid-cursor-medium.png", -1
)
self.image.convert_alpha()
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ class CircuitGridModel:
"""

def __init__(self, max_wires, max_columns):
self.max_wires = max_wires
self.max_wires = 5
self.max_columns = max_columns
self.nodes = np.full(
(max_wires, max_columns),
(self.max_wires, max_columns),
CircuitGridNode(node_types.EMPTY),
dtype=CircuitGridNode,
)
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
File renamed without changes.
118 changes: 118 additions & 0 deletions DeveshAustineAtharv/DeveshAtharvAustine/qpong/utils/input.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#
# Copyright 2022 the original author or authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

"""
Quantum player input events and control
"""

import numpy as np

import pygame

from qpong.utils.navigation import MOVE_UP, MOVE_DOWN, MOVE_LEFT, MOVE_RIGHT


class Input:
"""
Handle input events
"""

def __init__(self):
self.running = True

def handle_input(self, level, screen, scene):
# pylint: disable=too-many-branches disable=too-many-statements
"""
Handle quantum player input
"""

circuit_grid = level.circuit_grid

# Handle Input Events
for event in pygame.event.get():
pygame.event.pump()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
self.running = False
elif event.key == pygame.K_a:
circuit_grid.move_to_adjacent_node(MOVE_LEFT)
circuit_grid.draw(screen)
pygame.display.flip()
elif event.key == pygame.K_d:
circuit_grid.move_to_adjacent_node(MOVE_RIGHT)
circuit_grid.draw(screen)
pygame.display.flip()
elif event.key == pygame.K_w:
circuit_grid.move_to_adjacent_node(MOVE_UP)
circuit_grid.draw(screen)
pygame.display.flip()
elif event.key == pygame.K_s:
circuit_grid.move_to_adjacent_node(MOVE_DOWN)
circuit_grid.draw(screen)
pygame.display.flip()
elif event.key == pygame.K_x:
circuit_grid.handle_input_x()
circuit_grid.draw(screen)
pygame.display.flip()
elif event.key == pygame.K_y:
circuit_grid.handle_input_y()
circuit_grid.draw(screen)
pygame.display.flip()
elif event.key == pygame.K_z:
circuit_grid.handle_input_z()
circuit_grid.draw(screen)
pygame.display.flip()
elif event.key == pygame.K_h:
circuit_grid.handle_input_h()
circuit_grid.draw(screen)
pygame.display.flip()
elif event.key == pygame.K_SPACE:
circuit_grid.handle_input_delete()
circuit_grid.draw(screen)
pygame.display.flip()
elif event.key == pygame.K_c:
# Add or remove a control
circuit_grid.handle_input_ctrl()
circuit_grid.draw(screen)
pygame.display.flip()
elif event.key == pygame.K_UP:
# Move a control qubit up
circuit_grid.handle_input_move_ctrl(MOVE_UP)
circuit_grid.draw(screen)
pygame.display.flip()
elif event.key == pygame.K_DOWN:
# Move a control qubit down
circuit_grid.handle_input_move_ctrl(MOVE_DOWN)
circuit_grid.draw(screen)
pygame.display.flip()
elif event.key == pygame.K_LEFT:
# Rotate a gate
circuit_grid.handle_input_rotate(-np.pi / 8)
circuit_grid.draw(screen)
pygame.display.flip()
elif event.key == pygame.K_RIGHT:
# Rotate a gate
circuit_grid.handle_input_rotate(np.pi / 8)
circuit_grid.draw(screen)
pygame.display.flip()
@staticmethod
def move_update_circuit_grid_display(screen, circuit_grid, direction):
"""
Update circuit grid after move
"""
circuit_grid.move_to_adjacent_node(direction)
circuit_grid.draw(screen)
pygame.display.flip()
Loading