ESG-Engine


NameESG-Engine JSON
Version 0.1.7 PyPI version JSON
download
home_pagehttps://github.com/yannis300307/ES_Engine
SummaryA little online game engine.
upload_time2023-01-02 14:50:29
maintainer
docs_urlNone
authorYannis300307
requires_python
licenseMIT
keywords python game engine pygame
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # ESG Engine
ESG Engine (Extremely Simple Game Engine) is a little Pygame based game engine which allow you to create online (or not) games really quikly and easily.
You can create a server with very few lines.

## How to make a client ?

### Create a client

You can create a client by inherit a class from the core.client.client_core.Client class, or you can simply create a Client object:

Example :
```python
from ESG_Engine.client.client_core import Client


class MyClient(Client):
    def __init__(self):
        super().__init__((500, 500))
```

or without classes :

```python
from ESG_Engine.client.client_core import Client


client = Client((500, 500))
```

### Game loop

Next, you have to create a game loop which contain the client.tick() function and the client.render() function.

```python
from ESG_Engine.client.client_core import Client


class MyClient(Client):
    def __init__(self):
        super().__init__((500, 500))
    
    def main_loop(self):
        while True:
            self.tick()
            self.render()
```

Before starting your game you need to import the map, the bg (optionnal) and the tileset.

### Import a map, backgrounds and tiles

The map json is made of "width" key, a "height" key and a "layers" key which is a list that contains the tiles (0 is nothing)

Example for a 3 x 3 empty map :

```json
{
  "width" : 3,
  "height" : 3,
  "layers" : [
    [0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0]
  ]
}
```

Note that the first layer is behind the player, the third is in front of the player and the second is used for collisions.

You can import the map with the `client.map.load_from_json(json)` function.


You can register the tileset using the `client.renderer.register_tile(tile_image)` function in a for loop for example.

Finally, you can register the backgrounds using `register_background(parralax, background_image)`. The parralax argument is how far is the background (-1 is no parralax).

So you can import all of that like this :

```python
from ESG_Engine.client.client_core import Client
import pygame


class MyClient(Client):
    def __init__(self):
        super().__init__((500, 500))
    
    def load_map(self):
        with open("my_map.json") as file:
            self.map.load_from_json(file.read())
            file.close()
    
    def load_tiles(self):
        for i in range(64):
            self.renderer.register_tile(pygame.image.load("tile" + str(i) + ".png"))
    
    def load_backgrounds(self):
        self.renderer.register_background(-1, pygame.image.load("background 1.png"))
        self.renderer.register_background(2, pygame.image.load("background 2.png"))
        self.renderer.register_background(6, pygame.image.load("background 3.png"))
```

### Create a player

We can now add a player, and we need to register it:

```python
import pygame
from ESG_Engine.client.client_core import Client
from ESG_Engine.client.player import Player


class MyClient(Client):
    def __init__(self):
        super().__init__((500, 500))
        self.player: Player = self.entity_manager.add_entity(Player(50, 200, 30, 175))
        self.player.set_collider_points([(0, 0), (16, 16), (0, 16), (16, 0)])
        
        self.event_handler.register_event(pygame.KEYDOWN, self.player.player_control_key_down)
        self.event_handler.register_event(pygame.KEYUP, self.player.player_control_key_up)
```

Note that the `add_entity()` function return the given entity.

The last 2 lines are for event handler. See the next section for more explanation.

To add an animation to the player (or Entity), you need a `Anim` object wich contain all the frames of the annimation and bind it to the player into the renderer:

```python
import pygame
from ESG_Engine.client.client_core import Client
from ESG_Engine.client.player import Player
from ESG_Engine.client.animation import Anim


class MyClient(Client):
    def __init__(self):
        super().__init__((500, 500))
        self.player: Player = self.entity_manager.add_entity(Player(50, 200, 30, 175))
        self.player_anim_1 = Anim(0.4)  # take the time between 2 frames
        self.renderer.bind_anim(self.player, self.player_anim_1)
    
    def load_animations(self):
        for i in range(4):
            self.player_anim_1.add_frame(pygame.image.load("player_anim_" + str(i) + ".png"))
```

