Beispiel #1
0
def main():

    # For avoiding sound delay - thanks to BigglesW at StackOverflow
    pygame.mixer.pre_init(44100, -16, 1, 512)

    # Initialization ======================
    pygame.init()

    # frames per second setting
    FPS = 60
    fpsClock = pygame.time.Clock()

    # set up the window
    screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
    pygame.display.set_caption('CometZ')

    # Setting window icon
    icon = pygame.image.load("iconShip.png")
    pygame.display.set_icon(icon)

    # gameState
    # States available:
    # menu, pause, game, gameOver

    # Create game instance
    game = Game()

    # =====================================

    # Main game loop
    while True:

        # ===========================================================
        # EVENTS     ================================================
        # ===========================================================

        # check all events in event list
        for event in pygame.event.get():
            game.handle(event)

        # for continuously pressed keys
        keys = pygame.key.get_pressed()
        game.keys(keys)

        # ===========================================================
        # GAME LOGIC ================================================
        # ===========================================================

        # update game
        game.update()

        # ===========================================================
        # DRAWING    ================================================
        # ===========================================================

        # draw to the screen
        game.draw(screen)
        pygame.display.update()

        # ===========================================================
        # OTHER      ================================================
        # ===========================================================

        fpsClock.tick(FPS)
Beispiel #2
0
def main():

    # For avoiding sound delay - thanks to BigglesW at StackOverflow
    pygame.mixer.pre_init(44100, -16, 1, 512)
    
    # Initialization ======================
    pygame.init()

    # frames per second setting
    FPS = 60
    fpsClock = pygame.time.Clock()


    
    # set up the window
    screen  = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
    pygame.display.set_caption('CometZ')

    # Setting window icon
    icon = pygame.image.load("iconShip.png")
    pygame.display.set_icon(icon)
    
    # gameState
    # States available:
    # menu, pause, game, gameOver
    
    # Create game instance
    game = Game()
    
    # =====================================

    # Main game loop
    while True:

        # ===========================================================
        # EVENTS     ================================================
        # ===========================================================
        
        # check all events in event list
        for event in pygame.event.get():
            game.handle(event)
                    
        # for continuously pressed keys
        keys = pygame.key.get_pressed()
        game.keys(keys)
        
        
        # ===========================================================    
        # GAME LOGIC ================================================
        # ===========================================================
        
        # update game
        game.update()
        
        # ===========================================================    
        # DRAWING    ================================================
        # ===========================================================
        
        # draw to the screen
        game.draw(screen)
        pygame.display.update()
        
        # ===========================================================    
        # OTHER      ================================================
        # ===========================================================
        
        fpsClock.tick(FPS)       
Beispiel #3
0
class ServerGameWindow(UIElement):
    def __init__(self, rect: Rect, color: Optional[Color], sock,
                 connection_list, receive_list, parent_conn, child_conn,
                 send_process, nicks):
        mod_loader.import_mods()

        super().__init__(rect, color)

        fps_font = Font('assets/fonts/arial.ttf', 20)

        sub_elem = UIElement(Rect(50, 50, 50, 50), None)
        sub_elem.append_child(FPSCounter(Rect(50, 50, 0, 0), fps_font))
        self.append_child(sub_elem)

        self.sock = sock
        self.connection_list = connection_list
        self.receive_list = receive_list
        self.child_conn = child_conn
        self.parent_conn = parent_conn
        self.send_process = send_process

        self.game = Game(Game.Side.SERVER,
                         mod_loader,
                         self.parent_conn,
                         nicks,
                         -1,
                         connection_list=self.connection_list)

        self.minimap_elem = UIImage(
            Rect(0, config['screen']['size'][1] - 388, 0, 0),
            'assets/sprite/minimap.png')

        self.minimap = Minimap(self.game)
        self.minimap_elem.append_child(self.minimap)

        self.append_child(self.minimap_elem)
        self.game.create_unit('warrior', (0, 0))
        self.game.create_unit('fortress', (500, 0))
        self.game.create_unit('fortress', (-500, 0))
        self.game.create_unit('archer', (-25, -25))
        self.game.create_unit('archer', (25, -25))
        self.game.create_unit('archer', (-25, 25))
        self.game.create_unit('archer', (25, 25))

    def update(self, event):
        self.game.update(event)

        if event.type == EVENT_UPDATE:
            while self.receive_list:
                sender, command = self.receive_list.pop(0)
                self.game.handle_command(command[0],
                                         command[1:],
                                         sender=sender)

        return super().update(event)

    def draw(self, screen):
        super().draw(screen)
        self.game.draw(screen)

    def shutdown(self):
        print('Shutdown')
        self.send_process.terminate()
        self.parent_conn.close()
        self.sock.close()
