import pyui, pygame from server import Server from client import Client from pyui.widgets import * from hoop import world, engine import environment, control, constants class Application: """ Creates application with the specified display width and height """ def __init__(self, width, height): print 'Initializing Application' self.defaultIp = constants.defaultIp self.launcher = LobbyFrame(self, width, height) self.displayWidth = width self.displayHeight = height self.width = constants.worldWidth #world width self.height = constants.worldHeight #world height self.world = world.World(self.width, self.height) control.world = self.world #self.controller = control.Controller() self.running = 0 self.backMethod = self.emptyBackMethod engine.initialize(constants.displayWidth, constants.displayHeight) pyui.desktop.getRenderer().setBackMethod(self.render) self.environment = environment.Environment(self.world) environment.Env = self.environment #Perhaps use datamanager.DataManager() ? """ Callback-method for the pyui renderer. Draws borders of the world and invokes the hoop engine rendering routines """ def render(self): engine.clear() width = self.width height = self.height x = environment.Env.localPlayer.avatar.posX y = environment.Env.localPlayer.avatar.posY color = pyui.colors.yellow engine.setView(x,y) #Coordinates of world corners ux = width-x + self.displayWidth/2 lx = -x + self.displayWidth/2 uy = height-y + self.displayHeight/2 ly = -y + self.displayHeight/2 #Might the offset in the above definitions actually express an error #in hoop.engine? It should not be necessary to perform that translation engine.drawLine(lx,ly,ux,ly,color) engine.drawLine(ux,ly,ux,uy,color) engine.drawLine(ux,uy,lx,uy,color) engine.drawLine(lx,uy,lx,ly,color) engine.render() engine.drawText('You : %d' %environment.Env.localPlayer.fragCount, (100,self.displayHeight-30), pyui.colors.yellow) engine.drawText('Enemy : %d' %environment.Env.remotePlayer.fragCount, (self.displayWidth-200,self.displayHeight-30), pyui.colors.red) """ The main loop of PySoldier. Repeats executing until self.running is set to 0. Each repeat will update graphics by means of pyui and hoop, plus the game logic as controlled in hoop. Further, a custom callbackmethod is called, which will handle network events. In the current implementation, the callback method is selected depending on whether the game runs in client or server mode. """ def mainLoop(self): print "main loop started" self.running = 1 frameCount = 0 lastTime = pyui.readTimer() currentTime = lastTime dt = 0 while self.running: pyui.draw() if pyui.update(): currentTime = pyui.readTimer() dt = currentTime-lastTime lastTime = currentTime #self.controller.update() if self.world.update(dt) == 0: self.running = 0 self.backMethod() else: self.running = 0 frameCount += 1 def update(self): self.networkManager.update() def setBackMethod(self, backMethod): self.backMethod = backMethod #def updateControls(self): # self.controller.update() # players.localPlayer.update() # players.remotePlayer.update() """ The main loop callback defaults to this method, which does nothing """ def emptyBackMethod(self): pass """ This frame contains buttons, allowing the user to create or join a server among other things. """ class LobbyFrame(Frame): """ Creates a lobby frame of specified width and height, for controlling the specified application """ def __init__(self, application, width, height): Frame.__init__(self, 10, 10, width/2, height/2, "Lobby") self.createButton = pyui.widgets.Button(" Create Game ", self.onCreate) self.joinButton = pyui.widgets.Button(" Join Game ", self.onJoin) self.exitButton = pyui.widgets.Button(" Exit ", self.onExit) self.networkManager = 'none' self.ipBox = pyui.widgets.Edit(application.defaultIp, 12, self.onJoin) #constants.defaultPlayerName <-- write player name too somewhere self.setLayout(pyui.layouts.TableLayoutManager(2,6)) self.addChild(self.createButton, (0,0,1,1)) self.addChild(self.joinButton, (0,2,1,1)) self.addChild(self.exitButton, (0,4,1,1)) self.addChild(self.ipBox, (1,0,1,1)) self.pack() self.application = application constants.SOUND_THEME.play() """ Create a server """ def onCreate(self, widget): print "Creating Server" constants.MODE = constants.MODE_SERVER server = Server(self.application.world, constants.clientToServerPort, constants.serverToClientPort) self.destroy() self.application.setBackMethod(server.update) """ Listen for network input """ def onJoin(self, widget): print "Creating Client" constants.MODE = constants.MODE_CLIENT client = Client(self.ipBox.text, self.application.world, constants.serverToClientPort, constants.clientToServerPort) self.destroy() self.application.setBackMethod(client.update) """ Quits PySoldier """ def onExit(self, widget): self.application.running = 0