Exemplo n.º 1
0
    def __init__(self, screen_height, screen_width):
        super().__init__(screen_height, screen_width)
        self.background = pygame.image.load(
            'Images/bg.jpg')  # where to get the background from
        self.platforms.empty()
        self.enemys.empty()
        self.dino_gun = False  # should he have a gun
        self.level_limit = -700  #changes per level
        level_platforms = [
            [
                pygame.Rect(-5, 0, 5, screen_height),
                (0, 0, 0),
                0,
                -0.12  # left safety wall
            ],
            [
                pygame.Rect(0, screen_height - 35,
                            screen_width - self.level_limit, 35),
                (0, 0, 0),
                0,
                -0.12  #ground
            ]
        ]  #populate with platforms

        for platform in level_platforms:
            self.platforms.add(Platform.from_list(platform))

        self.enemys.add(
            slime(700, 445, vec(3, 0),
                  pygame.Rect(600, 400, 200, 40)))  #customise and add more
Exemplo n.º 2
0
def new_game():
    global player
    global platforms
    global camera_top
    global score
    player = Player(*POS_INIT_PLAYER)
    platforms = [Platform(*POS_INIT_PLATFORM)]
    camera_top = 0
    score = 0
Exemplo n.º 3
0
def generate_platforms():
    current_hgt = platforms[-1].y

    while abs(current_hgt - camera_top) > DIST_BETWEEN_PLATFORMS:
        platforms.append(Platform(random.randint(0, WIDTH - Platform.width),
                                  current_hgt - DIST_BETWEEN_PLATFORMS))
        current_hgt -= DIST_BETWEEN_PLATFORMS

    while platforms[0].y > HEIGHT + camera_top:
        platforms.pop(0)
Exemplo n.º 4
0
def parse_levels():

    # Chaeck if the level file exists
    __level_file_name = 'levels.lvl'
    __level_folder = Path('levels')
    __level_path = Path('%s/%s' % (__level_folder.name, __level_file_name))
    __new_file = False
    __file_exists = __level_folder.exists() and __level_folder.is_dir(
    ) and __level_path.exists() and __level_path.is_file()
    if not __file_exists:
        if VERBOSE: print('Level file doesn\'t exist, making new')
        if not exists(__level_folder.name) and not isdir(__level_folder.name):
            os.mkdir(__level_folder.name)
        __level_path.touch()
        __new_file = True

    if not __new_file:
        if VERBOSE: print('Level file exists')
        with __level_path.open(mode='r') as f:
            file = str(f.read())
            levels = file.split('*LEVEL*\n')
            del levels[0]
            for l in levels:
                if VERBOSE: 'Parsing new level'
                lines = l.split('\n')
                del lines[len(lines) - 1]
                level = Level(screen, NOAUDIO)

                # Loop through all lines
                for line in lines:
                    if VERBOSE: print('Parsing line: %s') % line
                    item = line.split('$')
                    coords = json.loads('{"d":' + item[1] + '}')['d']
                    if item[0] == 'PLA':
                        level.add_platform(
                            Platform(coords, int(item[2]), int(item[3]),
                                     pygame.Color(item[4]), screen))
                    elif item[0] == 'PLS':
                        level.player_start = coords
                    elif item[0] == 'ENE':
                        level.enemies.add(Enemy(coords, screen))
                    elif item[0] == 'OBJ':
                        level.add_object(Movable_Object(coords))
                    elif item[0] == 'END':
                        level.set_ending(Ending(coords, screen))
                level.calc_max_values()
                __levels.append(level)
