예제 #1
0
def ERROR_MODE(sleep_time=constants.ERROR_SLEEP_TIME):
    error_led_on()
    status_led_off()
    NetworkManager.disconnect()
    NetworkManager.disable()
    time.sleep(constants.ERROR_PRE_SLEEP_TIME)
    enter_deepsleep(sleep_time)
    while True:
        pass
예제 #2
0
def setup():
    status_led_off()
    error_led_off()
    # connect to wifi
    if not NetworkManager.connect(credentials.WIFI_SSID,
                                  credentials.WIFI_PASS,
                                  timeout=constants.WIFI_TIMEOUT):
        ERROR_MODE()
예제 #3
0
파일: game.py 프로젝트: IonAgorria/VecWar
 def start(self):
     """Initializes the basic graphics, event handling, world, network, loops and reactor"""
      # Parse the arguments from cmdline, also put default values if arguments are not present
     self.headless = False
     self.listen_port = UDP_SERVER_PORT
     self.parse_args()
      # Graphics initialization
     self.graphics = get_graphics(self.headless)
     self.graphics.create_window()
     self.graphics.caption_window(DEFAULT_CAPTION)
     self.graphics.set_font(DEFAULT_FONT)
      # Event listener initialization
     self.event_listener = get_event_listener(self.headless)
     self.event_listener.add_handler(self.__game_event_handler)
     self.event_listener.add_handler(self.__menu_event_handler, MENU_EVENTS)
      # GUI related
     self.root_template = RootTemplate(self.graphics, self.event_listener)
     self.root_template.add_template("menu", MenuTemplate)
     self.root_template.set_active("menu")
      # World + EntityFunctions initialization
     self.world = World(self.graphics)
      # Networking initialization
     self.networking = NetworkManager(self)
      # Loop calling initialization
     self.worst_delta = -10
     self.last_call = 0  # Last update call
     self.last_window_resize = 0  # Last time the window was resized
     self.close_flag = False  # When true, means that close() must be called
     self.running_flag = True  # When false the loop finishes
     self.draw_update = False  # When true, means that draw must be done
     self.game_update = False  # When true, means that game update must be done
     self.main_finished = True  # Checks if the main call finished correctly
     if self.headless:
          # Send the fake button press
         self.event_listener.submit(EVENT_MENU_BUTTON_RELEASE, menu_action = "create new server", pos = (0, 0), button = 1)
      # Create the event intervals for draw and game updates
     event = self.event_listener.create(EVENT_DRAW_UPDATE)
     self.__uid_draw_update = self.event_listener.add_interval(event, 1.0 / DRAW_FPS)
     event = self.event_listener.create(EVENT_GAME_UPDATE)
     self.__uid_game_update = self.event_listener.add_interval(event, 1.0 / GAME_FPS)
      # Start the loop
     self.controlled_call(self.run)
예제 #4
0
# This file is executed on every boot (including wake-boot from deepsleep)
#import esp
#esp.osdebug(None)
# Set CPU frequency to minimum possible value 80MHz
import machine
import esp
machine.freq(80000000)
esp.sleep_type(esp.SLEEP_LIGHT)

import constants

# Check if the device was woken up by the doorbell or due to a timeout
# If the device didn't wake up due to an interrupt, then go back to sleep
# TODO: uncomment
# from machine import Pin
# if Pin(constants.INT_PIN, Pin.IN).value() == 0:
#     print("[info] woke up due to timeout. going back to sleep")
#     esp.deepsleep()

import gc
#import webrepl
#webrepl.start()
gc.collect()

from networking import NetworkManager
NetworkManager.init()
예제 #5
0
파일: start.py 프로젝트: remyroy/pyCraft
            sys.exit(1)
        sessionid = loginResponse['SessionID']
        user = loginResponse['Username']
        print "Logged in as " + loginResponse['Username'] + "! Your session id is: " + sessionid
    else:
        sessionid = None
        
    if(options.server != ""):
        serverAddress = options.server
    else:
        serverAddress = raw_input("Enter host and port if any: ")
    if ':' in serverAddress:
        StuffEnteredIntoBox = serverAddress.split(":")
        host = StuffEnteredIntoBox[0]
        port = int(StuffEnteredIntoBox[1])
    else:
        host = serverAddress
        port = 25565
    connection = NetworkManager.ServerConnection(user, sessionid, host, port, options)
    connection.start()
    while True:
        try:
            chat_input = raw_input()
            if (connection.isConnected):
                PacketSenderManager.send03(connection.grabSocket(), chat_input) 
            else:
                pass       
        except KeyboardInterrupt, e:
            connection.disconnect()
            sys.exit(1)
