Skip to content

Instantly share code, notes, and snippets.

@Zwork101
Created March 4, 2018 19:03
Show Gist options
  • Save Zwork101/42eaff6b8aa8e7aed5df5e7ddace28c9 to your computer and use it in GitHub Desktop.
Save Zwork101/42eaff6b8aa8e7aed5df5e7ddace28c9 to your computer and use it in GitHub Desktop.
client for quick-connect example
from json import load
import pygame
from pygame.locals import QUIT, K_UP, K_RIGHT, K_LEFT, K_DOWN
from quicknet.client import QClient
with open('settings.json') as file:
SETTINGS = load(file)
SELF = None
PLAYERS = {}
game_client = QClient(SETTINGS["ip_addr"], SETTINGS["port"])
@game_client.on("WELCOME")
def welcome_client(msg, id, players):
global PLAYERS, SELF
print(msg)
PLAYERS = players
SELF = id
@game_client.on("PLAYER_MOVED")
def move_player(player_name, x, y):
PLAYERS[player_name][0] = x
PLAYERS[player_name][1] = y
@game_client.on("NEW_PLAYER")
def new_player(player_name):
print("New Player!", player_name)
x, y = SETTINGS["window_size"][0] / 2, SETTINGS["window_size"][1] / 2
PLAYERS[player_name] = [x, y]
pygame.init()
screen = pygame.display.set_mode(SETTINGS["window_size"])
pygame.display.set_caption("My ONLINE game!")
clock = pygame.time.Clock()
game_client.run()
while True:
for event in pygame.event.get():
print(event)
if event.type == QUIT:
game_client.quit()
break
elif event.type == K_UP:
game_client.call("MOVE", "UP")
elif event.type == K_LEFT:
game_client.call("MOVE", "LEFT")
elif event.type == K_RIGHT:
game_client.call("MOVE", "RIGHT")
elif event.type == K_DOWN:
game_client.call("MOVE", "DOWN")
screen.fill((0, 0, 0)) # Clear the screen
for player, rect in PLAYERS.items():
if player == SELF:
pygame.draw.rect(screen, (100, 100, 100), pygame.Rect(x=rect[0], y=rect[1], width=50, height=50))
else:
pygame.draw.rect(screen, (200, 100, 200), pygame.Rect(x=rect[0], y=rect[1], width=50, height=50))
print("Reached end")
pygame.display.flip()
clock.tick(60)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment