Example #1
0
 def wsgi(self):
     output = super(GunicornFlaskApplication, self).wsgi()
     with self.application.app_context():
         self.run_migration()
         from src.background import Background
         Background.run()
     return output
Example #2
0
    def __init__(self):
        # Debug.
        self.debug = False

        # Colors.
        self.white = 255, 255, 255
        self.green = 100, 175, 130
        self.red = 245, 35, 20
        self.blue = 0, 0, 255

        #images
        self.image_night_sky = pygame.image.load('ressources/images/background/night_sky.png')
        self.image_obstacle = pygame.image.load('ressources/images/obstacles/spike_a.png')

        # Init screen.
        self.size = self.width, self.height = 640, 480
        self.screen = pygame.display.set_mode(self.size)

        self.test_surface = pygame.Surface((320, 240))

        # Define the bg class
        self.bg = Background()

        # Define the obstacle manager class
        self.ground = Background.build_ground(self.bg, self.height, self.width)
        self.om = ObstacleManager(self.ground.height, self.width)

        # Define the interface class
        self.interface = Interface(self.screen, self.height, self.width)
Example #3
0
    def wsgi(self):
        output = super(GunicornFlaskApplication, self).wsgi()
        with self.application.app_context():
            from src.background import Background
            Background.run()
            self.clear_download_state()

        return output
Example #4
0
 def __init__(self):
     self.win = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
     self.bird = Bird(200, 200)
     self.background = Background(WIN_WIDTH, WIN_HEIGHT)
     self.base = Base(WIN_HEIGHT - 75)
     self.pipes = [Pipe(600), Pipe(1000)]
     self.run = True
     self.pause = False
     self.loose = False
     self.clock = pygame.time.Clock()
     self.score = 0
Example #5
0
def run_game():
    pygame.init()
    screen_settings = Settings()
    stats = GameStats(screen_settings)

    screen = pygame.display.set_mode(
        (screen_settings.screen_width, screen_settings.screen_height))
    ship = Ship(screen)
    pygame.display.set_caption('Alien Inv')
    screen_y = 0
    background = Background(screen_settings.bg_image, [0, 0])
    play_button = Button(screen_settings, screen, "Play")
    bullets = Group()
    enemies = Group()

    while True:  # Game Loop
        gf.check_events(
            screen_settings,
            screen,
            ship,
            bullets,
            stats,
            play_button,
            enemies,
        )
        if stats.game_active:
            ship.update()
            gf.update_bullets(bullets, enemies)
            gf.update_enemies(enemies, ship, screen_settings, stats, screen,
                              bullets)
        gf.update_screen(screen_settings, screen, ship, screen_y, background,
                         bullets, enemies, play_button, stats)
        screen_y += screen_settings.bg_speed
Example #6
0
    def __init__(self,
                 screen: pygame.Surface,
                 username: str = None,
                 solo: bool = True,
                 is_left: bool = True):
        """
        Initialize the player's game.
        :param screen: surface to display game
        :param username: player's username
        :param solo: True if single player False if multi players
        :param is_left: True if solo and if multi player and player 1
        """
        # game settings
        self.end_game = False
        self.username = username
        self.screen = screen
        self.is_left = is_left
        self.solo = solo

        # display settings
        self.screenWidth, self.screenHeight = pygame.display.get_surface(
        ).get_size()
        self.game_window_width = self.screenWidth if self.solo else self.screenWidth / 2
        self.game_window_height = self.screenHeight
        self.game_window = pygame.Surface(
            (self.game_window_width, self.game_window_height))
        self.rect = self.screen.get_rect(
            left=0 if is_left else self.screenWidth / 2)

        # creating game objects
        self.ship = Ship(self.game_window, solo=solo, is_left=is_left)
        self.pipes = []
        self.pipes_number = 2 if solo else 1
        for i in range(0, self.pipes_number):
            self.pipes.append(
                Pipes(self.game_window,
                      first=True if (i == 0) else False,
                      single_player=solo,
                      is_left=is_left))
        self.bg = Background(self.game_window, is_left=is_left)
        self.hourglass = Hourglass(self.game_window)

        # control settings
        if is_left:
            self.jump_key = pygame.K_SPACE
        else:
            self.jump_key = pygame.K_RETURN
Example #7
0
 def __get_background_for_game(self):
     """
     Set proper background for Game for specific game level
     :return:
     """
     if self.running_level == 1:
         return Background(
             background_picture='pictures/game_backgrounds/bckg_level_1.png',
             last_background_image=
             'pictures/final_screens/final_1_static.png')
     elif self.running_level == 2:
         return Background(background_picture=
                           'pictures/game_backgrounds/bckg_level_2d.png',
                           last_background_image=
                           'pictures/final_screens/final_2_static.png')
     else:
         return Background(
             background_picture='pictures/game_backgrounds/bckg_level_1.png',
             last_background_image=
             'pictures/final_screens/final_1_static.png')
