def __init__(self,ip,port,max_duration=float("inf")): """Create a body game.""" self.context=Context(fullscreen=False) self.connection=socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.connection.connect((ip,port)) self.connection.setblocking(False) self.to=time.time() self.max_duration=max_duration self.user={"key":random.randint(0,10**10),"alive":True} self.sendMessage(self.user)
"""Set the center of the material segment.""" p=self.getCenter() v=Vector.createFromTwoPoints(np,p) self.points=[v(p) for p in self.points] def getP1(self): """Return the first point.""" return self.points[0] def setP1(self,p1): """Set the first point.""" self.points[0]=p1 def getP2(self): """Return the second point.""" return self.points[1] def setP2(self,p2): """Set the second point.""" self.points[1]=p2 p=center=property(getCenter,setCenter,"Representation of the material center of the segment.") p1=property(getP1,setP1,"Representation of the first material point of the material segment.") p2=property(getP2,setP2,"Representation of the second material point of the material segment.") if __name__=="__main__": from mysurface import Context context=Context() ms=MaterialSegment.random()
class ClientAsteroid(AsteroidGame): def __init__(self,ip,port,max_duration=float("inf")): """Create a body game.""" self.context=Context(fullscreen=False) self.connection=socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.connection.connect((ip,port)) self.connection.setblocking(False) self.to=time.time() self.max_duration=max_duration self.user={"key":random.randint(0,10**10),"alive":True} self.sendMessage(self.user) def receiveMessage(self,client_socket): """Receive a message from a client.""" try: message_header=client_socket.recv(HEADER_LENGTH) if not len(message_header): return None else: message_length=int(message_header.decode("utf-8").strip()) return {"header":message_header,"data":client_socket.recv(message_length)} except: return None def makeMessage(self): def sendMessage(self,dictionary,client_socket): """Send a message to a client.""" message=pickle.dumps(dictionary) header=f"{len(message):<{HEADER_LENGTH}}".encode('utf-8') client_socket.send(header+message) def main(self): """Main loop of the client.""" while self.context.open and time.time()-self.to<self.max_duration: self.receive() self.show() self.send() def send(self): """Send data to the server.""" cursor,shooting,spawn=self.control() dictionary={"cursor":cursor, "shooting":shooting, "spawn":spawn} self.sendMessage(dictionary,self.connection) def receive(self): """Hydrate the client body game.""" try: dictionary=self.receiveMessage(self.connection) self.asteroids=dictionary["asteroids"] self.spaceships=dictionary["spaceships"] self.missiles=dictionary["missiles"] self.user=dictionary["user"] except IOError as e: if e.errno != errno.EAGAIN or e.errno != errno.EWOULDBLOCK: print("Client: Reading error",str(e)) sys.exit() else: print("Client: Other io error",str(e)) sys.exit() except Exception as e: print("Client: General error",str(e)) sys.exit() def show(self): """Show the components on the screen.""" self.context.draw.plane.position=self.spaceships[self.user["key"]] self.context.check() self.context.clear() self.context.show() self.showBodies() self.showInfo() self.context.controlZoom() self.context.flip() def showBodies(self): """Show all the bodies of the game.""" self.showSpaceships() self.showMissiles() self.showAsteroids() def showSpaceships(self): """Show all the spaceships on the context.""" for i in range(len(self.spaceships)): self.spaceships[i].absolute.show(self.context) def showMissiles(self): """Show all the missiles on the context.""" for i in range(len(self.missiles)): self.missiles[i].absolute.show(self.context) def showAsteroids(self): """Show all the asteroids on the context.""" for i in range(len(self.asteroids)): self.asteroids[i].absolute.show(self.context) def showInfo(self): """Show infos about the game.""" pass def control(self): """Control the body.""" keys=self.context.press() if keys[K_SPACE]: shooting=True else: shooting=False cursor=Point(*self.context.point()) spawn=keys[K_r] return (cursor,shooting,spawn) if __name__=="__main__": game=ClientAsteroid(IP,PORT,max_duration=20) game.main() print("The client is done running.")
self.context = context def setPlayer(self, player): """Set the player of the expanse level.""" self.player = player def main(self): """Main loop of the level.""" while self.context.open: self.context.check() self.show() self.update() def update(self): """Update the level.""" pass def show(self): """Show the level.""" self.context.control() self.context.clear() self.context.show() self.map.show(self.context) self.context.flip() if __name__ == "__main__": context = Context(fullscreen=True, size=[1440, 900]) expanse = Expanse(context) expanse.main()
def __init__(self, context=Context()): """Create the game.""" self.context = context #self.player=Case(0,0) self.levels = [ExpanseLevel()] self.stage = 0
y += 30 for i in range(len(self.bodies)): self.context.draw.window.print("body: " + str(self.bodies[i]), (x, y)) y += 30 def getFocus(self): """Return the body being focused.""" if self.focus_index is not None: return self.bodies[self.focus_index] else: return None def setFocus(self, body): """Set the focus using a body.""" if body is not None: if not (body in self.bodies): self.bodies.append(body) self.focus_index = self.bodies.index(body) else: self.focus_index = None focus = property(getFocus, setFocus) if __name__ == "__main__": from mysurface import Context context = Context(fullscreen=True) builder = Builder(context) builder()
bt = t return t def directUpdate(self, dt=1): """Update all the objects without accounting for their collisions.""" for object in self.objects: object.update(dt) def collideWithGroup(self, group): """Deal with the collisions of the object of the group with another group.""" pass if __name__ == "__main__": from mysurface import Context context = Context(name="Material Group Test", fullscreen=True) f1 = MaterialForm.random(corners=[-10, -10, 10, 10]) f1.motion = Motion(Vector(0, 20), Vector(0, -5, 0), Vector(0, -1)) f2 = MaterialForm.random(corners=[-10, -10, 10, 10]) #f2.fill=True f2.motion = Motion.null() g = MaterialGroup(f1, f2) t = g.getCollisionInstant(1) print(t) while context.open: context.check() context.control() context.clear() context.show() g.update(dt=0.01) g.show(context)
from myabstract import Form, Point from mybody import Body from mysurface import Context import copy import mycolors ps = [Point(10, 2), Point(12, 5), Point(15, -2)] f = Form(ps) b = Body.createFromAbsolute(f) context = Context() while context.open: context.check() context.control() context.clear() context.show() p = Point(*context.point()) nb = copy.deepcopy(b) nba = copy.deepcopy(nb.absolute) nba.points.append(p) for point in nba.points: point.show(context, color=mycolors.BLUE) nb.absolute = nba nb.show(context) nb.absolute.center.show(context, color=mycolors.RED) context.flip()
def isFocused(self,keys): """Determine if the button is being focused.""" self.focus=(self.clicked and not self.focus) or (self.focus and not self.clicked) def onClick(self): """Allow the user to overload the button to trigger an action when the button is clicked.""" pass def onFocus(self): """Allow the user to overload the button to trigger an action when the button is focused.""" pass def onHovering(self): """Allow the user to overload the button to trigger an action when the button is hovered.""" pass if __name__=="__main__": from mysurface import Context context=Context() buttons=[Button([0,0],[1,1])] panel=Panel([0,0],[10,10],buttons) while context.open: context.check() context.control() context.clear() context.show() panel.update(context) panel.show(context) context.flip()
for body in self.bodies: body.showMotion(context) def anyColliding(self): l = len(self.bodies) for i in range(l): for j in range(i + 1, l): self.colliding() def __len__(self): """Return the number of bodies.""" return len(self.bodies) if __name__ == "__main__": from mysurface import Context context = Context() bl = [Body.random(), Body.random()] bg = BodyGroup(bl) while context.open: context.clear() context.show() context.control() for event in context.events(): context.checking(event) bg.react(event) bg.update(dt=0.01) bg.show(context) bg.showMotion(context) context.flip()