Esempio n. 1
0
def main():
    # Refresh rate (in seconds) for the keyboard.
    KEYBOARD_REFRESH_DELAY = 0.01

    # Specifies superposition window size.
    SUPERPOSITION_RANGE = 2

    # Setup the canvas and scale for the keyboard.
    stddraw.setCanvasSize(1056, 300)
    stddraw.setXscale(0, 1056)
    stddraw.setYscale(0, 300)

    # Create guitar strings for notes corresponding to the keys in keyboard.
    keyboard = 'q2we4r5ty7u8i9op-[=zxdcfvgbnjmk,.;/\' '
    notes = []
    for i in range(len(keyboard)):
        hz = 440 * 2**((i - 24.0) / 12.0)
        notes += [guitar_string.create(hz)]

    pressed = -1  # index of the pressed key
    start = time.clock()  # for refreshing the keyboard
    while True:
        # Check if the user has typed a key; if so, process it, ie, pluck
        # the corresponding string.
        if stddraw.hasNextKeyTyped():
            key = stddraw.nextKeyTyped()
            if key in keyboard:
                pressed = index(keyboard, key)
                guitar_string.pluck(notes[pressed])

        if pressed != -1:
            # Superimpose samples for keys that are within the
            # 2 * SUPERPOSITION_RANGE window centered at the pressed key.
            sample = 0.0
            b = max(0, pressed - SUPERPOSITION_RANGE)
            e = min(len(keyboard) - 1, pressed + SUPERPOSITION_RANGE)
            for i in range(b, e + 1):
                note = notes[i]
                sample += guitar_string.sample(note)
                guitar_string.tic(note)

            # Play the sample on standard audio.
            stdaudio.playSample(sample)

        # Display the keyboard with the pressed key in red, every
        # KEYBOARD_REFRESH_DELAY seconds.
        if time.clock() - start > KEYBOARD_REFRESH_DELAY:
            start = time.clock()
            draw_keyboard(pressed)
            stddraw.show(0.0)
Esempio n. 2
0
def main():
    # Draw the board
    stddraw.setXscale(0, config.N)
    stddraw.setYscale(0, config.N)
    stddraw.clear(stddraw.BLACK)

    snake = Snake()
    apple = Apple(snake)

    key = new_key = old_key = None
    # Main game loop
    while not end_game(snake):
        if stddraw.hasNextKeyTyped():
            new_key = stddraw.nextKeyTyped()
            if is_valid(new_key, old_key):
                key = new_key
                old_key = new_key
        if key:
            snake.move(key, apple)
        stddraw.show(100)

    print('Game Over!')
    print('Total points:', config.PTS)
Esempio n. 3
0
    def check_events(self):
        if stddraw.mousePressed():
            x = stddraw.mouseX()
            y = stddraw.mouseY()
            localized = self._localize_click(x, y)
            if localized:
                avenue = localized[0]
                street = localized[1]
                location = localized[2]
                if constants.NORTH <= location <= constants.WEST:
                    self._toggle_wall(avenue, street, location)
                elif location == TOP:
                    self._toggle_beeper(avenue, street, 1)
                elif location == BOTTOM:
                    self._toggle_beeper(avenue, street, -1)

        if stddraw.hasNextKeyTyped():
            key = stddraw.nextKeyTyped()
            if key == 'p':
                print(self._world)
            elif key == 's':
                with open(self._filename, 'w') as file:
                    file.write(str(self._world))
                print('Saved Karel world to file:', self._filename)
