def __init__(self, ai_settings, screen):
        """Initialize the ship, and set its starting position."""
        super(Ship, self).__init__()
        self.screen = screen
        self.ai_settings = ai_settings

        # Load the ship image, and get its rect.
        self.ss = spritesheet('images/SpriteSheet.png')
        self.image = self.ss.image_at((0,160,32,32))
        self.rect = self.image.get_rect()
        self.screen_rect = screen.get_rect()

        # Start each new ship at the bottom center of the screen.
        self.rect.centerx = self.screen_rect.centerx
        self.rect.bottom = self.screen_rect.bottom

        # Store a decimal value for the ship's center.
        self.center = float(self.rect.centerx)

        # Movement flags.
        self.moving_right = False
        self.moving_left = False

        # Sound
        self.death_sound = pygame.mixer.Sound('sound/explosion.wav')
        self.fire_sound = pygame.mixer.Sound('sound/shoot.wav')
        self.channel = mixer.Channel(1)
        self.channel.set_volume(0.01)

        # Death tag
        self.dead = False

        # Death index
        self.dIndex = 0
Exemplo n.º 2
0
    def __init__(self, screen, type):
        super(Portal, self).__init__()
        self.screen = screen
        self.type = type
        ss = spritesheet('images/portals.png')

        self.orange_left = pygame.transform.scale(ss.image_at((0,0,35,96)), (15,41))
        self.blue_left = pygame.transform.scale(ss.image_at((0,96,35,96)), (15,41))

        self.orange_right = pygame.transform.rotate(self.orange_left, 180)
        self.orange_up = pygame.transform.rotate(self.orange_left, 270)
        self.orange_down = pygame.transform.rotate(self.orange_left, 90)

        self.blue_right = pygame.transform.rotate(self.blue_left, 180)
        self.blue_up = pygame.transform.rotate(self.blue_left, 270)
        self.blue_down = pygame.transform.rotate(self.blue_left, 90)

        if(type == 'orange'):
            img = self.orange_left
        elif(type == 'blue'):
            img = self.blue_left

        img = pygame.transform.scale(img, (15, 15))
        self.rect = img.get_rect()
        self.image = img

        # direction pacman would be coming out
        self.output = 'left'

        # to make sure portal is placed somewhere
        self.portal_placed = False
Exemplo n.º 3
0
 def __init__(self,
              filename,
              rect,
              count,
              colorkey=None,
              loop=False,
              frames=1):
     """construct a SpriteStripAnim
     
     filename, rect, count, and colorkey are the same arguments used
     by spritesheet.load_strip.
     
     loop is a boolean that, when True, causes the next() method to
     loop. If False, the terminal case raises StopIteration.
     
     frames is the number of ticks to return the same image before
     the iterator advances to the next image.
     """
     self.filename = filename
     ss = spritesheet(filename)
     self.images = ss.load_strip(rect, count, colorkey)
     self.i = 0
     self.loop = loop
     self.frames = frames
     self.f = frames
    def __init__(self, ai_settings, screen, type=0):
        """Initialize the alien, and set its starting position."""
        super(Alien, self).__init__()
        self.screen = screen
        self.ai_settings = ai_settings

        #Alien classification
        self.type = type

        #Alien's death status
        self.dead = False

        # Load the alien image, and set its rect attribute.
        self.ss = spritesheet('images/SpriteSheet.png')
        if (type == 0):
            self.imageT1 = self.ss.image_at((96, 160, 32, 32))
            self.imageT2 = self.ss.image_at((96, 192, 32, 32))
            self.image = self.imageT1
        elif (type == 1):
            self.imageT1 = self.ss.image_at((0, 96, 32, 32))
            self.imageT2 = self.ss.image_at((0, 128, 32, 32))
            self.image = self.imageT1
        elif (type == 2):
            self.imageT1 = self.ss.image_at((64, 96, 32, 32))
            self.imageT2 = self.ss.image_at((64, 128, 32, 32))
            self.image = self.imageT1
        self.rect = self.image.get_rect()

        # Start each new alien near the top left of the screen.
        self.rect.x = self.rect.width
        self.rect.y = self.rect.height

        # Store the alien's exact position.
        self.x = float(self.rect.x)

        #Animation variable detector
        self.index = 0

        # Death frames Storage
        self.dFrames = [
            self.ss.image_at((0, 0, 32, 32)),
            self.ss.image_at((32, 0, 32, 32)),
            self.ss.image_at((0, 32, 32, 32)),
            self.ss.image_at((32, 32, 32, 32)),
            self.ss.image_at((0, 64, 32, 32)),
            self.ss.image_at((32, 64, 32, 32))
        ]
        # Death Frames Location
        self.dIndex = 0

        # Sound
        self.death_sound = pygame.mixer.Sound('sound/invaderkilled.wav')
        self.fire_sound = pygame.mixer.Sound('sound/shoot.wav')
        self.channel = mixer.Channel(2)
        self.channel.set_volume(0.01)
