GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — python-tutorials/tutorial1d/scripts/tutorial1.py at master · pcps-afa/python-tutorials Expand file tree/ Copy pathtutorial1.pyMore file actions 295 lines (233 loc) · 10.5 KB / Copy pathtutorial1.pyFile metadata and controls295 lines (233 loc) · 10.5 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 #!/usr/bin/env python # -*- coding: utf-8 -*- # #################################################################### # Copyright (C) 2005-2013 by the FIFE team # http://www.fifengine.net # This file is part of FIFE. # # FIFE is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either # version 2.1 of the License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; if not, write to the # Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA # #################################################################### import sys, os from fife import fife from fife.extensions import pychan # Import the PychanApplicationBase. This is where a lot of magic happens but # you don't need to know the inner workings just yet. Once you are more # familiar with FIFE you'll understand PychanApplicationBase much more. # PychanApplicationBase extends ApplicationBase and adds the proper code to # initialize pychan for GUI support. from fife.extensions.pychan import pychanbasicapplication class QueryDialog(object): """ This class is a simple class that displays a dialog box with accept and decline buttons. @param acceptedfunction the function to call if the user click on accept @param title the title of the dialog box @param text the query or text that you would like the user to answer """ def __init__(self, acceptedfunction, title, text): self._widget = None # This loads our GUI window and saves a reference to it. We'll need # it to set the text values of the title and text fields. self._widget = pychan.loadXML('gui/query.xml') # This searches for a child widget with the name "queryname" and # then sets that widgets text property. Easy isn't it? # This is the title of our dialog box. self._queryname = self._widget.findChild(name="queryname") self._queryname.text = str(title, 'utf-8') # This searches for a child widget with the name "querytext" and # then sets that widgets text property. Easy isn't it? self._querytext = self._widget.findChild(name="querytext") self._querytext.text = str(text, 'utf-8') # Pychan has some handy functions to bind callbacks to certain widget # events. In this case when the the 'accept' and 'decline' child # widgets get clicked the acceptedfunction() or hide() functions get # executed. Remember to not include the ()'s because that will run # the function when Python parses the line... not good! eventMap = { 'accept': acceptedfunction, 'decline': self._widget.hide, } # This function is what actually does the mapping of the function to # widget. There is some magic behind the scenes here. self._widget.mapEvents(eventMap) def show(self): # Shows the window. It's already in memory so it will be shown with the # correct values for the title and text. self._widget.show() def hide(self): # Hides the window. Note that it's always in memory just kicking around # until we show it. In some cases you might want to destroy the window # after you are done with it to save memory. self._widget.hide() class Tutorial1MouseListener(fife.IMouseListener): """ Main game listener. Listens for Mouse events. You must include all possible event functions or you will get an exception. These functions are called by the engine's event manager when the corresponding events are triggered. """ def __init__(self, application): self._engine = application.engine self._application = application self._eventmanager = self._engine.getEventManager() # We must make sure to call our base class' constructor to make sure # the listener is setup correctly. fife.IMouseListener.__init__(self) def mousePressed(self, event): # Mouse press was consumed by a PyChan widget so lets ignore it. if event.isConsumedByWidgets(): return # Save the click point in screen coordinates clickpoint = fife.ScreenPoint(event.getX(), event.getY()) if self._application.maploaded: # Tell the application to move the player instance to the screen # coordinate where the user clicked. self._application.movePlayer(clickpoint) def mouseReleased(self, event): pass def mouseMoved(self, event): pass def mouseEntered(self, event): pass def mouseExited(self, event): pass def mouseClicked(self, event): pass def mouseWheelMovedUp(self, event): pass def mouseWheelMovedDown(self, event): pass def mouseDragged(self, event): pass class Tutorial1Application(pychanbasicapplication.PychanApplicationBase): """ The main application. It inherits fife.extensions.pychan.pychanbasicapplication.PychanApplicationBase and implements the _pump() function which gets called every frame. """ def __init__(self, settings): # Call our base class's __init__ function and pass it settings. # This is where the FIFE engine instance gets created and configured # (amoungst other things). This includes reading the settings file, # applying the settings to the engine, initializing PyChan (our GUI # manager), creating the default application listener (more info on # this in our later tutorials), and setting up logging. All of # these things you can do on your own but we provide ApplicationBase # to make your life easier. super(Tutorial1Application,self).__init__(settings) # Save a copy of our settings. This could be useful in the future # as the Setting module allows you to store your own settins as # well as FIFE settings. self._settings = settings # Initialize an instance of fife.MapLoader which is a built in map # loader that we provide. We must pass it some internal FIFE objects # that the loader requires to properly load a map. self._loader = fife.MapLoader(self.engine.getModel(), self.engine.getVFS(), self.engine.getImageManager(), self.engine.getRenderBackend()) # True if we have a map loaded. False otherwise. self._mapLoaded = False # Since we want to listen for mouse events lets save a reference to # the event manager and create our event listener and attach it to # the manager. self._eventmanager = self.engine.getEventManager() self._mouselistener = Tutorial1MouseListener(self) # Listeners are executed in the order in which they are added. # Lets ensure this one gets executed first. self._eventmanager.addMouseListenerFront(self._mouselistener) # Add the quit botton to the top left of the screen # First use pychan to load the interface self._rootpanel = pychan.loadXML('gui/rootpanel.xml') # Map functions to the buttons on the root panel self._rootpanel.mapEvents({ 'quitButton' : self.onQuitButtonPress, }) # Finally show the panel so it's visible to the user self._rootpanel.show() # Initialize the dialog box to ask the user if they are sure they want # to quit. The dialog box doesn't get displayed until we call the # show() function. The first parameter is the function to call when # the user clicks the "accept" button on the dialog box. self._quitdialog = QueryDialog(self.quitAccepted, "Attention!", "Are you sure you wan to quit?") def onQuitButtonPress(self): """ This function gets called when the root panels "quit" button is pressed. """ # Show the quit dialog box asking the user if they want to quit. self._quitdialog.show() def loadMap(self, filename): """ Simple function to load and display a map file. We could of course have passed in the map filename but I'll leave that up to you. @param filename The filename. """ self._mapfilename = filename # Check to make sure the map file is loadable (i.e. it's in the correct # format) and attempt to load the map. if self._loader.isLoadable(self._mapfilename): self._map = self._loader.load(self._mapfilename) self._mapLoaded = True # Save a reference to the main camera. "camera1" must exist on the # map. self._camera = self._map.getCamera("camera1") # Save a ref to the actor layer and player on the map. There must # be a layer with an id of "actor_layer" and an instance with an id # of "player" in your map file for this to work. self._actorlayer = self._map.getLayer("actor_layer") self._player = self._actorlayer.getInstance("player") def getLocationAt(self, screenpoint): """ Query the main camera for the Map location (on the actor layer) that a screen point refers to. @param screenpoint A fife.ScreenPoint """ # We must transform the point from screen coords to map coords then # create a Location object based on a the actor layer target_mapcoord = self._camera.toMapCoordinates(screenpoint, False) target_mapcoord.z = 0 location = fife.Location(self._actorlayer) location.setMapCoordinates(target_mapcoord) return location def movePlayer(self, screenpoint): """ Simple function that moves the player instance to the given screenpoint. @param screenpoint A fife.ScreenPoint """ # You must tell the move function which action should be used for moving. # In this case we have the "walk" action defined for the player so we # use that. The 4.0 is how fast we want the player instance to move. self._player.move('walk', self.getLocationAt(screenpoint), 4.0) def quitAccepted(self): """ This function gets called when the user clicks "accept" on the quit dialog box. It tells FIFE to exit. """ # The quit function is part of ApplicationBase which we inherit in this # class. self.quit() def _pump(self): """ This function gets called every frame. This is where you want to call your main game logic code. """ # On our first frame the map will be loaded and we will grab a reference # to our camera, map and player instance. if not self._mapLoaded: self.loadMap("../assets/maps/tutorial1map.xml") def _isMapLoaded(self): return self._mapLoaded maploaded = property(_isMapLoaded) |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |