Example #1
0
 def return_to_menu():
     pickle_out = open(resource_path('data/settings.info'), 'wb')
     pickle.dump((self.level_stats, self.music, self.sounds),
                 pickle_out)
     pickle_out.close()
     self.ui_manager.purge_ui_elements()
     self.window.show_view(MainMenu(self.window))
Example #2
0
        def update_height():
            text_box = self.ui_manager.find_by_id('max_height')
            try:
                height = int(text_box.text)
                if not 1500 < height:
                    arcade.play_sound(
                        arcade.load_sound(resource_path('sounds/erro.mp3')),
                        0.05)
                    text_box.text = 'Must be over 1500'
                    height = 2500
            except ValueError:
                arcade.play_sound(
                    arcade.load_sound(resource_path('sounds/erro.mp3')), 0.05)
                text_box.text = 'Must be an integer'
                height = 2500

            self.level_stats.max_height = height
Example #3
0
        def update_platforms():
            text_box = self.ui_manager.find_by_id('max_platforms')
            try:
                length = int(text_box.text)
                if not 0 < length:
                    arcade.play_sound(
                        arcade.load_sound(resource_path('sounds/erro.mp3')),
                        0.05)
                    text_box.text = 'Must be over 0'
                    length = 10
            except ValueError:
                arcade.play_sound(
                    arcade.load_sound(resource_path('sounds/erro.mp3')), 0.05)
                text_box.text = 'Must be an integer'
                length = 10

            self.level_stats.pl_num = length
Example #4
0
 def on_click(self):
     if self.level_seed.lvl_id <= self.level_manager.current_level.lvl_id:
         click_sound()
         self.ui_manager.purge_ui_elements()
         self.window.show_view(Game(self.level_seed, self.level_manager))
     else:
         arcade.play_sound(
             arcade.load_sound(resource_path('sounds/erro.mp3')), 0.05)
Example #5
0
        def update_gap():
            text_box = self.ui_manager.find_by_id('max_gap')
            try:
                gap = int(text_box.text)
                if not 30 < gap:
                    arcade.play_sound(
                        arcade.load_sound(resource_path('sounds/erro.mp3')),
                        0.05)
                    text_box.text = 'Must be over 30'
                    gap = 160
            except ValueError:
                arcade.play_sound(
                    arcade.load_sound(resource_path('sounds/erro.mp3')), 0.05)
                text_box.text = 'Must be an integer'
                gap = 160

            self.level_stats.max_gap = gap
Example #6
0
 def get_mv_chance():
     mv_chance = self.ui_manager.find_by_id(id).text
     mv_chance = mv_chance.replace('%', '')
     try:
         mv_chance = float(mv_chance) / 100
         if not 0 <= mv_chance <= 1:
             arcade.play_sound(
                 arcade.load_sound(resource_path('sounds/erro.mp3')),
                 0.05)
             self.ui_manager.find_by_id(id).text = error_text
             mv_chance = 0.2
     except ValueError:
         arcade.play_sound(
             arcade.load_sound(resource_path('sounds/erro.mp3')), 0.05)
         self.ui_manager.find_by_id(id).text = error_text
         mv_chance = 0.2
     if id == 'mv_chance_box':
         self.level_stats.mv_chance = mv_chance
     elif id == 'mv_chance_x':
         self.level_stats.mv_chance_x = mv_chance
     elif id == 'mv_chance_y':
         self.level_stats.mv_chance_y = mv_chance
Example #7
0
 def __init__(self, levels):
     self.current_level = None
     self.levels = levels
     self.finished = False
     try:
         save_file = open(resource_path('data/current_level.save'), 'rb')
         lvl_id = pickle.load(save_file)
         if lvl_id == 21:
             self.current_level = self.levels[20]
             self.finished = True
         else:
             self.current_level = self.levels[lvl_id]
     except FileNotFoundError:
         self.current_level = self.levels[0]
Example #8
0
def click_sound():
    arcade.play_sound(arcade.load_sound(resource_path('sounds/click.wav')),
                      0.05)
