コード例 #1
0
def game_scene():
    # this function keeps the information of the buttons
    a_button_pressed = constants.button_state["button_up"]
    b_button_pressed = constants.button_state["button_up"]
    start_button_pressed = constants.button_state["button_up"]
    select_button_pressed = constants.button_state["button_up"]
    # LED
    # get sound ready
    pew_sound = open("pew.wav", 'rb')
    boom_sound = open("boom.wav", 'rb')
    sound = ugame.audio
    sound.stop()
    sound.mute(False)

    # an image bank for CircuitPython
    image_bank_1 = stage.Bank.from_bmp16("space_aliens.bmp")

    #
    # sets the background to image 0 in the bank
    background = stage.Grid(image_bank_1, constants.SCREEN_GRID_X,
                            constants.SCREEN_GRID_Y)
    for x_location in range(constants.SCREEN_GRID_X):
        for y_location in range(constants.SCREEN_GRID_Y):
            tile_picked = random.randint(1, 3)
            background.tile(x_location, y_location, tile_picked)

    # a list of sprites that will be updated every frame
    sprites = []

    lasers = []
    for laser_number in range(constants.TOTAL_NUMBER_OF_LASERS):
        a_single_laser = stage.Sprite(image_bank_1, 10, constants.OFF_SCREEN_X,
                                      constants.OFF_SCREEN_X)
        lasers.append(a_single_laser)

    ship = stage.Sprite(
        image_bank_1, 5,
        int(constants.SCREEN_X / 2 - constants.SPRITE_SIZE / 2),
        int(constants.SCREEN_Y - constants.SPRITE_SIZE +
            constants.SPRITE_SIZE / 2))
    sprites.append(ship)  # Insert at the top of the sprite list

    # create a stage for the background to show up on
    #   and set the frame rate to 60fps
    game = stage.Stage(ugame.display, constants.FPS)
    # set the layers, items show up in order
    game.layers = sprites + [background]
    # render the background and initial location of sprite list
    # most likely you will only render background once per scene
    game.render_block()

    # repeat forever, game loop
    while True:
        # get user input
        keys = ugame.buttons.get_pressed()
        # print(keys)
        # A button to fire
        if keys & ugame.K_X != 0:
            if a_button_pressed == constants.button_state["button_up"]:
                a_button_pressed = constants.button_state[
                    "button_just_pressed"]
            elif a_button_pressed == constants.button_state[
                    "button_just_pressed"]:
                a_button_pressed = constants.button_state[
                    "button_still_pressed"]
        else:
            a_button_pressed = constants.button_state["button_up"]

        # update game logic

        # move ship right
        if keys & ugame.K_RIGHT != 0:
            if ship.x > constants.SCREEN_X - constants.SPRITE_SIZE:
                ship.move(constants.SCREEN_X - constants.SPRITE_SIZE, ship.y)
            else:
                ship.move(ship.x + 1, ship.y)

        # move ship left
        if keys & ugame.K_LEFT != 0:
            if ship.x < 0:
                ship.move(0, ship.y)
            else:
                ship.move(ship.x - 1, ship.y)

        # play sound if A is pressed
        if a_button_pressed == constants.button_state["button_just_pressed"]:
            # fire a laser, if we have enough power
            for laser_number in range(len(lasers)):
                if lasers[laser_number].x < 0:
                    lasers[laser_number].move(ship.x, ship.y)
                    pixel[0] = COLOR_two
                    pixel[1] = COLOR_two
                    pixel[2] = COLOR_two
                    pixel[3] = COLOR_two
                    pixel[4] = COLOR_two
                    time.sleep(DELAY)
                    pixel[0] = CLEAR
                    pixel[1] = CLEAR
                    pixel[2] = CLEAR
                    pixel[3] = CLEAR
                    pixel[4] = CLEAR
                    sound.stop()
                    sound.play(pew_sound)
                    break
        # each frame move the lasers, that have been fired, up
        for laser_number in range(len(lasers)):
            if lasers[laser_number].x > 0:
                lasers[laser_number].move(
                    lasers[laser_number].x,
                    lasers[laser_number].y - constants.LASER_SPEED)
                if lasers[laser_number].y < constants.OFF_TOP_SCREEN:
                    lasers[laser_number].move(constants.OFF_SCREEN_X,
                                              constants.OFF_SCREEN_Y)

        # redraw sprite list
        game.render_sprites(sprites)
        game.tick()  # wait until refresh rate finishes
コード例 #2
0
def game_scene():
    # this function is the main game scene

    # import sound
    damage_sound = open("damage.wav", 'rb')
    boop_sound = open("boop.wav", 'rb')
    sound = ugame.audio
    sound.stop()
    sound.mute(False)

    # health, score, combo

    score = 0

    score_text = stage.Text(width=29, height=14)
    score_text.clear()
    score_text.cursor(0, 0)
    score_text.move(1, 1)
    score_text.text("Score: {0}".format(score))

    # imports image bank .bmp file
    image_bank = stage.Bank.from_bmp16("space_aliens_background.bmp")

    # sets the background to image 0 in the image Bank
    #   and the size (10x8 tiles of size 16x16)
    background = stage.Grid(image_bank, constants.SCREEN_GRID_X,
                            constants.SCREEN_GRID_Y)

    # sets randomized starry background
    for x_location in range(constants.SCREEN_GRID_X):
        for y_location in range(constants.SCREEN_GRID_Y):
            tile_picked = random.randint(0, 3)
            background.tile(x_location, y_location, tile_picked)

    # sets the lanes
    for x_location in range(5, 9):
        for y_location in range(constants.SCREEN_GRID_Y):
            background.tile(x_location, y_location,
                            4)  # 4th element = straight ship

    # places target pads
    background.tile(5, constants.SCREEN_GRID_Y - 1,
                    8)  # 8th element = chill squid
    background.tile(6, constants.SCREEN_GRID_Y - 1, 8)
    background.tile(7, constants.SCREEN_GRID_Y - 1, 8)
    background.tile(8, constants.SCREEN_GRID_Y - 1, 8)

    def show_note():
        # this function takes a note from off the screen and moves it on screen,
        #    picking 1 of 4 different lanes
        for note_number in range(len(notes)):
            if notes[note_number].x < 0:
                lane_picked = random.randint(5, 8)
                notes[note_number].move(lane_picked * constants.SPRITE_SIZE,
                                        constants.OFF_TOP_SCREEN)
                break

    # create a list of notes
    notes = []
    for note_number in range(constants.TOTAL_NUMBER_OF_NOTES):
        # (bmp file, element, x, y)
        a_single_note = stage.Sprite(image_bank, 9, constants.OFF_SCREEN_X,
                                     constants.OFF_SCREEN_Y)
        notes.append(a_single_note)

    # summon one note to start off
    show_note()

    # create a stage for the background to show up on
    #   and set the frame rate to 60fps
    game = stage.Stage(ugame.display, constants.FPS)
    # set the layers, items show up in order
    game.layers = [score_text] + notes + [background]
    # render the background and initial location of sprite list
    game.render_block()

    # repeat forever, game loop
    # repeatedly checks for player inputs, collisions, moves notes
    while True:
        # downward note movement, only for on-screen notes
        for note_number in range(len(notes)):
            if notes[note_number].x > 0:
                notes[note_number].move(
                    notes[note_number].x,
                    notes[note_number].y + constants.STARTING_NOTE_SPEED)
                # if note goes below screen:
                if notes[note_number].y > constants.SCREEN_Y:
                    notes[note_number].move(constants.OFF_SCREEN_X,
                                            constants.OFF_SCREEN_Y)
                    show_note()
                    # the game ends when the player misses a note
                    sound.play(damage_sound)

                    time.sleep(1.0)

                    game_over_scene(score)

        # redraw ONLY sprites
        game.render_sprites(notes)  # pads will be added later
        game.tick()
コード例 #3
0
def menu_scene():
    # this function is a scene

    # an image bank for CircuitPython
    image_bank_2 = stage.Bank.from_bmp16("mt_game_studio.bmp")

    # sets the background to image 0 in the bank
    background = stage.Grid(image_bank_2, constants.SCREEN_GRID_X,
                            constants.SCREEN_GRID_Y)

    # used this program to split the iamge into tile: https://ezgif.com/
    # sprite-cutter/ezgif-5-818cdbcc3f66.png

    background.tile(2, 2, 0)  # blank white
    background.tile(3, 2, 1)
    background.tile(4, 2, 2)
    background.tile(5, 2, 3)
    background.tile(6, 2, 4)
    background.tile(7, 2, 0)  # blank white

    background.tile(2, 3, 0)  # blank white
    background.tile(3, 3, 5)
    background.tile(4, 3, 6)
    background.tile(5, 3, 7)
    background.tile(6, 3, 8)
    background.tile(7, 3, 0)  # blank white

    background.tile(2, 4, 0)  # blank white
    background.tile(3, 4, 9)
    background.tile(4, 4, 10)
    background.tile(5, 4, 11)
    background.tile(6, 4, 12)
    background.tile(7, 4, 0)  # blank white

    background.tile(2, 5, 0)  # blank white
    background.tile(3, 5, 0)
    background.tile(4, 5, 13)
    background.tile(5, 5, 14)
    background.tile(6, 5, 0)
    background.tile(7, 5, 0)  # blank white

    # a list of sprites
    sprites = []

    # add text objects
    text = []

    text1 = stage.Text(width=29,
                       height=14,
                       font=None,
                       palette=constants.NEW_PALETTE,
                       buffer=None)
    text1.move(20, 10)
    text1.text("MT Game Studios")
    text.append(text1)

    text2 = stage.Text(width=29,
                       height=14,
                       font=None,
                       palette=constants.NEW_PALETTE,
                       buffer=None)
    text2.move(40, 110)
    text2.text("PRESS START")
    text.append(text2)

    # create a stage for the background to show up on
    #   and set the frame rate to 60fps
    game = stage.Stage(ugame.display, 60)
    # set the layers, items show up in order
    game.layers = text + sprites + [background]
    # render the background and inital location of sprite list
    # most likely you will only render background once per scene
    game.render_block()

    # repeat forever, game loop
    while True:
        # get user input

        # update game logic
        keys = ugame.buttons.get_pressed()
        # print(keys)

        if keys & ugame.K_START != 0:  # Start button
            gamescene()

        # game.render_sprites(sprites)
        game.tick()  # wait until refresh rate finishes
