Beispiel #1
0
 def process(self, display):
     if not self.dead:
         self.draw_self(display)
         self.y -= self.SPEED
         if self.y < -100:
             self.dead = True
             ObjectCollector.remove_this("bullets", self)
Beispiel #2
0
 def bullet_process(self, bullet, game):
     if self.surface.get_rect().move(self.x, self.y).colliderect(
             bullet.surface.get_rect().move(bullet.x, bullet.y)):
         bullet.dead = True
         ObjectCollector.remove_this("bullets", bullet)
         self.dead = True
         ObjectCollector.remove_this("fish", self)
         game.change_score(1)
Beispiel #3
0
 def __init__(self, x, y, BULLET_SPEED):
     self.objtype = "bullet"
     ObjectCollector.__init__(self)
     pygame.sprite.Sprite.__init__(self)
     self.x = x
     self.y = y
     self.surface = pygame.image.load("assets/fish/fish2.png")
     self.surface = pygame.transform.scale(self.surface, (66, 84))
     self.surface = pygame.transform.rotate(self.surface, -90)
     self.SPEED = BULLET_SPEED
     self.dead = False
Beispiel #4
0
 def __init__(self, COOLDOWN, BULLET_SPEED):
     self.objtype = "blaster"
     ObjectCollector.__init__(self)
     self.surface = pygame.image.load("assets/fish/fish1.png")
     self.surface = pygame.transform.rotate(self.surface, -90)
     self.x = 390
     self.y = 422
     self.cooldown = COOLDOWN
     self.active_cooldown = 0
     self.BULLET_SPEED = BULLET_SPEED
     self.pressed = False
     self.font = pygame.font.Font("assets/fonts/PressStart2P.ttf", 25)
Beispiel #5
0
 def __init__(self, DIMENSIONS, SPEED, STILLFRAMES_MAX):
     self.objtype = "fish"
     ObjectCollector.__init__(self)
     pygame.sprite.Sprite.__init__(self)
     self.type = choice(["fish1", "fish2"])
     self.fishname = "assets/fish/" + self.type + ".png"
     self.surface = pygame.Surface((109, 71), flags=SRCALPHA)
     self.surface.fill((255, 255, 255, 0))
     self.surface.blit(pygame.image.load(self.fishname), (0, 0))
     self.surface = pygame.transform.rotate(self.surface, 90)
     self.x = randint(10, DIMENSIONS[0] - 50)
     self.y = 20
     self.SPEED = SPEED
     self.WINDOWHEIGHT = DIMENSIONS[1]
     self.dead = False
     self.moving = False
     self.stillframes = randint(30, STILLFRAMES_MAX)
Beispiel #6
0
 def process(self, display, game, debug=False):
     """Processes fish movement down the screen"""
     if not self.dead:
         if self.moving:
             self.y += self.SPEED
         self.draw_self(display)
         if self.y > self.WINDOWHEIGHT + 100:
             self.dead = True
             ObjectCollector.remove_this("fish", self)
             game.change_score(-1)
             if debug:
                 print("fish type: " + self.type + ", x: " + str(self.x) +
                       " deleted")
         else:
             if self.stillframes == 0:
                 self.moving = True
             else:
                 self.stillframes -= 1