Example #8
0
def eval_genome(genomes, config):
    global GEN, SCREEN
    GEN += 1
    nets = []
    ge = []
    ships = []

    for _, g in genomes:
        net = neat.nn.FeedForwardNetwork.create(g, config)
        nets.append(net)
        ships.append(Ship(SCREEN))
        g.fitness = 0
        ge.append(g)

    bg = Background(SCREEN, is_left=True)
    pipes = [PipesIA(SCREEN)]

    score = 0
    clock = pygame.time.Clock()

    play = True
    while play:
        clock.tick(60)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                play = False
                pygame.quit()
                quit()

        pipe_ind = 0
        if len(ships) > 0:
            if len(pipes) > 1 and ships[0].x_pos > pipes[0].x_pos + pipes[
                    0].width:  # if we passed the pipe
                pipe_ind = 1  # we will look at the second pipe
        else:
            play = False
            break

        for x, ship in enumerate(ships):
            ship.move()
            ge[x].fitness += 0.1

            output = nets[x].activate(
                (ship.y_pos,
                 abs(ship.y_pos - pipes[pipe_ind].y_pos_up +
                     pipes[pipe_ind].height),
                 abs(ship.y_pos - pipes[pipe_ind].y_pos_down)))

            if output[0] > 0.5:
                ship.jump()

        remove = []
        add_pipe = False
        for pipe in pipes:

            for x, ship in enumerate(ships):
                if pipe.collide_with_ship(ship):
                    ge[x].fitness -= 1
                    ships.pop(x)
                    nets.pop(x)
                    ge.pop(x)

                if not pipe.passed and pipe.x_pos < ship.x_pos:
                    pipe.passed = True
                    add_pipe = True

            if pipe.x_pos + pipe.width < 0:
                remove.append(pipe)

            pipe.move()

        if add_pipe:
            score += 1
            for g in ge:
                g.fitness += 5
            pipes.append(PipesIA(SCREEN))

        for r in remove:
            pipes.remove(r)

        for x, ship in enumerate(ships):
            if ship.y_pos + ship.height >= SCREEN_HEIGHT or ship.y_pos < 0:
                ships.pop(x)
                nets.pop(x)
                ge.pop(x)

        bg.move()
        draw_window(SCREEN, ships, pipes, bg, score, GEN)
