Exemplo n.º 1
0
    def __init__(self, screen, unit, preferences):
        self.screen = screen
        self.unit = unit
        self.preferences = preferences
        self.running = True

        self.sounds = SoundPlayer(
            (('fall', 'fall.wav'), ('ding', 'ding.wav'), ('eat', 'eat.wav'),
             ('move', 'move.wav'), ('splat', 'splat.wav')))
        self.music = MusicPlayer("game.mp3")

        self.clock = pygame.time.Clock()
        self.tick = Constants.FPS

        self.size = Constants.UNITS * self.unit

        self.surface = pygame.Surface((self.size, self.size))
        self.surface_rect = self.surface.get_rect()
        self.surface_rect.centerx = self.screen.get_rect().centerx
        self.surface_rect.centery = self.screen.get_rect().centery

        self.img_background = pygame.image.load(
            data.filepath("title", "background.jpg"))
        self.img_background = pygame.transform.smoothscale(
            self.img_background,
            util.scale(self.img_background,
                       width=self.screen.get_rect().width))
        self.img_background.set_alpha(30)
        self.img_background_rect = self.img_background.get_rect()
        self.img_background_rect.centerx = self.screen.get_rect().centerx
        self.img_background_rect.centery = self.screen.get_rect().centery
Exemplo n.º 2
0
    def __init__(self, screen, unit, preferences):
        self.screen = screen
        self.unit = unit
        self.preferences = preferences
        self.running = True

        self.sounds = SoundPlayer((('fall','fall.wav'),('ding','ding.wav'),('eat','eat.wav'),('move','move.wav'),('splat','splat.wav')))
        self.music = MusicPlayer("game.mp3")

        self.clock = pygame.time.Clock()
        self.tick = Constants.FPS

        self.size = Constants.UNITS*self.unit

        self.surface = pygame.Surface((self.size,self.size))
        self.surface_rect = self.surface.get_rect()
        self.surface_rect.centerx = self.screen.get_rect().centerx
        self.surface_rect.centery = self.screen.get_rect().centery

        self.img_background = pygame.image.load(data.filepath("title","background.jpg"))
        self.img_background = pygame.transform.smoothscale(self.img_background,util.scale(self.img_background,width=self.screen.get_rect().width))
        self.img_background.set_alpha(30)
        self.img_background_rect = self.img_background.get_rect()
        self.img_background_rect.centerx = self.screen.get_rect().centerx
        self.img_background_rect.centery = self.screen.get_rect().centery
Exemplo n.º 3
0
    def __init__(self, screen, unit, preferences):
        self.screen = screen
        self.unit = unit
        self.preferences = preferences
        self.running = True
        self.music_player = MusicPlayer("title.mp3")
        self.clock = pygame.time.Clock()
        self.choice = 0
        self.size = self.screen.get_size()

        self.img_title = pygame.image.load(data.filepath("title", "title.png"))
        self.img_credits = pygame.image.load(
            data.filepath("title", "credits.png"))
        self.img_background = pygame.image.load(
            data.filepath("title", "background.jpg"))
        self.img_background = pygame.transform.smoothscale(
            self.img_background,
            util.scale(self.img_background, width=self.size[0]))
        self.img_background.set_alpha(50)
        self.img_background_rect = self.img_background.get_rect()
        self.img_background_rect.centerx = self.screen.get_rect().centerx
        self.img_background_rect.centery = self.screen.get_rect().centery
Exemplo n.º 4
0
    def __init__(self, screen, unit, preferences):
        self.screen = screen
        self.unit = unit
        self.preferences = preferences
        self.running = True
        self.music_player = MusicPlayer(random.choice(listaMenu))
        self.clock = pygame.time.Clock()
        self.choice = 0
        self.size = self.screen.get_size()

        self.img_title = pygame.image.load(data.filepath("title","logo_unla.png"))
        self.img_credits = pygame.image.load(data.filepath("title","logo_unla.png"))
        self.img_background = pygame.image.load(data.filepath("title","background.jpg"))
        self.img_background = pygame.transform.smoothscale(self.img_background,util.scale(self.img_background,width=self.size[0]))
        self.img_background.set_alpha(50)
        self.img_background_rect = self.img_background.get_rect()
        self.img_background_rect.centerx = self.screen.get_rect().centerx
        self.img_background_rect.centery = self.screen.get_rect().centery