Example #9
0
    def setup(self):
        try:
            file = open(resource_path('data/settings.info'), 'rb')
            info = pickle.load(file)
            self.level_stats = info[0]
            self.music = info[1]
            self.sounds = info[2]
        except FileNotFoundError:
            self.level_stats = generator_data.GeneratorData()

        arcade.set_background_color(arcade.color.BLACK)
        if self.level_stats.seed:
            text = str(self.level_stats.seed)
        else:
            text = 'Enter Seed'
        seed_box = UIInputBox(100, C.SCREEN_HEIGHT - 100, 150, 40, text,
                              'seed_box')

        self.ui_manager.add_ui_element(seed_box)

        @seed_box.event('on_enter')
        def set_seed():
            self.level_stats.seed = self.ui_manager.find_by_id('seed_box').text

        self.create_input_box_mv(
            'mv_chance_box',
            str(int(self.level_stats.mv_chance * 100)) + '%',
            'Value must be between 0 and 100%',
            [475, C.SCREEN_HEIGHT - 100, 500, 40], self.level_stats)

        self.create_input_box_mv(
            'mv_chance_x',
            str(int(self.level_stats.mv_chance_x * 100)) + '%',
            'Value must be between 0 and 100%',
            [475, C.SCREEN_HEIGHT - 300, 500, 40], self.level_stats)

        self.create_input_box_mv(
            'mv_chance_y',
            str(int(self.level_stats.mv_chance_y * 100)) + '%',
            'Value must be between 0 and 100%',
            [475, C.SCREEN_HEIGHT - 500, 500, 40], self.level_stats)

        if self.level_stats.mv_chance_xy:
            text = 'True'
        else:
            text = 'False'
        mv_allowed_xy = UIFlatButton(text,
                                     300,
                                     C.SCREEN_HEIGHT - 700,
                                     width=150,
                                     height=50,
                                     id='mv_allowed_xy')
        mv_allowed_xy.set_style_attrs(border_color_hover=arcade.color.WHITE,
                                      border_color_press=arcade.color.WHITE)
        self.ui_manager.add_ui_element(mv_allowed_xy)

        @mv_allowed_xy.event('on_click')
        def switch():
            button = self.ui_manager.find_by_id('mv_allowed_xy')
            if button.text == 'True':
                button.text = 'False'
                output = False
            else:
                button.text = 'True'
                output = True
            button.render()
            self.level_stats.mv_chance_xy = output

        max_gap = UIInputBox(975, C.SCREEN_HEIGHT - 100, 400, 40,
                             str(self.level_stats.max_gap), 'max_gap')

        self.ui_manager.add_ui_element(max_gap)

        @max_gap.event('on_enter')
        def update_gap():
            text_box = self.ui_manager.find_by_id('max_gap')
            try:
                gap = int(text_box.text)
                if not 30 < gap:
                    arcade.play_sound(
                        arcade.load_sound(resource_path('sounds/erro.mp3')),
                        0.05)
                    text_box.text = 'Must be over 30'
                    gap = 160
            except ValueError:
                arcade.play_sound(
                    arcade.load_sound(resource_path('sounds/erro.mp3')), 0.05)
                text_box.text = 'Must be an integer'
                gap = 160

            self.level_stats.max_gap = gap

        max_height = UIInputBox(975, C.SCREEN_HEIGHT - 300, 400, 40,
                                str(self.level_stats.max_height), 'max_height')

        self.ui_manager.add_ui_element(max_height)

        @max_height.event('on_enter')
        def update_height():
            text_box = self.ui_manager.find_by_id('max_height')
            try:
                height = int(text_box.text)
                if not 1500 < height:
                    arcade.play_sound(
                        arcade.load_sound(resource_path('sounds/erro.mp3')),
                        0.05)
                    text_box.text = 'Must be over 1500'
                    height = 2500
            except ValueError:
                arcade.play_sound(
                    arcade.load_sound(resource_path('sounds/erro.mp3')), 0.05)
                text_box.text = 'Must be an integer'
                height = 2500

            self.level_stats.max_height = height

        max_platforms = UIInputBox(975, C.SCREEN_HEIGHT - 500, 400, 40,
                                   str(self.level_stats.pl_num),
                                   'max_platforms')

        self.ui_manager.add_ui_element(max_platforms)

        @max_platforms.event('on_enter')
        def update_platforms():
            text_box = self.ui_manager.find_by_id('max_platforms')
            try:
                length = int(text_box.text)
                if not 0 < length:
                    arcade.play_sound(
                        arcade.load_sound(resource_path('sounds/erro.mp3')),
                        0.05)
                    text_box.text = 'Must be over 0'
                    length = 10
            except ValueError:
                arcade.play_sound(
                    arcade.load_sound(resource_path('sounds/erro.mp3')), 0.05)
                text_box.text = 'Must be an integer'
                length = 10

            self.level_stats.pl_num = length

        return_to_menu = UIFlatButton('BACK', 1400, 100, 150, 50, id='return')

        return_to_menu.set_style_attrs(border_color_hover=arcade.color.WHITE,
                                       border_color_press=arcade.color.WHITE)

        self.ui_manager.add_ui_element(return_to_menu)

        @return_to_menu.event('on_click')
        def return_to_menu():
            pickle_out = open(resource_path('data/settings.info'), 'wb')
            pickle.dump((self.level_stats, self.music, self.sounds),
                        pickle_out)
            pickle_out.close()
            self.ui_manager.purge_ui_elements()
            self.window.show_view(MainMenu(self.window))

        if self.music:
            text = 'True'
        else:
            text = 'False'
        music = UIFlatButton(text, 850, 200, width=150, height=50, id='music')
        music.set_style_attrs(border_color_hover=arcade.color.WHITE,
                              border_color_press=arcade.color.WHITE)
        self.ui_manager.add_ui_element(music)

        @music.event('on_click')
        def switch():
            button = self.ui_manager.find_by_id('music')
            if button.text == 'True':
                button.text = 'False'
                output = False
            else:
                button.text = 'True'
                output = True
            button.render()
            self.music = output

        if self.sounds:
            text = 'True'
        else:
            text = 'False'
        sounds = UIFlatButton(text,
                              1100,
                              200,
                              width=150,
                              height=50,
                              id='sounds')
        sounds.set_style_attrs(border_color_hover=arcade.color.WHITE,
                               border_color_press=arcade.color.WHITE)
        self.ui_manager.add_ui_element(sounds)

        @sounds.event('on_click')
        def switch():
            button = self.ui_manager.find_by_id('sounds')
            if button.text == 'True':
                button.text = 'False'
                output = False
            else:
                button.text = 'True'
                output = True
            button.render()
            self.sounds = output