Exemplo n.º 5
0
    def __init__(self, screen_height, screen_width):
        super().__init__(screen_height, screen_width)
        self.background = pygame.image.load('Images/bg.jpg')
        self.platforms.empty()
        self.enemys.empty()
        self.level_limit = -700
        self.dino_gun = True
        half_screen_width = self.screen_width // 2
        level_platforms = [
            [
                pygame.Rect(0, self.screen_height - 40,
                            half_screen_width - 200, 40), (0, 0, 0), 0, -0.099
            ],  # 1st ground
            [
                pygame.Rect(0, self.screen_height - 300,
                            (self.screen_width // 2) - 200, 40), (0, 0, 0), 0,
                -0.099
            ],  # special area
            [
                pygame.Rect((self.screen_width // 2) + 200,
                            self.screen_height - 40, 1800, 40), (0, 0, 0), 0,
                -0.099
            ],  # ground 2
            [
                pygame.Rect(self.screen_width // 2 - 150,
                            self.screen_height // 2 + 150, 100, 30), (0, 0, 0),
                0, -0.12
            ],  # stair 1
            [
                pygame.Rect(self.screen_width // 2 - 60,
                            self.screen_height // 2 + 100, 100, 30), (0, 0, 0),
                0, -0.12
            ],  # stair 2
            [
                pygame.Rect(self.screen_width // 2 + 50,
                            self.screen_height // 2 + 50, 100, 30), (0, 0, 0),
                0, -0.12
            ]  # stair 3
        ]
        for platform in level_platforms:
            self.platforms.add(Platform.from_list(platform))

        self.enemys.add(
            Slime(700, 441, VEC(3, 0), pygame.Rect(600, 400, 200, 40)))
        self.enemys.add(
            Slime(20, 181, VEC(3, 0),
                  pygame.Rect(0, screen_height - 400, 200, 40)))
Exemplo n.º 6
0
    def __init__(self, screen_height, screen_width):
        super().__init__(screen_height, screen_width)
        self.background = pygame.image.load(
            'Images/bg.jpg')  # where to get the background from
        self.platforms.empty()
        self.enemys.empty()
        self.dino_gun = False  # it shouldn't have a gun
        self.level_limit = -3530  #changes per level
        level_platforms = [
            [
                pygame.Rect(-5, 0, 5, screen_height),
                (0, 0, 0),
                0,
                -0.12  # left safety wall
            ],
            [
                pygame.Rect(0, screen_height - 40, 1960, 40),
                (0, 0, 0),
                0,
                -0.12  # ground
            ],
            [
                pygame.Rect(300, screen_height - 75, 217, 50),
                (0, 0, 0),
                0,
                -0.12  #first block
            ],
            [
                pygame.Rect(650, screen_height - 75, 615, 50),
                (0, 0, 0),
                0,
                -0.12  #second block
            ],
            [
                pygame.Rect(935, screen_height - 180, 60, 20),
                (0, 0, 0),
                0,
                -0.1  #special place ?what to put?
            ],
            [
                pygame.Rect(1347, screen_height - 130, 60, 20),
                (0, 0, 0),
                0,
                -0.12  #first air block
            ],
            [
                pygame.Rect(1500, screen_height - 180, 60, 20),
                (0, 0, 0),
                0,
                -0.12  #second air block
            ],
            [
                pygame.Rect(1660, screen_height - 230, 100, 205),
                (0, 0, 0),
                0,
                -0.12  #high ground
            ],
            [
                pygame.Rect(2100, screen_height - 40, 400, 40),
                (0, 0, 0),
                0,
                -0.12  # ground
            ],
            [
                pygame.Rect(2640, screen_height - 70, 60, 30),
                (0, 0, 0),
                0,
                -0.12  # air platform
            ],
            [
                pygame.Rect(2880, screen_height - 40, 200, 40),
                (0, 0, 0),
                0,
                -0.12  # ground
            ],
            [
                pygame.Rect(3047, screen_height - 130, 60, 20),
                (0, 0, 0),
                0,
                -0.12  #first air block
            ],
            [
                pygame.Rect(3200, screen_height - 180, 60, 20),
                (0, 0, 0),
                0,
                -0.12  #second air block
            ],
            [
                pygame.Rect(3360, screen_height - 230, 100, 205),
                (0, 0, 0),
                0,
                -0.12  #high ground
            ],
            [
                pygame.Rect(3360, screen_height - 40,
                            screen_width - self.level_limit, 40),
                (0, 0, 0),
                0,
                -0.12  # ground
            ],
        ]  #populate with platforms

        for platform in level_platforms:
            self.platforms.add(Platform.from_list(platform))
Exemplo n.º 7
0
    def start(self, screen, edit_level, index):

        e_offset = 75 / 2

        clock = pygame.time.Clock()
        button_y = self.screen_y - 90
        BUTTON_SIZE = 90
        BUTTON_X_DELTA = 45
        button_x = 90

        self.set_player_start_button = Button([button_x, button_y], BUTTON_SIZE, BUTTON_SIZE, 'Player', 30, screen)
        button_x += BUTTON_X_DELTA + BUTTON_SIZE
        self.set_platform_button = Button([button_x, button_y], BUTTON_SIZE, BUTTON_SIZE, 'Platform', 20, screen)
        button_x += BUTTON_X_DELTA + BUTTON_SIZE
        self.set_enemy_button = Button([button_x, button_y], BUTTON_SIZE, BUTTON_SIZE, 'Enemy', 30, screen)
        button_x += BUTTON_X_DELTA + BUTTON_SIZE
        self.set_ending_button = Button([button_x, button_y], BUTTON_SIZE, BUTTON_SIZE, 'Ending', 30, screen)
        button_x += BUTTON_X_DELTA + BUTTON_SIZE
        self.set_object_button = Button([button_x, button_y], BUTTON_SIZE, BUTTON_SIZE, 'Object', 25, screen)

        self.delete_button = Button([self.screen_x - 90, self.screen_y - 135], BUTTON_SIZE, BUTTON_SIZE, 'Delete', 25, screen)
        self.save_button = Button([self.screen_x - 90, self.screen_y - 405], BUTTON_SIZE, BUTTON_SIZE, 'Save', 30, screen)
        self.save_button.disabled = True
        self.cancel_buttton = Button([self.screen_x - 90, self.screen_y - 270], BUTTON_SIZE, BUTTON_SIZE, 'Cancel', 27, screen)

        if edit_level is not None and index is not None:
            self.editing = True
            self.player_start = edit_level.player_start
            self.sprites = edit_level.level_group
            self.enemies = edit_level.enemies
            self.objects = edit_level.objects
            self.ending = [edit_level.ending.rect.x, edit_level.ending.rect.y]
        else: self.delete_button.disabled = True

        while self.level_creator_is_active:

            mouse_pos = pygame.mouse.get_pos()
            button_clicks = self.__calculate_button_clicks(mouse_pos)
            all_sprites = self.sprites.copy()
            all_sprites.add(self.enemies)
            all_sprites.add(self.objects)

            if self.save_button.disabled and self.player_start is not None and self.ending is not None and len(self.sprites) > 0: self.save_button.disabled = False

            # Handle events
            for event in pygame.event.get():

                # If user quits
                if event.type == pygame.QUIT or pygame.key.get_pressed()[pygame.K_ESCAPE]:
                    self.stop()

                # Mouse movements
                if event.type == pygame.MOUSEBUTTONDOWN and not self.is_checking_for_mouse_up_event:
                    mouse_pos = pygame.mouse.get_pos()
                    button_clicks = self.__calculate_button_clicks(mouse_pos)
                    if self.type == Creation_Type.PLATFORM:
                        self.is_checking_for_mouse_up_event = True
                        self.initial_mouse_pos = pygame.mouse.get_pos()
                    elif not self.prevent_dual_click and True not in button_clicks:
                        self.prevent_dual_click = True
                        if self.type == Creation_Type.PLAYER:
                            self.player_start = [mouse_pos[0] + self.offset[0] - 75, mouse_pos[1] + self.offset[1] - 75]
                        elif self.type == Creation_Type.ENEMY:
                            self.enemies.add(Enemy([mouse_pos[0] + self.offset[0] - e_offset, mouse_pos[1] + self.offset[1] - e_offset], screen))
                        elif self.type == Creation_Type.ENDING:
                            self.ending = [mouse_pos[0] + self.offset[0] - 100, mouse_pos[1] + self.offset[1] - 100]
                        elif self.type == Creation_Type.OBJECT:
                            self.objects.add(Movable_Object([mouse_pos[0] + self.offset[0] - 50, mouse_pos[1] + self.offset[1] - 50]))
                if event.type == pygame.MOUSEBUTTONUP and (self.is_checking_for_mouse_up_event or self.prevent_dual_click):
                    if self.prevent_dual_click:
                        self.prevent_dual_click = False
                    elif self.is_checking_for_mouse_up_event:
                        self.is_checking_for_mouse_up_event = False
                        self.final_mouse_pos = pygame.mouse.get_pos()
                        if self.final_mouse_pos != self.initial_mouse_pos and self.final_mouse_pos[0] != self.initial_mouse_pos[0] and self.final_mouse_pos[1] != self.initial_mouse_pos[1]:

                            top_left = self.__calculate_top_left(self.final_mouse_pos)

                            w = abs(self.final_mouse_pos[0] - self.initial_mouse_pos[0])
                            h = abs(self.final_mouse_pos[1] - self.initial_mouse_pos[1])

                            top_left[0] += self.offset[0]
                            top_left[1] += self.offset[1]

                            self.sprites.add(Platform(top_left, w, h, pygame.Color('black'), screen))

            # Screen movement
            keys = pygame.key.get_pressed()
            if keys[pygame.K_w]:
                self.offset[1] -= self.ON_KEY_PRESS_CHANGE_BY_VALUE
            if keys[pygame.K_a]:
                self.offset[0] -= self.ON_KEY_PRESS_CHANGE_BY_VALUE
            if keys[pygame.K_s]:
                self.offset[1] += self.ON_KEY_PRESS_CHANGE_BY_VALUE
            if keys[pygame.K_d]:
                self.offset[0] += self.ON_KEY_PRESS_CHANGE_BY_VALUE

            # Delete sprites
            if keys[pygame.K_DELETE] or keys[pygame.K_BACKSPACE]:
                for sprite in all_sprites:
                    if sprite.rect.collidepoint((mouse_pos[0] + self.offset[0], mouse_pos[1] + self.offset[1])):
                        sprite.kill()
                        break;

            # Reset camera
            if keys[pygame.K_SPACE]:
                self.offset = [75 + self.player_start[0] - self.screen_x / 2, 75 + self.player_start[1] - self.screen_y / 2] if self.player_start is not None else [0, 0]

            # Change placement types
            if button_clicks[0]:
                self.type = Creation_Type.PLAYER
            if button_clicks[1]:
                self.type = Creation_Type.PLATFORM
            if button_clicks[2]:
                self.type = Creation_Type.ENEMY
            if button_clicks[3]:
                self.type = Creation_Type.ENDING
            if button_clicks[4]:
                self.type = Creation_Type.OBJECT
            if button_clicks[7]:
                self.delete = True

            # Save the file
            if button_clicks[5] or self.delete:
                level_path = Path('levels/levels.lvl')
                if not self.editing:
                    with level_path.open(mode='a+') as file:
                        file.write(u'*LEVEL*\n')
                        file.write(u'PLS$[%d,%d]\n' % (self.player_start[0], self.player_start[1]))
                        for platform in self.sprites: file.write(u'PLA$[%d,%d]$%d$%d$#000000\n' % (platform.position[0], platform.position[1], platform.width, platform.height))
                        for enemy in self.enemies: file.write(u'ENE$[%d,%d]\n' % (enemy.pos[0], enemy.pos[1]))
                        for object in self.objects: file.write(u'OBJ$[%d,%d]\n' % (object.pos[0], object.pos[1]))
                        file.write(u'END$[%d,%d]\n' % (self.ending[0], self.ending[1]))
                else:
                    data = None
                    with level_path.open(mode='r+') as f: data = str(f.read()).split('*LEVEL*\n')
                    del data[0]
                    for d in range(len(data)): data[d] = '*LEVEL*\n' + data[d]
                    if not self.delete:
                        data[index] = '*LEVEL*\n'
                        data[index] += 'PLS$[%d,%d]\n' % (self.player_start[0], self.player_start[1])
                        for platform in self.sprites: data[index] += 'PLA$[%d,%d]$%d$%d$#000000\n' % (platform.position[0], platform.position[1], platform.width, platform.height)
                        for enemy in self.enemies: data[index] += 'ENE$[%d,%d]\n' % (enemy.pos[0], enemy.pos[1])
                        for object in self.objects: data[index] += 'OBJ$[%d,%d]\n' % (object.pos[0], object.pos[1])
                        data[index] += 'END$[%d,%d]\n' % (self.ending[0], self.ending[1])
                    else:
                        del data[index]
                    with level_path.open(mode='w+') as f: f.write(unicode(''.join(data)))
                self.level_creator_is_active = False
                return

            if button_clicks[6]:
                self.level_creator_is_active = False
                return

            # If the delete button is enabled
            if (keys[pygame.K_RSHIFT] | keys[pygame.K_LSHIFT]) and edit_level is not None and index is not None:
                self.delete_button.disabled = False
            else: self.delete_button.disabled = True

            # Draw sky
            screen.fill(pygame.Color('lightblue'))
            screen.blit(self.SUN, (50, 50))

            # Draw and offset platforms
            for sprite in all_sprites:
                sprite.rect.x -= self.offset[0]
                sprite.rect.y -= self.offset[1]

            all_sprites.draw(screen)

            if len(all_sprites) == 0: self.save_button.disabled = True

            for sprite in all_sprites:
                sprite.rect.x += self.offset[0]
                sprite.rect.y += self.offset[1]

            if self.player_start != None:
                s = pygame.Surface((150, 150))
                s.blit(self.PLAYER_IMAGE, s.get_rect())
                screen.blit(s, [self.player_start[0] - self.offset[0], self.player_start[1] - self.offset[1]])

            if self.ending != None:
                s = pygame.Surface((200, 200))
                s.fill(pygame.Color('green'))
                screen.blit(s, [self.ending[0] - self.offset[0], self.ending[1] - self.offset[1]])

            # Draw preview
            if self.type == Creation_Type.PLAYER:
                s = pygame.Surface((150, 150))
                s.set_alpha(128)
                s.blit(self.PLAYER_IMAGE, s.get_rect())
                screen.blit(s, (mouse_pos[0] - 75, mouse_pos[1] - 75))
            elif self.is_checking_for_mouse_up_event and self.type == Creation_Type.PLATFORM:

                top_left = self.__calculate_top_left(mouse_pos)

                w = abs(mouse_pos[0] - self.initial_mouse_pos[0])
                h = abs(mouse_pos[1] - self.initial_mouse_pos[1])

                s = pygame.Surface((w, h))
                s.set_alpha(128)
                s.fill(pygame.Color('black'))
                screen.blit(s, top_left)
            elif self.type == Creation_Type.ENEMY:
                s = pygame.Surface((75, 75), pygame.SRCALPHA)
                s.set_alpha(128)
                s.blit(self.ENEMY_IMAGE, s.get_rect())
                screen.blit(s, (mouse_pos[0] - e_offset, mouse_pos[1] - e_offset))
            elif self.type == Creation_Type.ENDING:
                s = pygame.Surface((200, 200))
                s.set_alpha(128)
                s.fill(pygame.Color('green'))
                screen.blit(s, (mouse_pos[0] - 100, mouse_pos[1] - 100))
            elif self.type == Creation_Type.OBJECT:
                s = pygame.Surface((100, 100))
                s.set_alpha(128)
                s.fill(pygame.Color('red'))
                screen.blit(s, (mouse_pos[0] - 50, mouse_pos[1] - 50))

            self.set_player_start_button.draw()
            self.set_platform_button.draw()
            self.set_enemy_button.draw()
            self.set_ending_button.draw()
            self.set_object_button.draw()
            self.delete_button.draw()
            self.save_button.draw()
            self.cancel_buttton.draw()

            if self.SHOW_FPS: screen.blit(self.fps_font.render(str(int(clock.get_fps())), True, pygame.Color('white'), pygame.Color('black')), (0, 0))

            pygame.display.flip()
            clock.tick(self.FPS)