Initial Commit
This commit is contained in:
parent
08a70ecf63
commit
1e27e28a39
48
client.py
Executable file
48
client.py
Executable file
@ -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()
|
BIN
intro_ball.gif
Normal file
BIN
intro_ball.gif
Normal file
Binary file not shown.
After Width: | Height: | Size: 983 B |
87
main.py
Executable file
87
main.py
Executable file
@ -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)
|
132
server.py
Executable file
132
server.py
Executable file
@ -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")
|
Loading…
Reference in New Issue
Block a user