Beispiel #4
0
def main():
    # initialize the pygame module
    pygame.init()
    pygame.display.set_caption("Learning car")

    # create a surface on screen that has the size of 720 x 480
    screen = pygame.display.set_mode((1000, 500))

    # initialize game
    game = Game()
    force = pygame.math.Vector2()

    # initialize rendering
    input_state = DRAWING
    checkpoint_start = None

    # variables
    # update_buttons = True
    # update_track = False
    # update_background = False

    segoe_print = pygame.font.SysFont('segoe print', 25)
    text_buttons = [
        segoe_print.render(t, True, (127, 127, 127)) for t in
        ['Save current track', 'Load track 0', 'Clear active track', 'Start']
    ]
    text_cycler = [
        segoe_print.render(t, True, (127, 127, 127))
        for t in ['Drawing', 'Checkpoints', 'Start', 'Idle']
    ]

    buttons = []
    right_bar_x = screen.get_rect().width * 0.75
    right_bar_width = screen.get_rect().width * 0.25
    button_height = 50
    for index, line in enumerate(text_buttons):
        buttons.append(
            Button(
                pygame.Rect(right_bar_x, (index + 1) * button_height,
                            right_bar_width, button_height), line))
    cycler_buttons = [
        Button(pygame.Rect(right_bar_x, 0, right_bar_width, button_height),
               line) for line in text_cycler
    ]
    input_state_cycler = Cycler(cycler_buttons)

    # main loop
    running = True
    while running:
        # event handling, gets all event from the event queue
        for event in pygame.event.get():
            # red cross handling
            if event.type == pygame.QUIT:
                # change the value to False, to exit the main loop
                running = False

            # mouse presses handling
            if event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == pygame.BUTTON_LEFT:
                    # if it is LMB pressed
                    if event.pos[0] < right_bar_x:
                        if input_state == DRAWING:
                            update_track = True
                            game.track_m.active_track.add_vertex(
                                pygame.math.Vector2(event.pos))
                        elif input_state == CHECKPOINTS:
                            if checkpoint_start:
                                game.track_m.active_track.add_checkpoint(
                                    checkpoint_start,
                                    pygame.math.Vector2(event.pos))
                                checkpoint_start = None
                            else:
                                checkpoint_start = pygame.math.Vector2(
                                    event.pos)
                        elif input_state == START:
                            game.track_m.active_track.set_start(
                                pygame.math.Vector2(event.pos))
                    else:
                        # update_buttons = True
                        if input_state_cycler.is_inside(event.pos):
                            input_state = (input_state + 1) % 4
                        if buttons[0].is_inside(event.pos):
                            print('saved')
                            game.track_m.save_active_track()
                        if buttons[1].is_inside(event.pos):
                            print('loaded')
                            # update_background = True
                            # update_track = True
                            game.track_m.load_to_active_track('track_0.pickle')
                        if buttons[2].is_inside(event.pos):
                            # update_background = True
                            game.track_m.clear_active_track()
                        if buttons[3].is_inside(event.pos):
                            game.start()

            # controls handling
            if event.type == pygame.KEYDOWN:
                # print('down a key')
                if event.key == pygame.K_RIGHT:
                    force.x += 1
                if event.key == pygame.K_LEFT:
                    force.x -= 1
                if event.key == pygame.K_DOWN:
                    force.y += 1
                if event.key == pygame.K_UP:
                    force.y -= 1

            if event.type == pygame.KEYUP:
                # print('up a key')
                if event.key == pygame.K_RIGHT:
                    force.x -= 1
                if event.key == pygame.K_LEFT:
                    force.x += 1
                if event.key == pygame.K_DOWN:
                    force.y -= 1
                if event.key == pygame.K_UP:
                    force.y += 1

        if force.length() > 0:
            force_ = force.normalize()
            force_.scale_to_length(0.0001)
            game.agent.add_force(force_, 0)

        game.update()
        # print(int(game.agent.pos.x), int(game.agent.pos.y))

        # drawing
        pygame.draw.rect(screen, (0, 0, 0), screen.get_rect())
        draw_track(screen, game.track_m.active_track)
        draw_button(screen, input_state_cycler.get_active_button())
        for button in buttons:
            draw_button(screen, button)
        pygame.draw.circle(screen, (255, 255, 0),
                           (int(game.agent.pos.x), int(game.agent.pos.y)), 12)
        pygame.display.flip()
Beispiel #5
0
class ClientGameWindow(UIElement):
    def __init__(self, rect: Rect, color: Optional[Color], sock, receive_list,
                 socket_process, parent_conn, child_conn, send_process, nicks,
                 team_id):
        super().__init__(rect, color)

        self.sock = sock
        self.receive_list = receive_list
        self.socket_process = socket_process
        self.parent_conn = parent_conn
        self.child_conn = child_conn
        self.send_process = send_process

        mod_loader.import_mods()

        config.reload()

        fps_font = Font('assets/fonts/arial.ttf', 20)

        sub_elem = UIElement(Rect(50, 50, 50, 50), None)
        sub_elem.append_child(FPSCounter(Rect(50, 50, 0, 0), fps_font))
        self.append_child(sub_elem)

        self.game = Game(Game.Side.CLIENT, mod_loader, self.parent_conn, nicks,
                         team_id)

        self.minimap = Minimap(self.game)
        self.minimap_elem = UIImage(
            Rect(0, config['screen']['size'][1] - 388, 0, 0),
            'assets/sprite/minimap.png')
        self.minimap_elem.append_child(self.minimap)

        self.minimap_elem.append_child(
            ResourceMenu(self.game.current_player, Rect(45, 108, 0, 0),
                         Font('assets/fonts/arial.ttf', 25)))

        menu_parent = UIElement(Rect(0, 0, 0, 0), None)
        self.append_child(menu_parent)
        menu_parent.append_child(self.minimap_elem)
        menu_parent.append_child(BuildMenu(self.relative_bounds, self.game))
        menu_parent.append_child(ProduceMenu(self.relative_bounds, self.game))

    def update(self, event):
        self.game.update(event)

        if event.type == EVENT_UPDATE:
            while self.receive_list:
                args = self.receive_list.pop(0)
                self.game.handle_command(args[0], args[1:])

        return super().update(event)

    def draw(self, screen):
        super().draw(screen)
        self.game.draw(screen)

    def shutdown(self):
        self.sock.close()
        self.send_process.terminate()
        self.socket_process.terminate()
        self.parent_conn.close()
        self.child_conn.close()
        print('Closed')