Example #9
0
class Core:
    def __init__(self):
        self.win = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
        self.bird = Bird(200, 200)
        self.background = Background(WIN_WIDTH, WIN_HEIGHT)
        self.base = Base(WIN_HEIGHT - 75)
        self.pipes = [Pipe(600), Pipe(1000)]
        self.run = True
        self.pause = False
        self.loose = False
        self.clock = pygame.time.Clock()
        self.score = 0

    def start(self):
        while self.run:
            self.clock.tick(30)
            self.event()
            if not self.pause and not self.loose:
                self.update()
            self.display()
        pygame.quit()
        quit()

    def event(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.run = False
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                    self.bird.jump()
                if event.key == pygame.K_ESCAPE:
                    self.run = False
                if event.key == pygame.K_p:
                    self.pause = not self.pause

    def update(self):
        if self.bird.y >= WIN_HEIGHT - 100:
            self.loose = True
        rm = []
        for pipe in self.pipes:
            if pipe.x < -100:
                rm.append(pipe)
            if pipe.collide(self.bird):
                self.loose = True
            if not pipe.passed and pipe.x < self.bird.x:
                pipe.passed = True
                self.score += 1
                self.pipes.append(Pipe(1000))
        for r in rm:
            self.pipes.remove(r)

        self.background.move()
        self.bird.move()
        for pipe in self.pipes:
            pipe.move()
        self.base.move()

    def display(self):
        self.background.draw(self.win)
        self.bird.draw(self.win)
        for pipe in self.pipes:
            pipe.draw(self.win)
        self.base.draw(self.win)

        text = font.render("Score: " + str(self.score), 1, (255, 255, 255))
        self.win.blit(text, (WIN_WIDTH - 10 - text.get_width(), 10))
        pygame.display.update()
Example #10
0
import pygame

from src.background import Background
from src.config import clock, FPS, bg_pos_y_1, bg_speed, music_1, bg_space
from src.update import Update
from src.player import Player
from src.ship import Ship
from src.util import display_ui

pygame.display.set_caption("Space Shooter")

player = Player("Marcelo", Ship())
background = Background(pos_y_1=bg_pos_y_1,
                        speed=bg_speed,
                        pos_x=0,
                        surface=bg_space)
game_exit = False
update = Update()

music = music_1
pygame.mixer.music.set_volume(0.4)
pygame.mixer.music.play(-1)

while not game_exit:
    background.display()

    for event in pygame.event.get():
        if (event.type == pygame.KEYDOWN
                and event.key == pygame.K_ESCAPE) or event.type == pygame.QUIT:
            game_exit = True
Example #11
0
class PlayGame:
    """
    PlayGame class used for solo and duo mode.
    One user plays in one PlayGame object
    """

    username = None

    def __init__(self,
                 screen: pygame.Surface,
                 username: str = None,
                 solo: bool = True,
                 is_left: bool = True):
        """
        Initialize the player's game.
        :param screen: surface to display game
        :param username: player's username
        :param solo: True if single player False if multi players
        :param is_left: True if solo and if multi player and player 1
        """
        # game settings
        self.end_game = False
        self.username = username
        self.screen = screen
        self.is_left = is_left
        self.solo = solo

        # display settings
        self.screenWidth, self.screenHeight = pygame.display.get_surface(
        ).get_size()
        self.game_window_width = self.screenWidth if self.solo else self.screenWidth / 2
        self.game_window_height = self.screenHeight
        self.game_window = pygame.Surface(
            (self.game_window_width, self.game_window_height))
        self.rect = self.screen.get_rect(
            left=0 if is_left else self.screenWidth / 2)

        # creating game objects
        self.ship = Ship(self.game_window, solo=solo, is_left=is_left)
        self.pipes = []
        self.pipes_number = 2 if solo else 1
        for i in range(0, self.pipes_number):
            self.pipes.append(
                Pipes(self.game_window,
                      first=True if (i == 0) else False,
                      single_player=solo,
                      is_left=is_left))
        self.bg = Background(self.game_window, is_left=is_left)
        self.hourglass = Hourglass(self.game_window)

        # control settings
        if is_left:
            self.jump_key = pygame.K_SPACE
        else:
            self.jump_key = pygame.K_RETURN

    def draw(self):
        """
        Draw all game objects on game window and display player's current score (to be called in main loop).
        """
        self.screen.blit(self.game_window, self.rect)

        self.bg.draw()
        self.ship.draw()
        for pipe in self.pipes:
            pipe.draw()
        self.hourglass.draw()

        text_font = pygame.font.Font(font["bradbunr"], 25)

        string1 = "Score {0} : {1}".format(self.username, self.ship.score)
        textSurface, textRect = createTextObj(string1, text_font)
        self.game_window.blit(textSurface, textRect)

        if not self.solo:
            x_rect_split = self.game_window_width if self.is_left else 0
            pygame.draw.rect(self.game_window, colors["black"],
                             (x_rect_split, 0, 3, self.game_window_height))

    def update(self):
        """
        Update position and state of all game objects (to be called in main loop).
        """
        if self.ship.y_pos + self.ship.height > self.game_window_height:  # Ship falls
            self.end_game = True
        self.ship.move()

        if not self.ship.goForward:
            self.bg.move()
            self.hourglass.move()
            for pipe in self.pipes:
                pipe.move()

        self.updateScore()

        # if self.ship.collision_pipes(self.pipes):
        #     self.end_game = True

        for pipe in self.pipes:
            if pipe.collide_with_ship(self.ship):
                self.end_game = True

        if self.ship.collision_hourglass(self.hourglass):
            sounds["slow"].play()
            self.hourglass.updateCoordinates()
            for pipe in self.pipes:
                pipe.velocity = pipe.origin_velocity
            self.bg.velocity = self.bg.origin_velocity
            self.hourglass.x_velocity = self.hourglass.origin_x_velocity

    def updateScore(self):
        """
        Checks if ship passed current pipe without colliding it and add 1 point if it's the case.
        It also increase objects velocity and reduce space between top and bottom pipes.
        """
        for pipe in self.pipes:
            if self.ship.x_pos > pipe.x_pos and not pipe.passed:
                self.ship.score += 1
                sounds["score"].play()
                if pipe.velocity < 13:
                    for pipe_2 in self.pipes:
                        pipe_2.velocity += 0.5
                    self.hourglass.x_velocity += 0.5

                if self.bg.velocity < 4:
                    self.bg.velocity += 0.2

                if pipe.space > 230 and self.ship.score % 2 != 0:
                    for pipe_2 in self.pipes:
                        pipe_2.space -= 5

                pipe.passed = True

                if self.ship.score % 4 == 0 and self.ship.score != 0:
                    self.hourglass.start()

    def reset(self):
        """
        Resets all game settings to start a new game quickly.
        """
        self.end_game = False
        self.ship.reset()
        self.bg.reset()
        self.hourglass.reset()
        for pipe in self.pipes:
            pipe.reset()