%matplotlib notebook

import pew
pew.init()
screen=pew.Pix()
# First we set variables of the game:

# The snake will be represented by a list of (x,y) coordinates. The last element of the list (snake[-1]) will
# represent the head of the snake. The 0th element of the list will be the last pixel of the tail. 

# Let's create the list by setting the initial position of the head at (3,3):
snake=[(3,3)]
# Now we create a variable to store the speed of the movement:
game_speed=5
# We also have to define the initial velocity of the snake. Let's set it to move in the x direction by 1 pixel.
dx, dy= 1, 0
# First we create and endless loop for the movement of the snake
while True:
    x,y=snake[-1] # We store the current position of the head in two variables x and y.
    screen.pixel(x, y, 3) # This command turns on the pixel (x,y) with the maximum brightnes (3) to print on
    # on the screen the current position of the head
    pew.show(screen) # Lights up the active pixels on the screen. We need to pew.show(screen) each frame of the game.
    pew.tick(1/game_speed) # Sets the refresh rate. When bigger "game_speed" the lower the refresh rate and the game
    # runs faster
# Now we have to check for user input by looking which keys are pressed. Depending on which keys are pressed the velocity of
# of the snake will change.
    keys=pew.keys() # Return a number telling which keys (or buttons) have been pressed since the
    # last check.  The number can then be filtered with the ``&`` operator and
    # the ``K_X``, ``K_DOWN``, ``K_LEFT``, ``K_RIGHT``, ``K_UP``, and ``K_O``
    # constants to see whether any of the keys was pressed.
    if keys & pew.K_UP and dy == 0:
            dx, dy = 0, -1
            # When the snake is moving in x and wee press UP the velocity of the snake changes to go up in the screen. 
            # Note that in the pew-pew screen the index of the top row is 0 and the index of the bottom row is 7.
            # This explains why we have dy=-1 when we press UP. The rest of the keys follow a similar logic.
    elif keys & pew.K_LEFT and dx == 0:
            dx, dy = -1, 0
    elif keys & pew.K_RIGHT and dx == 0:
            dx, dy = 1, 0
    elif keys & pew.K_DOWN and dy == 0:
            dx, dy = 0, 1
    # Now we calculate the next position of the snake's head:
    x=(x+dx)%8
    y=(y+dy)%8
    # We insert %8 to apply mod 8 to the coordinates, so when we pass through one of the walls we restart the counting and
    # appear in the other side of the screen.
    
    # We add to the list representing our snake a the next position of the head.
    snake.append((x,y))
    #We delete from the list the last position of the snake (in this case the head) and turn of that pixel
    (x,y)=snake.pop(0)
    screen.pixel(x,y,0)

import pew
pew.init()
screen=pew.Pix()
snake=[(3,3)]
game_speed=5
dx, dy= 1, 0
while True:
    x,y=snake[-1]
    screen.pixel(x, y, 3)
    pew.show(screen)
    pew.tick(1/game_speed)
    keys=pew.keys()
    if keys & pew.K_UP and dy == 0:
            dx, dy = 0, -1
    elif keys & pew.K_LEFT and dx == 0:
            dx, dy = -1, 0
    elif keys & pew.K_RIGHT and dx == 0:
            dx, dy = 1, 0
    elif keys & pew.K_DOWN and dy == 0:
            dx, dy = 0, 1
    #To implement the crashing mechanism we first have to get rid from the periodic screen, so we remove %8
    x=(x+dx)
    y=(y+dy)
    #Now add an if condition that breaks the while loop if any of the coordinates for the new head goes out from the grid.
    if x >7 or y > 7 or x < 0 or y < 0:
        # We create a loop for turning of all the pixels that forms the screen.
            for (i,j) in snake:
                screen.pixel(i,j,0)
            break
    snake.append((x,y))
    (x,y)=snake.pop(0)
    screen.pixel(x,y,0)
# Now that we are out the loop we show the "Game Over" screen:
text = pew.Pix.from_text("Game over!")
for dx in range(-8, text.width):
    screen.blit(text, -dx, 1)
    pew.show(screen)
    pew.tick(1 / 12) 

from aether import QuantumCircuit, simulate 
def qrand(nbits):
    # We import the necessary elements form the aether library
    circ= QuantumCircuit(1,1) # We create a circuit with 1 qubit register and 1 classical bit register
    circ.h(0) # We apply the Hadamard gate to the qubit (note that by default the qubits are already intializated at 0)
    circ.measure(0, 0) # We make a measurement in the qubit and we store the result in the classical bit
    val = simulate(circ, shots=nbits, get='memory') # We simulate three times the circuit and we store the results in a list
    # We create a string with the binary number to use the function int() to obtain the integer
    b='' 
    for i in range(nbits):
        b+= str(val[i])
    # Trnsform the string into an int
    integ=int(b,2)
    # Return the integer
    return integ

import pew
from aether import QuantumCircuit, simulate