コード例 #4
0
    #
    # Stage 0
    # This is a callback stage, demonstrating how python logic is used
    # to create multiple cluster-based processing instances of the same
    # core FreeSurfer command, each with slightly different operating
    # flags.
    # 
    #
    # PRECONDITIONS:
    # o Check that script is running on a cluster node.
    # 
    stage0 = stage.Stage(
                        name            = 'mris_calc',
                        fatalConditions = True,
                        syslog          = True,
                        logTo           = 'MC-mris_calc.log',
                        logTee          = True
                        )
    def f_stage0callback(**kwargs):
        str_cwd         =  os.getcwd()
        for key, val in kwargs.iteritems():
            if key == 'operand':        l_operand       = val
            if key == 'obj':            stage           = val
            if key == 'pipe':           pipeline        = val
        count = 0
        for i in range(1, len(l_operand)):
            if i == 1: str_previousOutput = l_operand[i-1]
            str_cumulativeOutput = "%d-%s" % (i, args.output)
            str_cmd = 'mris_calc -o %s %s %s %s ; cp %s %s' % \
                        (str_cumulativeOutput, str_previousOutput, args.operation, l_operand[i],
コード例 #5
0
def gamescene():

    # an image bank for CircuitPython
    image_bank_1 = stage.Bank.from_bmp16("space_aliens.bmp")

    # a list of sprites
    sprites = []

    # button state information
    a_button = constants.button_state["button_up"]
    b_button = constants.button_state["button_up"]
    start_button = constants.button_state["button_up"]
    select_button = constants.button_state["button_up"]

    # get sound ready
    pew_sound = open("pew.wav", 'rb')
    sound = ugame.audio
    sound.stop()
    sound.mute(False)

    # sets the background to image 0 in the bank
    background = stage.Grid(image_bank_1, constants.SCREEN_X,
                            constants.SCREEN_Y)
    for x_location in range(constants.SCREEN_GRID_X):
        for y_location in range(constants.SCREEN_GRID_Y):
            tile_picked = random.randint(1, 3)
            background.tile(x_location, y_location, tile_picked)

    # create a sprite
    # parameters (image bank, image # in bank, x, y)
    alien = stage.Sprite(image_bank_1, 7, 70, 35)
    sprites.append(alien)
    ship = stage.Sprite(image_bank_1, 4, 12, 112)
    sprites.insert(0, ship)  # insert at top of sprite list

    # create a stage for the background to show up on
    #   and set the frame rate to 60fps
    game = stage.Stage(ugame.display, 60)
    # set the background layer
    game.layers = sprites + [background]
    # render the background
    game.render_block()

    # repeat forever, game loop
    while True:
        # get user input
        keys = ugame.buttons.get_pressed()
        # (keys)
        if keys & ugame.K_X != 0:  # a button (fire)
            if a_button == constants.button_state["button_up"]:
                a_button = constants.button_state["button_just_pressed"]
            elif a_button == constants.button_state["button_just_pressed"]:
                a_button = constants.button_state["button_still_pressed"]
        else:
            if a_button == constants.button_state["button_still_pressed"]:
                a_button = constants.button_state["button_released"]
            else:
                a_button = constants.button_state["button_up"]
        if keys & ugame.K_X:
            # print("A")
            pass
        if keys & ugame.K_O:
            # print("B")
            pass
        if keys & ugame.K_START:
            # print("K_START")
            pass
        if keys & ugame.K_SELECT:
            # print("K_SELECT")
            pass
        # move ship right
        if keys & ugame.K_RIGHT != 0:
            if ship.x > constants.SCREEN_X - constants.SPRITE_SIZE:
                ship.move(constants.SCREEN_X - constants.SPRITE_SIZE, ship.y)
            else:
                ship.move(ship.x + 1, ship.y)
            pass
        # move ship left
        if keys & ugame.K_LEFT != 0:
            if ship.x < 0:
                ship.move(0, ship.y)
            else:
                ship.move(ship.x - 1, ship.y)
            pass
        if keys & ugame.K_UP:
            # print("B")
            ship.move(ship.x, ship.y)
            pass
        if keys & ugame.K_DOWN:
            # print("K_DOWN")
            ship.move(ship.x, ship.y)
            pass
        if a_button == constants.button_state["button_just_pressed"]:
            sound.play(pew_sound)
        # update game logic

        # redraw sprite list
        game.render_sprites(sprites)
        game.tick()  # wait until refresh rate finishes
コード例 #6
0
    while ugame.buttons.get_pressed() & ugame.K_O:
        time.sleep(0.25)
    text.cursor(0, 0)
    text.text(info)
    game.render_block()
    while not ugame.buttons.get_pressed() & ugame.K_O:
        time.sleep(0.25)
    text.clear()
    game.render_block()


tiles = stage.Bank.from_bmp16("tiles.bmp")
while True:
    space = stage.Grid(tiles)
    aliens = Aliens()
    game = stage.Stage(ugame.display, 12)
    for y in range(8):
        for x in range(8):
            space.tile(x, y, 1)
    for i in range(8):
        space.tile(random.randint(0, 7), random.randint(0, 7),
                   random.randint(2, 3))
    aliens.move(8, 17)
    saucer = Saucer()
    bomb = Bomb()
    ship = Ship()
    missile = Missile(0)
    missile1 = Missile(1)
    missile2 = Missile(2)
    text = stage.Text(9, 1)
    text.move(28, 60)
コード例 #7
0
ファイル: code.py プロジェクト: ben-whitten/ICS3U-FP-Copy
def main_menu_scene():
    # this function is the main menu scene
    text = []
    sprites = []
    sun = []

    image_bank_5 = stage.Bank.from_bmp16("Backgrounds.bmp")
    image_bank_5 = stage.Bank.from_bmp16("backgrounds.bmp")
    image_bank_3 = stage.Bank.from_bmp16("jungle_joe.bmp")

    # sets the background to image 0 in the bank
    background = stage.Grid(image_bank_5, constants.SCREEN_GRID_X, constants.SCREEN_GRID_Y)

    background.tile(2, 8, 1)
    background.tile(2, 8, 2)
    background.tile(2, 8, 3)
    background.tile(2, 8, 4)
    background.tile(2, 8, 5)
    background.tile(2, 8, 6)
    background.tile(2, 8, 7)
    background.tile(2, 8, 8)
    background.tile(2, 8, 9)
    background.tile(2, 8, 10)

    text_1 = stage.Text(width=29, height=14, font=None, palette=constants.SCORE_PALETTE, buffer=None)
    text_1.move(40, 35)
    text_1.move(40, 48)
    text_1.text("JUNGLE JOE")
    text.append(text_1)

    text_2 = stage.Text(width=29, height=14, font=None, palette=constants.SCORE_PALETTE, buffer=None)
    text_2.move(40, 45)
    text_2.move(40, 58)
    text_2.text("& SNAKOB'S")
    text.append(text_2)

    text_3 = stage.Text(width=29, height=14, font=None, palette=constants.SCORE_PALETTE, buffer=None)
    text_3.move(25, 55)
    text_3.move(25, 68)
    text_3.text("BONGO BANANZA!")
    text.append(text_3)
    

    text_4 = stage.Text(width=29, height=14, font=None, palette=constants.SCORE_PALETTE, buffer=None)
    text_4.move(35, 118)
    text_4.text("PRESS START!")
    text.append(text_4)

    # Displays the tree tops
    tree_top_1 = stage.Sprite(image_bank_5, 1, 0, 112)
    sprites.append(tree_top_1)
    tree_top_2 = stage.Sprite(image_bank_5, 1, 16, 112)
    sprites.append(tree_top_2)
    tree_top_3 = stage.Sprite(image_bank_5, 1, 32, 112)
    sprites.append(tree_top_3)
    tree_top_4 = stage.Sprite(image_bank_5, 1, 48, 112)
    sprites.append(tree_top_4)
    tree_top_5 = stage.Sprite(image_bank_5, 1, 64, 112)
    sprites.append(tree_top_5)
    tree_top_6 = stage.Sprite(image_bank_5, 1, 80, 112)
    sprites.append(tree_top_6)
    tree_top_7 = stage.Sprite(image_bank_5, 1, 96, 112)
    sprites.append(tree_top_7)
    tree_top_8 = stage.Sprite(image_bank_5, 1, 112, 112)
    sprites.append(tree_top_8)
    tree_top_9 = stage.Sprite(image_bank_5, 1, 128, 112)
    sprites.append(tree_top_9)
    tree_top_10 = stage.Sprite(image_bank_5, 1, 144, 112)
    sprites.append(tree_top_10)
    # Displays the sun
    sun_top_left = stage.Sprite(image_bank_5, 11, 128, 0)
    sun.append(sun_top_left)
    sun_top_right = stage.Sprite(image_bank_5, 10, 144, 0)
    sun.append(sun_top_right)
    sun_bottom_left = stage.Sprite(image_bank_5, 8, 128, 16)
    sun.append(sun_bottom_left)
    sun_bottom_right = stage.Sprite(image_bank_5, 9, 144, 16)
    sun.append(sun_bottom_right)
    # Displays Jungle Joe
    jungle_joe = stage.Sprite(image_bank_3, 15, 71, 98)
    sprites.append(jungle_joe)

    clouds = []
    for cloud_number in range(constants.TOTAL_CLOUDS):
        a_single_cloud = stage.Sprite(image_bank_5, 4, constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y)
        clouds.append(a_single_cloud)

    def Show_clouds():
        for cloud_number in range(len(clouds)):
            if clouds[cloud_number].y < 0:
                clouds[cloud_number].move(constants.OFF_LEFT_SCREEN, random.randint(0 - constants.SPRITE_SIZE, constants.SCREEN_Y - constants.SPRITE_SIZE))
                break

    cloud_count = 6
    Show_clouds()
    Show_clouds()

    game = stage.Stage(ugame.display, 60)
    # set the layers, items show up in order
    game.layers = text + clouds + [background]
    game.layers = text + sprites + clouds + sun + [background]
    # render the background and inital location of sprite list
    # most likely you will only render background once per scene
    # wait until refresh rate finishes
    game.render_block()
    # repeat forever, game loop

    while True:
        # get user input

        keys = ugame.buttons.get_pressed()
        #print(keys)

        if keys & ugame.K_START != 0:  # Start button
            game_scene()

        # update game logic
        for cloud_number in range (len(clouds)):
            if clouds[cloud_number].y > 0:
                clouds[cloud_number].move(clouds[cloud_number].x
                                          + constants.CLOUD_SPEED,
                                          clouds[cloud_number].y)
                if clouds[cloud_number].x > constants.SCREEN_X + constants.SPRITE_SIZE:
                    clouds[cloud_number].move(constants.OFF_SCREEN_X,
                                              constants.OFF_SCREEN_Y)
                    Show_clouds()
                if clouds[cloud_number].x > constants.SCREEN_X / 2:
                    Show_clouds()
        # redraw sprite list
        pass # just a placeholder until you write the code

        game.render_sprites(clouds)
        game.tick()
コード例 #8
0
ファイル: annotator.py プロジェクト: dalini/dhmon
                index = results[(oid, ctxt)].value

            oid = ''.join((key, index))
            index = part.get(index, None)
            if not index:
                return None

        value = results[(oid, ctxt)].value

        # Try enum resolution
        _, enum = self.mibcache[oid]
        if enum:
            enum_value = enum.get(value, None)
            if enum_value is None:
                logging.warning('Got invalid enum value for %s (%s), ignoring',
                                oid, value)
                return None
            value = enum_value
        return value

    def string_to_label_value(self, value):
        value = ''.join(x for x in value.strip()
                        if x in self.ALLOWED_CHARACTERS)
        return value.strip()


if __name__ == '__main__':
    annotator = stage.Stage(Annotator())
    annotator.listen(actions.Result)
    annotator.run()
コード例 #9
0
def game_scene():
    # this function sets the scene

    def show_alien():
        # I know this function is not portable but its being shown in 2 places
        # make an alien show up in two different places
        for alien_number in range(len(aliens)):
            if aliens[alien_number].x < 0:
                aliens[alien_number].move(
                    random.randint(0 + constants.SPRITE_SIZE,
                                   constants.SCREEN_X - constants.SPRITE_SIZE),
                    constants.OFF_TOP_OF_SCREEN)
                break

    # an image bank for circuitpython
    image_bank_1 = stage.Bank.from_bmp16("space_aliens.bmp")
    # a list of sprites that will be updated every frame
    sprites = []

    # buttons to keep state information
    a_button = constants.button_state["button_up"]
    b_button = constants.button_state["button_up"]
    start_button = constants.button_state["button_up"]
    select_button = constants.button_state["button_up"]

    # get sound ready
    pew_sound = open("pew.wav", 'rb')
    sound = ugame.audio
    sound.stop()
    sound.mute(False)

    # sets the background to image 0 in the bank
    background = stage.Grid(image_bank_1, 10, 8)
    for x_location in range(constants.SCREEN_GRID_X):
        for y_location in range(constants.SCREEN_GRID_Y):
            tile_picked = random.randint(1, 3)
            background.tile(x_location, y_location, tile_picked)

    # create lazers
    lasers = []
    for laser_number in range(constants.TOTAL_NUMBER_OF_LASERS):
        a_single_laser = stage.Sprite(image_bank_1, 10, constants.OFF_SCREEN_X,
                                      constants.OFF_SCREEN_Y)
        lasers.append(a_single_laser)

    # create aliens
    aliens = []
    for alien_number in range(constants.TOTAL_NUMBER_OF_ALIENS):
        a_single_alien = stage.Sprite(image_bank_1, 9, constants.OFF_SCREEN_X,
                                      constants.OFF_SCREEN_Y)
        aliens.append(a_single_alien)

    show_alien()

    # create a sprite
    # parameters (image_bank, image # in bank, x, y)
    ship = stage.Sprite(
        image_bank_1, 5,
        int(constants.SCREEN_X / 2 - constants.SPRITE_SIZE / 2),
        int(constants.SCREEN_Y - constants.SPRITE_SIZE +
            constants.SPRITE_SIZE / 2))
    sprites.append(ship)  # insert at the top of the sprite list

    # create a stage for the background to show up on
    #  and set the frame rate to 60 fps
    game = stage.Stage(ugame.display, constants.FPS)
    # set the layers, items show up in order
    game.layers = sprites + lasers + aliens + [background]
    # render the background and initial location of sprite list
    # most likely you will only render background once per scene
    game.render_block()

    # repeat forever, or you turn it off
    while True:
        # get user input
        keys = ugame.buttons.get_pressed()

        if keys & ugame.K_O:  # b
            pass
        if keys & ugame.K_START:  # start
            pass
        if keys & ugame.K_SELECT:  # select
            pass
        if keys & ugame.K_RIGHT != 0:  # right
            if ship.x > constants.SCREEN_X - constants.SPRITE_SIZE:
                ship.move(constants.SCREEN_X - constants.SPRITE_SIZE, ship.y)
            else:
                ship.move(ship.x + 1, ship.y)
            pass
        if keys & ugame.K_LEFT != 0:  # left
            if ship.x < 0:
                ship.move(0, ship.y)
            else:
                ship.move(ship.x - 1, ship.y)
            pass
        if keys & ugame.K_UP:  # up
            ship.move(ship.x, ship.y - 1)
            pass
        if keys & ugame.K_DOWN:  # down
            ship.move(ship.x, ship.y + 1)
            pass

        # each frame move the aliens
        for alien_number in range(len(aliens)):
            if aliens[alien_number].x > 0:  # meaning that it is on the screen
                aliens[alien_number].move(
                    aliens[alien_number].x,
                    aliens[alien_number].y + constants.ALIEN_SPEED)
                if aliens[alien_number].y > constants.OFF_TOP_OF_SCREEN:
                    aliens[alien_number].move(constants.OFF_SCREEN_X,
                                              constants.OFF_SCREEN_Y)
                    # make it randomly show up at top
                    show_alien()

        # if A button is pressed
        if keys & ugame.K_X != 0:
            if a_button == constants.button_state["button_up"]:
                a_button = constants.button_state["button_just_pressed"]
            elif a_button == constants.button_state["button_just_pressed"]:
                a_button = constants.button_state["button_still_pressed"]
        else:
            if a_button == constants.button_state["button_still_pressed"]:
                a_button = constants.button_state["button_released"]
            else:
                a_button = constants.button_state["button_up"]

        # if A button is pressed fire a laser, play a sound
        if a_button == constants.button_state["button_just_pressed"]:
            for laser_number in range(len(lasers)):
                if lasers[laser_number].x < 0:
                    lasers[laser_number].move(ship.x, ship.y)
                    sound.stop()
                    sound.play(pew_sound)
                    break

        # each frame move the lasers
        for laser_number in range(len(lasers)):
            if lasers[laser_number].x > 0:  # meaning that it is on the screen
                lasers[laser_number].move(
                    lasers[laser_number].x,
                    lasers[laser_number].y - constants.LASER_SPEED)
                if lasers[laser_number].y < constants.OFF_TOP_OF_SCREEN:
                    lasers[laser_number].move(constants.OFF_SCREEN_X,
                                              constants.OFF_SCREEN_Y)

        # update game logic

        # redraw sprite list
        game.render_sprites(sprites)
        game.tick()  # wait until refresh rate finishes
コード例 #10
0
def game_scene():
    # this function is the main game game_scene

    def show_alien():
        # this function takes an alien from off screen and moves it on screen
        for alien_number in range(len(aliens)):
            if aliens[alien_number].x < 0:
                aliens[alien_number].move(
                    random.randint(0 + constants.SPRITE_SIZE,
                                   constants.SCREEN_X - constants.SPRITE_SIZE),
                    constants.OFF_TOP_SCREEN)
                break

    neopixels = neopixel.NeoPixel(board.NEOPIXEL,
                                  constants.NEOPIXEL_COUNT,
                                  auto_write=False)

    # image banks for CircuitPython
    image_bank_bankground = stage.Bank.from_bmp16(
        "space_aliens_background.bmp")
    image_bank_sprites = stage.Bank.from_bmp16("space_aliens.bmp")

    # buttons that you want to keep state information on
    a_button = constants.button_state["button_up"]
    b_button = constants.button_state["button_up"]
    start_button = constants.button_state["button_up"]
    select_button = constants.button_state["button_up"]

    # get sound ready
    pew_sound = open("pew.wav", 'rb')
    sound = ugame.audio
    sound.stop()
    sound.mute(False)

    # set the background to image 0 in the image Bank
    #   and the size (10x8 tiles of the size 16x16)
    background = stage.Grid(image_bank_bankground, constants.SCREEN_X,
                            constants.SCREEN_Y)
    for x_location in range(constants.SCREEN_GRID_X):
        for y_location in range(constants.SCREEN_GRID_Y):
            tile_picked = random.randint(1, 3)
            background.tile(x_location, y_location, tile_picked)

    # a sprite that will be updated every frame
    ship = stage.Sprite(image_bank_sprites, 5, 75,
                        constants.SCREEN_Y - (2 * constants.SPRITE_SIZE))

    alien = stage.Sprite(
        image_bank_sprites, 9,
        int(constants.SCREEN_X / 2 - constants.SPRITE_SIZE / 2), 16)

    # create list of aliens
    aliens = []
    for alien_number in range(constants.TOTAL_NUMBER_OF_ALIENS):
        a_single_alien = stage.Sprite(image_bank_sprites, 9,
                                      constants.OFF_SCREEN_X,
                                      constants.OFF_SCREEN_Y)
        aliens.append(a_single_alien)
    #place 1 alien on the screen
    show_alien()

    # create list of lasers for when we shoot
    lasers = []
    for laser_number in range(constants.TOTAL_NUMBER_OF_LASERS):
        a_single_laser = stage.Sprite(image_bank_sprites, 10,
                                      constants.OFF_SCREEN_X,
                                      constants.OFF_SCREEN_Y)
        lasers.append(a_single_laser)

    # create a stage for the background to show up on
    #   and set the frame rate for 60fps
    game = stage.Stage(ugame.display, constants.FPS)
    # set layers of all sprites, items show up in order
    game.layers = lasers + [ship] + aliens + [background]
    # render all sprites
    #   most likely you will only render the background once per game scene
    game.render_block()

    # repeat forever, game loop
    while True:
        # get user input
        keys = ugame.buttons.get_pressed()

        if keys & ugame.K_O != 0:
            if a_button == constants.button_state["button_up"]:
                a_button = constants.button_state["button_just_pressed"]
            elif a_button == constants.button_state["button_just_pressed"]:
                a_button = constants.button_state["button_still_pressed"]
        else:
            if a_button == constants.button_state["button_still_pressed"]:
                a_button = constants.button_state["button_released"]
            else:
                a_button = constants.button_state["button_up"]
        # B button
        if keys & ugame.K_X != 0:
            pass
        if keys & ugame.K_START:
            pass
        if keys & ugame.K_SELECT:
            print("Select")

        if keys & ugame.K_RIGHT != 0:
            if ship.x < (constants.SCREEN_X - constants.SPRITE_SIZE):
                ship.move((ship.x + constants.SPRITE_MOVEMENT_SPEED), ship.y)
            else:
                ship.move((constants.SCREEN_X - constants.SPRITE_SIZE), ship.y)

        if keys & ugame.K_LEFT != 0:
            if ship.x > 0:
                ship.move((ship.x - constants.SPRITE_MOVEMENT_SPEED), ship.y)
            else:
                ship.move(0, ship.y)

        if keys & ugame.K_UP:
            pass
        if keys & ugame.K_DOWN:
            pass

        #set neopixels to green by default
        neopixels[0] = (0, 10, 0)
        neopixels[1] = (0, 10, 0)
        neopixels[2] = (0, 10, 0)
        neopixels[3] = (0, 10, 0)
        neopixels[4] = (0, 10, 0)
        # update game logic
        # play sound if A was just button_just_pressed
        if a_button == constants.button_state["button_just_pressed"]:
            # fire a laser, if we have enough power (have not used up all the
            # lasers)
            for laser_number in range(len(lasers)):
                if lasers[laser_number].x < 0:
                    lasers[laser_number].move(ship.x, ship.y)
                    sound.play(pew_sound)
                    break

        # each frame move the lasers, that have been fired up and set neopixels
        # to yellow when laser is on screen.
        for laser_number in range(len(lasers)):
            if lasers[laser_number].x > 0:
                lasers[laser_number].move(
                    lasers[laser_number].x,
                    lasers[laser_number].y - constants.LASER_SPEED)
                neopixels[laser_number] = (10, 10, 0)
                if lasers[laser_number].y < constants.OFF_TOP_SCREEN:
                    lasers[laser_number].move(constants.OFF_SCREEN_X,
                                              constants.OFF_SCREEN_Y)

        #Set neopixel to red if all 5 lasers are on screen.
        if (neopixels[0] == (10, 10, 0) and neopixels[1] == (10, 10, 0)
                and neopixels[2] == (10, 10, 0) and neopixels[3] == (10, 10, 0)
                and neopixels[4] == (10, 10, 0)):
            neopixels[0] = (10, 0, 0)
            neopixels[1] = (10, 0, 0)
            neopixels[2] = (10, 0, 0)
            neopixels[3] = (10, 0, 0)
            neopixels[4] = (10, 0, 0)

        #each frame move the aliens down, that are on the screen
        for alien_number in range(len(aliens)):
            if aliens[alien_number].x > 0:
                aliens[alien_number].move(
                    aliens[alien_number].x,
                    aliens[alien_number].y + constants.ALIEN_SPEED)
                if aliens[alien_number].y > constants.SCREEN_Y:
                    aliens[alien_number].move(constants.OFF_SCREEN_X,
                                              constants.OFF_SCREEN_Y)
                    show_alien()
        neopixels.show()

        # redraw sprites
        game.render_sprites(aliens + lasers + [ship])
        game.tick()
コード例 #11
0
def splash_scene():
    # this function is the menu scene

    # get sound ready
    coin_sound = open("coin.wav", 'rb')
    sound = ugame.audio
    sound.stop()
    sound.mute(False)
    sound.play(coin_sound)

    # image banks for CircuitPython
    image_bank_mt_background = stage.Bank.from_bmp16("mt_game_studio.bmp")

    # create a stage for the background to show up on
    #   and set the frame rate to 60fps
    # sets the background to image 0 in the image bank
    background = stage.Grid(image_bank_mt_background, constants.SCREEN_X,
                            constants.SCREEN_Y)

    # used this program to split the image into tile:
    #    https://ezgif.com/sprite-cutter/ezgif-5-818cdbcc3f66.png
    background.tile(2, 2, 0)  # blank white
    background.tile(3, 2, 1)
    background.tile(4, 2, 2)
    background.tile(5, 2, 3)
    background.tile(6, 2, 4)
    background.tile(7, 2, 0)  # blank white

    background.tile(2, 3, 0)  # blank white
    background.tile(3, 3, 5)
    background.tile(4, 3, 6)
    background.tile(5, 3, 7)
    background.tile(6, 3, 8)
    background.tile(7, 3, 0)  # blank white

    background.tile(2, 4, 0)  # blank white
    background.tile(3, 4, 9)
    background.tile(4, 4, 10)
    background.tile(5, 4, 11)
    background.tile(6, 4, 12)
    background.tile(7, 4, 0)  # blank white

    background.tile(2, 5, 0)  # blank white
    background.tile(3, 5, 0)
    background.tile(4, 5, 13)
    background.tile(5, 5, 14)
    background.tile(6, 5, 0)
    background.tile(7, 5, 0)  # blank white

    # create a stage for the background to show up on
    #    and set the frame rate to 60fps
    game = stage.Stage(ugame.display, constants.FPS)
    # set the layers, items show up in order
    game.layers = [background]
    # render the background and initial location of sprite list
    # most likely you will only render background once per scene
    game.render_block()

    # repeat forever, game loop
    while True:
        # Wait for 2 seconds
        time.sleep(2.0)
        menu_scene()
コード例 #12
0
            # i = 0 --> A = medium, i = 1 --> B = slow, i = 2 --> X = fast, i = 3 --> Y = stop
            for i in range(0, 4):
                if (self.joystick.get_button(i)):
                    self.sampleStage.setJogSpeed(self.mode[i])
#                    self.holderStage.setJogSpeed(self.mode[i])

            LX = self._inThreshold(self.joystick.get_axis(0))
            LY = self._inThreshold(self.joystick.get_axis(1))
            #            TR = self._inThreshold(self.joystick.get_axis(2))
            #            RX = self._inThreshold(self.joystick.get_axis(3))
            #            RY = self._inThreshold(self.joystick.get_axis(4))
            HY = self.joystick.get_hat(0)[1]

            self.sampleStage.jogMotors([-1 * LX, -1 * LY, HY])


#            self.holderStage.jogMotors([RX, RY])

# Use for testing
if __name__ == '__main__':

    agController = agilisControl.Controller('COM5')
    sampleStage = stage.Stage('1', agController)
    holderStage = stage.Stage('3', agController)

    time.sleep(3)

    print("Starting Motors")

    joy = joyControl(sampleStage, holderStage)
    joy.move()
コード例 #13
0
ファイル: code.py プロジェクト: Ryan-Nguyen-7/Pybadge
def game_scene():
    # this function is the main game game_scene

    # image banks for CircuitPython
    image_bank_background = stage.Bank.from_bmp16(
        "space_aliens_background.bmp")
    image_bank_sprites = stage.Bank.from_bmp16("space_aliens.bmp")

    # buttons that you want to keep state information on
    a_button = constants.button_state["button_up"]
    b_button = constants.button_state["button_up"]
    start_button = constants.button_state["button_up"]
    select_button = constants.button_state["button_up"]

    # get sound ready
    pew_sound = open("pew.wav", 'rb')
    sound = ugame.audio
    sound.stop()
    sound.mute(False)

    # sets the background to image 0 in the image Bank
    #   and the size (10x8 tiles of size 16x16)
    background = stage.Grid(image_bank_background, constants.SCREEN_GRID_X,
                            constants.SCREEN_GRID_Y)

    for x_location in range(constants.SCREEN_X):
        for y_location in range(constants.SCREEN_GRID_Y):
            tile_picked = random.randint(1, 3)
            background.tile(x_location, y_location, tile_picked)

    ship = stage.Sprite(image_bank_sprites, 5, 75,
                        constants.SCREEN_Y - (2 * constants.SPRITE_SIZE))

    alien = stage.Sprite(
        image_bank_sprites, 9,
        int(constants.SCREEN_X / 2 - constants.SPRITE_SIZE / 2), 16)

    # create a stage for the background to show up on
    #   and set the frame rate to 60fps
    game = stage.Stage(ugame.display, 60)
    # set the layers, items show up in order
    game.layers = [ship] + [alien] + [background]
    # render the background and initial location of sprite list
    # most likely you will only render background once per scene
    game.render_block()

    # repeat forever, game loop
    while True:
        # get user input
        keys = ugame.buttons.get_pressed()

        # A button to fire
        if keys & ugame.K_O != 0:
            if a_button == constants.button_state["button_up"]:
                a_button = constants.button_state["button_just_pressed"]
            elif a_button == constants.button_state["button_just_pressed"]:
                a_button = constants.button_state["button_still_pressed"]
        else:
            if a_button == constants.button_state["button_still_pressed"]:
                a_button = constants.button_state["button_released"]
            else:
                a_button = constants.button_state["button_up"]
        # B button
        if keys & ugame.K_X != 6:
            pass
        if keys & ugame.K_START:
            pass
        if keys & ugame.K_SELECT:
            pass
        if keys & ugame.K_RIGHT:
            if ship.x <= constants.SCREEN_X - constants.SPRITE_SIZE:
                ship.move(ship.x + 1, ship.y)
            else:
                ship.move(constants.SCREEN_X - constants.SPRITE_SIZE, ship.y)

        if keys & ugame.K_LEFT:
            if ship.x >= 0:
                ship.move(ship.x - 1, ship.y)
            else:
                ship.move(0, ship.y)
        if keys & ugame.K_UP:
            pass
        if keys & ugame.K_DOWN:
            pass

        # update game logic
        # play sound if A was just button_just_pressed
        if a_button == constants.button_state["button_just_pressed"]:
            sound.play(pew_sound)

        # redraw image_bank_sprites
        game.render_sprites([ship] + [alien])
        game.tick()
コード例 #14
0
def game_scene():
    # this function is the game scene on the PyBadge

    def show_alien():
        # make an alien show up on screen on the x-axis
        for alien_number in range(len(aliens)):
            if aliens[alien_number].x < 0:
                aliens[alien_number].move(
                    random.randint(0 + constants.SPRITE_SIZE,
                                   constants.SCREEN_X - constants.SPRITE_SIZE),
                    constants.OFF_TOP_SCREEN)
                break

    # an image bank for CircuitPython
    image_bank_1 = stage.Bank.from_bmp16("space_aliens.bmp")
    # a list of sprites
    sprites = []

    # Buttons that you want to keep state information on
    a_button = constants.button_state["button_up"]
    b_button = constants.button_state["button_up"]
    start_button = constants.button_state["button_up"]
    select_button = constants.button_state["button_up"]

    # Get laser shooting sound ready
    pew_sound = open("pew.wav", "rb")
    sound = ugame.audio
    sound.stop()
    sound.mute(False)

    # Get sound ready
    boom_sound = open("boom.wav", "rb")
    sound = ugame.audio
    sound.stop()
    sound.mute(False)

    # sets the background to image 0 in the bank
    background = stage.Grid(image_bank_1, 10, 8)
    for x_location in range(constants.SCREEN_GRID_X):
        for y_location in range(constants.SCREEN_GRID_X):
            tile_picked = random.randint(1, 3)
            background.tile(x_location, y_location, tile_picked)

    # create lasers
    lasers = []
    for laser_number in range(constants.TOTAL_NUMBER_OF_LASERS):
        a_single_laser = stage.Sprite(image_bank_1, 10, constants.OFF_SCREEN_X,
                                      constants.OFF_SCREEN_Y)
        lasers.append(a_single_laser)

    # create lasers
    aliens = []
    for alien_number in range(constants.TOTAL_NUMBER_OF_ALIENS):
        a_single_alien = stage.Sprite(image_bank_1, 9, constants.OFF_SCREEN_X,
                                      constants.OFF_SCREEN_Y)
        aliens.append(a_single_alien)

    alien_count = 1
    show_alien()

    text = []
    score_text = stage.Text(width=29,
                            height=14,
                            font=None,
                            palette=constants.NEW_PALETTE,
                            buffer=None)
    score_text.clear()
    score_text.cursor(0, 0)
    score_text.move(1, 1)
    score_text.text("Score: {0}".format(alien_count))
    text.append(score_text)

    # create sprite
    alien = stage.Sprite(image_bank_1, 9, 64, 56)
    sprites.append(alien)
    ship = stage.Sprite(image_bank_1, 5, 75, 56)
    sprites.insert(0, ship)

    # create a stage for the background to show up on
    #   and set the frame rate to 60fps
    game = stage.Stage(ugame.display, 60)
    # set the background layer
    game.layers = sprites + text + lasers + aliens + [background]
    # render the background
    # most likely you will only render background once per scene
    game.render_block()

    while True:
        # get user input
        keys = ugame.buttons.get_pressed()
        # print(keys)

        if keys & ugame.K_X:
            # print("A")
            pass
        if keys & ugame.K_O:
            # print("B")
            pass
        if keys & ugame.K_START:
            # print("K_START")
            pass
        if keys & ugame.K_SELECT:
            # print("K_SELECT")
            pass
        if keys & ugame.K_RIGHT:
            ship.move(ship.x + 1, ship.y)
            pass
        if keys & ugame.K_LEFT:
            ship.move(ship.x - 1, ship.y)
            pass
        if keys & ugame.K_UP:
            ship.move(ship.x, ship.y - 1)
            pass
        if keys & ugame.K_DOWN:
            ship.move(ship.x, ship.y + 1)
            pass

        for alien_number in range(len(aliens)):
            if aliens[alien_number].x > 0:
                aliens[alien_number].move(
                    aliens[alien_number].x,
                    aliens[alien_number].y + constants.ALIEN_SPEED)
                if aliens[alien_number].y > constants.SCREEN_Y:
                    aliens[alien_number].move(constants.OFF_SCREEN_X,
                                              constants.OFF_SCREEN_Y)
                    show_alien()

        # A button to fire
        if keys & ugame.K_X != 0:
            if a_button == constants.button_state["button_up"]:
                a_button = constants.button_state["button_just_pressed"]
            elif a_button == constants.button_state["button_just_pressed"]:
                a_button = constants.button_state["button_still_pressed"]
        else:
            if a_button == constants.button_state["button_still_pressed"]:
                a_button = constants.button_state["button_released"]
            else:
                a_button = constants.button_state["button_up"]

        # Play sound if A is pressed
        if a_button == constants.button_state["button_just_pressed"]:
            for laser_number in range(len(lasers)):
                if lasers[laser_number].x < 0:
                    lasers[laser_number].move(ship.x, ship.y)
                    sound.stop()
                    sound.play(pew_sound)
                    break

        for laser_number in range(len(lasers)):
            if lasers[laser_number].x > 0:
                lasers[laser_number].move(
                    lasers[laser_number].x,
                    lasers[laser_number].y - constants.LASER_SPEED)
                if lasers[laser_number].y < constants.OFF_TOP_SCREEN:
                    lasers[laser_number].move(constants.OFF_SCREEN_X,
                                              constants.OFF_SCREEN_Y)

        for laser_number in range(len(lasers)):
            if lasers[laser_number].x > 0:
                for alien_number in range(len(lasers)):
                    if aliens[alien_number].x > 0:
                        if stage.collide(lasers[laser_number].x + 6,
                                         lasers[laser_number].y + 2,
                                         lasers[laser_number].x + 11,
                                         lasers[laser_number].y + 12,
                                         aliens[alien_number].x + 1,
                                         aliens[alien_number].y,
                                         aliens[alien_number].x + 15,
                                         aliens[alien_number].y + 15):
                            aliens[alien_number].move(constants.OFF_SCREEN_X,
                                                      constants.OFF_SCREEN_Y)
                            sound.stop()
                            sound.play(boom_sound)
                            show_alien()
                            show_alien()
                            alien_count += 1
                            score_text.clear()
                            score_text.cursor(0, 0)
                            score_text.move(1, 1)
                            score_text.text("Score: {0}".format(alien_count))
                            game.render_block()

        # update game logic

        # redraw sprite list
        game.render_sprites(sprites + lasers + aliens)
        game.tick()
コード例 #15
0
def main():
    # this function is a scene

    # sets the background to image 0 in the bank
    background = stage.Grid(image_bank_1, 10, 8)

    # create a sprite
    # parameters (image_bank, image # in the bank, x, y)
    alien = stage.Sprite(image_bank_1, 9, 64, 56)
    sprites.append(alien)
    ship = stage.Sprite(image_bank_1, 5, 75, 56)
    sprites.insert(0, ship)  # insert at top of sprite list

    # create a stage for the background to show up on
    #   and set the frame rate to 60fps
    game = stage.Stage(ugame.display, 60)
    # set the layers, items show up in order
    game.layers = sprites + [background]
    # render the background and initial location of sprite list
    # most likely you will only render background once per scene
    game.render_block()

    # repeat forever, game loop
    while True:
        # get user input
        keys = ugame.buttons.get_pressed()
        # print(keys)

        if keys & ugame.K_X:
            # print("A")
            pass

        if keys & ugame.K_O:
            # print("B")
            pass

        if keys & ugame.K_START:
            # print("K_START")
            pass

        if keys & ugame.K_SELECT:
            # print("K_SELECT")
            pass

        if keys & ugame.K_RIGHT:
            ship.move(ship.x + 1, ship.y)
            pass

        if keys & ugame.K_LEFT:
            ship.move(ship.x - 1, ship.y)
            pass

        if keys & ugame.K_UP:
            ship.move(ship.x, ship.y - 1)
            pass

        if keys & ugame.K_DOWN:
            ship.move(ship.x, ship.y + 1)
            pass

        # update game logic

        # redraw sprite list
        game.render_sprites(sprites)
        game.tick()  # wait until refresh rate finishes
コード例 #16
0
ファイル: code.py プロジェクト: Ryan-Walsh-6/PyBadge
def game_scene():
    # this function is the main game game_scene

    # image banks for CircuitPython
    image_bank_bankground = stage.Bank.from_bmp16(
        "space_aliens_background.bmp")
    image_bank_sprites = stage.Bank.from_bmp16("space_aliens.bmp")

    # buttons that you want to keep state information on
    a_button = constants.button_state["button_up"]
    b_button = constants.button_state["button_up"]
    start_button = constants.button_state["button_up"]
    select_button = constants.button_state["button_up"]

    # get sound ready
    pew_sound = open("pew.wav", 'rb')
    sound = ugame.audio
    sound.stop()
    sound.mute(False)

    # set the background to image 0 in the image Bank
    #   and the size (10x8 tiles of the size 16x16)
    background = stage.Grid(image_bank_bankground, 10, 8)

    # a sprite that will be updated every frame
    ship = stage.Sprite(image_bank_sprites, 5, 75,
                        constants.SCREEN_Y - (2 * constants.SPRITE_SIZE))

    alien = stage.Sprite(
        image_bank_sprites, 9,
        int(constants.SCREEN_X / 2 - constants.SPRITE_SIZE / 2), 16)

    # create a stage for the background to show up on
    #   and set the frame rate for 60fps
    game = stage.Stage(ugame.display, 60)
    # set layers of all sprites, items show up in order
    game.layers = [ship] + [alien] + [background]
    # render all sprites
    #   most likely you will only render the background once per game scene
    game.render_block()

    # repeat forever, game loop
    while True:
        # get user input
        keys = ugame.buttons.get_pressed()

        if keys & ugame.K_O != 0:
            if a_button == constants.button_state["button_up"]:
                a_button = constants.button_state["button_just_pressed"]
            elif a_button == constants.button_state["button_just_pressed"]:
                a_button = constants.button_state["button_still_pressed"]
        else:
            if a_button == constants.button_state["button_still_pressed"]:
                a_button = constants.button_state["button_released"]
            else:
                a_button = constants.button_state["button_up"]
        # B button
        if keys & ugame.K_X != 0:
            pass
        if keys & ugame.K_START:
            pass
        if keys & ugame.K_SELECT:
            print("Select")

        if keys & ugame.K_RIGHT != 0:
            if ship.x < (constants.SCREEN_X - constants.SPRITE_SIZE):
                ship.move((ship.x + constants.SPRITE_MOVEMENT_SPEED), ship.y)
            else:
                ship.move((constants.SCREEN_X - constants.SPRITE_SIZE), ship.y)

        if keys & ugame.K_LEFT != 0:
            if ship.x > 0:
                ship.move((ship.x - constants.SPRITE_MOVEMENT_SPEED), ship.y)
            else:
                ship.move(0, ship.y)

        if keys & ugame.K_UP:
            pass
        if keys & ugame.K_DOWN:
            pass

        # update game logic
        # play sound if A was just button_just_pressed
        if a_button == constants.button_state["button_just_pressed"]:
            sound.play(pew_sound)

        # redraw sprites
        game.render_sprites([ship] + [alien])
        game.tick()
コード例 #17
0
def game_scene():
    # this function is a scene

    # an image bank for CircuitPython
    image_bank_1 = stage.Bank.from_bmp16("space_aliens.bmp")

    # buttons that you want to keep state information on
    a_button = constants.button_state["button_up"]
    b_button = constants.button_state["button_up"]
    start_button = constants.button_state["button_up"]
    select_button = constants.button_state["button_up"]

    # get sound ready
    pew_sound = open("pew.wav", 'rb')
    sound = ugame.audio
    sound.stop()
    sound.mute(False)

    # sets the background to image 0 in the bank
    background = stage.Grid(image_bank_1, constants.SCREEN_X,
                            constants.SCREEN_Y)

    # a list of sprites that will be updated every frame
    sprites = []

    ship = stage.Sprite(
        image_bank_1, 5,
        int(constants.SCREEN_X / 2 - constants.SPRITE_SIZE / 2),
        int(constants.SCREEN_Y - constants.SPRITE_SIZE +
            constants.SPRITE_SIZE / 2))
    sprites.append(ship)  # insert at the top of sprite list

    # create a stage for the background to show up on
    #   and set the frame rate to 60fps
    game = stage.Stage(ugame.display, constants.FPS)
    # set the layers, items show up in order
    game.layers = sprites + [background]
    # render the background and initial location of sprite list
    # most likely you will only render background once per scene
    game.render_block()

    # repeat forever, game loop
    while True:
        # get user input
        keys = ugame.buttons.get_pressed()
        # print (keys)

        # a button to fire
        if keys & ugame.K_X != 0:
            if a_button == constants.button_state["button_up"]:
                a_button = constants.button_state["button_just_pressed"]
            elif a_button == constants.button_state["button_just_pressed"]:
                a_button = constants.button_state["button_still_pressed"]
        else:
            if a_button == constants.button_state["button_still_pressed"]:
                a_button = constants.button_state["button_released"]
            else:
                a_button = constants.button_state["button_up"]

        # update game logic
        # move ship right
        if keys & ugame.K_RIGHT != 0:
            if ship.x > constants.SCREEN_X - constants.SPRITE_SIZE:
                ship.move(constants.SCREEN_X - constants.SPRITE_SIZE, ship.y)
            else:
                ship.move(ship.x + 1, ship.y)

        # move ship right
        if keys & ugame.K_LEFT != 0:
            if ship.x < 0:
                ship.move(0, ship.y)
            else:
                ship.move(ship.x - 1, ship.y)

        # play sound if A is pressed
        if a_button == constants.button_state["button_just_pressed"]:
            sound.play(pew_sound)

        # redraw sprite list
        game.render_sprites(sprites)
        game.tick()  # wait until refresh rate finishes
コード例 #18
0
def main():
    # this sets the background
    a_button = constants.button_state["button_up"]
    b_button = constants.button_state["button_up"]
    start_button = constants.button_state["button_up"]
    select_button = constants.button_state["button_up"]

    pew_sound = open("pew.wav", 'rb')
    sound = ugame.audio
    sound.stop()
    sound.mute(False)
    background = stage.Grid(image_bank_1, 10, 8)

    # create a sprite
    # parameters (image_bank_1, image # in bank, x, y)
    ball_one = stage.Sprite(image_bank_1, 3, 64, 56)
    sprites.append(ball_one)
    ball_two = stage.Sprite(image_bank_1, 2, 75, 56)
    sprites.insert(0, ball_two)

    game = stage.Stage(ugame.display, constants.FPS)
    game.layers = sprites + [background]

    game.render_block()

    while True:
        # get user inputs
        keys = ugame.buttons.get_pressed()
        # print(keys)
        if keys & ugame.K_X != 0:
            if a_button == constants.button_state["button_up"]:
                a_button = constants.button_state["button_just_pressed"]
            elif a_button == constants.button_state["button_just_pressed"]:
                a_button = constants.button_state["button_still_pressed"]
        else:
            if a_button == constants.button_state["button_still_pressed"]:
                a_button = constants.button_state["button_released"]
            else:
                a_button = constants.button_state["button_up"]
        if keys & ugame.K_UP != 0:
            if ball_one.y < 0:
                ball_one.move(ball_one.x, 0)
            else:
                ball_one.move(ball_one.x,
                              ball_one.y - constants.SPRITE_MOVEMENT_SPEED)
            pass
        if keys & ugame.K_DOWN != 0:
            if ball_one.y > constants.SCREEN_Y - constants.SCREEN_GRID_Y:
                ball_one.move(ball_one.x,
                              constants.SCREEN_Y - constants.SPRITE_SIZE)
            else:
                ball_one.move(ball_one.x,
                              ball_one.y + constants.SPRITE_MOVEMENT_SPEED)
            pass
        if keys & ugame.K_LEFT != 0:
            if ball_one.x < 0:
                ball_one.move(0, ball_one.y)
            else:
                ball_one.move(ball_one.x - constants.SPRITE_MOVEMENT_SPEED,
                              ball_one.y)
            pass
        if keys & ugame.K_RIGHT != 0:
            if ball_one.x > constants.SCREEN_X - constants.SCREEN_GRID_X:
                ball_one.move(constants.SCREEN_X - constants.SPRITE_SIZE,
                              ball_one.y)
            else:
                ball_one.move(ball_one.x + constants.SPRITE_MOVEMENT_SPEED,
                              ball_one.y)
            pass
        # update game logic
        if a_button == constants.button_state["button_just_pressed"]:
            sound.play(pew_sound)

        # redraw sprtie list
        game.render_sprites(sprites)
        game.tick()  # wait until refresh rate finishes
コード例 #19
0
ファイル: code.py プロジェクト: DJ-Watson/CP113
def game_scene():
    # image bank
    image_bank_1 = stage.Bank.from_bmp16("space_aliens.bmp")
    # setting button state
    a_button = constants.button_state["button_up"]
    # setting sound
    pew_sound = open("pew.wav", 'rb')
    boom_sound = open("boom.wav", 'rb')
    crash_sound = open("pew2.wav", 'rb')
    sound = ugame.audio
    sound.stop()
    sound.mute(False)
    # set background
    background = stage.Grid(image_bank_1, constants.SCREEN_X,
                            constants.SCREEN_Y)
    for x_location in range(constants.SCREEN_GRID_X):
        for y_location in range(constants.SCREEN_GRID_Y):
            tile_picked = random.randint(1,3)
            background.tile(x_location, y_location, tile_picked)
    # sprite bank
    sprites = []
    # load ship sprite
    ship = stage.Sprite(image_bank_1, 4, int(constants.SCREEN_X / 2 -
                        constants.SPRITE_SIZE / 2),
                        int(constants.SCREEN_Y - constants.SPRITE_SIZE +
                        constants.SPRITE_SIZE / 2))
    sprites.append(ship)
    # load lasers
    lasers = []
    for laser_number in range(constants.TOTAL_NUMBER_OF_LASERS):
        single_laser = stage.Sprite(image_bank_1, 10, constants.OFF_SCREEN_X,
                                    constants.OFF_SCREEN_Y)
        lasers.append(single_laser)
    # load aliens
    aliens = []
    for alien_number in range(constants.TOTAL_NUMBER_OF_ALIENS):
        single_alien = stage.Sprite(image_bank_1, 9, constants.OFF_SCREEN_X,
                                    constants.OFF_SCREEN_Y)
        aliens.append(single_alien)
    # number of aliens moving down
    alien_count = 1
    show_alien(aliens)

    # add score text
    global score
    scoretext = []
    score_text = stage.Text(width=29, height=14, font=None, palette=constants.PLT, buffer=None)
    score_text.clear()
    score_text.cursor(0, 0)
    score_text.move(1, 1)
    score_text.text("Score: {0}".format(score))
    scoretext.append(score_text)
    # set game configurations
    game = stage.Stage(ugame.display, constants.FPS)
    game.layers = scoretext + sprites + lasers + aliens + [background]
    game.render_block()

    while True:
        keys = ugame.buttons.get_pressed()
        # print (keys)

        if keys & ugame.K_X != 0:
            if a_button == constants.button_state["button_up"]:
                a_button = constants.button_state["button_just_pressed"]
            elif a_button == constants.button_state["button_just_pressed"]:
                a_button = constants.button_state["button_still_pressed"]
        else:
            if a_button == constants.button_state["button_still_pressed"]:
                a_button = constants.button_state["button_released"]
            else:
                a_button = constants.button_state["button_up"]

        if keys & ugame.K_RIGHT != 0:
            if ship.x > constants.SCREEN_X - constants.SPRITE_SIZE:
                ship.move(constants.SCREEN_X - constants.SPRITE_SIZE, ship.y)
            else:
                ship.move(ship.x + 3, ship.y)

        if keys & ugame.K_LEFT != 0:
            if ship.x < 0:
                ship.move(0, ship.y)
            else:
                ship.move(ship.x - 3, ship.y)

        if a_button == constants.button_state["button_just_pressed"]:
            for laser_number in range(len(lasers)):
                if lasers[laser_number].x < 0:
                    lasers[laser_number] .move(ship.x, ship.y)
                    sound.stop()
                    sound.play(pew_sound)
                    break

        for laser_number in range(len(lasers)):
            if lasers[laser_number].x > 0:
                lasers[laser_number].move(lasers[laser_number].x, lasers[laser_number].y - constants.LASER_SPEED)
                lasers[laser_number].move(lasers[laser_number].x, lasers[laser_number].y - constants.LASER_SPEED)
                if lasers[laser_number].y < constants.OFF_SCREEN_Y:
                    lasers[laser_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y)

        for alien_number in range(len(aliens)):
            if aliens[alien_number].x > 0:
                aliens[alien_number].move(aliens[alien_number].x, aliens[alien_number].y + constants.ALIEN_SPEED)
                if aliens[alien_number].y > constants.SCREEN_Y:
                    aliens[alien_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y)
                    show_alien(aliens)

        for laser_number in range(len(lasers)):
            if lasers[laser_number].x > 0:
                for alien_number in range(len(aliens)):
                    if aliens[alien_number].x > 0:
                        if stage.collide(lasers[laser_number].x, lasers[laser_number].y,
                                         lasers[laser_number].x + 16, lasers[laser_number].y + 16,
                                          aliens[alien_number].x, aliens[alien_number].y,
                                         aliens[alien_number].x + 16, aliens[alien_number].y + 16):
                            aliens[alien_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y)
                            lasers[laser_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y)
                            score += 1
                            score_text.clear()
                            score_text.cursor(0, 0)
                            score_text.move(1, 1)
                            score_text.text("Score: {0}".format(score))
                            sound.stop()
                            sound.play(boom_sound)
                            show_alien(aliens)
                            show_alien(aliens)
                        if stage.collide(aliens[alien_number].x, aliens[alien_number].y,
                                         aliens[alien_number].x + 16, aliens[alien_number].y + 16
                                         ship.x, ship.y,
                                         ship.x + 15, ship.y + 15):
                            sound.stop()
                            sound.play(crash_sound)
                            time.sleep(4.0)
                            sounds.stop()
                            game_over_scene()
        game.render_sprites(sprites + lasers + aliens)
        game.tick()  # wait until refresh rate finishes
コード例 #20
0
def game_scene():
    # This function shows a sprite and makes a sound

    score = 0
    a_button = constants.button_state["button_up"]
    b_button = constants.button_state["button_up"]
    start_button = constants.button_state["button_up"]
    select_button = constants.button_state["button_up"]

    def show_alien():
        # make an alien show up on screen in the x-axis
        for alien_number in range(len(aliens)):
            if aliens[alien_number].x < 0:
                aliens[alien_number].move(random.randint(0 +
                                          constants.SPRITE_SIZE,
                                          constants.SCREEN_X -
                                          constants.SPRITE_SIZE),
                                          constants.OFF_TOP_SCREEN)
                break

    # an image bank for circuitpython
    image_bank_1 = stage.Bank.from_bmp16("space_aliens.bmp")
    # a list of sprites that will be updated every frame
    sprites = []

    # create lasers for when you shoot
    lasers = []
    for lasers_number in range(constants.TOTAL_NUMBER_OF_LASERS):
        a_single_laser = stage.Sprite(image_bank_1, 10,
                                      constants.OFF_SCREEN_X,
                                      constants.OFF_SCREEN_Y)
        lasers.append(a_single_laser)

    # create aliens
    aliens = []
    for alien_number in range(constants.TOTAL_NUMBER_OF_ALIENS):
        a_single_alien = stage.Sprite(image_bank_1, 9,
                                      constants.OFF_SCREEN_X,
                                      constants.OFF_SCREEN_Y)
        aliens.append(a_single_alien)

    # current number of aliens that should be moving down screen,
    # start with just 1
    alien_count = 1
    show_alien()

    # Add text at top of the screen
    score_text = stage.Text(width=29, height=29, font=None,
                            palette=constants.NEW_PALETTE, buffer=None)
    score_text.clear()
    score_text.cursor(0, 0)
    score_text.move(1, 1)
    score_text.text("Score: {0}".format(score))

    # get sound ready
    pew_sound = open("pew.wav", 'rb')
    sound = ugame.audio
    sound.stop()
    sound.mute(False)

    boom_sound = open("boom.wav", 'rb')
    sound = ugame.audio
    sound.stop()
    sound.mute(False)
    sound.play(boom_sound)
    # sprites in the scene
    # parameters (image_bank, image # in bank, x, y)
    ship = stage.Sprite(image_bank_1, 5, int(constants.SCREEN_X / 2),
                        int(constants.SCREEN_Y - constants.SPRITE_SIZE))
    sprites.append(ship)  # insert at the top of the sprite list

    # sets the background to image 0 in the bank
    # backgrounds do not have magents as a transparent color
    background = stage.Grid(image_bank_1, constants.SCREEN_GRID_X,
                            constants.SCREEN_GRID_Y)
    for x_location in range(constants.SCREEN_GRID_X):
        for y_location in range(constants.SCREEN_GRID_Y):
            tile_picked = random.randint(1, 3)
            background.tile(x_location, y_location, tile_picked)

    # create a stage for the background to show up on
    #  and set the frame rate to 60
    game = stage.Stage(ugame.display, constants.FPS)
    # set the layers, items show up in order
    game.layers = [score_text] + sprites + lasers + aliens + [background]
    # render the background and initial location of sprite list
    # most likely you will only render background once per scene
    game.render_block()

    # repeat forever, or you turn it off
    while True:
        # get user input
        keys = ugame.buttons.get_pressed()
        if keys & ugame.K_X != 0:  # a button (fire)
            if a_button == constants.button_state["button_up"]:
                a_button = constants.button_state["button_just_pressed"]
            elif a_button == constants.button_state["button_just_pressed"]:
                a_button = constants.button_state["button_still_pressed"]
        else:
            if a_button == constants.button_state["button_still_pressed"]:
                a_button = constants.button_state["button_released"]
            else:
                a_button = constants.button_state["button_up"]
        if keys & ugame.K_X:
            # print("A")
            pass
        if keys & ugame.K_O:
            # print("B")
            pass
        if keys & ugame.K_START:
            # print("K_START")
            pass
        if keys & ugame.K_SELECT:
            # print("K_SELECT")
            pass
        # update_game_logic
        # move ship to the right and the left
        if keys & ugame.K_RIGHT != 0:
            if ship.x > constants.SCREEN_X - constants.SPRITE_SIZE:
                ship.move(constants.SCREEN_X - constants.SPRITE_SIZE, ship.y)
            else:
                ship.move(ship.x + 1, ship.y)

        if keys & ugame.K_LEFT != 0:
            if ship.x < 0:
                ship.move(0, ship.y)
            else:
                ship.move(ship.x - 1, ship.y)
            pass
        if a_button == constants.button_state["button_just_pressed"]:
            sound.play(pew_sound)

        if a_button == constants.button_state["button_just_pressed"]:
            # firea laser
            for lasers_number in range(len(lasers)):
                if lasers[lasers_number].x < 0:
                    lasers[lasers_number].move(ship.x, ship.y)
                    sound.stop()
                    sound.play(pew_sound)
                    break
        # each frame move the lasers, that have been fired
        for lasers_number in range(len(lasers)):
            if lasers[lasers_number].x > 0:
                lasers[lasers_number].move(lasers[lasers_number].x,
                                           lasers[lasers_number].y
                                           - constants.LASER_SPEED)
                if lasers[lasers_number].y < constants.OFF_TOP_SCREEN:
                    lasers[lasers_number].move(constants.OFF_SCREEN_X,
                                               constants.OFF_SCREEN_Y)

        for alien_number in range(len(aliens)):
            if aliens[alien_number].x > 0:
                aliens[alien_number].move(aliens[alien_number].x,
                                          aliens[alien_number].y +
                                          constants.ALIEN_SPEED)
                if aliens[alien_number].y > constants.SCREEN_Y:
                    aliens[alien_number].move(constants.OFF_SCREEN_X,
                                              constants.OFF_SCREEN_Y)
                    show_alien()  # make it randomly show up at top again

        # Each frame check if any lasers are touching any of the aliens
        for lasers_number in range(len(lasers)):
            if lasers[lasers_number].x > 0:
                for alien_number in range(len(aliens)):
                    if aliens[alien_number].x > 0:
                        # the first 4 numbers are the coordinates of A box
                        # since the laser is thin, it made it thinner
                        # and slightly smaller

                        # the second 4 numbers are the alien, it is more of
                        # a box so I just made it slightly smaller
                        #
                        if stage.collide(lasers[lasers_number].x + 6,
                                         lasers[lasers_number].y + 2,
                                         lasers[lasers_number].x + 11,
                                         lasers[lasers_number].y + 12,
                                         aliens[alien_number].x + 1,
                                         aliens[alien_number].y,
                                         aliens[alien_number].x + 15,
                                         aliens[alien_number].y + 15):
                            # you hit an alien
                            aliens[alien_number].move(constants.OFF_SCREEN_X,
                                                      constants.OFF_SCREEN_Y)
                            lasers[lasers_number].move(constants.OFF_SCREEN_X,
                                                       constants.OFF_SCREEN_Y)

                            # add 1 to the score
                            score += 1
                            score_text.clear()
                            score_text.cursor(0, 0)
                            score_text.move(1, 1)
                            score_text.text("Score: {0}".format(score))
                            game.render_block()

                            # play sound effect
                            sound.stop()
                            sound.play(boom_sound)
                            show_alien()
                            show_alien()
                            alien_count = alien_count + 1

        # redraw sprite list
        game.render_sprites(sprites + lasers + aliens)
        game.tick()  # wait until refresh rate finishes
コード例 #21
0
ファイル: code.py プロジェクト: ben-whitten/ICS3U-FP-Copy
def game_scene():
    # this function is the game scene
    border = []
    sprites = []
    image_bank_5 = stage.Bank.from_bmp16("backgrounds.bmp")
    image_bank_3 = stage.Bank.from_bmp16("jungle_joe.bmp")

    background = stage.Grid(image_bank_5, constants.SCREEN_GRID_X, constants.SCREEN_GRID_Y)
    for x_location in range(constants.SCREEN_GRID_2_X):
        for y_location in range(constants.SCREEN_GRID_Y):
            tile_picked = random.randint(2,3)
            background.tile(x_location, y_location, tile_picked)
    for x_location in range(constants.SCREEN_GRID_2_X, constants.SCREEN_GRID_3_X):
        for y_location in range(constants.SCREEN_GRID_Y):
            background.tile(x_location, y_location, 5)

    # Displays the border.
    border_1 = stage.Sprite(image_bank_5, 6, constants.BORDER_LOCATION, 0)
    border.append(border_1)
    border_2 = stage.Sprite(image_bank_5, 6, constants.BORDER_LOCATION, 16)
    border.append(border_2)
    border_3 = stage.Sprite(image_bank_5, 6, constants.BORDER_LOCATION, 32)
    border.append(border_3)
    border_4 = stage.Sprite(image_bank_5, 6, constants.BORDER_LOCATION, 48)
    border.append(border_4)
    border_5 = stage.Sprite(image_bank_5, 6, constants.BORDER_LOCATION, 64)
    border.append(border_5)
    border_6 = stage.Sprite(image_bank_5, 6, constants.BORDER_LOCATION, 80)
    border.append(border_6)
    border_7 = stage.Sprite(image_bank_5, 6, constants.BORDER_LOCATION, 96)
    border.append(border_7)
    border_8 = stage.Sprite(image_bank_5, 6, constants.BORDER_LOCATION, 112)
    border.append(border_8)

    # Displays key sprites.
    a_button = stage.Sprite(image_bank_3, 12, constants.A_BUTTON_LOCATION, constants.BUTTON_HEIGHT)
    sprites.append(a_button)
    b_button = stage.Sprite(image_bank_3, 11, constants.B_BUTTON_LOCATION, constants.BUTTON_HEIGHT)
    sprites.append(b_button)
    left_arrow = stage.Sprite(image_bank_3, 8, constants.LEFT_ARROW_LOCATION, constants.BUTTON_HEIGHT)
    sprites.append(left_arrow)
    right_arrow = stage.Sprite(image_bank_3, 7, constants.RIGHT_ARROW_LOCATION, constants.BUTTON_HEIGHT)
    sprites.append(right_arrow)
    up_arrow = stage.Sprite(image_bank_3, 10, constants.UP_ARROW_LOCATION, constants.BUTTON_HEIGHT)
    sprites.append(up_arrow)
    down_arrow = stage.Sprite(image_bank_3, 9, constants.DOWN_ARROW_LOCATION, constants.BUTTON_HEIGHT)
    sprites.append(down_arrow)


    # create a stage for the background to show up on
    #   and set the frame rate to 60fps
    game = stage.Stage(ugame.display, 60)
    # set the layers, items show up in order
    game.layers = sprites + border + [background]
    # render the background and inital location of sprite list
    # most likely you will only render background once per scene
    game.render_block()


    # repeat forever, game loop
    while True:
        # get user input
        # update game logic
        # redraw sprite list
        pass # just a placeholder until you write the code
コード例 #22
0
def game_scene():
    # comment

    def show_alien():
        for alien_number in range(len(aliens)):
            if aliens[alien_number].y < 0:
                aliens[alien_number].move(random.randint(constants.SPRITE_SIZE,
                                          constants.SCREEN_X -
                                          constants.SPRITE_SIZE), 0)
                break

    # a list of sprites that will be updated every frame
    sprites = []
    lasers = []
    aliens = []
    score = 0

    # buttons that keep state information
    a_button = constants.button_state["button_up"]
    b_button = constants.button_state["button_up"]
    start_button = constants.button_state["button_up"]
    select_button = constants.button_state["button_up"]

    # get sound ready
    pew_sound = open("pew.wav", 'rb')
    boom_sound = open("boom.wav", 'rb')
    coin = open("coin.wav", 'rb')
    sound = ugame.audio
    sound.stop()
    sound.mute(False)

    # an image bank for CircuitPython
    image_bank_1 = stage.Bank.from_bmp16("space_aliens.bmp")

    # sets the background to image 0 in the bank
    background = stage.Grid(image_bank_1, constants.SCREEN_X,
                            constants.SCREEN_Y)
    for x_location in range(constants.SCREEN_GRID_X):
        for y_location in range(constants.SCREEN_GRID_Y):
            tile_picked = random.randint(1, 3)
            background.tile(x_location, y_location, tile_picked)

    # create a sprite
    # parameters (image_bank, image # in bank, x, y)
    ship = stage.Sprite(image_bank_1, 5, int(constants.SCREEN_X / 2 -
                        constants.SPRITE_SIZE / 2),
                        int(constants.SCREEN_Y - constants.SPRITE_SIZE +
                        constants.SPRITE_SIZE / 2))
    sprites.append(ship)  # insert at the top of sprite list

    # create aliens
    for alien_number in range(constants.TOTAL_NUMBER_OF_ALIENS):
        a_single_alien = stage.Sprite(image_bank_1, 7,
                                      constants.OFF_SCREEN_X,
                                      constants.OFF_SCREEN_Y)
        aliens.append(a_single_alien)

    alien_count = 2
    show_alien()

    # text at top of screen for score
    score_text = stage.Text(width=29, height=14, font=None,
                            palette=constants.SCORE_PALETTE, buffer=None)
    score_text.clear()
    score_text.cursor(0, 0)
    score_text.move(1, 1)
    score_text.text("Score: {0}".format(score))

    # create lasers
    for laser_number in range(constants.TOTAL_NUMBER_OF_LASERS):
        a_single_laser = stage.Sprite(image_bank_1, 10,
                                      constants.OFF_SCREEN_X,
                                      constants.OFF_SCREEN_Y)
        lasers.append(a_single_laser)

    # create a stage for the background to show up on
    #   and set the frame rate to 60fps
    game = stage.Stage(ugame.display, constants.FPS)
    # set the layers, items show up in order
    game.layers = sprites + aliens + lasers + [score_text] + [background]
    # render the background and inital location of sprite list
    # most likely you will only render background once per scene
    game.render_block()

    # repeat forever, game loop
    while True:
        # get user input
        keys = ugame.buttons.get_pressed()
        # (keys)
        if keys & ugame.K_X != 0:  # a button (fire)
            if a_button == constants.button_state["button_up"]:
                a_button = constants.button_state["button_just_pressed"]
            elif a_button == constants.button_state["button_just_pressed"]:
                a_button = constants.button_state["button_still_pressed"]
        else:
            if a_button == constants.button_state["button_still_pressed"]:
                a_button = constants.button_state["button_released"]
            else:
                a_button = constants.button_state["button_up"]

        if keys & ugame.K_O:  # b
            pass
        if keys & ugame.K_START:  # start
            pass
        if keys & ugame.K_SELECT:  # select
            pass
        if keys & ugame.K_RIGHT != 0:  # right
            if ship.x > constants.SCREEN_X - constants.SPRITE_SIZE:
                ship.move(constants.SCREEN_X - constants.SPRITE_SIZE, ship.y)
            else:
                ship.move(ship.x + 1, ship.y)
            pass
        if keys & ugame.K_LEFT != 0:  # left
            if ship.x < 0:
                ship.move(0, ship.y)
            else:
                ship.move(ship.x - 1, ship.y)
            pass
        if keys & ugame.K_UP:  # up
            if ship.y < 0 + constants.SPRITE_SIZE:
                ship.move(ship.x, 0 + constants.SPRITE_SIZE)
            else:
                ship.move(ship.x, ship.y - 1)
            pass
        if keys & ugame.K_DOWN:  # down
            if ship.y > constants.SCREEN_Y - constants.SPRITE_SIZE:
                ship.move(ship.x, constants.SCREEN_Y - constants.SPRITE_SIZE)
            else:
                ship.move(ship.x, ship.y + 1)
            pass

        # update game logic
        # Play sound if a button is pressed

        # if A button is pressed
        if a_button == constants.button_state["button_just_pressed"]:
            for laser_number in range(len(lasers)):
                if lasers[laser_number].x < 0:
                    lasers[laser_number].move(ship.x, ship.y)
                    sound.stop()
                    sound.play(pew_sound)
                    break

        # each frame move the lasers
        for laser_number in range(len(lasers)):
            if lasers[laser_number].x > 0:
                lasers[laser_number].move(lasers[laser_number].x,
                                          lasers[laser_number].y -
                                          constants.LASER_SPEED)
                if lasers[laser_number].y < constants.OFF_TOP_SCREEN:
                    lasers[laser_number].move(constants.OFF_SCREEN_X,
                                              constants.OFF_SCREEN_Y)

        # each frame move the aliens
        for alien_number in range(len(aliens)):
            if aliens[alien_number].x > 0:
                aliens[alien_number].move(aliens[alien_number].x,
                                          aliens[alien_number].y +
                                          constants.ALIEN_SPEED)
                if aliens[alien_number].y > constants.OFF_BOTTOM_SCREEN:
                    aliens[alien_number].move(constants.OFF_SCREEN_X,
                                              constants.OFF_SCREEN_Y)
                    show_alien()

        # Each frame check if lasers are touching aliens.
        for laser_number in range(len(lasers)):
            if lasers[laser_number].x > 0:
                for alien_number in range(len(aliens)):
                    if aliens[alien_number].x > 0:
                        if stage.collide(lasers[laser_number].x + 6,
                                         lasers[laser_number].y + 2,
                                         lasers[laser_number].x + 11,
                                         lasers[laser_number].y + 12,
                                         aliens[alien_number].x + 1,
                                         aliens[alien_number].y,
                                         aliens[alien_number].x + 15,
                                         aliens[alien_number].y + 15):
                            aliens[alien_number].move(constants.OFF_SCREEN_X,
                                                      constants.OFF_SCREEN_Y)
                            lasers[laser_number].move(constants.OFF_SCREEN_X,
                                                      constants.OFF_SCREEN_Y)
                            # add to score
                            score = score + constants.SCORE_ADDER
                            score_text.clear()
                            score_text.cursor(0, 0)
                            score_text.move(1, 1)
                            score_text.text("Score: {0}".format(score))
                            game.render_block()
                            # play sound
                            sound.stop()
                            sound.play(boom_sound)
                            show_alien()
                            show_alien()
                            alien_count = alien_count + 1

        # if aliens are touching the ship
        for alien_number in range(len(aliens)):
            if aliens[alien_number].x > 0:
                if stage.collide(aliens[alien_number].x + 1,
                                 aliens[alien_number].y,
                                 aliens[alien_number].x + 15,
                                 aliens[alien_number].y + 15,
                                 ship.x, ship.y, ship.x + 15, ship.y + 15):
                    sound.stop()
                    sound.play(coin)
                    time.sleep(2)
                    sound.stop()
                    game_over_scene(score_text)
                    # game.render_block()

        # redraw sprite list
        game.render_sprites(sprites + lasers + aliens)
        game.tick()  # wait until refresh rate finishes
コード例 #23
0
def game_scene():
    # this function is a scene
    # buttons that you want to keep state information in
    a_button = constants.button_state["button_up"]
    b_button = constants.button_state["button_up"]
    start_button = constants.button_state["button_up"]
    select_button = constants.button_state["button_up"]

    # get sound defined
    boom_sound = open("boom.wav, 'rb' ")
    sound = ugame.audio
    sound.stop()
    sound.mute(False)

    # sets the background to image 0 in the bank
    background = stage.Grid(image_bank_1, constants.SCREEN_X,
                            constants.SCREEN_Y)

    # create a sprite
    ship = stage.Sprite(
        image_bank_1, 5,
        int(constants.SCREEN_X / 2 - constants.SPRITE_SIZE / 2),
        int(constants.SCREEN_Y - constants.SPRITE_SIZE +
            constants.SPRITE_SIZE / 2))
    sprites.append(ship)  # insert at the top of sprite list

    # create a stage for the background to show up on
    #   and set the frame rate to 60fps
    game = stage.Stage(ugame.display, constants.FPS)
    # set the layers, items show up in order
    game.layers = sprites + [background]
    # render the background and inital location of sprite list
    # most likely you will only render background once per scene
    game.render_block()

    # repeat forever, game loop
    while True:
        # get user input
        keys = ugame.buttons.get_pressed()
        # print(keys)

        # update game logic
        if keys & ugame.K_X != 0:
            if a_button == constants.button_state["button_up"]:
                a_button == constants.button_state["button_just_pressed"]
            elif a_button == constants.button_state["button_just_pressed"]:
                a_button == constants.button_state["button_just_pressed"]
        else:
            if a_button == constants.button_state["button_just_pressed"]:
                a_button == constants.button_state["button_released"]
            else:
                a_button == constants.button_state["button_up"]

        if keys & ugame.K_O:
            pass
        if keys & ugame.K_START:
            pass
        if keys & ugame.K_SELECT:
            pass
        if keys & ugame.K_RIGHT != 0:
            if ship.x > constants.SCREEN_X - constants.SPRITE_SIZE:
                ship.move(constants.SCREEN_X - constants.SPRITE_SIZE, ship.y)
            else:
                ship.move(ship.x + 1, ship.y)
            pass
        if keys & ugame.K_LEFT != 0:
            if ship.x < 0:
                ship.move(0, ship.y)
            else:
                ship.move(ship.x - 1, ship.y)
            pass
        if keys & ugame.K_UP:
            ship.move(ship.x, ship.y - 1)
            pass
        if keys & ugame.K_DOWN:
            ship.move(ship.x, ship.y + 1)
        pass

        # play sound if A is pressed
        if a_button == constants.button_state["button_just_pressed"]:
            sound.play(boom_sound)

        # redraw sprite list
        game.render_sprites(sprites)
        game.tick()  # wait until refresh rate finishes
コード例 #24
0
def game_scene():
    # this function is the game scene
    score = 0

    text = []
    all_the_bricks = []
    both_paddles = []
    ball_sprite = []

    # an image bank for CircuitPython
    image_bank_2 = stage.Bank.from_bmp16("break_out.bmp")

    background = stage.Grid(image_bank_2, constants.SCREEN_GRID_X,
                            constants.SCREEN_GRID_Y)

    ball = stage.Sprite(image_bank_2, 1, 74, 56)
    sprites.insert(0, ball)  # insert at the top of sprite list

    paddle1 = stage.Sprite(image_bank_2, 2, 74, 100)
    sprites.insert(0, paddle1)  # insert at the top of sprite list

    paddle2 = stage.Sprite(image_bank_2, 2, 90, 100)
    sprites.insert(0, paddle2)  # insert at the top of sprite list

    brick1 = stage.Sprite(image_bank_2, 5, 90, 40)
    all_the_bricks.insert(0, brick1)  # insert at the top of sprite list

    brick2 = stage.Sprite(image_bank_2, 6, 74, 40)
    all_the_bricks.insert(0, brick2)  # insert at the top of sprite list

    brick3 = stage.Sprite(image_bank_2, 5, 58, 40)
    all_the_bricks.insert(0, brick3)  # insert at the top of sprite list

    brick4 = stage.Sprite(image_bank_2, 6, 42, 40)
    all_the_bricks.insert(0, brick4)  # insert at the top of sprite list

    brick5 = stage.Sprite(image_bank_2, 5, 26, 40)
    all_the_bricks.insert(0, brick5)  # insert at the top of sprite list

    brick6 = stage.Sprite(image_bank_2, 6, 10, 40)
    all_the_bricks.insert(0, brick6)  # insert at the top of sprite list

    brick7 = stage.Sprite(image_bank_2, 5, -6, 40)
    all_the_bricks.insert(0, brick7)  # insert at the top of sprite list

    brick8 = stage.Sprite(image_bank_2, 6, 106, 40)
    all_the_bricks.insert(0, brick8)  # insert at the top of sprite list

    brick9 = stage.Sprite(image_bank_2, 5, 122, 40)
    all_the_bricks.insert(0, brick9)  # insert at the top of sprite list

    brick10 = stage.Sprite(image_bank_2, 6, 138, 40)
    all_the_bricks.insert(0, brick10)  # insert at the top of sprite list

    # create a stage for the background to show up
    # setting the frame rate to 60fps
    game = stage.Stage(ugame.display, 60)
    # setting the layers to show them in order
    game.layers = text + sprites + all_the_bricks + [background]
    # rendering the background and the locations of the sprites
    game.render_block()

    # repeat forever game loop
    while True:
        #move the ball
        ball.move(ball.x + constants.BALL_SPEED, ball.y + constants.BALL_SPEED)

        #check if touching top or bottom of the screen
        if not 0 < ball.x < 150:
            constants.BALL_SPEED = -constants.BALL_SPEED
        if not 0 < ball.y < 118:
            constants.BALL_SPEED = -constants.BALL_SPEED
            #check if touching a paddle1
            for ball in sprites:
                if ball.x > 0:
                    pass

        # Each frame check if the ball is touching any of the bricks
        for brick_number in range(len(all_the_bricks)):
            if all_the_bricks[brick_number].x > 0:
                # the first 4 numbers are the coordinates of A box
                # the second 4 numbers are the alien, it is more of a box so I just made it slightly smaller
                if stage.collide(ball.x, ball.y, ball.x + 16, ball.y + 16,
                                 all_the_bricks[brick_number].x,
                                 all_the_bricks[brick_number].y,
                                 all_the_bricks[brick_number].x + 16,
                                 all_the_bricks[brick_number].y + 16):
                    # you hit a brick
                    all_the_bricks[brick_number].move(constants.OFF_SCREEN_X,
                                                      constants.OFF_SCREEN_Y)
                    # bounce ball

        # redraw sprite list
        game.render_sprites(sprites)
        game.tick()  # wait until refresh rate finishes

        # get user input
        keys = ugame.buttons.get_pressed()
        #print(keys)

        # update game logic
        # move ship right
        if keys & ugame.K_RIGHT != 0:
            if paddle1.x > constants.SCREEN_X - constants.SPRITE_SIZE:
                paddle1.move(constants.SCREEN_X - constants.SPRITE_SIZE,
                             paddle1.y)
            else:
                paddle1.move(paddle1.x + 1, paddle1.y)

        # move ship right
        if keys & ugame.K_LEFT != 0:
            if paddle1.x < 0:
                paddle1.move(0, paddle1.y)
            else:
                paddle1.move(paddle1.x - 1, paddle1.y)

        if keys & ugame.K_RIGHT != 0:
            if paddle2.x > constants.SCREEN_X - constants.SPRITE_SIZE:
                paddle2.move(constants.SCREEN_X - constants.SPRITE_SIZE,
                             paddle2.y)
            else:
                paddle2.move(paddle2.x + 1, paddle2.y)

        # move ship right
        if keys & ugame.K_LEFT != 0:
            if paddle2.x < 0:
                paddle2.move(0, paddle2.y)
            else:
                paddle2.move(paddle2.x - 1, paddle2.y)

        # redraw sprite list
        game.render_sprites(sprites)
        game.tick()  # wait until refresh rate finishes
コード例 #25
0
    hpc = CLUSTER(
                        jobs            = args.jobs,
                        logTo           = 'hpctest.log',
                        syslog          = True,
                        logTee          = True
                        )

    hpc.verbosity(args.verbosity)
    pipeline    = hpc.pipeline()
    pipeline.poststdout(True)
    pipeline.poststderr(True)

    stage0 = stage.Stage(
                        name            = 'scheduler',
                        fatalConditions = True,
                        syslog          = True,
                        logTo           = 'hpctest-schedule.log',
                        logTee          = True,
                        )
    def f_stage0callback(**kwargs):
        for key, val in kwargs.iteritems():
            if key == 'jobs':   jobs            = val
            if key == 'obj':    stage           = val
            if key == 'pipe':   pipeline        = val

        # Create shell for scheduling/executing on the remote HPC
        pipeline.stageShell_createRemoteInstance(args.cluster, stage=stage)
            
        for job in range(0, int(args.jobs)):
            log = stage.log()
            log('Processing job: %d...\n' % job)
コード例 #26
0
ファイル: code.py プロジェクト: paul-madut/ICS3U-2019-Group1
def game_scene():
    # this function is the game scene

    # game score
    score = 0

    def show_alien():
        # I know this is a function that is using variables outside of itself!
        #   BUT this code is going to be used in 2 places :)
        # make an alien show up on screen in the x-axis
        for alien_number in range(len(aliens)):
            if aliens[
                    alien_number].x < 0:  # meaning it is off the screen, so available to move on the screen
                aliens[alien_number].move(
                    random.randint(0 + constants.SPRITE_SIZE,
                                   constants.SCREEN_X - constants.SPRITE_SIZE),
                    constants.OFF_TOP_SCREEN)
                break

    # buttons that you want to keep state information on
    a_button = constants.button_state["button_up"]
    b_button = constants.button_state["button_up"]
    start_button = constants.button_state["button_up"]
    select_button = constants.button_state["button_up"]

    # get sound ready
    pew_sound = open(
        "pew2.wav",
        'rb')  # to change the wav volume: https://audioalter.com/volume/
    boom_sound = open("boom.wav", 'rb')
    crash_sound = open("crash.wav", 'rb')
    sound = ugame.audio
    sound.stop()
    sound.mute(False)

    # an image bank for CircuitPython
    image_bank_1 = stage.Bank.from_bmp16("space_aliens.bmp")
    image_bank_2 = stage.Bank.from_bmp16("sprites.bmp")
    # a list of sprites that will be updated every frame
    sprites = []

    # create lasers for when we shoot
    lasers = []
    for laser_number in range(constants.TOTAL_ATTACKS):
        a_single_laser = stage.Sprite(image_bank_2, 1, constants.OFF_SCREEN_X,
                                      constants.OFF_SCREEN_X)
        lasers.append(a_single_laser)

    # set up the NeoPixels to match the # of lasers fired
    pixels = neopixel.NeoPixel(board.NEOPIXEL, 5, auto_write=False)
    for pixel_number in range(0, 5):
        pixels[pixel_number] = (0, 10, 0)
    pixels.show()

    # create aliens
    aliens = []
    for alien_number in range(constants.TOTAL_NUMBER_OF_ALIENS):
        a_single_alien = stage.Sprite(image_bank_2, 3, constants.OFF_SCREEN_X,
                                      constants.OFF_SCREEN_X)
        aliens.append(a_single_alien)

    # current number of aliens that should be moving down screen, start with just 1
    alien_count = 1
    show_alien()

    # add text at top of screen for score
    score_text = stage.Text(width=29,
                            height=14,
                            font=None,
                            palette=constants.SCORE_PALETTE,
                            buffer=None)
    score_text.clear()
    score_text.cursor(0, 0)
    score_text.move(1, 1)
    score_text.text("Score: {0}".format(score))

    ship = stage.Sprite(image_bank_2, 4, int(constants.SCREEN_X / 2),
                        constants.SCREEN_Y - constants.SPRITE_SIZE)
    sprites.append(ship)  # insert at the top of sprite list

    # sets the background to image 0 in the bank
    background = stage.Grid(image_bank_2, constants.SCREEN_GRID_X,
                            constants.SCREEN_GRID_Y)
    for x_location in range(constants.SCREEN_GRID_X):
        for y_location in range(constants.SCREEN_GRID_Y):
            tile_picked = random.randint(0, 0)
            background.tile(x_location, y_location, tile_picked)

    # create a stage for the background to show up on
    #   and set the frame rate to 60fps
    game = stage.Stage(ugame.display, 60)
    # set the layers, items show up in order
    game.layers = sprites + lasers + aliens + [score_text] + [background]
    # render the background and inital location of sprite list
    # most likely you will only render background once per scene
    game.render_block()

    # repeat forever, game loop
    while True:
        # get user input
        keys = ugame.buttons.get_pressed()
        #print(keys)

        if keys & ugame.K_X != 0:  # A button
            if a_button == constants.button_state["button_up"]:
                a_button = constants.button_state["button_just_pressed"]
            elif a_button == constants.button_state["button_just_pressed"]:
                a_button = constants.button_state["button_still_pressed"]
        else:
            if a_button == constants.button_state["button_still_pressed"]:
                a_button = constants.button_state["button_released"]
            else:
                a_button = constants.button_state["button_up"]

        # update game logic

        # if right D-Pad is pressed
        if keys & ugame.K_RIGHT != 0:
            # if ship moves off right screen, move it back
            if ship.x > constants.SCREEN_X - constants.SPRITE_SIZE:
                ship.x = constants.SCREEN_X - constants.SPRITE_SIZE
            # else move ship right
            else:
                ship.move(ship.x + constants.BALL_SPEED, ship.y)
                ship.set_frame(frame=None, rotation=1)
                ship_direction = "right"

        # if left D-Pad is pressed
        if keys & ugame.K_LEFT != 0:
            # if ship moves off left screen, move it back
            if ship.x < 0:
                ship.x = 0
            # else move ship left
            else:
                ship.move(ship.x - constants.BALL_SPEED, ship.y)
                ship.set_frame(frame=None, rotation=3)
                ship_direction = "left"

        if keys & ugame.K_UP != 0:
            # if ship moves off up screen, move it back
            if ship.y > constants.SCREEN_Y - constants.SPRITE_SIZE:
                ship.y = constants.SCREEN_Y - constants.SPRITE_SIZE
            # else move ship up
            else:
                ship.move(ship.x, ship.y - 1)
                ship.set_frame(frame=None, rotation=0)
                ship_direction = "up"

        # if left D-Pad is pressed
        if keys & ugame.K_DOWN != 0:
            # if ship moves off down screen, move it back
            if ship.y < 0:
                ship.y = 0
            # else move ship down
            else:
                ship.move(ship.x, ship.y + 1)
                ship.set_frame(frame=None, rotation=2)
                ship_direction = "down"

        # if A Button (fire) is pressed
        if a_button == constants.button_state["button_just_pressed"]:
            # fire a laser, if we have enough power (meaning we have not used up all the lasers)
            for laser_number in range(len(lasers)):
                if lasers[laser_number].x < 0:
                    lasers[laser_number].move(ship.x, ship.y)
                    sound.stop()
                    sound.play(pew_sound)
                    break

        # each frame move the lasers, that have been fired, up

        # first make all the neopixels yellow, then make them green if it is moving up
        lasers_moving_counter = -1
        for pixel_number in range(0, 5):
            pixels[pixel_number] = (0, 10, 0)

        for laser_number in range(len(lasers)):
            if lasers[laser_number].x > 0 and ship_direction == "up":
                lasers[laser_number].move(
                    lasers[laser_number].x,
                    lasers[laser_number].y - constants.ATTACK_SPEED)
                lasers_moving_counter = lasers_moving_counter + 1
                pixels[lasers_moving_counter] = (
                    10, 10 - (2 * lasers_moving_counter + 2), 0)
                if lasers[laser_number].y < constants.OFF_TOP_SCREEN:
                    lasers[laser_number].move(constants.OFF_SCREEN_X,
                                              constants.OFF_SCREEN_Y)
        if lasers_moving_counter == 4:
            for pixel_number in range(0, 5):
                pixels[pixel_number] = (10, 0, 0)
        pixels.show()

        for laser_number in range(len(lasers)):
            if lasers[laser_number].x > 0 and ship_direction == "down":
                lasers[laser_number].move(
                    lasers[laser_number].x,
                    lasers[laser_number].y - constants.ATTACK_SPEED)
                lasers_moving_counter = lasers_moving_counter + 1
                pixels[lasers_moving_counter] = (
                    10, 10 - (2 * lasers_moving_counter + 2), 0)
                if lasers[laser_number].y < constants.OFF_TOP_SCREEN:
                    lasers[laser_number].move(constants.OFF_SCREEN_X,
                                              constants.OFF_SCREEN_Y)
        if lasers_moving_counter == 4:
            for pixel_number in range(0, 5):
                pixels[pixel_number] = (10, 0, 0)

        # each frame move the aliens down the screen
        for alien_number in range(len(aliens)):
            if aliens[alien_number].x > 0:  # meaning it is on the screen
                aliens[alien_number].move(
                    aliens[alien_number].x,
                    aliens[alien_number].y + constants.ALIEN_SPEED)
                if aliens[alien_number].y > constants.SCREEN_Y:
                    aliens[alien_number].move(constants.OFF_SCREEN_X,
                                              constants.OFF_SCREEN_Y)
                    show_alien()  # make it randomly show up at top again

        # each frame check if any of the lasers are touching any of the aliens
        for laser_number in range(len(lasers)):
            if lasers[laser_number].x > 0:
                for alien_number in range(len(aliens)):
                    if aliens[alien_number].x > 0:
                        # https://circuitpython-stage.readthedocs.io/en/latest/#stage.collide
                        # and https://stackoverflow.com/questions/306316/determine-if-two-rectangles-overlap-each-other

                        # the first 4 numbers are the coordinates of A box
                        # since the laser is thin, it made it thinner and slightly smaller
                        #
                        # the second 4 numbers are the alien, it is more of a box so I just made it slightly smaller
                        #
                        # if you slow down the FPS, then you can see the interaction more easily to alter these numbers
                        if stage.collide(lasers[laser_number].x + 6,
                                         lasers[laser_number].y + 2,
                                         lasers[laser_number].x + 11,
                                         lasers[laser_number].y + 12,
                                         aliens[alien_number].x + 1,
                                         aliens[alien_number].y,
                                         aliens[alien_number].x + 15,
                                         aliens[alien_number].y + 15):
                            # you hit an alien
                            aliens[alien_number].move(constants.OFF_SCREEN_X,
                                                      constants.OFF_SCREEN_Y)
                            lasers[laser_number].move(constants.OFF_SCREEN_X,
                                                      constants.OFF_SCREEN_Y)
                            # add 1 to the score
                            score += 1
                            score_text.clear()
                            score_text.cursor(0, 0)
                            score_text.move(1, 1)
                            score_text.text("Score: {0}".format(score))
                            # this will freeze the screen for a split second, but we have no option
                            game.render_block()
                            # play sound effect
                            sound.stop()
                            sound.play(boom_sound)
                            show_alien()
                            show_alien()
                            alien_count = alien_count + 1

        # each frame check if any of the aliens are touching the ship
        for alien_number in range(len(aliens)):
            if aliens[alien_number].x > 0:
                # https://circuitpython-stage.readthedocs.io/en/latest/#stage.collide
                # and https://stackoverflow.com/questions/306316/determine-if-two-rectangles-overlap-each-other
                if stage.collide(aliens[alien_number].x + 1,
                                 aliens[alien_number].y,
                                 aliens[alien_number].x + 15,
                                 aliens[alien_number].y + 15, ship.x, ship.y,
                                 ship.x + 15, ship.y + 15):
                    # alien hit the ship
                    sound.stop()
                    sound.play(crash_sound)
                    for pixel_number in range(0, 5):
                        pixels[pixel_number] = (25, 0, 25)
                    pixels.show()
                    # Wait for 1 seconds
                    time.sleep(4.0)
                    # need to release the NeoPixels
                    pixels.deinit()
                    sound.stop()
                    game_over_scene(score)

        # redraw sprite list
        game.render_sprites(sprites + lasers + aliens)
        game.tick()  # wait until refresh rate finishes
コード例 #27
0
def gamescene():
    def show_alien():
        for alien_number in range(len(aliens)):
            if aliens[alien_number].x < 0:
                aliens[alien_number].move(
                    random.randint(0 + constants.SPRITE_SIZE,
                                   constants.SCREEN_X - constants.SPRITE_SIZE),
                    constants.OFF_TOP_SCREEN)

    # an image bank for CircuitPython
    image_bank_1 = stage.Bank.from_bmp16("space_aliens.bmp")

    # a list of sprites
    sprites = []

    # button state information
    a_button = constants.button_state["button_up"]
    b_button = constants.button_state["button_up"]
    start_button = constants.button_state["button_up"]
    select_button = constants.button_state["button_up"]

    # get sound ready
    pew_sound = open("pew.wav", 'rb')
    sound = ugame.audio
    sound.stop()
    sound.mute(False)

    # sets the background to image 0 in the bank
    background = stage.Grid(image_bank_1, constants.SCREEN_X,
                            constants.SCREEN_Y)
    for x_location in range(constants.SCREEN_GRID_X):
        for y_location in range(constants.SCREEN_GRID_Y):
            tile_picked = random.randint(1, 3)
            background.tile(x_location, y_location, tile_picked)

    # create a sprite
    # parameters (image bank, image # in bank, x, y)
    ship = stage.Sprite(image_bank_1, 4, 12, 112)
    sprites.insert(0, ship)  # insert at top of sprite list

    aliens = []
    for alien_number in range(constants.TOTAL_NUMBER_OF_ALIENS):
        a_single_alien = stage.Sprite(image_bank_1, 9, constants.OFF_SCREEN_X,
                                      constants.OFF_SCREEN_X)
        aliens.append(a_single_alien)

    alien_count = 1
    show_alien()

    # Create lasers for shooting
    lasers = []
    for laser_number in range(constants.TOTAL_NUMBER_OF_LASERS):
        a_single_laser = stage.Sprite(image_bank_1, 10, constants.OFF_SCREEN_X,
                                      constants.OFF_SCREEN_Y)
        lasers.append(a_single_laser)

    # create a stage for the background to show up on
    #   and set the frame rate to 60fps
    game = stage.Stage(ugame.display, 60)
    # set the background layer
    game.layers = sprites + lasers + aliens + [background]
    # render the background
    game.render_block()

    # repeat forever, game loop
    while True:
        # get user input
        keys = ugame.buttons.get_pressed()
        # (keys)
        if keys & ugame.K_X != 0:  # a button (fire)
            if a_button == constants.button_state["button_up"]:
                a_button = constants.button_state["button_just_pressed"]
            elif a_button == constants.button_state["button_just_pressed"]:
                a_button = constants.button_state["button_still_pressed"]
        else:
            if a_button == constants.button_state["button_still_pressed"]:
                a_button = constants.button_state["button_released"]
            else:
                a_button = constants.button_state["button_up"]
        if keys & ugame.K_X:
            # print("A")
            pass
        if keys & ugame.K_O:
            # print("B")
            pass
        if keys & ugame.K_START:
            # print("K_START")
            pass
        if keys & ugame.K_SELECT:
            # print("K_SELECT")
            pass
        # move ship right
        if keys & ugame.K_RIGHT != 0:
            if ship.x > constants.SCREEN_X - constants.SPRITE_SIZE:
                ship.move(constants.SCREEN_X - constants.SPRITE_SIZE, ship.y)
            else:
                ship.move(ship.x + constants.SHIP_SPEED, ship.y)
            pass
        # move ship left
        if keys & ugame.K_LEFT != 0:
            if ship.x < 0:
                ship.move(0, ship.y)
            else:
                ship.move(ship.x - constants.SHIP_SPEED, ship.y)
            pass
        if keys & ugame.K_UP:
            # print("B")
            ship.move(ship.x, ship.y)
            pass
        if keys & ugame.K_DOWN:
            # print("K_DOWN")
            ship.move(ship.x, ship.y)
            pass

        if a_button == constants.button_state["button_just_pressed"]:
            sound.play(pew_sound)
            if a_button == constants.button_state["button_just_pressed"]:
                # fire a laser, if we have some
                for laser_number in range(len(lasers)):
                    if lasers[laser_number].x < 0:
                        lasers[laser_number].move(ship.x, ship.y)
                        sound.stop()
                        sound.play(pew_sound)
                        break

        # moving laser up
        for laser_number in range(len(lasers)):
            if lasers[laser_number].x > 0:
                lasers[laser_number].move(
                    lasers[laser_number].x,
                    lasers[laser_number].y - constants.LASER_SPEED)

            # if off top of screen
            if lasers[laser_number].y < constants.OFF_TOP_SCREEN:
                lasers[laser_number].move(constants.OFF_SCREEN_X,
                                          constants.OFF_SCREEN_Y)

        # moves aliens down screen
        for alien_number in range(len(aliens)):
            if aliens[alien_number].x > 0:
                aliens[alien_number].move(
                    aliens[alien_number].x,
                    aliens[alien_number].y + constants.ALIEN_SPEED)
                if aliens[alien_number].y > constants.SCREEN_Y:
                    aliens[alien_number].move(constants.OFF_SCREEN_X,
                                              constants.OFF_SCREEN_Y)
                    show_alien()

        # redraw sprite list
        game.render_sprites(sprites + lasers + aliens)
        game.tick()  # wait until refresh rate finishes
コード例 #28
0
ファイル: code.py プロジェクト: paul-madut/ICS3U-2019-Group1
def menu_scene():
    # this function is the menu scene

    # an image bank for CircuitPython
    image_bank_2 = stage.Bank.from_bmp16("mt_game_studio.bmp")

    # sets the background to image 0 in the bank
    background = stage.Grid(image_bank_2, constants.SCREEN_GRID_X,
                            constants.SCREEN_GRID_Y)

    # used this program to split the iamge into tile: https://ezgif.com/sprite-cutter/ezgif-5-818cdbcc3f66.png
    background.tile(2, 2, 0)  # blank white
    background.tile(3, 2, 1)
    background.tile(4, 2, 2)
    background.tile(5, 2, 3)
    background.tile(6, 2, 4)
    background.tile(7, 2, 0)  # blank white

    background.tile(2, 3, 0)  # blank white
    background.tile(3, 3, 5)
    background.tile(4, 3, 6)
    background.tile(5, 3, 7)
    background.tile(6, 3, 8)
    background.tile(7, 3, 0)  # blank white

    background.tile(2, 4, 0)  # blank white
    background.tile(3, 4, 9)
    background.tile(4, 4, 10)
    background.tile(5, 4, 11)
    background.tile(6, 4, 12)
    background.tile(7, 4, 0)  # blank white

    background.tile(2, 5, 0)  # blank white
    background.tile(3, 5, 0)
    background.tile(4, 5, 13)
    background.tile(5, 5, 14)
    background.tile(6, 5, 0)
    background.tile(7, 5, 0)  # blank white

    text = []

    text1 = stage.Text(width=29,
                       height=14,
                       font=None,
                       palette=constants.NEW_PALETTE,
                       buffer=None)
    text1.move(20, 10)
    text1.text("Sniper Shootout")
    text.append(text1)

    text2 = stage.Text(width=29,
                       height=14,
                       font=None,
                       palette=constants.NEW_PALETTE,
                       buffer=None)
    text2.move(35, 110)
    text2.text("PRESS START")
    text.append(text2)

    # get sound ready
    # follow this guide to convert your other sounds to something that will work
    #    https://learn.adafruit.com/microcontroller-compatible-audio-file-conversion
    coin_sound = open("coin.wav", 'rb')
    sound = ugame.audio
    sound.stop()
    sound.mute(False)
    sound.play(coin_sound)

    # create a stage for the background to show up on
    #   and set the frame rate to 60fps
    game = stage.Stage(ugame.display, 60)
    # set the layers, items show up in order
    game.layers = text + [background]
    # render the background and inital location of sprite list
    # most likely you will only render background once per scene
    game.render_block()

    # repeat forever, game loop
    while True:
        # get user input

        # update game logic
        keys = ugame.buttons.get_pressed()
        #print(keys)

        if keys & ugame.K_START != 0:  # Start button
            game_scene()
コード例 #29
0
def game_scene():
    # this function is the main game scene

    # image bank
    image_bank_background = stage.Bank.from_bmp16(
        "space_aliens_background.bmp")
    image_bank_sprites = stage.Bank.from_bmp16("space_aliens.bmp")

    # buttons that you want to keep state information on
    a_button = constants.button_state["button_up"]
    b_button = constants.button_state["button_up"]
    start_button = constants.button_state["button_up"]
    select_button = constants.button_state["button_up"]

    # get sound ready
    pew_sound = open("pew.wav", 'rb')
    sound = ugame.audio
    sound.stop()
    sound.mute(False)

    # set background to img 0 and 10x8 tiles of size 16x16
    background = stage.Grid(image_bank_background, constants.SCREEN_GRID_X,
                            constants.SCREEN_GRID_Y)

    # sprite that will be updated every frame
    ship = stage.Sprite(image_bank_sprites, 5, 75,
                        constants.SCREEN_Y - (2 * constants.SPRITE_SIZE))

    alien = stage.Sprite(
        image_bank_sprites, 9,
        int(constants.SCREEN_X / 2 - constants.SPRITE_SIZE / 2), 16)

    # creates stage and sets it to 60fps
    game = stage.Stage(ugame.display, constants.FPS)

    # sets the layers of all sprites, in order
    game.layers = [ship] + [alien] + [background]

    # renders all sprites, only once
    game.render_block()

    # forever game loop
    while True:
        # get user input
        keys = ugame.buttons.get_pressed()

        # A button to fire
        if keys & ugame.K_O != 0:
            if a_button == constants.button_state["button_up"]:
                a_button = constants.button_state["button_just_pressed"]
            elif a_button == constants.button_state["button_just_pressed"]:
                a_button = constants.button_state["button_still_pressed"]
        else:
            if a_button == constants.button_state["button_still_pressed"]:
                a_button = constants.button_state["button_released"]
            else:
                a_button = constants.button_state["button_up"]

        if keys & ugame.K_X != 0:
            pass
        if keys & ugame.K_START != 0:
            pass
        if keys & ugame.K_SELECT != 0:
            pass
        if keys & ugame.K_RIGHT != 0:
            if ship.x <= constants.SCREEN_X - constants.SPRITE_SIZE:
                ship.move(ship.x + constants.SPRITE_MOVEMENT_SPEED, ship.y)
            else:
                ship.move(constants.SCREEN_X - constants.SPRITE_SIZE, ship.y)
        if keys & ugame.K_LEFT != 0:
            if ship.x >= 0:
                ship.move(ship.x - constants.SPRITE_MOVEMENT_SPEED, ship.y)
            else:
                ship.move(0, ship.y)
        if keys & ugame.K_UP != 0:
            pass
        if keys & ugame.K_DOWN != 0:
            pass

        # update game logic
        # play sound if A was button_just_pressed
        if a_button == constants.button_state["button_just_pressed"]:
            sound.play(pew_sound)

        # redraw Sprite
        game.render_sprites([ship] + [alien])
        game.tick()
コード例 #30
0
def game_scene():
    # this function is the main game scene

    score = 0

    def show_alien():

        for alien_number in range(len(aliens)):
            if aliens[alien_number].x < 0:
                aliens[alien_number].move(
                    random.randint(0 + constants.SPRITE_SIZE,
                                   constants.SCREEN_X - constants.SPRITE_SIZE),
                    constants.OFF_TOP_SCREEN)
                break

    # image bank
    image_bank_background = stage.Bank.from_bmp16(
        "space_aliens_background.bmp")
    image_bank_sprites = stage.Bank.from_bmp16("space_aliens.bmp")

    # buttons that you want to keep state information on
    a_button = constants.button_state["button_up"]
    b_button = constants.button_state["button_up"]
    start_button = constants.button_state["button_up"]
    select_button = constants.button_state["button_up"]

    # get sound ready
    pew_sound = open("pew.wav", 'rb')
    sound = ugame.audio
    sound.stop()
    sound.mute(False)

    boom_sound = open("boom.wav", 'rb')
    sound = ugame.audio
    sound.stop()
    sound.mute(False)

    # set background to img 0 and 10x8 tiles of size 16x16
    background = stage.Grid(image_bank_background, constants.SCREEN_GRID_X,
                            constants.SCREEN_GRID_Y)
    for x_location in range(constants.SCREEN_GRID_X):
        for y_location in range(constants.SCREEN_GRID_Y):
            tile_picked = random.randint(1, 3)
            background.tile(x_location, y_location, tile_picked)

    # sprite that will be updated every frame
    ship = stage.Sprite(image_bank_sprites, 5, 75,
                        constants.SCREEN_Y - (2 * constants.SPRITE_SIZE))

    aliens = []
    for alien_number in range(constants.TOTAL_NUMBER_OF_ALIENS):
        a_single_alien = stage.Sprite(image_bank_sprites, 9,
                                      constants.OFF_SCREEN_X,
                                      constants.OFF_SCREEN_Y)
        aliens.append(a_single_alien)
    show_alien()

    lasers = []
    for laser_number in range(constants.TOTAL_NUMBER_OF_LASERS):
        a_single_laser = stage.Sprite(image_bank_sprites, 10,
                                      constants.OFF_SCREEN_X,
                                      constants.OFF_SCREEN_Y)
        lasers.append(a_single_laser)

    # creates stage and sets it to 60fps
    game = stage.Stage(ugame.display, constants.FPS)

    # sets the layers of all sprites, in order
    game.layers = lasers + [ship] + aliens + [background]

    # renders all sprites, only once
    game.render_block()

    # forever game loop
    while True:
        # get user input
        keys = ugame.buttons.get_pressed()

        # A button to fire
        if keys & ugame.K_O != 0:
            if a_button == constants.button_state["button_up"]:
                a_button = constants.button_state["button_just_pressed"]
            elif a_button == constants.button_state["button_just_pressed"]:
                a_button = constants.button_state["button_still_pressed"]
        else:
            if a_button == constants.button_state["button_still_pressed"]:
                a_button = constants.button_state["button_released"]
            else:
                a_button = constants.button_state["button_up"]

        if keys & ugame.K_X != 0:
            pass
        if keys & ugame.K_START != 0:
            pass
        if keys & ugame.K_SELECT != 0:
            pass
        if keys & ugame.K_RIGHT != 0:
            if ship.x <= constants.SCREEN_X - constants.SPRITE_SIZE:
                ship.move(ship.x + constants.SPRITE_MOVEMENT_SPEED, ship.y)
            else:
                ship.move(constants.SCREEN_X - constants.SPRITE_SIZE, ship.y)
        if keys & ugame.K_LEFT != 0:
            if ship.x >= 0:
                ship.move(ship.x - constants.SPRITE_MOVEMENT_SPEED, ship.y)
            else:
                ship.move(0, ship.y)
        if keys & ugame.K_UP != 0:
            pass
        if keys & ugame.K_DOWN != 0:
            pass

        # update game logic
        if a_button == constants.button_state["button_just_pressed"]:
            for laser_number in range(len(lasers)):
                if lasers[laser_number].x < 0:
                    lasers[laser_number].move(ship.x, ship.y)
                    sound.play(pew_sound)
                    break

        for laser_number in range(len(lasers)):
            if lasers[laser_number].x > 0:
                lasers[laser_number].move(
                    lasers[laser_number].x,
                    lasers[laser_number].y - constants.LASER_SPEED)
                if lasers[laser_number].y < constants.OFF_TOP_SCREEN:
                    lasers[laser_number].move(constants.OFF_SCREEN_X,
                                              constants.OFF_SCREEN_Y)
        for alien_number in range(len(aliens)):
            if aliens[alien_number].x > 0:
                aliens[alien_number].move(
                    aliens[alien_number].x,
                    aliens[alien_number].y + constants.ALIEN_SPEED)
                if aliens[alien_number].y > constants.SCREEN_Y:
                    aliens[alien_number].move(constants.OFF_SCREEN_X,
                                              constants.OFF_SCREEN_Y)
                    show_alien()

        for laser_number in range(len(lasers)):
            if lasers[laser_number].x > 0:
                for alien_number in range(len(aliens)):
                    if aliens[alien_number].x > 0:
                        if stage.collide(lasers[laser_number].x + 6,
                                         lasers[laser_number].y + 2,
                                         lasers[laser_number].x + 11,
                                         lasers[laser_number].y + 12,
                                         aliens[alien_number].x + 1,
                                         aliens[alien_number].y,
                                         aliens[alien_number].x + 15,
                                         aliens[alien_number].y + 15):
                            aliens[alien_number].move(constants.OFF_SCREEN_X,
                                                      constants.OFF_SCREEN_Y)
                            lasers[laser_number].move(constants.OFF_SCREEN_X,
                                                      constants.OFF_SCREEN_Y)
                            sound.stop()
                            sound.play(boom_sound)
                            show_alien()
                            show_alien()
                            score = score + 1

        # redraw Sprite
        game.render_sprites(aliens + lasers + [ship])
        game.tick()