예제 #6
0
파일: game.py 프로젝트: IonAgorria/VecWar
class Game(object):  # pylint: disable=R0902
    """The game class, controls the events, graphics, teams and entities"""
    def __init__(self, error_flag):
        self.log = get_logger("game")
        self.error_flag = error_flag

    def start(self):
        """Initializes the basic graphics, event handling, world, network, loops and reactor"""
         # Parse the arguments from cmdline, also put default values if arguments are not present
        self.headless = False
        self.listen_port = UDP_SERVER_PORT
        self.parse_args()
         # Graphics initialization
        self.graphics = get_graphics(self.headless)
        self.graphics.create_window()
        self.graphics.caption_window(DEFAULT_CAPTION)
        self.graphics.set_font(DEFAULT_FONT)
         # Event listener initialization
        self.event_listener = get_event_listener(self.headless)
        self.event_listener.add_handler(self.__game_event_handler)
        self.event_listener.add_handler(self.__menu_event_handler, MENU_EVENTS)
         # GUI related
        self.root_template = RootTemplate(self.graphics, self.event_listener)
        self.root_template.add_template("menu", MenuTemplate)
        self.root_template.set_active("menu")
         # World + EntityFunctions initialization
        self.world = World(self.graphics)
         # Networking initialization
        self.networking = NetworkManager(self)
         # Loop calling initialization
        self.worst_delta = -10
        self.last_call = 0  # Last update call
        self.last_window_resize = 0  # Last time the window was resized
        self.close_flag = False  # When true, means that close() must be called
        self.running_flag = True  # When false the loop finishes
        self.draw_update = False  # When true, means that draw must be done
        self.game_update = False  # When true, means that game update must be done
        self.main_finished = True  # Checks if the main call finished correctly
        if self.headless:
             # Send the fake button press
            self.event_listener.submit(EVENT_MENU_BUTTON_RELEASE, menu_action = "create new server", pos = (0, 0), button = 1)
         # Create the event intervals for draw and game updates
        event = self.event_listener.create(EVENT_DRAW_UPDATE)
        self.__uid_draw_update = self.event_listener.add_interval(event, 1.0 / DRAW_FPS)
        event = self.event_listener.create(EVENT_GAME_UPDATE)
        self.__uid_game_update = self.event_listener.add_interval(event, 1.0 / GAME_FPS)
         # Start the loop
        self.controlled_call(self.run)

    def run(self):
        """Handles the loop"""
        while self.running_flag:
            if self.close_flag:  # If we need to close, call it and return
                self.close()
                continue

            if self.error_flag.is_set():
                self.log.error("Error flag was raised. Terminating")
                self.close()
                return

            self.controlled_call(self.main_loop)

    def close(self):
        """Closes graphics/window and event listener"""
        self.log.info("Closing the game")
        self.controlled_call(self.root_template.close)
        self.controlled_call(self.networking.close)
        self.controlled_call(self.event_listener.close)
        self.controlled_call(self.graphics.close_window)
        self.graphics = None
        self.event_listener = None
        self.networking = None
        self.world = None
        self.close_flag = False
        self.running_flag = False

    def main_loop(self):
        """The main loop of the game"""
        self.event_listener.update()         # Handle the events
        if self.networking.status():
            self.networking.update()         # Handle the networking
        if self.game_update:
            self.world.update()              # Propagate update call
            self.game_update = False
        if self.draw_update:
            self.update_timing()             # Update timing information
            self.root_template.draw()        # Draw the GUI
            self.graphics.update_window()    # Finally update the window
            self.draw_update = False

    def battle_template(self, client_mode):
        """Prepares the battle configuration template"""
        self.root_template.add_template("battle", BattleTemplate, self.world, client_mode)
        self.root_template.set_active("battle")

    def start_game(self):
        """Prepares a new game"""
        if self.root_template.has_template("battle"):
            self.root_template.del_template("battle")
        self.root_template.add_template("game", GameTemplate, self.world)
        self.root_template.set_active("game")

    def stop_game(self):
        """Stops the actual running game"""
        if self.root_template.has_template("battle"):
            self.root_template.del_template("battle")
        if self.root_template.has_template("game"):
            self.root_template.del_template("game")
        self.game_update = False
        self.world.close()

    def __game_event_handler(self, event):   # pylint: disable=R0912
        """Called when we need to handle events"""
        etype = event.type
        if   etype == EVENT_QUIT:
            self.close_flag = True
        elif etype == EVENT_WINDOW_CLOSE:
             # There is a game running? show the menu, if not, close it
            if False and self.root_template.has_template("game"):
                self.event_listener.submit(EVENT_MENU_REQUEST)
            else:
                self.event_listener.submit(EVENT_QUIT)
        elif etype == EVENT_WINDOW_RESIZE:
            size = list(event.size)
            if size[0] < MINIMAL_RESOLUTION[0]: size[0] = MINIMAL_RESOLUTION[0]
            if size[1] < MINIMAL_RESOLUTION[1]: size[1] = MINIMAL_RESOLUTION[1]
            self.graphics.set_size(size)
            self.root_template.set_size(size)
            self.last_window_resize = time()
        elif etype == EVENT_DRAW_UPDATE:
            self.draw_update = True
        elif etype == EVENT_GAME_UPDATE:
            self.game_update = True
        elif etype == EVENT_NETWORK_STATUS:
            status = event.network_status
            address = event.network_address
            host = "%s:%i" % address
            if   status == STATUS_CONNECTED:
                self.battle_template(True)
            elif status == STATUS_DISCONNECTED:
                menu_template = self.root_template.get_template("menu")
                if self.root_template.get_active() != menu_template:
                    self.stop_game()
                    menu_template.start()
                    menu_template.message("Connection finished", "You has been disconnected from \"%s\", reason: \"%s\"" % (host, event.network_reason))
                    self.root_template.set_active("menu")
        elif etype == EVENT_MENU_SELECTED_MAP:
            if self.networking.status() and self.networking.mode != CLIENT_MODE:
                self.networking.handler.send_initial()

        return True  # Cancel the rest of event handlers

    def __menu_event_handler(self, event):  # pylint: disable=R0912
        """Called when menu event occurs"""
        etype = event.type
        if  self.root_template.get_active() != self.root_template.get_template("menu"):
            return  # We are not in menu
        elif   etype == EVENT_MENU_BUTTON_RELEASE:
            if event.button != 1:
                return  # Only accept left button
            name = event.menu_action.lower()
            if name == "battle":
                self.battle_template(False)
            elif name == "connect":
                server = str(event.extra.text)
                if ":" in server: server, port = server.split(":")
                else: port = UDP_SERVER_PORT
                self.networking.start_connection([server, int(port)])
                 # Now the network interface will send a event regarding to connection status
            elif name == "create new server":
                try:
                    self.networking.start_listening(self.listen_port)
                except socket_error as e:
                    self.log.exception(e)
                    self.log.error("Error trying to open listener")
                    self.networking.close_handler()
                else:
                    self.battle_template(False)

            elif name == "exit":
                self.event_listener.submit(EVENT_QUIT)

    def update_timing(self):
        """Updates the timing stuff"""
        if self.last_call == 0: self.last_call = time()
        delta = time() - self.last_call
        self.last_call = time()
        if delta == 0.0: delta = 0.000001  # Happens on main PC, wow nowadays computers are really fast
        fps = float(str(1.0 / delta)[0:4])
        delta = float(str(delta)[0:6])
        if self.worst_delta < 0:
            self.worst_delta += 1
        if 0 <= self.worst_delta < delta:
            self.worst_delta = delta
        worst_fps = float(str(1.0 / self.worst_delta)[0:4])
        self.graphics.caption_window("%s | WFPS: %s - Worst Delta: %s | FPS: %s - Delta: %s" % (DEFAULT_CAPTION, worst_fps, self.worst_delta, fps, delta))

    def parse_args(self):
        """Parses each cmd line argument and sets the corresponding variable"""
        ignore_positions = [0]  # This list saves the ignored positions, first argument 0 is the executing file
        for index, arg in enumerate(sys_argv):
            if index in ignore_positions:
                pass
            elif arg == ARG_DEBUG or arg == ARG_GUI_DEBUG:  # Debug flags are checked in get_debug_state
                pass
            elif arg == ARG_HEADLESS:  # Get if we need to run as dedicated server
                self.headless = True
            elif arg == ARG_PORT:      # Get the specified listening port in cmdline
                try:
                    self.listen_port = int(sys_argv[index + 1])
                    ignore_positions.append(index + 1)
                except IndexError:
                    self.log.error("You didn't specified any port after port argument")
                    raise SystemExit()
                except ValueError:
                    self.log.error("You didn't specified a valid numerical port")
                    raise SystemExit()
            else:
                self.log.warning("Argument '%s' at position %i is unknown" % (arg, index))

    def controlled_call(self, function, *args, **kwargs):
        """This wrappers controlled_exception"""
        controlled_exception(function, self.log, self.error_flag, False, *args, **kwargs)