def main():

    stddraw.createWindow(1024, 256)
    stddraw.setPenRadius(0)
    stddraw.setXscale(0, _SAMPLES_PER_REDRAW)
    stddraw.setYscale(-0.75, +0.75)
    stddraw.show()

    # Create keyboardDict, a dictionary relating each keyboard key
    # to a guitar string.
    keyboardDict = {}
    i = 0
    for key in _KEYBOARD:
        factor = 2 ** ((i - 24) / 12.0)
        guitarString = guitarstring.GuitarString(_CONCERT_A * factor)
        keyboardDict[key] = guitarString
        i += 1

    # pluckedGuitarStrings is the set of all guitar strings that have
    # been plucked.
    pluckedGuitarStrings = set()

    t = 0

    # The main input loop.
    while True:

        if stddraw.hasNextKeyTyped():

            # Fetch the key that the user just typed.
            key = stddraw.nextKeyTyped()

            # Figure out which guitar string to pluck, and pluck it.
            try:
                guitarString = keyboardDict[key]
                guitarString.pluck()
                pluckedGuitarStrings.add(guitarString)
            except KeyError:
                pass

        # Add up the samples from each plucked guitar string. Also
        # advance the simulation of each plucked guitar string by
        # one step.
        sample = 0.0
        faintGuitarStrings = set()
        for guitarString in pluckedGuitarStrings:
            sample += guitarString.sample()
            guitarString.tic()
            if guitarString.isFaint():
                faintGuitarStrings.add(guitarString)

        # Remove faint guitar strings from the set of plucked guitar
        # strings.
        for guitarString in faintGuitarStrings:
            pluckedGuitarStrings.remove(guitarString)

        # Play the total.
        stdaudio.playSample(sample)

        # Plot
        stddraw.point(t % _SAMPLES_PER_REDRAW, sample)

        if t == (_SAMPLES_PER_REDRAW - 1):
            stddraw.show()
            stddraw.clear()
            t = 0

        t += 1
Esempio n. 5
0
def multiplier(key, notes):
    for index, num in enumerate(notes):
        if num == key:
            return index


def frequency(num):
    freq = 110.00 * 2**(num / 12)
    return freq


play = GuitarString(110.00)

p = Picture('piano.png')
stddraw.picture(p)
stddraw.show(0.0)

escape = False
while not escape:
    stddraw._checkForEvents()
    while stddraw.hasNextKeyTyped():
        key = stddraw.nextKeyTyped()
        if key == chr(27):
            escape = True
        freq = frequency(multiplier(key, notes))
        play = GuitarString(freq)
        play.pluck()

    y = play.tick()
    stdaudio.playSample(y)
Esempio n. 6
0
def start():
    # set the dimensions of the game grid
    grid_h, grid_w = 20, 12
    # set the size of the drawing canvas
    canvas_h, canvas_w = 40 * grid_h, 40 * grid_w
    stddraw.setCanvasSize(canvas_w, canvas_h)
    # set the scale of the coordinate system
    stddraw.setXscale(-0.5, grid_w - 0.5)
    stddraw.setYscale(-0.5, grid_h - 0.5)

    # create the game grid
    grid = GameGrid(grid_h, grid_w)
    # create the first tetromino to enter the game grid
    # by using the create_tetromino function defined below
    current_tetromino = create_tetromino(grid_h, grid_w)
    next_tetromino = create_tetromino(grid_h, grid_w)
    grid.current_tetromino = current_tetromino

    # display a simple menu before opening the game
    display_game_menu(grid_h, grid_w)
    # main game loop (keyboard interaction for moving the tetromino)
    while True:
        # check user interactions via the keyboard
        if stddraw.hasNextKeyTyped():
            key_typed = stddraw.nextKeyTyped()
            # if the left arrow key has been pressed
            if key_typed == "left":
                # move the tetromino left by one
                current_tetromino.move(key_typed, grid)
            # if the right arrow key has been pressed
            elif key_typed == "right":
                # move the tetromino right by one
                current_tetromino.move(key_typed, grid)
            # if the down arrow key has been pressed
            elif key_typed == "down":
                # move the tetromino down by one
                # (causes the tetromino to fall down faster)
                current_tetromino.move(key_typed, grid)
            # clear the queue that stores all the keys pressed/typed
            elif key_typed == "up":
                current_tetromino.rotateTetromino()
            elif key_typed == "space":
                temp = current_tetromino.move("down", grid)
                while (temp):
                    temp = current_tetromino.move("down", grid)
            stddraw.clearKeysTyped()

        # move (drop) the tetromino down by 1 at each iteration
        success = current_tetromino.move("down", grid)

        # place the tetromino on the game grid when it cannot go down anymore
        if not success:
            # get the tile matrix of the tetromino
            tiles_to_place = current_tetromino.tile_matrix
            # update the game grid by adding the tiles of the tetromino
            game_over = grid.update_grid(tiles_to_place)
            rowSet = rowsToCheck(tiles_to_place)
            grid.rowCheck(rowSet)
            columnSet = columnsToCheck(tiles_to_place)
            grid.sumCheck(columnSet, current_tetromino)
            # end the main game loop if the game is over
            if game_over:
                break
            # create the next tetromino to enter the game grid
            # by using the create_tetromino function defined below
            current_tetromino = next_tetromino
            grid.current_tetromino = current_tetromino
            next_tetromino = create_tetromino(grid_h, grid_w)
            print("Score = " + str(grid.score))
            print("Next tetromino type is: " + next_tetromino.type)

        # display the game grid and as well the current tetromino
        grid.display()

    print("Game over")