Example #10
0
 def play_song(self):
     self.menu_music = arcade.Sound(resource_path('music/MenuSong.wav'),
                                    streaming=True)
     self.menu_music.play(0.1)
     sleep(0.03)
Example #11
0
    def setup(self):
        try:
            file = open(resource_path('data/settings.info'), 'rb')
            info = pickle.load(file)
            self.custom_settings = info[0]
            self.music = info[1]
            self.sounds = info[2]
        except FileNotFoundError:
            self.custom_settings = generator_data.GeneratorData()

        self.level_manager = LevelManager(generator_data.level_list)
        arcade.set_background_color(arcade.color.BLACK)
        if self.music:
            self.play_song()
        self.ui_manager.purge_ui_elements()
        self.window.set_viewport(0, C.SCREEN_WIDTH, 0, C.SCREEN_HEIGHT)
        play_button = PlayButton(self.window, self.ui_manager,
                                 self.level_manager, 'PLAY',
                                 self.window.width // 2, 500, 200, 50)
        play_button.set_style_attrs(font_color=arcade.color.BLACK,
                                    font_color_hover=arcade.color.BLACK,
                                    font_color_press=arcade.color.BLACK,
                                    bg_color=arcade.color.GREEN,
                                    bg_color_hover=(0, 150, 0),
                                    bg_color_press=arcade.color.DARK_GREEN,
                                    border_color=arcade.color.GREEN,
                                    border_color_hover=arcade.color.WHITE,
                                    border_color_press=arcade.color.WHITE)

        self.ui_manager.add_ui_element(play_button)

        play_button_endless = PlayButtonEndless(self.window, self.ui_manager,
                                                'PLAY ENDLESS',
                                                self.window.width // 2, 700,
                                                300, 70)
        play_button_endless.set_style_attrs(
            font_color=arcade.color.WHITE,
            bg_color=arcade.color.BLACK,
            border_color=arcade.color.BLACK,
            border_color_hover=arcade.color.WHITE,
            border_color_press=arcade.color.WHITE)

        self.ui_manager.add_ui_element(play_button_endless)

        play_button_custom = UIFlatButton('PLAY CUSTOM',
                                          self.window.width // 2, 600, 250, 50)
        play_button_custom.set_style_attrs(
            font_color=arcade.color.BLACK,
            font_color_hover=arcade.color.BLACK,
            font_color_press=arcade.color.BLACK,
            bg_color=arcade.color.GREEN,
            bg_color_hover=(0, 150, 0),
            bg_color_press=arcade.color.DARK_GREEN,
            border_color=arcade.color.GREEN,
            border_color_hover=arcade.color.WHITE,
            border_color_press=arcade.color.WHITE)

        self.ui_manager.add_ui_element(play_button_custom)

        @play_button_custom.event('on_click')
        def play():
            click_sound()
            self.ui_manager.purge_ui_elements()
            self.window.show_view(Game(self.custom_settings))

        level_button = LevelMenuButton(self.window, self.ui_manager,
                                       self.level_manager, 'LEVEL MENU',
                                       self.window.width // 2, 400, 200, 50)
        level_button.set_style_attrs(font_color=arcade.color.BLACK,
                                     font_color_hover=arcade.color.BLACK,
                                     font_color_press=arcade.color.BLACK,
                                     bg_color=arcade.color.BLUE,
                                     bg_color_hover=(0, 0, 150),
                                     bg_color_press=arcade.color.DARK_BLUE,
                                     border_color=arcade.color.BLUE,
                                     border_color_hover=arcade.color.WHITE,
                                     border_color_press=arcade.color.WHITE)

        self.ui_manager.add_ui_element(level_button)

        options_button = OptionsMenuButton(self.window, self.ui_manager,
                                           'OPTIONS', self.window.width // 2,
                                           300, 200, 50)
        options_button.set_style_attrs(font_color=arcade.color.BLACK,
                                       font_color_hover=arcade.color.BLACK,
                                       font_color_press=arcade.color.BLACK,
                                       bg_color=arcade.color.BLUE,
                                       bg_color_hover=(0, 0, 150),
                                       bg_color_press=arcade.color.DARK_BLUE,
                                       border_color=arcade.color.BLUE,
                                       border_color_hover=arcade.color.WHITE,
                                       border_color_press=arcade.color.WHITE)

        self.ui_manager.add_ui_element(options_button)

        exit_button = ExitButton('EXIT', self.window.width // 2, 200, 200, 50)
        exit_button.set_style_attrs(font_color=arcade.color.BLACK,
                                    font_color_hover=arcade.color.BLACK,
                                    font_color_press=arcade.color.BLACK,
                                    bg_color=arcade.color.RED,
                                    bg_color_hover=(150, 0, 0),
                                    bg_color_press=arcade.color.DARK_RED,
                                    border_color=arcade.color.RED,
                                    border_color_hover=arcade.color.WHITE,
                                    border_color_press=arcade.color.WHITE)

        self.ui_manager.add_ui_element(exit_button)

        self.ui_manager.add_ui_element(
            UILabel('Created by FalconFX9 for the timathon challenge',
                    C.SCREEN_WIDTH // 2, 100))
Example #12
0
def main():
    pygame.mixer.pre_init(44100, 16, 2,
                          4096)  #frequency, size, channels, buffersize
    pygame.init()

    #call an instance of game
    game = Game(START_SNAKE_COORDS, START_FPS)

    #define fonts
    BASICFONT_SCORE = pygame.font.Font(
        resource_path(os.path.join('resources', 'freesansbold.ttf')), 18)
    BASICFONT_GO = pygame.font.Font(
        resource_path(os.path.join('resources', 'freesansbold.ttf')), 40)

    pygame.display.set_caption('Snake')
    fpsClock = pygame.time.Clock()
    pygame.mixer.music.load(
        resource_path(os.path.join('resources', "gnam.ogg")))
    baseMusic = pygame.mixer.Sound(
        resource_path(os.path.join('resources', "gigi.ogg")))
    gameover_sound = pygame.mixer.Sound(
        resource_path(os.path.join('resources', "azz.ogg")))
    baseMusic.set_volume(0.3)
    channel = baseMusic.play(1)
    musicIsPaused = False
    game.frameCounter = 0
    #main loop
    while True:
        game.frameCounter += 1

        #BACKGROUND
        game.display.fill(game.BLACK)
        # initialize the game field
        game.initializeWindow()

        #CAPTURE EVENT AND CHANGE DIRECTION
        evdir = None
        for event in pygame.event.get():
            # Capture quit event
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            #Capture restart match
            if event.type == MOUSEBUTTONDOWN:
                pos = pygame.mouse.get_pos()
                #position in the range of the button restart
                if game.POSITION_RESTART_BUTT[0] < pos[0] < game.POSITION_RESTART_BUTT[0]+game.SIZE_RESTART_BUTT and \
                game.POSITION_RESTART_BUTT[1] < pos[1] < game.POSITION_RESTART_BUTT[1]+game.SIZE_RESTART_BUTT:
                    #re-initialize game
                    game = Game(START_SNAKE_COORDS, START_FPS)
                if game.POSITION_VOL_BUTT[0] < pos[0] < game.POSITION_VOL_BUTT[0]+game.SIZE_VOL_BUTT and \
                game.POSITION_VOL_BUTT[1] < pos[1] < game.POSITION_VOL_BUTT[1]+game.SIZE_VOL_BUTT:
                    #sound
                    if musicIsPaused:
                        channel.unpause()
                        musicIsPaused = False
                    else:
                        channel.pause()
                        musicIsPaused = True

            # if you do not lose the match, capture changing direction
            if game.game_over_status is False:
                if event.type == KEYDOWN:
                    evdir = game.changeDir(event)
                    if evdir is not None:
                        game.direction = evdir
                        game.move()  #move the snake
                        if game.game_over_status:
                            gameover_sound.play()
                        break  #just one direction for fps

        #if you did not change direction, move the snake
        #in the direction that it had previously
        if evdir is None and not game.game_over_status:
            game.move()
            if game.game_over_status:
                gameover_sound.play()

        # check if you eat fruit
        game.eatFruit()

        if game.fruitcoords[1] == (
        ) and game.frameCounter >= APPEAR_2PT_FRUIT * (game.fps +
                                                       game.score / 3):
            game.locateFruit(1)
            game.frameCounter = 0
        elif game.fruitcoords[1] != (
        ) and game.frameCounter >= DISAPPEAR_2PT_FRUIT * (game.fps +
                                                          game.score / 3):
            game.fruitcoords[1] = tuple()
            game.frameCounter = 0

        # eaten fruit becomes snake when it "leave" the tail of the snake
        game.processEatenFruit()

        # draw the game
        game.drawVolume(not musicIsPaused)
        game.drawsnake()
        game.drawFruit()
        game.drawScore(BASICFONT_SCORE)
        if game.game_over_status:
            game.drawGameOver(BASICFONT_GO)

        #update display
        pygame.display.update()
        #change the speed with the score
        fpsClock.tick(game.fps + game.score / 3)