Beispiel #7
0
def run():
    ## GAME SIZE CONSTANTS
    DIMENSIONS = (870, 520)
    MENUCOORDS = (20, 20)
    ACTUALMENUCOORDS = (40, 40)
    MENUDIMENSIONS = (830, 480)
    ACTUALMENUDIMENSIONS = (790, 440)

    ## GENERAL CONSTANTS
    BLACK = (0,0,0)
    RED = (255,0,0)
    GREEN = (0,255,0)
    BLUE = (0,0,255)
    WHITE = (255,255,255)
    TRANSPARENT_GREEN = (0,255,0)

    ## CUSTOMISATION
    FPS = 30
    SPAWNING_SPEED = 0.25 # spawns per second
    SPEED = 6 # pixels per frame
    BULLET_COOLDOWN = 30 # frames
    BULLET_SPEED = 4
    MAX_STILLFRAMES = 180

    ## TRANSPARENCIES (ALPHA)
    text_transparency = 255
    button_transparency = 255
    innerm_transparency = 255
    outerm_transparency = 128
    menu_transparencies = [text_transparency, button_transparency, innerm_transparency, outerm_transparency]

    ## GAME FLOW
    running = True
    game = Game(DIMENSIONS, FPS)
    clock = pygame.time.Clock()

    ## MENU CONTROL
    menu = "menu"
    menustatus = False
    infade = False
    outfade = False

    ## INITIALISATION
    pygame.init()
    screen = pygame.display.set_mode(DIMENSIONS)
    pygame.display.set_caption("Sea Invaders!")
    icon = pygame.image.load("assets/icon/icon.jpg")
    pygame.display.set_icon(icon)

    ## BACKGROUND
    sea = pygame.image.load("assets/background/sea.jpg").convert()

    ## CALLBACKS
    def play_resume():
        """Updates and returns the new menustatus. Since this callback is only called to resume the game, sets menustatus to false."""
        menustatus = False
        return menustatus

    ## BUTTONS
    startbtn = Button((300, 48), (285,240), play_resume)

    ## MENU
    menufont = pygame.font.Font("assets/fonts/PressStart2P.ttf", 40)
    menutext = "Sea Invaders!"
    btnfont = pygame.font.Font("assets/fonts/PressStart2P.ttf", 35)
    btntext = "Start!"
    menuobj_alpha = pygame.Surface(MENUDIMENSIONS, flags=SRCALPHA)
    menuobj = pygame.Surface(ACTUALMENUDIMENSIONS, flags=SRCALPHA)
    score = pygame.font.Font("assets/fonts/PressStart2P.ttf", 25)

    ## FISH SPAWNER INITIALISATION
    spawner = FishController(SPAWNING_SPEED, SPEED, MAX_STILLFRAMES)
    blaster = Blaster(BULLET_COOLDOWN, BULLET_SPEED)


    ## MAINLOOP
    while running:
        ## PER-FRAME LOGIC
        menu_transparencies = [text_transparency, button_transparency, innerm_transparency, outerm_transparency]
        prev_menustatus = menustatus

        ## RENDER BACKGROUND
        screen.blit(sea, (0,0))
        screen.blit(sea, (435,0))

        ## RENDER MENU (INCLUDING TRANSPARENCY)
        menuobj_alpha.fill((0,255,0,outerm_transparency))
        menuobj.fill((0,255,0,innerm_transparency))
        menuobj_alpha.blit(menuobj, (ACTUALMENUCOORDS[0] - MENUCOORDS[0], ACTUALMENUCOORDS[1] - MENUCOORDS[1]))
        menutextsurface = menufont.render(menutext, True, RED)
        menutext_surf = pygame.Surface((menutextsurface.get_rect().width, menutextsurface.get_rect().height))
        menutext_surf.blit(menutextsurface, pygame.Rect(0, 0, 10, 10), special_flags=pygame.BLEND_RGBA_MULT)
        menutext_surf.set_alpha(text_transparency)
        menuobj_alpha.blit(menutext_surf, ((MENUDIMENSIONS[0]/2)-(menutext_surf.get_rect().width/2), 60))
        if menustatus:
            screen.blit(menuobj_alpha, MENUCOORDS)
    
        ## RENDER BUTTONS
        #btntextsurface = menufont.render(menutext, True, GREEN)
        #btntext_surf = pygame.Surface((btntextsurface.get_rect().width, btntextsurface.get_rect().height))
        #btntext_surf.blit(btntextsurface, pygame.Rect(0, 0, 10, 10), special_flags=pygame.BLEND_RGBA_MULT)
        if menustatus:
            startbtn.draw_self(screen, (255,0,0,button_transparency))#, btntext_surf)

        ## PROCESS MENU
        menustatus = startbtn.process(menustatus)

        ## EVENT PROCESSING
        for event in pygame.event.get():
            if event.type == QUIT:
                sys.exit(0)
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    if not infade and not outfade:
                        if menustatus:
                            menustatus = False
                        else:
                            menustatus = True

        ## CUSTOM EVENT PROCESSING

        ## MENU LOGIC
        if game.score < 0:
            menustatus = True
            game.score = 0
            ObjectCollector.clear_all()


        if menustatus:
            pygame.mouse.set_visible(True)
        else:
            pygame.mouse.set_visible(False)

        if not menustatus:
            if prev_menustatus:
                outfade = True

        if menustatus:
            if not prev_menustatus:
                infade = True

        ## PROCESS OBJECTS
        if not prev_menustatus:
            if outfade == False:
                game, screen = ObjectCollector.process_all(game, screen)

        ## FADING LOGIC
        if outfade:
            for i in range(len(menu_transparencies)):
                menu_transparencies[i] -= 8
            text_transparency = menu_transparencies[0]
            button_transparency = menu_transparencies[1]
            innerm_transparency = menu_transparencies[2]
            outerm_transparency = menu_transparencies[3]
            if text_transparency < 0: 
                text_transparency = 0
            if outerm_transparency < 0: 
                outerm_transparency = 0
            if innerm_transparency < 0: 
                innerm_transparency = 0
            if button_transparency < 0: 
                button_transparency = 0
            if button_transparency == 0 and innerm_transparency == 0 and outerm_transparency == 0:
                outfade = False
                startbtn.visible = False

        if infade:
            for i in range(len(menu_transparencies)):
                menu_transparencies[i] += 8
            text_transparency = menu_transparencies[0]
            button_transparency = menu_transparencies[1]
            innerm_transparency = menu_transparencies[2]
            outerm_transparency = menu_transparencies[3]
            if outerm_transparency > 128: 
                outerm_transparency = 128
            if innerm_transparency > 255: 
                innerm_transparency = 255
            if button_transparency > 255: 
                button_transparency = 255
            if button_transparency == 255 and innerm_transparency == 255 and outerm_transparency == 128:
                infade = False
                startbtn.visible = True

        ## RENDER SCORE
        score_surf = score.render(str(game.score), True, WHITE)
        screen.blit(score_surf, (70, 70))

        ## UPDATE DISPLAY
        pygame.display.update(screen.get_rect())

        ## FPS TRACKING
        clock.tick(FPS)
        game.increment_frames()
Beispiel #8
0
 def __init__(self, SPAWNING_SPEED, SPEED, STILLFRAMES_MAX):
     self.objtype = "controller"
     ObjectCollector.__init__(self)
     self.spawning_speed = SPAWNING_SPEED
     self.SPEED = SPEED
     self.STILLFRAMES_MAX = STILLFRAMES_MAX