# We define the function that we just created at the beginning of our code
def qrand(nbits):
    circ= QuantumCircuit(1,1)
    circ.h(0)
    circ.measure(0, 0)
    val = simulate(circ, shots=nbits, get='memory')
    b='' 
    for i in range(nbits):
        b+= str(val[i])
    integ=int(b,2)
    return integ

pew.init()
screen=pew.Pix()
snake=[(3,3)]
game_speed=5
dx, dy= 1, 0
# We must define the initial position of the apple:
apple_x, apple_y = 6, 6
screen.pixel(apple_x, apple_y, 3)

while True:
    x,y=snake[-1]
    screen.pixel(x, y, 3)
    pew.show(screen)
    pew.tick(1/game_speed)
    keys=pew.keys()
    if keys & pew.K_UP and dy == 0:
            dx, dy = 0, -1
    elif keys & pew.K_LEFT and dx == 0:
            dx, dy = -1, 0
    elif keys & pew.K_RIGHT and dx == 0:
            dx, dy = 1, 0
    elif keys & pew.K_DOWN and dy == 0:
            dx, dy = 0, 1
    x=(x+dx)
    y=(y+dy)
    # We need to add the condition that if the next position of the snake's head is in the snake's body the player dies
    if (x,y) in snake or x >7 or y > 7 or x < 0 or y < 0:
        # We create a loop for turning of all the pixels that forms the screen.
            for (i,j) in snake:
                screen.pixel(i,j,0)
            break
    snake.append((x,y))
    # In this loop we implement the generation of the apples each time the next position of the snake's head
    # lies into the current position of the apple
    if x == apple_x and y == apple_y:
        screen.pixel(apple_x, apple_y, 0)
        # We try the random generation of the apple until the apple appears out of the snake
        apple_x, apple_y = snake[0]
        while (apple_x, apple_y) in snake:
            apple_x=qrand(3)
            apple_y=qrand(3)
        screen.pixel(apple_x, apple_y, 2)
        # We also increase the velocity of the game each time the snake eats an apple
        gm_sp += 0.2
    # Otherwise we remove the tail of the snake
    else:
        x, y = snake.pop(0)
        screen.pixel(x, y, 0)
text = pew.Pix.from_text("Game over!")
for dx in range(-8, text.width):
    screen.blit(text, -dx, 1)
    pew.show(screen)
    pew.tick(1 / 12) 

import pew
from aether import QuantumCircuit, simulate
def qrand(nbits):
    circ= QuantumCircuit(1,1)
    circ.h(0)
    circ.measure(0, 0)
    val = simulate(circ, shots=nbits, get='memory')
    b='' 
    for i in range(nbits):
        b+= str(val[i])
    integ=int(b,2)
    return integ
pew.init()
screen=pew.Pix()
l=True # We initialize a boolean that will help us to reset the initial conditions of the game
while True:
    #Now we put the condition of reseting the initial variables only if l=True
    if l: 
        game_speed=5
        points=0 # We initialize a variable to count the points
        snake = [(3,3)]
        dx, dy = 1, 0
        apple_x, apple_y = 6, 6
        screen.pixel(apple_x, apple_y, 3)
        l=False
    while True:
        x,y=snake[-1]
        screen.pixel(x, y, 3)
        pew.show(screen)
        pew.tick(1/game_speed)
        keys=pew.keys()
        if keys & pew.K_UP and dy == 0:
                dx, dy = 0, -1
        elif keys & pew.K_LEFT and dx == 0:
                dx, dy = -1, 0
        elif keys & pew.K_RIGHT and dx == 0:
                dx, dy = 1, 0
        elif keys & pew.K_DOWN and dy == 0:
                dx, dy = 0, 1
        x=(x+dx)
        y=(y+dy)
        if (x,y) in snake or x >7 or y > 7 or x < 0 or y < 0:
                for (i,j) in snake:
                    screen.pixel(i,j,0)
                break
        snake.append((x,y))
        if x == apple_x and y == apple_y:
            screen.pixel(apple_x, apple_y, 0)
            apple_x, apple_y = snake[0]
            while (apple_x, apple_y) in snake:
                apple_x=qrand(3)
                apple_y=qrand(3)
            screen.pixel(apple_x, apple_y, 2)
            game_speed += 0.2
            # We add one to the points counter each time we eat an apple
            points=points+1
        else:
            x, y = snake.pop(0)
            screen.pixel(x, y, 0)
    l=True # We set l to True to restart to the initial variables in the next iteration
    #Now let's display the points in the screen:
    text = pew.Pix.from_text('Points: ' + str(points))
    for dx in range(-8, text.width):
        screen.blit(text, -dx, 1)
        pew.show(screen)
        pew.tick(1 / 12)
    text = pew.Pix.from_text("Game over!")
    for dx in range(-8, text.width):
        screen.blit(text, -dx, 1)
        pew.show(screen)
        pew.tick(1 / 12) 

