A simple to use API for integration between your Pyxel games with servers.
Project description
Pyxel Server
A simple to use API for integration between your Pyxel games with servers.
Install
pip install pyxel-server
or
https://pypi.org/project/pyxel-server/
Example
Code
client.py
import pyxel
from pyxel_server import client
class App:
def __init__(self):
self.address = input("Server Address: ")
self.port = input("Server Port: ")
self.clientPort = input("Client Port: ")
self.username = input("Choose Username: ")
self.Updating = False
self.points = 0
# Connects to server and runs pyxel app
self.client = client.run(self.address, self.port, False, "127.0.0.1", self.clientPort)
self.client.connect(self.username)
self.client.appinfo()
pyxel.init(self.client.width, self.client.height, caption=self.username, fps=self.client.fps, quit_key=pyxel.KEY_F2)
pyxel.run(self.update, self.draw)
def update(self):
self.Updating = True
# Custom Quit Key, Force quit is F2
if pyxel.btnr(pyxel.KEY_ESCAPE):
self.client.disconnect()
pyxel.quit()
# Checks if button is pressed and sends to server
self.client.btnp(pyxel.KEY_SPACE)
# Every half second it will get the user's points
if pyxel.frame_count % round(pyxel.DEFAULT_FPS / 2) == 0:
self.points = self.client.getLocalVar("points")
self.Updating = False
def draw(self):
if not self.Updating:
# Clear screen
pyxel.cls(0)
# Draw score
pyxel.text(0, 0, str(self.points), 10)
# Render all objects
self.client.renderAll()
App()
server.py
from pyxel_server import pyxelobj, server
import pyxel
import random
def update(self):
# If the dot is not activated
if not self.variables.activated:
# Create a new one and send it
self.variables.x = random.randrange(0, 256)
self.variables.y = random.randrange(0, 144)
obj = pyxelobj.obj(pyxelobj.new("dot", self.variables.x, self.variables.y, 0, 0, [[7]]))
self.variables.activated = True
self.sendObj(obj)
# Loops through every single user
for Username, User in self.Users.__getallusers__().items():
# If user pressed space
if User.input.get(str(pyxel.KEY_SPACE)) and self.variables.visible:
# Add 1 point to user
User.variables.points += 1
# Set the dot to be not activated
self.variables.pressed = False
User.input[str(pyxel.KEY_SPACE)] = False
LocalVariables = {
"points": 0
}
GlobalVariables = {
"pressed": False,
"x": random.randrange(0, 256),
"y": random.randrange(0, 144)
}
server.run("127.0.0.1", "5000", 256, 144, 12, update,LocalVariables=LocalVariables, GlobalVariables=GlobalVariables)
What will happen
This is a game of who pressed space first when the dot apears.
More
Contributing
Issues and Suggestions
Submit the issue or suggestion using the issue tracker, make sure it has not been repeated.
Issues
It should include the os, error, python modules used, python version and other relevant information.
Suggestions
It should include sample usages, mock-ups and other relevant information.
Patches, Feature implementations, and Optimizations
Please submit it in a pull request, issues should be listed if it is a fix.
Note: All pull requests submitted are licensed under the MIT License
Reference
Note: pyxel_server's intended features are not fully implemented yet.
server
System
run(Host, Port, AppWidth, AppHeight, AppFPS, UpdateScript, [WebScript], [InitScript], [Variables])
Initializes the server and runs it.
Host: The ip or domain of the server. e.g.Host="127.0.0.1"
Port: The port to be opened in theHost. e.g.Port="5000"
AppWidth: The width of the client's window when connected. e.g.AppWidth=256
AppHeight: The height of the client's window when connected. e.g.AppHeight=144
AppFPS: The FPS of the client's window when connected. e.g.AppFPS=24
UpdateScript: The function to run every 1/AppFPS. e.g.UpdateScript=update
Note: The function must have the parameterself.
WebScript: The custom flask events and routes. e.g.WebScript=web
Note: The function must have the parameterself&app.
InitScript: The custom initialization function that will be called whenserver()is called. e.g.InitScript=init
Note: The function must have the parameterself.
Variables: A dictionary of variables needed. e.g.Variables={"Name": "Value"}
Connection
sendObj(obj)
Converts obj to a json format and sends to all of the clients connected.
obj: Thepyxelobj.obje.g.obj=pyxelobj.obj()
Data
variables
A class of all of the global variables.
Note: The class will not be created if no global variables are specified inself.run()
e.g.
variables.Name = "Value"
Users
Users.__getuser__(user)
Returns the User's class.
user: The name of the user e.g.user="Name"Users.__getallusers__()
Returns all of the Users in a dictionary in a format like this:{"Name": <Class>, "Name": <Class>, ...}
User
User.variablesA class of all of the user's variables.User.keyThe user's key.User.input
A dictionary of all of the keys pressed in a format like this:{"Button Number": Bool, "Button Number": Bool, ...}
Note: In the client, you must usebtnp(button)forUser.inputto update.
client
Connection
run(Host, Port)
Initializes the client with necessary information.
Host: The ip or domain of the server. e.g.Host="127.0.0.1"
Port: The port to be opened in theHost. e.g.Port="5000"
Note: You must run this command before anything that needs to use theclient.connect(Username)
Connects the client to the necessary information.
User: The username e.g.User="Name"
Note: You must run this command afterrunbefore using anything from theclient.request.post(Route, json)
Posts data to a specified route and returns json back.
Route: The path to post e.g.Route="/var"
json: The json to post to theRoutee.g.json={"Name": "Value"}
Data
getGlobalvar(Variable, [Value])
Returns & optionaly changes a global variable from the server.
Variable: The variable name e.g.Variable="Name"
Value: The value of variable e.g.Value="Value"
Note: The variable will be changed before it returns.getLocalvar(Variable, [Value])
Returns & optionaly changes a local variable only accessible to the client from the server.
Variable: The variable name e.g.Variable="Name"
Value: The value of variable e.g.Value="Value"
Note: The variable will be changed before it returns.
Input
btnp(button)
Checks for a button press (see pyxel documentation) then sends result to the server and returns result
button: The button to press e.g.button=pyxel.KEY_SPACEbtnr(button)
Checks for a button release (see pyxel documentation) then sends result to the server and returns result
button: The button to press e.g.button=pyxel.KEY_SPACE
Objects
renderAll()
Renders all of the objects inclient.objectsaddObject(obj)
Adds an object toclient.objects
obj: Thepyxelobj.obje.g.obj=pyxelobj.obj()addObject(objname)
Removes an object fromclient.objectspredict(obj)
Predicts an object's next location by addingobj.addXtoobj.xandobj.addYtoobj.y
obj: Thepyxelobj.obje.g.obj=pyxelobj.obj(json)
Removes an object fromclient.objects
objname: The name of the object to remove e.g.objname="Name"
Used software
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyxel_server-0.0.3.1.tar.gz.
File metadata
- Download URL: pyxel_server-0.0.3.1.tar.gz
- Upload date:
- Size: 11.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1bfed070ed0ef6616c15a765b5380d7b0899d613b7f258d9033ad2c69e5d4390
|
|
| MD5 |
2005b14154509630608ff30aff408fd4
|
|
| BLAKE2b-256 |
942cf13985a250f96fd6aad190d2bacf9dac0516c0b221a4d0ccc875765bc931
|
File details
Details for the file pyxel_server-0.0.3.1-py3-none-any.whl.
File metadata
- Download URL: pyxel_server-0.0.3.1-py3-none-any.whl
- Upload date:
- Size: 10.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9cad168fc0a4c3ee076f542ff4b14f0743e9316f6a6599abb9b181d6decfd4f4
|
|
| MD5 |
8b866b123cf1ee4389d6e0ddc9b1a300
|
|
| BLAKE2b-256 |
b7df71e87a293c0483fd51cabb70352361ebec11dccb7dba2b99edc5a6da4173
|