## Camera

The camera represent the view of the user. The camera is already created in the client object, but we can move it :
```python
from ESG_Engine.client.client_core import Client


class MyClient(Client):
    def __init__(self):
        super().__init__((500, 500))
        self.camera.x = 50
        self.camera.y = 100
```

A better way to move the camera is to modify `camera.target_x` and `camera.target_y`.
You can configure how smooth is the camera's moving with `camera.smooth = value`.
You can also change `camera.zoom` and `camera.lock` (to lock all the camera's movings).

```python
from ESG_Engine.client.client_core import Client


class MyClient(Client):
    def __init__(self):
        super().__init__((500, 500))
        self.camera.target_x = 50
        self.camera.target_y = 100
```

### Event handler

The player object is a simple Entity object the only difference is that the player's controls are already built-in in the player.
So we need to add the players controls into the event_handler with `self.event_handler.register_event(pygame.KEYDOWN, self.player.player_control_key_down)` and `self.event_handler.register_event(pygame.KEYUP, self.player.player_control_key_up)`.

You can add all the events you want in the event handler. When the event handler call a registered function, it gives the pygame event object to the registered function. So your function should be like this :
```python
import pygame
from ESG_Engine.client.client_core import Client


def my_function(event):
    if event.key == pygame.K_SPACE:
        print("Space pressed !")

client = Client((500, 500))

client.event_handler.register_event(pygame.KEYDOWN, my_function)

# Game loop
```

### Clock

The clock object allows you to automatically get the FPS (number of frames per second) and the delta time (time elapsed between 2 frames).
You can also register function that will be repeated each given delays (in second) :

```python
from ESG_Engine.client.client_core import Client


def print_hello():
    print("hello everyone !")

class MyClient(Client):
    def __init__(self):
        super().__init__((500, 500))
        self.clock.register_task(2., print_hello)
    
    def main_loop(self):
        while True:
            self.tick()
            self.render()
            print("FPS :", self.clock.get_fps(), "Delta time :", self.clock.last_delta)
```
Note that the `self.clock.get_delta()` function return the delta time, but it also resets the delta time. This function is already calles if `core.tick()` so prefer to use `core.clock.last_delta`.

### Particles

You can create and render particles easily using a ParticleEmitter object. This object represent a group of same particles. You can give to it :
- the particle emission area with x, y, width and height
- the maximum speed of a particle (the speed will be between 0 and your given speed)
- the maximum lifetime of a single particle (the lifetime will be between 0 and your given lifetime)
- the texture wich is an animation (the particle annimation will start at a random frame of the animation)

You can also add some extra speed in the direction you want :

```python
from ESG_Engine.client.client_core import Client
from ESG_Engine.client.particles_emitter import ParticlesEmitter
from ESG_Engine.client.animation import Anim

smoke_anim = Anim(0.2)  # Add some frames next to that

client = Client((500, 500))

smoke_emitter = client.particles_emitters_manager.add_particles_emitter(ParticlesEmitter(10, 10, 16, 16, 10, 2, smoke_anim))
smoke_emitter.extra_up = 100
smoke_emitter.extra_right = 50
```

As you can see, we must register our particle into the `client.particles_emitters_manager` object with the `add_particles_emitter()` function (this function return the given particle emitter).

To create particles we need to call the `particle_emitter.create_particles(nbr):` function. The "nbr" argument is the number of particles to create.

### Sound

This Engine allows you to make positionnal sound (The sound volume corresponds to how far you are).
Before playing the sound, you need to register it in the SoundManager object.

```python
from ESG_Engine.client.client_core import Client
import pygame


class MyClient(Client):
    def __init__(self):
        super().__init__((500, 500))
        self.sound_manager.register_sound("incredible_sound", pygame.mixer.Sound("mysound.mp3"))
        
        self.sound_manager.play_sound("incredible_sound", 200, (0, 0), (50, 50))
```

You can also modify the sound's volume during its playing with the `sound_manager.set_sound_volume(sound_name, volume, pos, listening_pos)` function.

## Init the network

To make an online game, you need to init the engine network.

With this engine you can make a server at a very high level.
To create and register a packet you need to give it a name and a schema. The schema is a string wich represent the datas stored in the packet. It's conposed of letters wich represent a data :

s : string
i : int (positive and negative)
ui : unsigned int (only positive)
f : float
b: a byte
ba : a byte array

The letters must be separated by "-".

So if we want a packet that contain a string, an usigned int and a float we must do that like this : `"s-ui-f"`
The packets shema must be **imperatively** the sames client side and server side, and you can't send or receive a packet if it is not registered.

### Init the client side network

Before registering the packets you have to init the client's network with the address and the port :

```python
from ESG_Engine.client.client_core import Client


class MyClient(Client):
    def __init__(self):
        super().__init__((500, 500))
        self.network.init_client("localhost", 9999)
```

To send packets to the server you need to register a new packet :

```python
from ESG_Engine.client.client_core import Client


class MyClient(Client):
    def __init__(self):
        super().__init__((500, 500))
        self.network.init_client(("localhost", 9999))
    
    def register_packets(self):
        self.network.register_new_packet("hello", "s-i-i-f-ba")
        self.network.register_new_packet("another_packet", "i-ui-s-f")
```

To read the incoming packets, you can make a for loop like a pygame event for loop :

```python
from ESG_Engine.client.client_core import Client


class MyClient(Client):
    def __init__(self):
        super().__init__((500, 500))
        self.network.init_client(("localhost", 9999))
        self.register_packets()
    
    def register_packets(self):
        self.network.register_new_packet("hello", "s-i-i-f-ba")
    
    def read_packets(self):
        for packet in self.network.get_packets():
            if packet.name == "hello":
                print("The server say hello !", packet.data)
```

Note that `packet.data` is a list which contain the decoded data.

You can send data to the server using the `client_send_packet_to_server()` function like this :

```python
from ESG_Engine.client.client_core import Client


class MyClient(Client):
    def __init__(self):
        super().__init__((500, 500))
        self.network.init_client(("localhost", 9999))
        self.register_packets()
    
    def register_packets(self):
        self.network.register_new_packet("hello", "s")
    
    def say_hello_to_server(self):
        self.network.client_send_packet_to_server("hello", ["Hello the server !"])  # data argument must be a list
    
```

To close the client's network you just have to call the `client.network.close()` function.

### Create a server and init the server side network

#### Create a server
The way to create a server and init its network is almost the same way that creating a client.

You can simply create a server by inheriting your class from the `Server` class or, like the client, create a `Client` object.

The network functions are almost the same.

```python
from ESG_Engine.server.server_core import Server


class MyServer(Server):
    def __init__(self):
        super().__init__()
        self.network.init_server(9999)
        self.register_packets()
    
    def register_packets(self):
        self.network.register_new_packet("hello", "s")
```

Note that the `network.init_server()` take only the `port` argument and doesn't need an adress.

The packet registering is the same as the client but the packet sending is not. You only can send a packet to the client.

```python
from ESG_Engine.server.server_core import Server


class MyServer(Server):
    def __init__(self):
        super().__init__()
        self.network.init_server(9999)
        self.register_packets()
    
    def register_packets(self):
        self.network.register_new_packet("hello", "s")
    
    def server_loop(self):
        while True:
            self.tick()
            for client in self.network.get_clients():
                self.network.server_send_packet_to_client(client, "hello", ["Hello client !!"])
```

As you can see, the `server.network.get_clients()` function return the list of connected clients and `server.network.server_send_packet_to_client(client, packet_name, data)` allows you to send a packet to the client.

#### Network Events

When a client disconnects or connects, you can catch a network event. The `self.network.get_events()` return the list of all the events.

```python
from ESG_Engine.server.server_core import Server
from ESG_Engine.core.constants import *


class MyServer(Server):
    def __init__(self):
        super().__init__()
        self.network.init_server(9999)
        self.register_packets()
    
    def register_packets(self):
        self.network.register_new_packet("hello", "s")
    
    def server_loop(self):
        while True:
            self.tick()
            for event in self.network.get_events():
                if event.type == CLIENT_QUIT_EVENT:
                    print("client", event.client.id, "disconected !")
```

When a client quit event or a client connection event is created it also contain the `client` key that is a `ServerClient`.

To close the server, just call the `server.close()` function.

If you have some problems you can go to my support Discord server : https://discord.gg/acursxkUj8

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/yannis300307/ES_Engine",
    "name": "ESG-Engine",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "python,Game Engine,Pygame",
    "author": "Yannis300307",
    "author_email": "",
    "download_url": "https://files.pythonhosted.org/packages/ce/0e/30370354dbe0e322e6014d5c710088a62a8f436ef214c6a2c232a325fccd/ESG%20Engine-0.1.7.tar.gz",
    "platform": null,
    "description": "# ESG Engine\r\nESG Engine (Extremely Simple Game Engine) is a little Pygame based game engine which allow you to create online (or not) games really quikly and easily.\r\nYou can create a server with very few lines.\r\n\r\n## How to make a client ?\r\n\r\n### Create a client\r\n\r\nYou can create a client by inherit a class from the core.client.client_core.Client class, or you can simply create a Client object:\r\n\r\nExample :\r\n```python\r\nfrom ESG_Engine.client.client_core import Client\r\n\r\n\r\nclass MyClient(Client):\r\n    def __init__(self):\r\n        super().__init__((500, 500))\r\n```\r\n\r\nor without classes :\r\n\r\n```python\r\nfrom ESG_Engine.client.client_core import Client\r\n\r\n\r\nclient = Client((500, 500))\r\n```\r\n\r\n### Game loop\r\n\r\nNext, you have to create a game loop which contain the client.tick() function and the client.render() function.\r\n\r\n```python\r\nfrom ESG_Engine.client.client_core import Client\r\n\r\n\r\nclass MyClient(Client):\r\n    def __init__(self):\r\n        super().__init__((500, 500))\r\n    \r\n    def main_loop(self):\r\n        while True:\r\n            self.tick()\r\n            self.render()\r\n```\r\n\r\nBefore starting your game you need to import the map, the bg (optionnal) and the tileset.\r\n\r\n### Import a map, backgrounds and tiles\r\n\r\nThe map json is made of \"width\" key, a \"height\" key and a \"layers\" key which is a list that contains the tiles (0 is nothing)\r\n\r\nExample for a 3 x 3 empty map :\r\n\r\n```json\r\n{\r\n  \"width\" : 3,\r\n  \"height\" : 3,\r\n  \"layers\" : [\r\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\r\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\r\n    [0, 0, 0, 0, 0, 0, 0, 0, 0]\r\n  ]\r\n}\r\n```\r\n\r\nNote that the first layer is behind the player, the third is in front of the player and the second is used for collisions.\r\n\r\nYou can import the map with the `client.map.load_from_json(json)` function.\r\n\r\n\r\nYou can register the tileset using the `client.renderer.register_tile(tile_image)` function in a for loop for example.\r\n\r\nFinally, you can register the backgrounds using `register_background(parralax, background_image)`. The parralax argument is how far is the background (-1 is no parralax).\r\n\r\nSo you can import all of that like this :\r\n\r\n```python\r\nfrom ESG_Engine.client.client_core import Client\r\nimport pygame\r\n\r\n\r\nclass MyClient(Client):\r\n    def __init__(self):\r\n        super().__init__((500, 500))\r\n    \r\n    def load_map(self):\r\n        with open(\"my_map.json\") as file:\r\n            self.map.load_from_json(file.read())\r\n            file.close()\r\n    \r\n    def load_tiles(self):\r\n        for i in range(64):\r\n            self.renderer.register_tile(pygame.image.load(\"tile\" + str(i) + \".png\"))\r\n    \r\n    def load_backgrounds(self):\r\n        self.renderer.register_background(-1, pygame.image.load(\"background 1.png\"))\r\n        self.renderer.register_background(2, pygame.image.load(\"background 2.png\"))\r\n        self.renderer.register_background(6, pygame.image.load(\"background 3.png\"))\r\n```\r\n\r\n### Create a player\r\n\r\nWe can now add a player, and we need to register it:\r\n\r\n```python\r\nimport pygame\r\nfrom ESG_Engine.client.client_core import Client\r\nfrom ESG_Engine.client.player import Player\r\n\r\n\r\nclass MyClient(Client):\r\n    def __init__(self):\r\n        super().__init__((500, 500))\r\n        self.player: Player = self.entity_manager.add_entity(Player(50, 200, 30, 175))\r\n        self.player.set_collider_points([(0, 0), (16, 16), (0, 16), (16, 0)])\r\n        \r\n        self.event_handler.register_event(pygame.KEYDOWN, self.player.player_control_key_down)\r\n        self.event_handler.register_event(pygame.KEYUP, self.player.player_control_key_up)\r\n```\r\n\r\nNote that the `add_entity()` function return the given entity.\r\n\r\nThe last 2 lines are for event handler. See the next section for more explanation.\r\n\r\nTo add an animation to the player (or Entity), you need a `Anim` object wich contain all the frames of the annimation and bind it to the player into the renderer:\r\n\r\n```python\r\nimport pygame\r\nfrom ESG_Engine.client.client_core import Client\r\nfrom ESG_Engine.client.player import Player\r\nfrom ESG_Engine.client.animation import Anim\r\n\r\n\r\nclass MyClient(Client):\r\n    def __init__(self):\r\n        super().__init__((500, 500))\r\n        self.player: Player = self.entity_manager.add_entity(Player(50, 200, 30, 175))\r\n        self.player_anim_1 = Anim(0.4)  # take the time between 2 frames\r\n        self.renderer.bind_anim(self.player, self.player_anim_1)\r\n    \r\n    def load_animations(self):\r\n        for i in range(4):\r\n            self.player_anim_1.add_frame(pygame.image.load(\"player_anim_\" + str(i) + \".png\"))\r\n```\r\n\r\n## Camera\r\n\r\nThe camera represent the view of the user. The camera is already created in the client object, but we can move it :\r\n```python\r\nfrom ESG_Engine.client.client_core import Client\r\n\r\n\r\nclass MyClient(Client):\r\n    def __init__(self):\r\n        super().__init__((500, 500))\r\n        self.camera.x = 50\r\n        self.camera.y = 100\r\n```\r\n\r\nA better way to move the camera is to modify `camera.target_x` and `camera.target_y`.\r\nYou can configure how smooth is the camera's moving with `camera.smooth = value`.\r\nYou can also change `camera.zoom` and `camera.lock` (to lock all the camera's movings).\r\n\r\n```python\r\nfrom ESG_Engine.client.client_core import Client\r\n\r\n\r\nclass MyClient(Client):\r\n    def __init__(self):\r\n        super().__init__((500, 500))\r\n        self.camera.target_x = 50\r\n        self.camera.target_y = 100\r\n```\r\n\r\n### Event handler\r\n\r\nThe player object is a simple Entity object the only difference is that the player's controls are already built-in in the player.\r\nSo we need to add the players controls into the event_handler with `self.event_handler.register_event(pygame.KEYDOWN, self.player.player_control_key_down)` and `self.event_handler.register_event(pygame.KEYUP, self.player.player_control_key_up)`.\r\n\r\nYou can add all the events you want in the event handler. When the event handler call a registered function, it gives the pygame event object to the registered function. So your function should be like this :\r\n```python\r\nimport pygame\r\nfrom ESG_Engine.client.client_core import Client\r\n\r\n\r\ndef my_function(event):\r\n    if event.key == pygame.K_SPACE:\r\n        print(\"Space pressed !\")\r\n\r\nclient = Client((500, 500))\r\n\r\nclient.event_handler.register_event(pygame.KEYDOWN, my_function)\r\n\r\n# Game loop\r\n```\r\n\r\n### Clock\r\n\r\nThe clock object allows you to automatically get the FPS (number of frames per second) and the delta time (time elapsed between 2 frames).\r\nYou can also register function that will be repeated each given delays (in second) :\r\n\r\n```python\r\nfrom ESG_Engine.client.client_core import Client\r\n\r\n\r\ndef print_hello():\r\n    print(\"hello everyone !\")\r\n\r\nclass MyClient(Client):\r\n    def __init__(self):\r\n        super().__init__((500, 500))\r\n        self.clock.register_task(2., print_hello)\r\n    \r\n    def main_loop(self):\r\n        while True:\r\n            self.tick()\r\n            self.render()\r\n            print(\"FPS :\", self.clock.get_fps(), \"Delta time :\", self.clock.last_delta)\r\n```\r\nNote that the `self.clock.get_delta()` function return the delta time, but it also resets the delta time. This function is already calles if `core.tick()` so prefer to use `core.clock.last_delta`.\r\n\r\n### Particles\r\n\r\nYou can create and render particles easily using a ParticleEmitter object. This object represent a group of same particles. You can give to it :\r\n- the particle emission area with x, y, width and height\r\n- the maximum speed of a particle (the speed will be between 0 and your given speed)\r\n- the maximum lifetime of a single particle (the lifetime will be between 0 and your given lifetime)\r\n- the texture wich is an animation (the particle annimation will start at a random frame of the animation)\r\n\r\nYou can also add some extra speed in the direction you want :\r\n\r\n```python\r\nfrom ESG_Engine.client.client_core import Client\r\nfrom ESG_Engine.client.particles_emitter import ParticlesEmitter\r\nfrom ESG_Engine.client.animation import Anim\r\n\r\nsmoke_anim = Anim(0.2)  # Add some frames next to that\r\n\r\nclient = Client((500, 500))\r\n\r\nsmoke_emitter = client.particles_emitters_manager.add_particles_emitter(ParticlesEmitter(10, 10, 16, 16, 10, 2, smoke_anim))\r\nsmoke_emitter.extra_up = 100\r\nsmoke_emitter.extra_right = 50\r\n```\r\n\r\nAs you can see, we must register our particle into the `client.particles_emitters_manager` object with the `add_particles_emitter()` function (this function return the given particle emitter).\r\n\r\nTo create particles we need to call the `particle_emitter.create_particles(nbr):` function. The \"nbr\" argument is the number of particles to create.\r\n\r\n### Sound\r\n\r\nThis Engine allows you to make positionnal sound (The sound volume corresponds to how far you are).\r\nBefore playing the sound, you need to register it in the SoundManager object.\r\n\r\n```python\r\nfrom ESG_Engine.client.client_core import Client\r\nimport pygame\r\n\r\n\r\nclass MyClient(Client):\r\n    def __init__(self):\r\n        super().__init__((500, 500))\r\n        self.sound_manager.register_sound(\"incredible_sound\", pygame.mixer.Sound(\"mysound.mp3\"))\r\n        \r\n        self.sound_manager.play_sound(\"incredible_sound\", 200, (0, 0), (50, 50))\r\n```\r\n\r\nYou can also modify the sound's volume during its playing with the `sound_manager.set_sound_volume(sound_name, volume, pos, listening_pos)` function.\r\n\r\n## Init the network\r\n\r\nTo make an online game, you need to init the engine network.\r\n\r\nWith this engine you can make a server at a very high level.\r\nTo create and register a packet you need to give it a name and a schema. The schema is a string wich represent the datas stored in the packet. It's conposed of letters wich represent a data :\r\n\r\ns : string\r\ni : int (positive and negative)\r\nui : unsigned int (only positive)\r\nf : float\r\nb: a byte\r\nba : a byte array\r\n\r\nThe letters must be separated by \"-\".\r\n\r\nSo if we want a packet that contain a string, an usigned int and a float we must do that like this : `\"s-ui-f\"`\r\nThe packets shema must be **imperatively** the sames client side and server side, and you can't send or receive a packet if it is not registered.\r\n\r\n### Init the client side network\r\n\r\nBefore registering the packets you have to init the client's network with the address and the port :\r\n\r\n```python\r\nfrom ESG_Engine.client.client_core import Client\r\n\r\n\r\nclass MyClient(Client):\r\n    def __init__(self):\r\n        super().__init__((500, 500))\r\n        self.network.init_client(\"localhost\", 9999)\r\n```\r\n\r\nTo send packets to the server you need to register a new packet :\r\n\r\n```python\r\nfrom ESG_Engine.client.client_core import Client\r\n\r\n\r\nclass MyClient(Client):\r\n    def __init__(self):\r\n        super().__init__((500, 500))\r\n        self.network.init_client((\"localhost\", 9999))\r\n    \r\n    def register_packets(self):\r\n        self.network.register_new_packet(\"hello\", \"s-i-i-f-ba\")\r\n        self.network.register_new_packet(\"another_packet\", \"i-ui-s-f\")\r\n```\r\n\r\nTo read the incoming packets, you can make a for loop like a pygame event for loop :\r\n\r\n```python\r\nfrom ESG_Engine.client.client_core import Client\r\n\r\n\r\nclass MyClient(Client):\r\n    def __init__(self):\r\n        super().__init__((500, 500))\r\n        self.network.init_client((\"localhost\", 9999))\r\n        self.register_packets()\r\n    \r\n    def register_packets(self):\r\n        self.network.register_new_packet(\"hello\", \"s-i-i-f-ba\")\r\n    \r\n    def read_packets(self):\r\n        for packet in self.network.get_packets():\r\n            if packet.name == \"hello\":\r\n                print(\"The server say hello !\", packet.data)\r\n```\r\n\r\nNote that `packet.data` is a list which contain the decoded data.\r\n\r\nYou can send data to the server using the `client_send_packet_to_server()` function like this :\r\n\r\n```python\r\nfrom ESG_Engine.client.client_core import Client\r\n\r\n\r\nclass MyClient(Client):\r\n    def __init__(self):\r\n        super().__init__((500, 500))\r\n        self.network.init_client((\"localhost\", 9999))\r\n        self.register_packets()\r\n    \r\n    def register_packets(self):\r\n        self.network.register_new_packet(\"hello\", \"s\")\r\n    \r\n    def say_hello_to_server(self):\r\n        self.network.client_send_packet_to_server(\"hello\", [\"Hello the server !\"])  # data argument must be a list\r\n    \r\n```\r\n\r\nTo close the client's network you just have to call the `client.network.close()` function.\r\n\r\n### Create a server and init the server side network\r\n\r\n#### Create a server\r\nThe way to create a server and init its network is almost the same way that creating a client.\r\n\r\nYou can simply create a server by inheriting your class from the `Server` class or, like the client, create a `Client` object.\r\n\r\nThe network functions are almost the same.\r\n\r\n```python\r\nfrom ESG_Engine.server.server_core import Server\r\n\r\n\r\nclass MyServer(Server):\r\n    def __init__(self):\r\n        super().__init__()\r\n        self.network.init_server(9999)\r\n        self.register_packets()\r\n    \r\n    def register_packets(self):\r\n        self.network.register_new_packet(\"hello\", \"s\")\r\n```\r\n\r\nNote that the `network.init_server()` take only the `port` argument and doesn't need an adress.\r\n\r\nThe packet registering is the same as the client but the packet sending is not. You only can send a packet to the client.\r\n\r\n```python\r\nfrom ESG_Engine.server.server_core import Server\r\n\r\n\r\nclass MyServer(Server):\r\n    def __init__(self):\r\n        super().__init__()\r\n        self.network.init_server(9999)\r\n        self.register_packets()\r\n    \r\n    def register_packets(self):\r\n        self.network.register_new_packet(\"hello\", \"s\")\r\n    \r\n    def server_loop(self):\r\n        while True:\r\n            self.tick()\r\n            for client in self.network.get_clients():\r\n                self.network.server_send_packet_to_client(client, \"hello\", [\"Hello client !!\"])\r\n```\r\n\r\nAs you can see, the `server.network.get_clients()` function return the list of connected clients and `server.network.server_send_packet_to_client(client, packet_name, data)` allows you to send a packet to the client.\r\n\r\n#### Network Events\r\n\r\nWhen a client disconnects or connects, you can catch a network event. The `self.network.get_events()` return the list of all the events.\r\n\r\n```python\r\nfrom ESG_Engine.server.server_core import Server\r\nfrom ESG_Engine.core.constants import *\r\n\r\n\r\nclass MyServer(Server):\r\n    def __init__(self):\r\n        super().__init__()\r\n        self.network.init_server(9999)\r\n        self.register_packets()\r\n    \r\n    def register_packets(self):\r\n        self.network.register_new_packet(\"hello\", \"s\")\r\n    \r\n    def server_loop(self):\r\n        while True:\r\n            self.tick()\r\n            for event in self.network.get_events():\r\n                if event.type == CLIENT_QUIT_EVENT:\r\n                    print(\"client\", event.client.id, \"disconected !\")\r\n```\r\n\r\nWhen a client quit event or a client connection event is created it also contain the `client` key that is a `ServerClient`.\r\n\r\nTo close the server, just call the `server.close()` function.\r\n\r\nIf you have some problems you can go to my support Discord server : https://discord.gg/acursxkUj8\r\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A little online game engine.",
    "version": "0.1.7",
    "split_keywords": [
        "python",
        "game engine",
        "pygame"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "ec2bd3a9d0e8e5a08f9c92656f66864b",
                "sha256": "cbc5ba044d016a8aeb25342fe008796d4449a3e9cd2d7da8d91b0b2d12e26cc6"
            },
            "downloads": -1,
            "filename": "ESG_Engine-0.1.7-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "ec2bd3a9d0e8e5a08f9c92656f66864b",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 21074,
            "upload_time": "2023-01-02T14:50:27",
            "upload_time_iso_8601": "2023-01-02T14:50:27.260992Z",
            "url": "https://files.pythonhosted.org/packages/5b/77/7dfefd4f4c58163c66728ecfdf266354db6980a0fb83f90d64379a7ce9bd/ESG_Engine-0.1.7-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "md5": "b598623f051f76f7bf1f3a6dd58babe4",
                "sha256": "bd5b1fd6ba962d4650671cdf4b1f2ed2c9b561bc6d28908dc436c89b631fdfa1"
            },
            "downloads": -1,
            "filename": "ESG Engine-0.1.7.tar.gz",
            "has_sig": false,
            "md5_digest": "b598623f051f76f7bf1f3a6dd58babe4",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 15317,
            "upload_time": "2023-01-02T14:50:29",
            "upload_time_iso_8601": "2023-01-02T14:50:29.281781Z",
            "url": "https://files.pythonhosted.org/packages/ce/0e/30370354dbe0e322e6014d5c710088a62a8f436ef214c6a2c232a325fccd/ESG%20Engine-0.1.7.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-01-02 14:50:29",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "yannis300307",
    "github_project": "ES_Engine",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "esg-engine"
}
        
Elapsed time: 0.02321s