Exemplo n.º 5
0
class Game:
    def __init__(self, screen, unit, preferences):
        self.screen = screen
        self.unit = unit
        self.preferences = preferences
        self.running = True

        self.sounds = SoundPlayer((('fall','fall.wav'),('ding','ding.wav'),('eat','eat.wav'),('move','move.wav'),('splat','splat.wav')))
        self.music = MusicPlayer("game.mp3")

        self.clock = pygame.time.Clock()
        self.tick = Constants.FPS

        self.size = Constants.UNITS*self.unit

        self.surface = pygame.Surface((self.size,self.size))
        self.surface_rect = self.surface.get_rect()
        self.surface_rect.centerx = self.screen.get_rect().centerx
        self.surface_rect.centery = self.screen.get_rect().centery

        self.img_background = pygame.image.load(data.filepath("title","background.jpg"))
        self.img_background = pygame.transform.smoothscale(self.img_background,util.scale(self.img_background,width=self.screen.get_rect().width))
        self.img_background.set_alpha(30)
        self.img_background_rect = self.img_background.get_rect()
        self.img_background_rect.centerx = self.screen.get_rect().centerx
        self.img_background_rect.centery = self.screen.get_rect().centery

    def main(self):
        self.music.play()

        # Screen base
        self.screen.fill((0,0,0))
        self.screen.blit(self.img_background,self.img_background_rect)

        highscores = Highscores()

        # Main instances
        pavement = Pavement(self.unit)
        snake = Snake(self.unit)
        foods = Foods(self.unit)
        walls = Walls(self.unit)
        score = Score(self.unit,self.surface_rect)

        nextgold = random.randint(*Constants.TIMERANGE_GOLD) * Constants.FPS # first gold between 30 & 60 seconds
        makegold = False
        nextwall = random.randint(*Constants.TIMERANGE_WALL) * Constants.FPS # first gold between 30 & 60 seconds
        makewall = False

        updatestats = True

        counter = 0

        flag_music = True
        flag_pause = False
        #MAIN LOOP
        while self.running:
            time = pygame.time.get_ticks()

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    self.running = False
                elif event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        self.running = False

                    elif event.key == pygame.K_m:
                        flag_music = not flag_music
                        if flag_music:
                            self.music.play()
                        else:
                            self.music.stop()

                    elif event.key == pygame.K_1:
                        pygame.image.save(self.screen, "screenshot.jpg")

                    elif event.key == pygame.K_SPACE or event.key == pygame.K_p:
                        flag_pause = True
                        self.print_text("PAUSE")
                        while flag_pause:
                            for event in pygame.event.get():
                                if event.type==pygame.KEYDOWN:
                                    if event.key==pygame.K_p or event.key == pygame.K_SPACE:
                                        flag_pause = False

                    else:
                        # Time to change direction
                        action = 0
                        if event.key == pygame.K_UP or event.key == pygame.K_w: action = 1
                        elif event.key == pygame.K_DOWN or event.key == pygame.K_s: action = 2
                        elif event.key == pygame.K_LEFT or event.key == pygame.K_a: action = 3
                        elif event.key == pygame.K_RIGHT or event.key == pygame.K_d: action = 4
                        if action:
                            self.sounds.play("move")
                            snake.action(action)

            # Snake movements and pavement reactions
            snake.move()
            pavement.passage(snake.head)
            pavement.make_regrow(snake.tail)

            # Snake has eaten?
            if foods.check(snake.head):
                updatestats = True
                snake.grow(Constants.GROW)
                score.add_score(foods.score)
                self.sounds.play("eat")

            # Snake
            if walls.check(snake.head):
                snake.alive = False

            # Snake is dead?
            if not snake.alive:
                #blood splash (bin on head, little on body)
                pavement.bloodsplat(snake.head)
                [pavement.bloodsplat(x,1) for x in snake.body if random.randint(0,2)==0]
                #redraw all the snake
                snake.set_dirty(1)
                self.sounds.play("splat")
                self.running = False

            # Gold generator (After pseudo-random time a golden apple will appear)
            nextgold-=1
            if nextgold<0:
                makegold = True
                nextgold = random.randint(*Constants.TIMERANGE_GOLD)*Constants.FPS

            # Wall generator (After pseudo-random time a wall will appear)
            nextwall-=1
            if nextwall<0:
                makewall = True
                nextwall = random.randint(*Constants.TIMERANGE_WALL)*Constants.FPS

            # Foods request to create an apple
            # Game has to provide to Foods the list of forbidden blocks
            if foods.needapple or makegold or makewall:
                forbidden = [[-1,-1]]
                forbidden.extend(foods.get_forbidden())
                forbidden.extend(snake.get_forbidden())
                forbidden.extend(walls.get_forbidden())

                # Creates the apples and make the pavement Grass
                if foods.needapple:
                    newpos = foods.make_apple(forbidden)
                    pavement.make_grass(newpos)
                    forbidden.extend(newpos)
                if makegold:
                    self.sounds.play('ding')
                    newpos = foods.make_gold(forbidden)
                    pavement.make_grass(newpos)
                    forbidden.extend(newpos)
                    makegold = False
                if makewall:
                    self.sounds.play('fall')
                    newpos = walls.make_solid(forbidden)
                    pavement.make_none(newpos)
                    forbidden.extend(newpos)
                    makewall = False

            # Foods request pavement update
            for pos in foods.refresh:
                pavement.passage(pos)
                foods.refresh.remove(pos)
                del pos

            # Updates and draws
            pavement.update()
            pavement.draw(self.surface)
            walls.update()
            walls.draw(self.surface)
            snake.update()
            snake.draw(self.surface)
            foods.update()
            foods.draw(self.surface)

            if updatestats or not counter % Constants.FPS:
                score.set_length(snake.length)
                score.update()
                score.draw(self.screen)
                updatestats = False

            if not counter % Constants.FPS:
                score.add_second()
                counter = 0

            # Surface on surface... weeee!
            self.screen.blit(self.surface,self.surface_rect)

            pygame.display.update()
            self.clock.tick(self.tick)
            counter+=1
            #END OF MAIN LOOP

        if not snake.alive:

            self.print_text("GAME OVER")

            if highscores.check(score.score,score.elapse):
                current_string = ''
                complete = False

                inputbox = InputBox(self.unit)
                inputbox.rect.centerx = self.screen.get_rect().centerx
                inputbox.rect.centery = self.screen.get_rect().centery
                inputbox.draw(self.screen)

                pygame.display.update()
                pygame.event.clear()
                redraw = False
                while not complete:
                    event = pygame.event.wait()
                    if event.type == pygame.QUIT:
                        return
                    if event.type != pygame.KEYDOWN:
                        continue
                    if event.key == pygame.K_BACKSPACE:
                        current_string = current_string[:-1]
                        redraw = True
                    elif event.key == pygame.K_RETURN:
                        if len(current_string) == 0:
                            current_string = 'Mysterious player'
                        complete = True
                    elif event.unicode:
                        if len(current_string) <= 15:
                            c = ord(event.unicode)
                            if c >= 32 and c <= 126 or c==8:
                                current_string += event.unicode
                                redraw = True
                    if redraw:
                        redraw = False
                        inputbox.set_text(current_string)
                        inputbox.update()
                        inputbox.draw(self.screen)
                        pygame.display.update()
                position = highscores.insert(current_string,score.score,score.elapse)
                highscores.save()
                scored = {'index':position,'scored':True}
            else:
                counter = Constants.FPS*3 # 3 seconds
                pygame.display.update()
                while counter > 0:
                    for event in pygame.event.get():
                        if event.type == pygame.QUIT or event.type == pygame.KEYDOWN:
                            counter = 0
                    self.clock.tick(self.tick)
                    pygame.display.update()
                    counter-=1

                scored = {'elapse':score.elapse,'score':score.score,'scored':False}

            ts = TitleScreen(self.screen,self.unit,self.preferences)
            ts.highscores(scored)
            del ts

        self.music.stop()
        return

    def print_text(self,text):
        label = Label(self.unit,text,int(self.unit*10),(255,255,255))
        label.rect.centerx = self.screen.get_rect().centerx
        label.rect.y = self.unit*5
        label.draw(self.screen)
        label.set_fontsize(self.unit*11)
        label.set_fontcolor((140,20,10))
        label.rect.centerx = self.screen.get_rect().centerx
        label.update()
        label.draw(self.screen)
        del label
        pygame.display.update()
Exemplo n.º 6
0
 def __init__(self, screen, unit):
     self.screen = screen
     self.unit = unit
     self.running = True
     self.music_player = MusicPlayer("boot.mp3")
     self.clock = pygame.time.Clock()
Exemplo n.º 7
0
class BootScreen:
    """  The boot screen! """
    def __init__(self, screen, unit):
        self.screen = screen
        self.unit = unit
        self.running = True
        self.music_player = MusicPlayer("boot.mp3")
        self.clock = pygame.time.Clock()

    def main(self):
        """ Display the screen and a little bit of text at the bottom
            of the screen. """
        self.music_player.once()

        img_logo = pygame.image.load(data.filepath("title", "imente.png"))
        img_logo = pygame.transform.smoothscale(
            img_logo, util.scale(img_logo, width=self.unit * 15))
        img_logo_rect = img_logo.get_rect()
        img_logo_rect.centerx = self.screen.get_rect().centerx
        img_logo_rect.centery = self.screen.get_rect().centery

        #settings
        seconds_in = 2
        seconds_still = 1
        seconds_out = 2

        fps = 20
        logo_alpha = 0
        logo_alpha_add = int(255.0 / float(fps * seconds_in))
        logo_alpha_sub = -int(255.0 / float(fps * seconds_out))
        logo_alpha_sum = logo_alpha_add
        stop_counter = fps * seconds_still

        skip = False

        # LOGO INTRO
        while not skip:
            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    skip = True
                elif event.type == pygame.QUIT:
                    self.running = False
                    return

            self.screen.fill((0, 0, 0))
            img_logo.set_alpha(logo_alpha)
            self.screen.blit(img_logo, img_logo_rect)

            if logo_alpha == 255 and stop_counter > 0:
                stop_counter -= 1
            else:
                logo_alpha += logo_alpha_sum
                if logo_alpha > 255:
                    logo_alpha = 255
                    logo_alpha_sum = logo_alpha_sub
                elif logo_alpha < 0:
                    break

            pygame.display.update()
            self.clock.tick(fps)

        self.music_player.stop()
Exemplo n.º 8
0
class Game:
    def __init__(self, screen, unit, preferences):
        self.screen = screen
        self.unit = unit
        self.preferences = preferences
        self.running = True

        self.sounds = SoundPlayer(
            (('fall', 'fall.wav'), ('ding', 'ding.wav'), ('eat', 'eat.wav'),
             ('move', 'move.wav'), ('splat', 'splat.wav')))
        self.music = MusicPlayer("game.mp3")

        self.clock = pygame.time.Clock()
        self.tick = Constants.FPS

        self.size = Constants.UNITS * self.unit

        self.surface = pygame.Surface((self.size, self.size))
        self.surface_rect = self.surface.get_rect()
        self.surface_rect.centerx = self.screen.get_rect().centerx
        self.surface_rect.centery = self.screen.get_rect().centery

        self.img_background = pygame.image.load(
            data.filepath("title", "background.jpg"))
        self.img_background = pygame.transform.smoothscale(
            self.img_background,
            util.scale(self.img_background,
                       width=self.screen.get_rect().width))
        self.img_background.set_alpha(30)
        self.img_background_rect = self.img_background.get_rect()
        self.img_background_rect.centerx = self.screen.get_rect().centerx
        self.img_background_rect.centery = self.screen.get_rect().centery

    def main(self):
        self.music.play()

        # Screen base
        self.screen.fill((0, 0, 0))
        self.screen.blit(self.img_background, self.img_background_rect)

        highscores = Highscores()

        # Main instances
        pavement = Pavement(self.unit)
        snake = Snake(self.unit)
        foods = Foods(self.unit)
        walls = Walls(self.unit)
        score = Score(self.unit, self.surface_rect)

        nextgold = random.randint(
            *Constants.TIMERANGE_GOLD
        ) * Constants.FPS  # first gold between 30 & 60 seconds
        makegold = False
        nextwall = random.randint(
            *Constants.TIMERANGE_WALL
        ) * Constants.FPS  # first gold between 30 & 60 seconds
        makewall = False

        updatestats = True

        counter = 0

        flag_music = True
        flag_pause = False
        #MAIN LOOP
        while self.running:
            time = pygame.time.get_ticks()

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    self.running = False
                elif event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        self.running = False

                    elif event.key == pygame.K_m:
                        flag_music = not flag_music
                        if flag_music:
                            self.music.play()
                        else:
                            self.music.stop()

                    elif event.key == pygame.K_1:
                        pygame.image.save(self.screen, "screenshot.jpg")

                    elif event.key == pygame.K_SPACE or event.key == pygame.K_p:
                        flag_pause = True
                        self.print_text("PAUSE")
                        while flag_pause:
                            for event in pygame.event.get():
                                if event.type == pygame.KEYDOWN:
                                    if event.key == pygame.K_p or event.key == pygame.K_SPACE:
                                        flag_pause = False

                    else:
                        # Time to change direction
                        action = 0
                        if event.key == pygame.K_UP or event.key == pygame.K_w:
                            action = 1
                        elif event.key == pygame.K_DOWN or event.key == pygame.K_s:
                            action = 2
                        elif event.key == pygame.K_LEFT or event.key == pygame.K_a:
                            action = 3
                        elif event.key == pygame.K_RIGHT or event.key == pygame.K_d:
                            action = 4
                        if action:
                            self.sounds.play("move")
                            snake.action(action)

            # Snake movements and pavement reactions
            snake.move()
            pavement.passage(snake.head)
            pavement.make_regrow(snake.tail)

            # Snake has eaten?
            if foods.check(snake.head):
                updatestats = True
                snake.grow(Constants.GROW)
                score.add_score(foods.score)
                self.sounds.play("eat")

            # Snake
            if walls.check(snake.head):
                snake.alive = False

            # Snake is dead?
            if not snake.alive:
                #blood splash (bin on head, little on body)
                pavement.bloodsplat(snake.head)
                [
                    pavement.bloodsplat(x, 1) for x in snake.body
                    if random.randint(0, 2) == 0
                ]
                #redraw all the snake
                snake.set_dirty(1)
                self.sounds.play("splat")
                self.running = False

            # Gold generator (After pseudo-random time a golden apple will appear)
            nextgold -= 1
            if nextgold < 0:
                makegold = True
                nextgold = random.randint(
                    *Constants.TIMERANGE_GOLD) * Constants.FPS

            # Wall generator (After pseudo-random time a wall will appear)
            nextwall -= 1
            if nextwall < 0:
                makewall = True
                nextwall = random.randint(
                    *Constants.TIMERANGE_WALL) * Constants.FPS

            # Foods request to create an apple
            # Game has to provide to Foods the list of forbidden blocks
            if foods.needapple or makegold or makewall:
                forbidden = [[-1, -1]]
                forbidden.extend(foods.get_forbidden())
                forbidden.extend(snake.get_forbidden())
                forbidden.extend(walls.get_forbidden())

                # Creates the apples and make the pavement Grass
                if foods.needapple:
                    newpos = foods.make_apple(forbidden)
                    pavement.make_grass(newpos)
                    forbidden.extend(newpos)
                if makegold:
                    self.sounds.play('ding')
                    newpos = foods.make_gold(forbidden)
                    pavement.make_grass(newpos)
                    forbidden.extend(newpos)
                    makegold = False
                if makewall:
                    self.sounds.play('fall')
                    newpos = walls.make_solid(forbidden)
                    pavement.make_none(newpos)
                    forbidden.extend(newpos)
                    makewall = False

            # Foods request pavement update
            for pos in foods.refresh:
                pavement.passage(pos)
                foods.refresh.remove(pos)
                del pos

            # Updates and draws
            pavement.update()
            pavement.draw(self.surface)
            walls.update()
            walls.draw(self.surface)
            snake.update()
            snake.draw(self.surface)
            foods.update()
            foods.draw(self.surface)

            if updatestats or not counter % Constants.FPS:
                score.set_length(snake.length)
                score.update()
                score.draw(self.screen)
                updatestats = False

            if not counter % Constants.FPS:
                score.add_second()
                counter = 0

            # Surface on surface... weeee!
            self.screen.blit(self.surface, self.surface_rect)

            pygame.display.update()
            self.clock.tick(self.tick)
            counter += 1
            #END OF MAIN LOOP

        if not snake.alive:

            self.print_text("GAME OVER")

            if highscores.check(score.score, score.elapse):
                current_string = ''
                complete = False

                inputbox = InputBox(self.unit)
                inputbox.rect.centerx = self.screen.get_rect().centerx
                inputbox.rect.centery = self.screen.get_rect().centery
                inputbox.draw(self.screen)

                pygame.display.update()
                pygame.event.clear()
                redraw = False
                while not complete:
                    event = pygame.event.wait()
                    if event.type == pygame.QUIT:
                        return
                    if event.type != pygame.KEYDOWN:
                        continue
                    if event.key == pygame.K_BACKSPACE:
                        current_string = current_string[:-1]
                        redraw = True
                    elif event.key == pygame.K_RETURN:
                        if len(current_string) == 0:
                            current_string = 'Mysterious player'
                        complete = True
                    elif event.unicode:
                        if len(current_string) <= 15:
                            c = ord(event.unicode)
                            if c >= 32 and c <= 126 or c == 8:
                                current_string += event.unicode
                                redraw = True
                    if redraw:
                        redraw = False
                        inputbox.set_text(current_string)
                        inputbox.update()
                        inputbox.draw(self.screen)
                        pygame.display.update()
                position = highscores.insert(current_string, score.score,
                                             score.elapse)
                highscores.save()
                scored = {'index': position, 'scored': True}
            else:
                counter = Constants.FPS * 3  # 3 seconds
                pygame.display.update()
                while counter > 0:
                    for event in pygame.event.get():
                        if event.type == pygame.QUIT or event.type == pygame.KEYDOWN:
                            counter = 0
                    self.clock.tick(self.tick)
                    pygame.display.update()
                    counter -= 1

                scored = {
                    'elapse': score.elapse,
                    'score': score.score,
                    'scored': False
                }

            ts = TitleScreen(self.screen, self.unit, self.preferences)
            ts.highscores(scored)
            del ts

        self.music.stop()
        return

    def print_text(self, text):
        label = Label(self.unit, text, int(self.unit * 10), (255, 255, 255))
        label.rect.centerx = self.screen.get_rect().centerx
        label.rect.y = self.unit * 5
        label.draw(self.screen)
        label.set_fontsize(self.unit * 11)
        label.set_fontcolor((140, 20, 10))
        label.rect.centerx = self.screen.get_rect().centerx
        label.update()
        label.draw(self.screen)
        del label
        pygame.display.update()
Exemplo n.º 9
0
class TitleScreen:
    def __init__(self, screen, unit, preferences):
        self.screen = screen
        self.unit = unit
        self.preferences = preferences
        self.running = True
        self.music_player = MusicPlayer("title.mp3")
        self.clock = pygame.time.Clock()
        self.choice = 0
        self.size = self.screen.get_size()

        self.img_title = pygame.image.load(data.filepath("title", "title.png"))
        self.img_credits = pygame.image.load(
            data.filepath("title", "credits.png"))
        self.img_background = pygame.image.load(
            data.filepath("title", "background.jpg"))
        self.img_background = pygame.transform.smoothscale(
            self.img_background,
            util.scale(self.img_background, width=self.size[0]))
        self.img_background.set_alpha(50)
        self.img_background_rect = self.img_background.get_rect()
        self.img_background_rect.centerx = self.screen.get_rect().centerx
        self.img_background_rect.centery = self.screen.get_rect().centery

    def draw_background(self):
        self.screen.fill((0, 0, 0))
        self.screen.blit(self.img_background, self.img_background_rect)

    def main(self):
        self.music_player.play()

        img_title = pygame.transform.smoothscale(
            self.img_title, util.scale(self.img_title, width=self.unit * 35))
        img_title_rect = img_title.get_rect()
        img_title_rect.centerx = self.screen.get_rect().centerx
        img_title_rect.y = self.unit * 3

        self.choice = 0
        start_game = False

        btn_play = MenuButton("PLAY", self.unit,
                              (self.unit * 17, self.unit * 3))
        btn_play.rect.centerx = self.screen.get_rect().centerx
        btn_play.rect.y = self.unit * 29
        btn_play.set_status(1)

        btn_highscores = MenuButton("HIGH SCORES", self.unit,
                                    (self.unit * 17, self.unit * 3))
        btn_highscores.rect.centerx = self.screen.get_rect().centerx
        btn_highscores.rect.y = self.unit * 33

        btn_fullscreen = MenuButton(["FULLSCREEN", "WINDOWED"
                                     ][self.preferences.get('fullscreen')],
                                    self.unit, (self.unit * 17, self.unit * 3))
        btn_fullscreen.rect.centerx = self.screen.get_rect().centerx
        btn_fullscreen.rect.y = self.unit * 37

        btn_credits = MenuButton("CREDITS", self.unit,
                                 (self.unit * 17, self.unit * 3))
        btn_credits.rect.centerx = self.screen.get_rect().centerx
        btn_credits.rect.y = self.unit * 41

        btn_exit = MenuButton("QUIT", self.unit,
                              (self.unit * 17, self.unit * 3))
        btn_exit.rect.centerx = self.screen.get_rect().centerx
        btn_exit.rect.y = self.unit * 45
        buttons = pygame.sprite.Group()
        buttons.add(
            [btn_play, btn_fullscreen, btn_highscores, btn_credits, btn_exit])

        dbg = util.debugBackground(self.size, self.unit)
        self.draw_background()

        last_choice = -1

        while not start_game:
            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_UP or event.key == pygame.K_w:
                        self.choice -= 1
                    elif event.key == pygame.K_DOWN or event.key == pygame.K_s:
                        self.choice += 1
                    elif event.key == pygame.K_RETURN or event.key == pygame.K_SPACE:
                        if self.choice == 0:
                            start_game = True
                        elif self.choice == 1:
                            self.highscores()
                            self.draw_background()
                        elif self.choice == 2:
                            # return to MAIN.PY to save prefs and apply fullscreen mode
                            self.preferences.set(
                                'fullscreen',
                                not self.preferences.get('fullscreen'))
                            return
                        elif self.choice == 3:
                            self.credits()
                            self.draw_background()
                        else:
                            self.running = False
                            return
                    elif event.key == pygame.K_ESCAPE:
                        self.running = False
                        return
                    elif event.key == pygame.K_1:
                        pygame.image.save(self.screen, "screenshot.jpg")
                elif event.type == pygame.QUIT:
                    self.running = False
                    return
            if self.choice != last_choice:
                if self.choice < 0: self.choice = 4
                elif self.choice > 4: self.choice = 0
                btn_play.set_status(0)
                btn_highscores.set_status(0)
                btn_fullscreen.set_status(0)
                btn_credits.set_status(0)
                btn_exit.set_status(0)
                if self.choice == 0: btn_play.set_status(1)
                elif self.choice == 1: btn_highscores.set_status(1)
                elif self.choice == 2: btn_fullscreen.set_status(1)
                elif self.choice == 3: btn_credits.set_status(1)
                elif self.choice == 4: btn_exit.set_status(1)
                last_choice = self.choice

            buttons.update()
            self.screen.blit(img_title, img_title_rect)
            buttons.draw(self.screen)
            pygame.display.flip()
            self.clock.tick(30)
        self.music_player.stop()

    def highscores(self, scored=False):
        #scored = {'elapse':str,'score':int,'index':int,'scored':bool}

        back = False

        btn_back = MenuButton("BACK", self.unit,
                              (self.unit * 17, self.unit * 3))
        btn_back.rect.centerx = self.screen.get_rect().centerx
        btn_back.rect.y = self.unit * 45
        btn_back.set_status(1)

        self.draw_background()

        highscores = Highscores()
        if scored and not scored['scored']:
            highscores.insert("Your name", str(scored["score"]),
                              scored["elapse"])

        box = pygame.Surface((self.size[1], self.size[1]))
        box.fill((255, 255, 255))
        box.set_alpha(150)
        box_rect = box.get_rect()
        box_rect.centerx = self.screen.get_rect().centerx
        box_rect.y = 0
        self.screen.blit(box, box_rect)

        font_text = pygame.font.Font(data.filepath("font", "abel.ttf"),
                                     int(self.unit * 1.6))
        font_title = pygame.font.Font(data.filepath("font", "abel.ttf"),
                                      int(self.unit * 5))
        color_text = (30, 30, 30)
        color_title = (122, 30, 30)

        posy = 8 * self.unit
        advance = int(2.2 * self.unit)

        btn_back.update()
        self.screen.blit(btn_back.image, btn_back.rect)

        title = font_title.render("HIGH SCORES", True, color_title)
        rect = title.get_rect()
        rect.centerx = self.screen.get_rect().centerx
        rect.y = 1 * self.unit

        self.screen.blit(title, rect)
        line = pygame.Surface((self.size[1], int(self.unit * 2.2)))
        line.fill((255, 255, 255))
        line.set_alpha(50)
        line_rect = line.get_rect()
        line_rect.centerx = self.screen.get_rect().centerx
        line_me = pygame.Surface((self.size[1], int(self.unit * 2.2)))
        line_me.fill((30, 200, 30))
        line_me.set_alpha(50)
        ii = 0
        for el in highscores.scores:
            index = font_text.render(str(ii + 1), True, color_title)
            name = font_text.render(el["name"], True, color_text)
            elapse = font_text.render(el["elapse"], True, color_text)
            score = font_text.render(el["score"], True, color_text)
            line_rect.y = posy
            if not ii % 2:
                self.screen.blit(line, line_rect)
            if scored and scored["scored"] and ii == scored[
                    "index"] or ii == Constants.MAXSCORE:
                self.screen.blit(line_me, line_rect)
            rect = index.get_rect()
            rect.y = posy

            rect.x = self.screen.get_rect().centerx - self.unit * 13
            if ii < Constants.MAXSCORE: self.screen.blit(index, rect)
            rect.x += self.unit * 3
            self.screen.blit(name, rect)
            rect.x += self.unit * 16
            self.screen.blit(elapse, rect)
            rect.x += self.unit * 5
            self.screen.blit(score, rect)
            posy += advance
            ii += 1

        while not back:
            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE or event.key == pygame.K_RETURN or event.key == pygame.K_SPACE:
                        return
                    elif event.key == pygame.K_1:
                        pygame.image.save(self.screen, "screenshot.jpg")
                elif event.type == pygame.QUIT:
                    self.running = False
                    return

            btn_back.update()
            btn_back.draw(self.screen)
            pygame.display.flip()
            self.clock.tick(30)

    def credits(self):
        img_credits = pygame.transform.smoothscale(
            self.img_credits,
            util.scale(self.img_credits, height=self.unit * 13))
        img_credits_rect = img_credits.get_rect()
        img_credits_rect.centerx = self.screen.get_rect().centerx
        img_credits_rect.y = self.unit * 2

        back = False

        btn_back = MenuButton("BACK", self.unit,
                              (self.unit * 17, self.unit * 3))
        btn_back.rect.centerx = self.screen.get_rect().centerx
        btn_back.rect.y = self.unit * 45
        btn_back.set_status(1)

        self.draw_background()

        box = pygame.Surface((self.size[1], self.size[1]))
        box.fill((255, 255, 255))
        box.set_alpha(150)
        box_rect = box.get_rect()
        box_rect.centerx = self.screen.get_rect().centerx
        box_rect.y = 0
        self.screen.blit(box, box_rect)

        font = pygame.font.Font(data.filepath("font", "abel.ttf"),
                                int(self.unit * 1.5))
        color_text = (30, 30, 30)
        color_title = (122, 30, 30)

        credits = Constants.CREDITS.split("\n")
        spooler = []
        for line in credits:
            color = color_text
            if line and line[0] == "*":
                line = line[1:]
                color = color_title
            spooler.append(font.render(line, True, color))

        posy = 16 * self.unit
        advance = int(1.6 * self.unit)

        self.screen.blit(img_credits, img_credits_rect)
        btn_back.update()
        btn_back.draw(self.screen)

        for el in spooler:
            rect = el.get_rect()
            rect.centerx = self.screen.get_rect().centerx
            rect.y = posy
            self.screen.blit(el, rect)
            posy += advance

        while not back:
            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE or event.key == pygame.K_RETURN or event.key == pygame.K_SPACE:
                        return
                    elif event.key == pygame.K_1:
                        pygame.image.save(self.screen, "screenshot.jpg")
                elif event.type == pygame.QUIT:
                    self.running = False
                    return

            pygame.display.flip()
            self.clock.tick(30)