예제 #7
0
파일: start.py 프로젝트: xkuyax/pyCraft
            'Username'] + "! Your session id is: " + sessionid
    else:
        sessionid = None

    if (options.server != ""):
        serverAddress = options.server
    else:
        serverAddress = raw_input("Enter host and port if any: ")
    if ':' in serverAddress:
        StuffEnteredIntoBox = serverAddress.split(":")
        host = StuffEnteredIntoBox[0]
        port = int(StuffEnteredIntoBox[1])
    else:
        host = serverAddress
        port = 25565
    connection = NetworkManager.ServerConnection(pluginLoader, user, sessionid,
                                                 host, port, options)
    connection.setDaemon(True)
    connection.start()
    while True:
        try:
            chat_input = raw_input()
            if (connection.isConnected):
                PacketSenderManager.send03(connection.grabSocket(),
                                           chat_input.decode('utf-8')[:100])
            else:
                pass
        except KeyboardInterrupt, e:
            connection.disconnect()
            pluginLoader.disablePlugins()
            sys.exit(1)
예제 #8
0
파일: logger.py 프로젝트: remyroy/pyCraft
    sessionid = derp['SessionID']

    print "Logged in as " + derp[
        'Username'] + "! Your session id is: " + sessionid

    stuff = options.server

    if ':' in stuff:
        StuffEnteredIntoBox = stuff.split(":")
        host = StuffEnteredIntoBox[0]
        port = int(StuffEnteredIntoBox[1])
    else:
        host = stuff
        port = 25565

    connection = NetworkManager.ServerConnection(derp['Username'], sessionid,
                                                 host, port, options)
    connection.start()

    start = datetime.utcnow()

    delay = timedelta(seconds=40)

    try:
        while datetime.utcnow() - start < delay:
            pass
    except KeyboardInterrupt, e:
        pass
    connection.disconnect()
    sys.exit(1)
