source: trunk/PARPG/run.py @ 48

Revision 48, 4.3 KB checked in by maximinus_parpg, 11 years ago (diff)

All code is to our PEP standard as on the PARPG wiki.
Trivial adjustments to most of the .py files

  • Property svn:executable set to *
Line 
1#!/usr/bin/python
2
3#   This program is free software: you can redistribute it and/or modify
4#   it under the terms of the GNU General Public License as published by
5#   the Free Software Foundation, either version 3 of the License, or
6#   (at your option) any later version.
7
8#   This program is distributed in the hope that it will be useful,
9#   but WITHOUT ANY WARRANTY; without even the implied warranty of
10#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11#   GNU General Public License for more details.
12
13#   You should have received a copy of the GNU General Public License
14#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16import sys, os, shutil
17
18from scripts.common import utils
19# add paths to the swig extensions
20utils.addPaths ('../../engine/swigwrappers/python', '../../engine/extensions')
21
22import fife_compat
23import fife, fifelog
24from scripts import world
25from scripts.common import eventlistenerbase
26from basicapplication import ApplicationBase
27from settings import Setting
28
29TDS = Setting()
30
31class ApplicationListener(eventlistenerbase.EventListenerBase):
32    def __init__(self, engine, world):
33        super(ApplicationListener, self).__init__(engine,
34                                                  regKeys=True,regCmd=True, regMouse=False, 
35                                                  regConsole=True, regWidget=True)
36        self.engine = engine
37        self.world = world
38        engine.getEventManager().setNonConsumableKeys([fife.Key.ESCAPE,])
39        self.quit = False
40        self.aboutWindow = None
41
42    def keyPressed(self, evt):
43        """Function to deal with keypress events
44           @param evt: The event that was captured"""
45        keyval = evt.getKey().getValue()
46        if keyval == fife.Key.ESCAPE:
47            self.quit = True
48        evt.consume()
49         
50    def onCommand(self, command):
51        """Enables the game to be closed via the 'X' button on the window frame"""
52        self.quit = (command.getCommandType() == fife.CMD_QUIT_GAME)
53        if self.quit:
54            command.consume()
55
56class PARPG(ApplicationBase):
57    """Main Application class"""
58    def __init__(self):
59        super(PARPG,self).__init__()
60        self.world = world.World(self.engine)
61        self.listener = ApplicationListener(self.engine, self.world)
62        self.world.load(str(TDS.readSetting("MapFile")))
63
64    def loadSettings(self):
65        """Load the settings from a python file and load them into the engine.
66           Called in the ApplicationBase constructor."""
67        import settings
68        self.settings = settings
69
70        eSet = self.engine.getSettings()
71        eSet.setDefaultFontGlyphs(str(TDS.readSetting("FontGlyphs",strip=False)))
72        eSet.setDefaultFontPath(str(TDS.readSetting("Font")))
73        eSet.setBitsPerPixel(int(TDS.readSetting("BitsPerPixel")))
74        eSet.setInitialVolume(float(TDS.readSetting("InitialVolume")))
75        eSet.setSDLRemoveFakeAlpha(int(TDS.readSetting("SDLRemoveFakeAlpha")))
76        eSet.setScreenWidth(int(TDS.readSetting("ScreenWidth")))
77        eSet.setScreenHeight(int(TDS.readSetting("ScreenHeight")))
78        eSet.setRenderBackend(str(TDS.readSetting("RenderBackend")))
79        eSet.setFullScreen(int(TDS.readSetting("FullScreen")))
80        try:
81            eSet.setWindowTitle(str(TDS.readSetting("WindowTitle")))
82            eSet.setWindowIcon(str(TDS.readSetting("WindowIcon")))
83        except:
84            pass           
85        try:
86            eSet.setImageChunkingSize(int(TDS.readSetting("ImageChunkSize")))
87        except:
88            pass
89
90    def initLogging(self):
91        """Initialize the LogManager"""
92        LogModules = TDS.readSetting("LogModules",type='list')
93        self.log = fifelog.LogManager(self.engine,
94                                      int(TDS.readSetting("LogToPrompt")),
95                                      int(TDS.readSetting("LogToFile")))
96        if LogModules:
97            self.log.setVisibleModules(*LogModules)
98
99    def createListener(self):
100        # already created in constructor
101        pass
102
103    def _pump(self):
104        """Main game loop
105           There are in fact 2 main loops, this one and the one in World"""
106        if self.listener.quit:
107            self.breakRequested = True
108        else:
109            self.world.pump()
110
111def main():
112    """Application code starts from here"""
113    app = PARPG()
114    app.run()
115
116if __name__ == '__main__':
117    main()
118
Note: See TracBrowser for help on using the repository browser.