Esempio n. 7
0
def main():

    stddraw.createWindow()

    # Create keyboardDict, a dictionary relating each keyboard key
    # to a guitar string.
    keyboardDict = {}
    i = 0
    for key in _KEYBOARD:
        factor = 2 ** ((i - 24) / 12.0)
        guitarString = guitarstring.GuitarString(_CONCERT_A * factor)
        keyboardDict[key] = guitarString
        i += 1

    # pluckedGuitarStrings is the set of all guitar strings that have
    # been plucked.
    pluckedGuitarStrings = set()

    # loopCount is used to control the frequency of calls of
    # stddraw.show().
    loopCount = 1023

    # The main input loop.
    while True:

        # Call stddraw.show() occasionally to capture keyboard events.
        if loopCount == 1023:
            stddraw.show()
            loopCount = 0
        loopCount += 1

        if stddraw.hasNextKeyTyped():

            # Fetch the key that the user just typed.
            key = stddraw.nextKeyTyped()

            # Figure out which guitar string to pluck, and pluck it.
            try:
                guitarString = keyboardDict[key]
                guitarString.pluck()
                pluckedGuitarStrings.add(guitarString)
            except KeyError:
                pass

        # Add up the samples from each plucked guitar string. Also
        # advance the simulation of each plucked guitar string by
        # one step.
        sample = 0.0
        faintGuitarStrings = set()
        for guitarString in pluckedGuitarStrings:
            sample += guitarString.sample()
            guitarString.tic()
            if guitarString.isFaint():
                faintGuitarStrings.add(guitarString)

        # Remove faint guitar strings from the set of plucked guitar
        # strings.
        for guitarString in faintGuitarStrings:
            pluckedGuitarStrings.remove(guitarString)

        # Play the total.
        stdaudio.playSample(sample)
from balldeluxe import Ball

stddraw.setCanvasSize(500, 500)

stddraw.setXscale(-1.0, 1.0)
stddraw.setYscale(-1.0, 1.0)

balls = [
    Ball(.480, .860, .015, .023, .05, stddraw.RED),
    Ball(.480, .860, .030, .046, .1, stddraw.BLUE),
    Ball(.180, .260, .040, .026, .2, stddraw.GREEN)
]

while True:
    # get keystrokes
    if stddraw.hasNextKeyTyped():
        k = stddraw.nextKeyTyped()
        if k == stddraw.K_UP:
            for b in balls:
                b.increase_speed(0.1, 0.1)
        elif k == stddraw.K_DOWN:
            for b in balls:
                b.increase_speed(-0.1, -0.1)

    # update velocity
    for b in balls:
        b.update()

    # clear the background
    stddraw.clear(stddraw.LIGHT_GRAY)