예제 #9
0
class GameClient(GameModule):
    """Contains game client and pyGame functionality."""
    def __init__(self):
        self.in_queue = Queue()
        self.dispatch_queue = Queue()
        GameModule.__init__(self, self.in_queue, self.dispatch_queue)

        self.name = 'Client'
        self.module_id = GAME.INVALID_ID
        self.server_id = GAME.LOCAL_SERVER_ID
        self.server_addr = '127.0.0.1'

        self.local_server_instance = None
        self.dispatch = None
        self.network_mgr = None

        self.connected = False

        # Initialize pyGame
        pygame.mixer.pre_init()
        pygame.init()
        self.screen = pygame.display.set_mode((GAME.WIDTH, GAME.HEIGHT))
        pygame.display.set_caption('AstroBlast!')
        self.clock = pygame.time.Clock()
        pygame.mouse.set_visible(False)

        # Initialize pgu.gui
        # Title screen GUI
        self.title_app = gui.App()
        self.title_container = gui.Container(width=GAME.WIDTH,
                                             height=GAME.HEIGHT,
                                             x=0,
                                             y=0)
        self.title_sp_button = gui.Button("Single Player",
                                          width=150,
                                          height=100)
        self.title_sp_button.connect(gui.CLICK, self.gui_button_clicked,
                                     GAME.GUI_BUTTON_SP)
        self.title_container.add(self.title_sp_button, GAME.WIDTH / 2 - 75,
                                 GAME.HEIGHT / 2)
        self.title_mp_button = gui.Button("Multiplayer", width=150, height=100)
        self.title_mp_button.connect(gui.CLICK, self.gui_button_clicked,
                                     GAME.GUI_BUTTON_MP)
        self.title_container.add(self.title_mp_button, GAME.WIDTH / 2 - 75,
                                 2 * GAME.HEIGHT / 3)
        self.title_sp_button.connect(gui.CLICK, self.gui_button_clicked,
                                     GAME.GUI_BUTTON_MP)
        self.title_app.init(self.title_container, self.screen,
                            pygame.rect.Rect(0, 0, GAME.WIDTH, GAME.HEIGHT))

        # Multiplayer Menu GUI
        self.multi_app = gui.App()
        self.multi_container = gui.Container(width=GAME.WIDTH,
                                             height=GAME.HEIGHT,
                                             x=0,
                                             y=0)
        self.multi_host_button = gui.Button("Host Game", width=100, height=100)
        self.multi_host_button.connect(gui.CLICK, self.gui_button_clicked,
                                       GAME.GUI_BUTTON_HOST)
        self.multi_container.add(self.multi_host_button, GAME.WIDTH / 3,
                                 GAME.HEIGHT / 3)
        self.multi_join_button = gui.Button("Join Game", width=100, height=100)
        self.multi_join_button.connect(gui.CLICK, self.gui_button_clicked,
                                       GAME.GUI_BUTTON_JOIN)
        self.multi_container.add(self.multi_join_button, GAME.WIDTH / 3 + 150,
                                 GAME.HEIGHT / 3)
        self.multi_back_button = gui.Button("Back", width=50, height=50)
        self.multi_back_button.connect(gui.CLICK, self.gui_button_clicked,
                                       GAME.GUI_BUTTON_BACK)
        self.multi_container.add(self.multi_back_button, 30, GAME.HEIGHT - 80)
        self.multi_ip_input = gui.Input("IP address")
        self.multi_container.add(self.multi_ip_input, 2 * GAME.WIDTH / 3,
                                 GAME.HEIGHT / 3)
        self.multi_port_input = gui.Input("Port")
        self.multi_container.add(self.multi_port_input, 2 * GAME.WIDTH / 3,
                                 GAME.HEIGHT / 3 + 50)
        self.multi_app.init(self.multi_container, self.screen,
                            pygame.rect.Rect(0, 0, GAME.WIDTH, GAME.HEIGHT))

        # Mouse cursor
        cursor, rect = load_image('cursor.png')
        self.cursor = cursor

        # Images for sprites
        self.frames = {
            'ship': [
                load_image_all_rotations('ship.png'),
                load_image_all_rotations('ship_thrust.png')
            ],
            'asteroid_big': [load_image_all_rotations('asteroid_big.png')],
            'asteroid_med': [load_image_all_rotations('asteroid_med.png')],
            'asteroid_small': [load_image_all_rotations('asteroid_small.png')],
            'bullet_g': [load_image_all_rotations('bullet_g.png')],
            'explosion': [
                load_image_all_rotations('explosion_1.png'),
                load_image_all_rotations('explosion_2.png')
            ]
        }

        self.background = load_image('bg5.jpg')[0]

        # Sounds
        self.thrust_sound = pygame.mixer.Sound(
            path.join(ASSETSDIR, 'thrust.ogg'))
        self.thrust_sound.set_volume(1)
        self.shot_sound = pygame.mixer.Sound(path.join(ASSETSDIR, 'shot.wav'))
        self.shot_sound.set_volume(0.75)
        self.explode_sound = pygame.mixer.Sound(
            path.join(ASSETSDIR, 'explode.wav'))
        self.explode_sound.set_volume(1)

        self.entities = {}
        self.unused_entities = []
        # Generate some unused entities
        for x in range(100):
            self.unused_entities.append(EntitySprite(None))
        self.sprites = pygame.sprite.Group()

        self.player_entity_id = -1
        self.player_alive = False
        self.player_lives = 0
        self.player_score = 0

        self.game_state = GAME.STATE_TITLE

    def quit(self):
        """Sends quit message and halts client"""

        # Make sure Server and Dispatcher quit too
        if self.local_server_instance is not None:
            self.local_server_instance.running = False
        if self.dispatch is not None:
            self.dispatch.running = False
        if self.network_mgr is not None:
            self.network_mgr.running = False
        # self.send_msg(MESSAGES.TERMINATE, self.local_server_id)
        self.running = False

    def process_msg(self, msg_type, sender_id, msg_content):
        """Process an incoming message"""

        # Server accepted connection request
        if msg_type == MESSAGES.CONNECT_ACCEPT:
            self.connected = True
            self.send_msg(MESSAGES.CONNECT_SUCCESS, sender_id)

        # Server rejected connection request
        elif msg_type == MESSAGES.CONNECT_REJECT:
            self.log("Connection to server unsuccessful; connection rejected")

        # Server is disconnecting
        elif msg_type == MESSAGES.SIGNAL_DISCONNECT:
            self.disconnect(False)

        # Server created an entity
        elif msg_type == MESSAGES.CREATE_ENTITY:
            expected_content = (MSGCONTENT.ENTITY_ID, MSGCONTENT.ENTITY_TYPE,
                                MSGCONTENT.X_POS, MSGCONTENT.Y_POS,
                                MSGCONTENT.ROTATION)
            if self.assert_msg_content(msg_type, msg_content,
                                       *expected_content):
                entity_id = msg_content[MSGCONTENT.ENTITY_ID]
                entity_type = msg_content[MSGCONTENT.ENTITY_TYPE]
                pos = (msg_content[MSGCONTENT.X_POS],
                       msg_content[MSGCONTENT.Y_POS])
                rot = msg_content[MSGCONTENT.ROTATION]

                # Try to get an unused entity, or create one if there are none
                try:
                    e = self.unused_entities.pop()
                except IndexError:
                    e = EntitySprite(None)

                if entity_type == GAME.ENTITY_TEST:
                    info = SpriteInfo(entity_type, self.frames['ship'], 0, 0)
                    e.initialize(info, pos, rot, entity_id, entity_type)
                    self.entities[e.entity_id] = e
                    self.sprites.add(e)

                elif entity_type == GAME.ENTITY_PLAYERSHIP:
                    info = SpriteInfo(entity_type, self.frames['ship'], 0, 0)
                    e.initialize(info, pos, rot, entity_id, entity_type)
                    self.entities[e.entity_id] = e
                    self.sprites.add(e)
                    pid = msg_content.get(MSGCONTENT.PLAYER_ID, None)
                    if pid is not None and pid == self.module_id:
                        self.player_entity_id = e.entity_id
                        self.player_alive = True

                elif entity_type == GAME.ENTITY_ASTEROID_BIG:
                    info = SpriteInfo(entity_type, self.frames['asteroid_big'],
                                      0, 0)
                    e.initialize(info, pos, rot, entity_id, entity_type)
                    self.entities[e.entity_id] = e
                    self.sprites.add(e)

                elif entity_type == GAME.ENTITY_ASTEROID_MED:
                    info = SpriteInfo(entity_type, self.frames['asteroid_med'],
                                      0, 0)
                    e.initialize(info, pos, rot, entity_id, entity_type)
                    self.entities[e.entity_id] = e
                    self.sprites.add(e)

                elif entity_type == GAME.ENTITY_ASTEROID_SMALL:
                    info = SpriteInfo(entity_type,
                                      self.frames['asteroid_small'], 0, 0)
                    e.initialize(info, pos, rot, entity_id, entity_type)
                    self.entities[e.entity_id] = e
                    self.sprites.add(e)

                elif entity_type == GAME.ENTITY_BULLET:
                    info = SpriteInfo(entity_type, self.frames['bullet_g'], 0,
                                      0)
                    e.initialize(info, pos, rot, entity_id, entity_type)
                    self.entities[e.entity_id] = e
                    self.sprites.add(e)
                    self.shot_sound.play()

                elif entity_type == GAME.ENTITY_EXPLOSION:
                    info = SpriteInfo(entity_type, self.frames['explosion'], 1,
                                      500)
                    e.initialize(info, pos, rot, entity_id, entity_type)
                    self.entities[e.entity_id] = e
                    self.sprites.add(e)
                    self.explode_sound.play()

        # Server destroyed an entity
        elif msg_type == MESSAGES.DESTROY_ENTITY:
            if self.assert_msg_content(msg_type, msg_content,
                                       MSGCONTENT.ENTITY_ID):
                entity_id = msg_content[MSGCONTENT.ENTITY_ID]
                e = self.entities.pop(entity_id)
                if e is not None:
                    self.sprites.remove(e)
                    self.unused_entities.append(e)
                    if e.entity_id == self.player_entity_id:
                        self.player_alive = False
                else:
                    self.log(
                        'Received message to destroy sprite for entity %d, but sprite did not exist'
                        % entity_id)

        # An entity rotated by some amount
        elif msg_type == MESSAGES.UPDATEROT:
            expected_content = (MSGCONTENT.ENTITY_ID, MSGCONTENT.ROTATION)
            if self.assert_msg_content(msg_type, msg_content,
                                       *expected_content):
                entity_id = msg_content[MSGCONTENT.ENTITY_ID]
                rot = msg_content[MSGCONTENT.ROTATION]
                e = self.entities.get(entity_id)
                if e is not None:
                    e.rotation = rot
                else:
                    self.log(
                        'Received message to update rotation for entity %d, but sprite does not exist'
                        % entity_id)

        # An entity moved
        elif msg_type == MESSAGES.UPDATEPOS:
            expected_content = (MSGCONTENT.X_POS, MSGCONTENT.Y_POS)
            if self.assert_msg_content(msg_type, msg_content,
                                       *expected_content):
                entity_id = msg_content[MSGCONTENT.ENTITY_ID]
                pos = (msg_content[MSGCONTENT.X_POS],
                       msg_content[MSGCONTENT.Y_POS])
                e = self.entities.get(entity_id)
                if e is not None:
                    e.position = pos
                else:
                    self.log(
                        'Received message to update position for entity %d, but sprite does not exist'
                        % entity_id)

        # Player life count changed
        elif msg_type == MESSAGES.UPDATELIVES:
            expected_content = MSGCONTENT.PLAYER_LIVES
            if self.assert_msg_content(msg_type, msg_content,
                                       expected_content):
                self.player_lives = msg_content[MSGCONTENT.PLAYER_LIVES]

        # Player score changed
        elif msg_type == MESSAGES.UPDATESCORE:
            expected_content = MSGCONTENT.PLAYER_SCORE
            if self.assert_msg_content(msg_type, msg_content,
                                       expected_content):
                self.player_score = msg_content[MSGCONTENT.PLAYER_SCORE]

        # Server wants to change the client state to some other setting
        elif msg_type == MESSAGES.CHANGE_STATE:
            expected_content = MSGCONTENT.GAME_STATE
            if self.assert_msg_content(msg_type, msg_content,
                                       expected_content):
                self.game_state = msg_content[MSGCONTENT.GAME_STATE]

    def disconnect(self, should_send_signal=True):
        """Disconnect this client from a server"""

        if should_send_signal:
            self.send_msg(MESSAGES.SIGNAL_DISCONNECT, self.server_id,
                          self.server_addr)

        self.connected = False

        self.log("Disconnected from server")
        self.module_id = GAME.INVALID_ID

        # Delete sprites
        self.sprites.empty()
        self.entities.clear()

    def gui_button_clicked(self, button_type):
        """Called when one of the buttons in the UI is clicked"""

        # Singleplayer button
        if button_type == GAME.GUI_BUTTON_SP:
            server_queue = Queue()
            remote_queue = Queue()
            # Create server and dispatcher
            self.dispatch = Dispatcher(self.dispatch_queue, server_queue,
                                       self.in_queue, remote_queue)
            self.dispatch.start()
            self.out_queue = self.dispatch_queue
            self.module_id = GAME.LOCAL_CLIENT_ID
            self.local_server_instance = GameServer(self.dispatch,
                                                    server_queue)
            self.local_server_instance.start()
            self.server_id = self.local_server_instance.module_id
            self.server_addr = self.local_server_instance.addr
            # Start singleplayer by connecting to server
            self.send_msg(MESSAGES.REQCONNECT, self.server_id)

        # Switch to multiplayer menu
        elif button_type == GAME.GUI_BUTTON_MP:
            self.game_state = GAME.STATE_MULTIPLAYER_MENU

        # Host Multiplayer Session
        elif button_type == GAME.GUI_BUTTON_HOST:
            port = int(self.multi_port_input.value)
            server_queue = Queue()
            remote_queue = Queue()
            # Create server and dispatcher
            self.dispatch = Dispatcher(self.dispatch_queue, server_queue,
                                       self.in_queue, remote_queue)
            self.network_mgr = NetworkManager(remote_queue, self.dispatch,
                                              self.dispatch_queue,
                                              NETWORK.MODE_SERVER, port)
            self.dispatch.start()
            self.network_mgr.start()
            self.addr = self.network_mgr.addr
            self.server_addr = self.network_mgr.addr
            self.out_queue = self.dispatch_queue
            self.module_id = GAME.LOCAL_CLIENT_ID
            self.local_server_instance = GameServer(self.dispatch,
                                                    server_queue)
            self.local_server_instance.start()
            self.server_id = GAME.LOCAL_SERVER_ID
            self.send_msg(MESSAGES.REQCONNECT, self.server_id)

        # Join a Multiplayer session
        elif button_type == GAME.GUI_BUTTON_JOIN:
            server_queue = Queue()
            remote_queue = Queue()
            self.server_addr = self.multi_ip_input.value
            port = int(self.multi_port_input.value)
            self.dispatch = Dispatcher(self.dispatch_queue, server_queue,
                                       self.in_queue, remote_queue)
            self.dispatch.mode = NETWORK.MODE_CLIENT
            self.network_mgr = NetworkManager(remote_queue, self.dispatch,
                                              self.dispatch_queue,
                                              NETWORK.MODE_CLIENT, port,
                                              self.server_addr)
            self.dispatch.start()
            self.network_mgr.start()
            self.out_queue = self.dispatch_queue
            self.addr = get_lan_ip()
            self.module_id = GAME.REMOTE_CLIENT_ID
            self.send_msg(MESSAGES.REQCONNECT, self.server_id)

        elif button_type == GAME.GUI_BUTTON_BACK:
            self.game_state = GAME.STATE_TITLE

    def update(self):
        """Update game state/input state"""

        self.clock.tick(GAME.FPS)

        # Handle pyGame events
        events_found = False
        event_str = 'pyGame events: '
        for event in pygame.event.get():
            # Cache the event name for logging
            events_found = True
            event_str += pygame.event.event_name(event.type) + ', '

            if self.game_state == GAME.STATE_TITLE:
                # Pass events to gui
                self.title_app.event(event)

            elif self.game_state == GAME.STATE_MULTIPLAYER_MENU:
                # Pass events to gui
                self.multi_app.event(event)

            else:
                # Handle keyboard events
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        self.quit()
                    elif event.key == pygame.K_BACKSLASH:
                        pass
                    elif event.key == pygame.K_BACKSPACE:
                        self.disconnect()
                    elif event.key == pygame.K_LEFT:
                        self.send_msg(MESSAGES.INPUT_LEFT_DOWN, self.server_id)
                    elif event.key == pygame.K_RIGHT:
                        self.send_msg(MESSAGES.INPUT_RIGHT_DOWN,
                                      self.server_id)
                    elif event.key == pygame.K_UP:
                        pship = self.entities.get(self.player_entity_id)
                        if pship is not None:
                            pship.current_frame = 1
                            self.thrust_sound.play()
                        else:
                            self.thrust_sound.stop()
                        self.send_msg(MESSAGES.INPUT_THRUST_DOWN,
                                      self.server_id)
                    elif event.key == pygame.K_SPACE:
                        self.send_msg(MESSAGES.INPUT_SHOOT_DOWN,
                                      self.server_id)

                if event.type == pygame.KEYUP:
                    if event.key == pygame.K_LEFT:
                        self.send_msg(MESSAGES.INPUT_LEFT_UP, self.server_id)
                    elif event.key == pygame.K_RIGHT:
                        self.send_msg(MESSAGES.INPUT_RIGHT_UP, self.server_id)
                    elif event.key == pygame.K_UP:
                        pship = self.entities.get(self.player_entity_id)
                        if pship is not None:
                            pship.current_frame = 0
                        self.thrust_sound.stop()
                        self.send_msg(MESSAGES.INPUT_THRUST_UP, self.server_id)
                    elif event.key == pygame.K_SPACE:
                        self.send_msg(MESSAGES.INPUT_SHOOT_UP, self.server_id)

            if event.type == pygame.QUIT:
                self.quit()

        # Print the event name to stdout
        if events_found and GAME.PRINTEVENTS:
            print(event_str)

        # Clear surface
        self.screen.fill(GAME.BLACK)

        # Draw background
        self.screen.blit(self.background, (0, 0))

        # Draw sprites on the surface
        # self.ship_sprite.rotation += 10
        self.sprites.update()
        self.sprites.draw(self.screen)

        # Text/GUI
        if (self.game_state == GAME.STATE_TITLE):
            # Draw GUI
            self.title_app.paint(self.screen)
            # Draw cursor when appropriate
            if pygame.mouse.get_focused():
                self.screen.blit(self.cursor, pygame.mouse.get_pos())
        elif (self.game_state == GAME.STATE_MULTIPLAYER_MENU):
            # Draw GUI
            self.multi_app.paint(self.screen)
            # Draw cursor when appropriate
            if pygame.mouse.get_focused():
                self.screen.blit(self.cursor, pygame.mouse.get_pos())

        # Draw labels
        normal_font = pygame.font.SysFont('Helvetica, Arial', 20, 1)
        larger_font = pygame.font.SysFont('Helvetica, Arial', 30, 1)
        huge_font = pygame.font.SysFont('Helvetica, Arial,', 150, 1)
        if (self.game_state == GAME.STATE_TITLE):
            width, height = huge_font.size('AstroBlast!')
            title_label = huge_font.render('AstroBlast!', 1, (0, 255, 0))
            self.screen.blit(title_label, (GAME.WIDTH / 2 - width / 2, height))
        if (self.game_state == GAME.STATE_IN_GAME
                or self.game_state == GAME.STATE_GAME_START
                or self.game_state == GAME.STATE_PLAYER_DIED):
            width, height = normal_font.size('Lives:')
            lives_label = normal_font.render('Lives: %d' % self.player_lives,
                                             1, (255, 255, 0))
            self.screen.blit(lives_label, (20, 20))
            score_label = normal_font.render('Score: %d' % self.player_score,
                                             1, (255, 255, 0))
            self.screen.blit(score_label, (20, height + 20))

        if self.game_state == GAME.STATE_GAME_START or self.game_state == GAME.STATE_GAME_OVER:
            width, height = normal_font.size('Press Fire (Spacebar)')
            start_label = normal_font.render('Press Fire (Spacebar)', 1,
                                             (255, 255, 0))
            self.screen.blit(start_label,
                             (GAME.WIDTH / 2 - width / 2, GAME.HEIGHT / 2))

        if self.game_state == GAME.STATE_GAME_OVER:
            width, height = larger_font.size('Game Over!')
            game_over_label = larger_font.render('Game Over!', 1,
                                                 (255, 255, 0))
            self.screen.blit(
                game_over_label,
                (GAME.WIDTH / 2 - width / 2, GAME.HEIGHT / 2 - 40))

        # Display surface to the screen
        pygame.display.flip()

        # Handles quitting/etc.
        super().update()
