From 1e27e28a3942db902fd15add5e6bd3623bdacffd Mon Sep 17 00:00:00 2001 From: Suwako Moriya Date: Mon, 27 Jan 2020 16:21:31 +0100 Subject: [PATCH] Initial Commit --- client.py | 48 ++++++++++++++++++ intro_ball.gif | Bin 0 -> 983 bytes main.py | 87 ++++++++++++++++++++++++++++++++ server.py | 132 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 267 insertions(+) create mode 100755 client.py create mode 100644 intro_ball.gif create mode 100755 main.py create mode 100755 server.py diff --git a/client.py b/client.py new file mode 100755 index 0000000..4ac2bf9 --- /dev/null +++ b/client.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Created on Sun Jan 26 17:50:16 2020 + +@author: suwako +""" + +import sys, pygame, time, random, socket +from colorama import Fore, Style +import numpy as np + + +def debug(*s): print("[D ] : " + + str(', '.join(map(str, s))) + + Style.RESET_ALL) + + +def info(*s): print(Fore.GREEN + + Fore.LIGHTGREEN_EX + + "[I -] : " + + Style.RESET_ALL + + str(', '.join(map(str, s))) + + Style.RESET_ALL) + + +def warn(*s): print(Fore.MAGENTA + + "[W ~] : " + + Style.RESET_ALL + + str(', '.join(map(str, s))) + + Style.RESET_ALL) + + +def error(*s): print(Fore.RED + + Fore.LIGHTRED_EX + + "[E !] : " + + Style.RESET_ALL + + str(', '.join(map(str, s))) + + Style.RESET_ALL) + +if __name__ == "__main__": + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.connect(("localhost", 9000)) + data = b"some data" + sock.sendall(data) + result = sock.recv(1024) + print(result) + sock.close() diff --git a/intro_ball.gif b/intro_ball.gif new file mode 100644 index 0000000000000000000000000000000000000000..7e91ff7a1227642c9c445624b3c3272e1fe4fa77 GIT binary patch literal 983 zcmeHGO-oim6uwCdG1|Os#B5=i^h!a{2VKgjw-5CS#d~Yx<)x6Tpxj*DO*9aJvr)#N zoFHMKjAmglVB`c9Lxee$$bc<|K$t!@vnUw)fJN5HrpilS^v_hssvD4Sa~8kQ)Vap2k{hW4+BZQ)L)wWnOG} zTqD{l>sqPO+$U_zl+s#C84wAC#>#};3J4-$BVnpUNWle0LTSkvXH+plDXmc@rSQPY z63#=+mDh0AL1ZEf7m_KBh37Wo%6h-lg=VG9N?t2jRPwk~sF1+XxL~p4nWK^8xf95e zNONp(Bn45prWlkIC=^K)`o@xEj1r8oz^Ooq!m)rM!H|QH;he#Uz>MZ0Mi@efvl_`f z2M7XyFa{Y)Ljsa4&ru!&0CET^Oq>##Fjh+|si6ttegL2pRU8_cG9j2$j3PmRCb40_ z7t>lv1kH=V5!SS8P z;pqH><*D(m%9Z@_E;-rWb8Yqg{^2vxo(u8wzId*GpgX-Ze6>FE`f&HYr>95jyDOc& z*FQJL)<(|bv74QPZ3jozU~j!?`atzurSsIi@5g2<{gp>Ex7*()^`VQ=iI>UpRQ%=S zOm+QX&%4F(!6%LS%I{e@zdms&UD+7@`Ss(AEtgxmqPw34ZcPu=^!&FE>B^n0W_9)1 JkEI*I(LcqqufzZV literal 0 HcmV?d00001 diff --git a/main.py b/main.py new file mode 100755 index 0000000..26572b0 --- /dev/null +++ b/main.py @@ -0,0 +1,87 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Created on Sat Jan 25 20:54:34 2020 + +@author: suwako +""" + +import sys, pygame, time, random +import numpy as np + + +pygame.init() +black=(0,0,0) +size = width, height = 1366, 720 +TICK_RATE=60 +camera=np.array([0.,0.]) +origin=camera=np.array([0.,0.]) +follow=0 +screen = pygame.display.set_mode(size) + +class satellite(pygame.sprite.Sprite): + def __init__(self): + pygame.sprite.Sprite.__init__(self) + self.image = pygame.image.load("intro_ball.gif").convert_alpha() + self.position = self.image.get_rect() + self.speed=np.array([0.,0.]) + self.pos = np.array([0.,0.]) + self.mass=1 + self.G=0.0000001 + def tick(self, camera=np.array([0.,0.]), origin=np.array([0.,0.])): + self.pos += self.speed + def apply(self, camera=np.array([0.,0.]), origin=np.array([0.,0.])): + self.position.x, self.position.y = map(int, self.pos-camera-origin) + def apply_gravity(self, elements): + for element in elements: + if element != self: + d=(element.pos - self.pos ) + u=d/np.linalg.norm(d) + norm = self.G * element.mass*(d[0]**2+d[1]**2) + self.speed+=norm*u +premier = satellite() +#premier.position.center = (width/2, height/2) +tick=(time.time(), time.time(), 0) +def clip(val, minval, maxval): + return min(max(val, minval), maxval) +elements=[satellite() for _ in range(3)] +for element in elements: + element.pos=np.array([random.randint(0,width), random.randint(0,height)], dtype='float64') + element.speed=np.array([random.randint(0,50), random.randint(0,50)], dtype='float64') +elements[1].pos= np.array([float(width/2), float(height/2)]) +elements[0].pos= np.array([float(width/4), float(height/4)]) +elements[0].speed[1]=10 +elements[1].mass=100 +elements[2].mass=30 +keys=set() +while 1: + dtick=tick + tick=(tick[0], time.time(), int((time.time() - tick[0])*TICK_RATE)) + dtick=tick[2] - dtick[2] + if dtick>2: + print(dtick) + for _ in range(dtick): + for event in pygame.event.get(): + #print(event.type) + if event.type == pygame.QUIT: sys.exit() + if event.type == pygame.KEYDOWN: + keys.update({event.key}) + print(event.key) + if event.key == 102: + follow = (follow + 1)%len(elements) + if event.type == pygame.KEYUP: + if event.key in keys: + keys.remove(event.key) + screen.fill(black) + for key in keys: + if key in [275,276,273,274]: + exec("camera" + ("[0]+","[0]-","[1]-","[1]+")[[275,276,273,274].index(key)] + "=30") + for element in elements: + element.apply_gravity(elements) + element.tick(camera=camera) + origin=elements[follow].pos + for element in elements: + element.apply(camera=camera, origin=origin) + screen.blit(element.image, element.position) + pygame.display.flip() + time.sleep(0.001) diff --git a/server.py b/server.py new file mode 100755 index 0000000..a54ed76 --- /dev/null +++ b/server.py @@ -0,0 +1,132 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Created on Sun Jan 26 17:50:25 2020 + +@author: suwako +""" + +import sys, multiprocessing, socket, time +if sys.platform == 'win32': + import multiprocessing.reduction +from colorama import Fore, Style +import numpy as np + + +def debug(*s): print("[D ] : " + + str(', '.join(map(str, s))) + + Style.RESET_ALL) + + +def info(*s): print(Fore.GREEN + + Fore.LIGHTGREEN_EX + + "[I -] : " + + Style.RESET_ALL + + str(', '.join(map(str, s))) + + Style.RESET_ALL) + + +def warn(*s): print(Fore.MAGENTA + + "[W ~] : " + + Style.RESET_ALL + + str(', '.join(map(str, s))) + + Style.RESET_ALL) + + +def error(*s): print(Fore.RED + + Fore.LIGHTRED_EX + + "[E !] : " + + Style.RESET_ALL + + str(', '.join(map(str, s))) + + Style.RESET_ALL) + +level = 1 +for i in range(level): + exec("debug info warn error".split()[i] + "= lambda *x: None") +class Satellite(): + def __init__(self, pos=np.array([0., 0.]), + speed=np.array([0., 0.]), + mass=1): + self.pos = pos + self.speed = speed + self.mass = mass + self.G = 0.0000001 + + def tick(self): + self.pos += self.speed + + def apply_gravity(self, elements): + for element in elements: + if element.pos != self.pos: + d = element.pos - self.pos + u = d/np.linalg.norm(d) + norm = self.G * element.mass * (d[0]**2 + d[1]**2) + self.speed += norm*u + + +class Player(): + pass + + +class Map(): + def __init__(self): + self.elements = [] + + +def handle(connection, address): + try: + info("Connected at "+ ':'.join(map(str,address))) + while True: + data = connection.recv(1024) + if data == b"": + info("Socket closed remotely at " + ':'.join(map(str,address))) + break + debug("Received data %r" % data) + connection.sendall(data) + debug("Sent data") + except: + warn("Problem handling request") + connection.close() + raise + finally: + debug("Closing socket with " + ':'.join(map(str,address))) + connection.close() + +class Server(object): + def __init__(self, hostname, port): + self.hostname = hostname + self.port = port + + def start(self): + debug("listening") + self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + self.socket.bind((self.hostname, self.port)) + self.socket.listen(1) + + while True: + conn, address = self.socket.accept() + process = multiprocessing.Process(target=handle, + args=(conn, address)) + process.daemon = True + process.start() + + +if __name__ == "__main__": + server = Server("0.0.0.0", 9000) + try: + info("Listening") + server.start() + except: + error("Unexpected exception") + for process in multiprocessing.active_children(): + info("Shutting down process %r", process) + process.terminate() + process.join() + raise + finally: + info("Shutting down") + for process in multiprocessing.active_children(): + info("Shutting down process %r", process) + process.terminate() + process.join() + info("All done")