Esempio n. 9
0
def start():

    global grid
    # create the game grid
    grid = GameGrid(grid_h, grid_w)
    # create the first tetromino to enter the game grid
    # by using the create_tetromino function defined below
    current_tetromino = create_tetromino(grid_h, grid_w)
    # print("next tetromino:")
    next_tetromino = create_tetromino(grid_h, grid_w)

    grid.current_tetromino = current_tetromino
    grid.next_tetromino = next_tetromino
    stddraw.clearKeysTyped()
    pause = False
    # main game loop (keyboard interaction for moving the tetromino)
    while True:

        if not pause:
            mx, my = stddraw.getPosition()
            tileX = grid.current_tetromino.bottom_left_corner.x
            ax = int(mx / 42.35) - 1
            # print(ax, tileX)

            if ax > tileX:
                for i in range(ax - tileX):
                    grid.current_tetromino.move("right", grid)
            elif ax < tileX:
                for i in range(tileX - ax):
                    grid.current_tetromino.move("left", grid)

        # check user interactions via the keyboard
        if stddraw.hasNextKeyTyped():
            key_typed = stddraw.nextKeyTyped()

            # Pause
            if key_typed == 'p':
                print("Pause")
                if pause:
                    pause = False
                else:
                    pause = True

            elif not pause:

                # if the left arrow key has been pressed
                if key_typed == "left":
                    # move the tetromino left by one
                    # print("Left Typed")
                    current_tetromino.move(key_typed, grid)
                # if the right arrow key has been pressed
                elif key_typed == "right":
                    # print("Right Typed")
                    # move the tetromino right by one
                    current_tetromino.move(key_typed, grid)
                # if the down arrow key has been pressed
                elif key_typed == "down":

                    # move the tetromino down by one
                    # (causes the tetromino to fall down faster)
                    current_tetromino.move(key_typed, grid)
                # piece drop
                elif key_typed == 'space':
                    for i in range(grid_h):
                        current_tetromino.move('down', grid)
                # Speed Increase
                elif key_typed == 'w':
                    if grid.delta_time > 50:
                        grid.delta_time -= 40
                # Speed Decrease
                elif key_typed == 's':
                    if grid.delta_time < 500:
                        grid.delta_time += 40

                elif key_typed == 'e':
                    current_tetromino.rotate(grid)

                elif key_typed == 'q':
                    current_tetromino.rotate_ccw(grid)

            if key_typed == 'r':
                print("restart")
                start()

            # clear the queue that stores all the keys pressed/typed
            stddraw.clearKeysTyped()

        # move (drop) the tetromino down by 1 at each iteration
        if not pause:
            success = current_tetromino.move("down", grid)

        # place the tetromino on the game grid when it cannot go down anymore
        if not success and not pause:
            # get the tile matrix of the tetromino
            tiles_to_place = current_tetromino.tile_matrix
            # update the game grid by adding the tiles of the tetromino
            game_over = grid.update_grid(tiles_to_place)
            # end the main game loop if the game is over
            if game_over:
                if display_game_over(grid_h, grid_w + 5):
                    pause = True
                    start()

            # create the next tetromino to enter the game grid
            # by using the create_tetromino function defined below
            current_tetromino = next_tetromino
            grid.current_tetromino = current_tetromino
            print("next tetromino:")
            next_tetromino = create_tetromino(grid_h, grid_w)
            grid.next_tetromino = next_tetromino
            next_tetromino.draw_dummy()

        # display the game grid and as well the current tetromino
        grid.display(pause)

    print("Game over")