Exemplo n.º 10
0
 def __init__(self, screen, unit):
     self.screen = screen
     self.unit = unit
     self.running = True
     self.music_player = MusicPlayer("start.mp3")
     self.clock = pygame.time.Clock()
Exemplo n.º 11
0
class BootScreen:
    """  The boot screen! """
    def __init__(self, screen, unit):
        self.screen = screen
        self.unit = unit
        self.running = True
        self.music_player = MusicPlayer("start.mp3")
        self.clock = pygame.time.Clock()

    def main(self):
        """ Display the screen and a little bit of text at the bottom
            of the screen. """
        self.music_player.once()

        img_logo = pygame.image.load(data.filepath("title","logo_unla.png"))
        img_logo = pygame.transform.smoothscale(img_logo,util.scale(img_logo,width=self.unit*25))
        img_logo_rect = img_logo.get_rect()
        img_logo_rect.centerx = self.screen.get_rect().centerx
        img_logo_rect.centery = self.screen.get_rect().centery

        #settings
        seconds_in = 2
        seconds_still = 1
        seconds_out = 2

        fps = 20
        logo_alpha = 0
        logo_alpha_add = int(255.0/float(fps*seconds_in))
        logo_alpha_sub = -int(255.0/float(fps*seconds_out))
        logo_alpha_sum = logo_alpha_add
        stop_counter = fps*seconds_still

        skip = False

        # LOGO INTRO
        while not skip:
            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    skip = True
                elif event.type == pygame.QUIT:
                    self.running = False
                    return

            self.screen.fill((0, 0, 0))
            img_logo.set_alpha(logo_alpha)
            self.screen.blit(img_logo, img_logo_rect)

            if logo_alpha == 255 and stop_counter > 0:
                stop_counter-=1
            else:
                logo_alpha += logo_alpha_sum
                if logo_alpha > 255:
                    logo_alpha = 255
                    logo_alpha_sum = logo_alpha_sub
                elif logo_alpha < 0:
                    break

            pygame.display.update()
            self.clock.tick(fps)

        self.music_player.stop()
Exemplo n.º 12
0
class TitleScreen:
    def __init__(self, screen, unit, preferences):
        self.screen = screen
        self.unit = unit
        self.preferences = preferences
        self.running = True
        self.music_player = MusicPlayer(random.choice(listaMenu))
        self.clock = pygame.time.Clock()
        self.choice = 0
        self.size = self.screen.get_size()

        self.img_title = pygame.image.load(data.filepath("title","logo_unla.png"))
        self.img_credits = pygame.image.load(data.filepath("title","logo_unla.png"))
        self.img_background = pygame.image.load(data.filepath("title","background.jpg"))
        self.img_background = pygame.transform.smoothscale(self.img_background,util.scale(self.img_background,width=self.size[0]))
        self.img_background.set_alpha(50)
        self.img_background_rect = self.img_background.get_rect()
        self.img_background_rect.centerx = self.screen.get_rect().centerx
        self.img_background_rect.centery = self.screen.get_rect().centery

    def draw_background(self):
        self.screen.fill((0, 0, 0))
        self.screen.blit(self.img_background,self.img_background_rect)

    def main(self):
        self.music_player.play()

        img_title = pygame.transform.smoothscale(self.img_title,util.scale(self.img_title,width=self.unit*10))
        img_title_rect = img_title.get_rect()
        img_title_rect.centerx = self.screen.get_rect().centerx
        img_title_rect.y = self.unit*10

        self.choice = 0
        start_game = False

        btn_play = MenuButton("PLAY",self.unit,(self.unit*17, self.unit*3))
        btn_play.rect.centerx = self.screen.get_rect().centerx
        btn_play.rect.y = self.unit*24
        btn_play.set_status(1)

        btn_highscores = MenuButton("HIGH SCORES",self.unit,(self.unit*17, self.unit*3))
        btn_highscores.rect.centerx = self.screen.get_rect().centerx
        btn_highscores.rect.y = self.unit*28

        btn_fullscreen = MenuButton(["FULLSCREEN","WINDOWED"][self.preferences.get('fullscreen')],self.unit,(self.unit*17, self.unit*3))
        btn_fullscreen.rect.centerx = self.screen.get_rect().centerx
        btn_fullscreen.rect.y = self.unit*32

        btn_credits = MenuButton("CREDITS",self.unit,(self.unit*17, self.unit*3))
        btn_credits.rect.centerx = self.screen.get_rect().centerx
        btn_credits.rect.y = self.unit*36

        btn_exit = MenuButton("QUIT",self.unit,(self.unit*17, self.unit*3))
        btn_exit.rect.centerx = self.screen.get_rect().centerx
        btn_exit.rect.y = self.unit*40
        buttons = pygame.sprite.Group()
        buttons.add([btn_play,btn_fullscreen,btn_highscores,btn_credits,btn_exit])

        dbg = util.debugBackground(self.size,self.unit)
        self.draw_background()

        last_choice = -1

        while not start_game:
            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_UP or event.key == pygame.K_w:
                        self.choice -= 1
                    elif event.key == pygame.K_DOWN or event.key == pygame.K_s:
                        self.choice += 1
                    elif event.key == pygame.K_RETURN or event.key == pygame.K_SPACE:
                        if self.choice == 0:
                            start_game = True
                        elif self.choice == 1:
                            self.highscores()
                            self.draw_background()
                        elif self.choice == 2:
                            # return to MAIN.PY to save prefs and apply fullscreen mode
                            self.preferences.set('fullscreen',not self.preferences.get('fullscreen'))
                            return
                        elif self.choice == 3:
                            self.credits()
                            self.draw_background()
                        else:
                            self.running = False
                            return
                    elif event.key == pygame.K_ESCAPE:
                        self.running = False
                        return
                    elif event.key == pygame.K_1:
                        pygame.image.save(self.screen, "screenshot.jpg")
                elif event.type == pygame.QUIT:
                    self.running = False
                    return
            if self.choice != last_choice:
                if self.choice < 0: self.choice = 4
                elif self.choice > 4: self.choice = 0
                btn_play.set_status(0)
                btn_highscores.set_status(0)
                btn_fullscreen.set_status(0)
                btn_credits.set_status(0)
                btn_exit.set_status(0)
                if self.choice == 0: btn_play.set_status(1)
                elif self.choice == 1: btn_highscores.set_status(1)
                elif self.choice == 2: btn_fullscreen.set_status(1)
                elif self.choice == 3: btn_credits.set_status(1)
                elif self.choice == 4: btn_exit.set_status(1)
                last_choice = self.choice

            buttons.update()
            self.screen.blit(img_title, img_title_rect)
            buttons.draw(self.screen)
            pygame.display.flip()
            self.clock.tick(30)
        self.music_player.stop()

    def highscores(self,scored=False):
        #scored = {'elapse':str,'score':int,'index':int,'scored':bool}

        back = False

        btn_back = MenuButton("BACK",self.unit,(self.unit*17, self.unit*3))
        btn_back.rect.centerx = self.screen.get_rect().centerx
        btn_back.rect.y = self.unit*45
        btn_back.set_status(1)

        self.draw_background()

        highscores = Highscores()
        if scored and not scored['scored']:
            highscores.insert("Your name",str(scored["score"]),scored["elapse"])

        box = pygame.Surface((self.size[1],self.size[1]))
        box.fill((255,255,255))
        box.set_alpha(150)
        box_rect = box.get_rect()
        box_rect.centerx = self.screen.get_rect().centerx
        box_rect.y = 0
        self.screen.blit(box,box_rect)

        font_text = pygame.font.Font(data.filepath("font", "abel.ttf"), int(self.unit * 1.6))
        font_title = pygame.font.Font(data.filepath("font", "abel.ttf"), int(self.unit * 5))
        color_text = (30,30,30)
        color_title = (122,30,30)

        posy = 8*self.unit
        advance = int(2.2*self.unit)

        btn_back.update()
        self.screen.blit(btn_back.image, btn_back.rect)

        title = font_title.render("HIGH SCORES",True,color_title)
        rect = title.get_rect()
        rect.centerx = self.screen.get_rect().centerx
        rect.y = 1*self.unit

        self.screen.blit(title,rect)
        line = pygame.Surface((self.size[1],int(self.unit*2.2)))
        line.fill((255,255,255))
        line.set_alpha(50)
        line_rect = line.get_rect()
        line_rect.centerx = self.screen.get_rect().centerx
        line_me = pygame.Surface((self.size[1],int(self.unit*2.2)))
        line_me.fill((30,200,30))
        line_me.set_alpha(50)
        ii=0
        for el in highscores.scores:
            index = font_text.render(str(ii+1),True,color_title)
            name = font_text.render(el["name"],True,color_text)
            elapse = font_text.render(el["elapse"],True,color_text)
            score = font_text.render(el["score"],True,color_text)
            line_rect.y = posy
            if not ii % 2:
                self.screen.blit(line,line_rect)
            if scored and scored["scored"] and ii==scored["index"] or ii==Constants.MAXSCORE:
                self.screen.blit(line_me,line_rect)
            rect = index.get_rect()
            rect.y = posy

            rect.x = self.screen.get_rect().centerx-self.unit*13
            if ii < Constants.MAXSCORE: self.screen.blit(index, rect)
            rect.x+= self.unit*3
            self.screen.blit(name, rect)
            rect.x+= self.unit*16
            self.screen.blit(elapse, rect)
            rect.x+= self.unit*5
            self.screen.blit(score, rect)
            posy += advance
            ii+=1

        while not back:
            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE or event.key == pygame.K_RETURN or event.key == pygame.K_SPACE:
                        return
                    elif event.key == pygame.K_1:
                        pygame.image.save(self.screen, "screenshot.jpg")
                elif event.type == pygame.QUIT:
                    self.running = False
                    return

            btn_back.update()
            btn_back.draw(self.screen)
            pygame.display.flip()
            self.clock.tick(30)

    def credits(self):
        img_credits = pygame.transform.smoothscale(self.img_credits,util.scale(self.img_credits,height=self.unit*13))
        img_credits_rect = img_credits.get_rect()
        img_credits_rect.centerx = self.screen.get_rect().centerx
        img_credits_rect.y = self.unit*2

        back = False

        btn_back = MenuButton("BACK",self.unit,(self.unit*17, self.unit*3))
        btn_back.rect.centerx = self.screen.get_rect().centerx
        btn_back.rect.y = self.unit*45
        btn_back.set_status(1)

        self.draw_background()

        box = pygame.Surface((self.size[1],self.size[1]))
        box.fill((255,255,255))
        box.set_alpha(150)
        box_rect = box.get_rect()
        box_rect.centerx = self.screen.get_rect().centerx
        box_rect.y = 0
        self.screen.blit(box,box_rect)

        font = pygame.font.Font(data.filepath("font", "abel.ttf"), int(self.unit * 1.5))
        color_text = (30,30,30)
        color_title = (122,30,30)

        credits = Constants.CREDITS.split("\n")
        spooler = []
        for line in credits:
            color = color_text
            if line and line[0] == "*":
                line = line[1:]
                color = color_title
            spooler.append(font.render(line, True, color))

        posy = 16*self.unit
        advance = int(1.6*self.unit)

        self.screen.blit(img_credits, img_credits_rect)
        btn_back.update()
        btn_back.draw(self.screen)

        for el in spooler:
            rect = el.get_rect()
            rect.centerx = self.screen.get_rect().centerx
            rect.y = posy
            self.screen.blit(el, rect)
            posy += advance

        while not back:
            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE or event.key == pygame.K_RETURN or event.key == pygame.K_SPACE:
                        return
                    elif event.key == pygame.K_1:
                        pygame.image.save(self.screen, "screenshot.jpg")
                elif event.type == pygame.QUIT:
                    self.running = False
                    return


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