예제 #10
0
    def gui_button_clicked(self, button_type):
        """Called when one of the buttons in the UI is clicked"""

        # Singleplayer button
        if button_type == GAME.GUI_BUTTON_SP:
            server_queue = Queue()
            remote_queue = Queue()
            # Create server and dispatcher
            self.dispatch = Dispatcher(self.dispatch_queue, server_queue,
                                       self.in_queue, remote_queue)
            self.dispatch.start()
            self.out_queue = self.dispatch_queue
            self.module_id = GAME.LOCAL_CLIENT_ID
            self.local_server_instance = GameServer(self.dispatch,
                                                    server_queue)
            self.local_server_instance.start()
            self.server_id = self.local_server_instance.module_id
            self.server_addr = self.local_server_instance.addr
            # Start singleplayer by connecting to server
            self.send_msg(MESSAGES.REQCONNECT, self.server_id)

        # Switch to multiplayer menu
        elif button_type == GAME.GUI_BUTTON_MP:
            self.game_state = GAME.STATE_MULTIPLAYER_MENU

        # Host Multiplayer Session
        elif button_type == GAME.GUI_BUTTON_HOST:
            port = int(self.multi_port_input.value)
            server_queue = Queue()
            remote_queue = Queue()
            # Create server and dispatcher
            self.dispatch = Dispatcher(self.dispatch_queue, server_queue,
                                       self.in_queue, remote_queue)
            self.network_mgr = NetworkManager(remote_queue, self.dispatch,
                                              self.dispatch_queue,
                                              NETWORK.MODE_SERVER, port)
            self.dispatch.start()
            self.network_mgr.start()
            self.addr = self.network_mgr.addr
            self.server_addr = self.network_mgr.addr
            self.out_queue = self.dispatch_queue
            self.module_id = GAME.LOCAL_CLIENT_ID
            self.local_server_instance = GameServer(self.dispatch,
                                                    server_queue)
            self.local_server_instance.start()
            self.server_id = GAME.LOCAL_SERVER_ID
            self.send_msg(MESSAGES.REQCONNECT, self.server_id)

        # Join a Multiplayer session
        elif button_type == GAME.GUI_BUTTON_JOIN:
            server_queue = Queue()
            remote_queue = Queue()
            self.server_addr = self.multi_ip_input.value
            port = int(self.multi_port_input.value)
            self.dispatch = Dispatcher(self.dispatch_queue, server_queue,
                                       self.in_queue, remote_queue)
            self.dispatch.mode = NETWORK.MODE_CLIENT
            self.network_mgr = NetworkManager(remote_queue, self.dispatch,
                                              self.dispatch_queue,
                                              NETWORK.MODE_CLIENT, port,
                                              self.server_addr)
            self.dispatch.start()
            self.network_mgr.start()
            self.out_queue = self.dispatch_queue
            self.addr = get_lan_ip()
            self.module_id = GAME.REMOTE_CLIENT_ID
            self.send_msg(MESSAGES.REQCONNECT, self.server_id)

        elif button_type == GAME.GUI_BUTTON_BACK:
            self.game_state = GAME.STATE_TITLE