Esempio n. 10
0
def start():
    # set the dimensions of the game grid
    grid_h, grid_w = 17, 12
    # set the size of the drawing canvas
    canvas_h, canvas_w = 40 * grid_h, 40 * grid_w + 100
    stddraw.setCanvasSize(canvas_w, canvas_h)
    # set the scale of the coordinate system
    stddraw.setXscale(-0.5, grid_w + 3)
    stddraw.setYscale(-0.5, grid_h - 0.5)

    # create the game grid
    grid = GameGrid(grid_h, grid_w)
    # create the first tetromino to enter the game grid
    # by using the create_tetromino function defined below
    current_tetromino = create_tetromino(grid_h, grid_w)
    grid.current_tetromino = current_tetromino

    # display a simple menu before opening the game
    display_game_menu(grid_h, grid_w)

    # initial score
    score = 0
    speed = 250  #initial speed

    # main game loop (keyboard interaction for moving the tetromino)
    while True:
        # check user interactions via the keyboard
        if stddraw.hasNextKeyTyped():
            key_typed = stddraw.nextKeyTyped()
            # if the left arrow key has been pressed
            if key_typed == "left":
                # move the tetromino left by one
                current_tetromino.move(key_typed, grid)
            # if the right arrow key has been pressed
            elif key_typed == "right":
                # move the tetromino right by one
                current_tetromino.move(key_typed, grid)
            # if the down arrow key has been pressed
            elif key_typed == "down":
                # move the tetromino down by one
                # (causes the tetromino to fall down faster)
                current_tetromino.move(key_typed, grid)
            elif key_typed == "up":
                # rotate the tetromino 90 degree clock-wise
                current_tetromino.rotate(grid)
            elif key_typed == "space":
                # drop the tetromino
                for i in range(grid_h):
                    current_tetromino.move("down", grid)

            # clear the queue that stores all the keys pressed/typed
            stddraw.clearKeysTyped()

        # move (drop) the tetromino down by 1 at each iteration
        success = current_tetromino.move("down", grid)
        grid.connected_4()

        # place the tetromino on the game grid when it cannot go down anymore
        if not success:
            # get the tile matrix of the tetromino
            tiles_to_place = current_tetromino.tile_matrix
            # update the game grid by adding the tiles of the tetromino
            game_over = grid.update_grid(tiles_to_place)

            indv_score = 0  # starting value for a full row's score
            ind_score = 0  # starting value for a merged tiles score

            # check is_row_full for all rows
            for i in range(grid_h):
                grid.check_2048(grid.tile_matrix)
                # score from merged tiles
                ind_score = grid.update_score(grid.tile_num2)
                if grid.is_row_full(i, grid.tile_matrix):
                    # score from deleted full rows
                    indv_score = grid.update_score(grid.tile_num)
            grid.tile_num2 = np.zeros(100)  # for merged score
            score_val = ind_score + indv_score
            score += int(score_val)

            print(score)
            # end the main game loop if the game is over
            if game_over:
                break
            # increasing difficulty by increasing speed as the game process
            if score > 450:
                speed = 10
            elif score > 250:
                speed = 50
            elif score > 150:
                speed = 100
            if score > 10000:
                break
            # create the next tetromino to enter the game grid
            # by using the create_tetromino function defined below
            current_tetromino = create_tetromino(grid_h, grid_w)
            grid.current_tetromino = current_tetromino

        # display the game grid and as well the current tetromino
        grid.display(score, speed)

    # finish the game and display game over
    finish_game(grid_h, grid_w)
    print("Game over")