Exemplo n.º 5
0
 def __init__(self, filename, rect, count, colorkey=None, loop=False, frames=1):
     """construct a SpriteStripAnim
     
     filename, rect, count, and colorkey are the same arguments used
     by spritesheet.load_strip.
     
     loop is a boolean that, when True, causes the next() method to
     loop. If False, the terminal case raises StopIteration.
     
     frames is the number of ticks to return the same image before
     the iterator advances to the next image.
     """
     self.filename = filename
     ss = spritesheet(filename)
     self.images = ss.load_strip(rect, count, colorkey)
     self.i = 0
     self.loop = loop
     self.frames = frames
     self.f = frames
Exemplo n.º 6
0
    def __init__(self, ai_settings, screen, sound=True):
        super().__init__()
        # screen, settings, score values
        self.screen = screen
        self.ai_settings = ai_settings
        self.possible_scores = ai_settings.ufo_point_values
        self.score = None

        # images, score text
        self.ss = spritesheet('images/SpriteSheet.png')
        self.image = self.ss.image_at((64, 0, 32, 32))
        self.rect = self.image.get_rect()
        self.score_image = None
        self.font = SysFont(None, 32, italic=True)
        self.prep_score()

        # death frames
        self.death_frames = []
        self.death_index = None
        self.death_frames.append(self.ss.image_at((32, 64, 32, 32)))
        self.death_frames.append(self.score_image)
        self.last_frame = None
        self.wait_interval = 500

        # sound
        self.entrance_sound = pygame.mixer.Sound('sound/ufo_highpitch.wav')
        self.death_sound = pygame.mixer.Sound('sound/invaderkilled.wav')
        self.channel = pygame.mixer.Channel(3)
        self.channel.set_volume(0.01)

        # initial position, speed/direction
        self.speed = (choice([-1, 1]))
        self.rect.x = 0 if self.speed > 0 else ai_settings.screen_width
        self.rect.y = ai_settings.screen_height * 0.1

        # death flag
        self.dead = False

        if sound:
            self.channel.play(self.entrance_sound, loops=-1)
Exemplo n.º 7
0
def update_screen(ai_settings, screen, stats, sb, ship, aliens, bunkers, beams,
                  bullets, play_button, hs_button, ufo_group):
    """Update images on the screen, and flip to the new screen."""
    # Draw the main menu if the game is inactive.
    if not stats.game_active:
        screen.fill((0, 0, 0))  # Clear the screen
        # High Scores menu
        if (stats.hs_menu):
            pygame.font.init()
            myfont = pygame.font.SysFont(None, 40)
            score1 = myfont.render('1. ' + str(stats.high_scores_all[0]),
                                   False, (255, 255, 255))
            score2 = myfont.render('2. ' + str(stats.high_scores_all[1]),
                                   False, (255, 255, 255))
            score3 = myfont.render('3. ' + str(stats.high_scores_all[2]),
                                   False, (255, 255, 255))
            notify = myfont.render('Click anywhere on the screen to go back',
                                   False, (255, 255, 255))
            screen.blit(score1, (150, 58))
            screen.blit(score2, (150, 188))
            screen.blit(score3, (150, 318))
            screen.blit(notify, (50, 400))
        else:
            play_button.draw_button()
            hs_button.draw_button()

            ss = spritesheet('images/SpriteSheet.png')
            alienType0 = ss.image_at((96, 160, 32, 32))
            alienType1 = ss.image_at((0, 96, 32, 32))
            alienType2 = ss.image_at((64, 96, 32, 32))
            alienType3 = ss.image_at((64, 0, 32, 32))
            screen.blit(alienType0, (250, 150))
            screen.blit(alienType1, (250, 180))
            screen.blit(alienType2, (250, 210))
            screen.blit(alienType3, (250, 240))

            pygame.font.init()
            myfont = pygame.font.SysFont(None, 20)
            title = pygame.font.SysFont(None,
                                        80).render('Space Invaders', False,
                                                   (255, 255, 255))
            textsurface0 = myfont.render('= 10 points', False, (255, 255, 255))
            textsurface1 = myfont.render('= 20 points', False, (255, 255, 255))
            textsurface2 = myfont.render('= 40 points', False, (255, 255, 255))
            textsurface3 = myfont.render('= ??? points', False,
                                         (255, 255, 255))
            screen.blit(title, (120, 70))
            screen.blit(textsurface0, (300, 158))
            screen.blit(textsurface1, (300, 188))
            screen.blit(textsurface2, (300, 218))
            screen.blit(textsurface3, (300, 248))

    # Going in-game
    else:
        ufo_event_check(ai_settings, screen, ufo_group)
        # Redraw the screen, each pass through the loop.
        screen.fill(ai_settings.bg_color)

        # Redraw all bullets, behind ship and aliens.
        for bullet in bullets.sprites():
            bullet.draw_bullet()
        # Redraw all beams
        for beam in beams.sprites():
            beam.blitme()
        if ufo_group:
            ufo_group.update()
            for ufo in ufo_group.sprites():
                ufo.blitme()
        ship.blitme()
        aliens.draw(screen)
        check_bunker_collisions(beams, bullets, bunkers)
        # Draw the score information.
        sb.show_score()

        bunkers.update()

        #Playing the music
        ai_settings.play_music()
    # Make the most recently drawn screen visible.
    pygame.display.flip()