Esempio n. 11
0
def main():

    stddraw.createWindow()

    # Create keyboardDict, a dictionary relating each keyboard key
    # to a guitar string.
    keyboardDict = {}
    i = 0
    for key in _KEYBOARD:
        factor = 2**((i - 24) / 12.0)
        guitarString = guitarstring.GuitarString(_CONCERT_A * factor)
        keyboardDict[key] = guitarString
        i += 1

    # pluckedGuitarStrings is the set of all guitar strings that have
    # been plucked.
    pluckedGuitarStrings = set()

    # loopCount is used to control the frequency of calls of
    # stddraw.show().
    loopCount = 1023

    # The main input loop.
    while True:

        # Call stddraw.show() occasionally to capture keyboard events.
        if loopCount == 1023:
            stddraw.show()
            loopCount = 0
        loopCount += 1

        if stddraw.hasNextKeyTyped():

            # Fetch the key that the user just typed.
            key = stddraw.nextKeyTyped()

            # Figure out which guitar string to pluck, and pluck it.
            try:
                guitarString = keyboardDict[key]
                guitarString.pluck()
                pluckedGuitarStrings.add(guitarString)
            except KeyError:
                pass

        # Add up the samples from each plucked guitar string. Also
        # advance the simulation of each plucked guitar string by
        # one step.
        sample = 0.0
        faintGuitarStrings = set()
        for guitarString in pluckedGuitarStrings:
            sample += guitarString.sample()
            guitarString.tic()
            if guitarString.isFaint():
                faintGuitarStrings.add(guitarString)

        # Remove faint guitar strings from the set of plucked guitar
        # strings.
        for guitarString in faintGuitarStrings:
            pluckedGuitarStrings.remove(guitarString)

        # Play the total.
        stdaudio.playSample(sample)
def main():

    stddraw.createWindow(1024, 256)
    stddraw.setPenRadius(0)
    stddraw.setXscale(0, _SAMPLES_PER_REDRAW)
    stddraw.setYscale(-.75, +.75)
    stddraw.show()

    # Create keyboardDict, a dictionary relating each keyboard key
    # to a guitar string.
    keyboardDict = {}
    i = 0
    for key in _KEYBOARD:
        factor = 2 ** ((i-24) / 12.0)
        guitarString = guitarstring.GuitarString(_CONCERT_A * factor)
        keyboardDict[key] = guitarString
        i += 1

    # pluckedGuitarStrings is the set of all guitar strings that have
    # been plucked.
    pluckedGuitarStrings = set()

    t = 0

    # The main input loop.
    while True:

        if stddraw.hasNextKeyTyped():

            # Fetch the key that the user just typed.
            key = stddraw.nextKeyTyped()

            # Figure out which guitar string to pluck, and pluck it.
            try:
                guitarString = keyboardDict[key]
                guitarString.pluck()
                pluckedGuitarStrings.add(guitarString)
            except KeyError:
                pass

        # Add up the samples from each plucked guitar string. Also
        # advance the simulation of each plucked guitar string by
        # one step.
        sample = 0.0
        faintGuitarStrings = set()
        for guitarString in pluckedGuitarStrings:
            sample += guitarString.sample()
            guitarString.tic()
            if guitarString.isFaint():
                faintGuitarStrings.add(guitarString)

        # Remove faint guitar strings from the set of plucked guitar
        # strings.
        for guitarString in faintGuitarStrings:
            pluckedGuitarStrings.remove(guitarString)

        # Play the total.
        stdaudio.playSample(sample)

        # Plot
        stddraw.point(t % _SAMPLES_PER_REDRAW, sample);

        if t == (_SAMPLES_PER_REDRAW - 1):
            stddraw.show()
            stddraw.clear()
            t = 0

        t += 1
Esempio n. 13
0
3。do other stuff in  a small finite amount of time


"""
import stddraw

# while we don't want to quit

#respond to those events
circle_radius = 0.05
#do other things I need to

while True:
    mouse_clicked = stddraw.mousePressed()  # return boolean

    key_has_been_typed = stddraw.hasNextKeyTyped()

    if mouse_clicked:

        x = stddraw.mouseX()
        y = stddraw.mouseY()
        stddraw.filledCircle(x, y, circle_radius)

    if key_has_been_typed:
        the_key = stddraw.hasNextKeyTyped()
        if the_key == "1":
            circle_radius = 0.01

        elif the_key == "2":
            circle_radius = 0.02