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/tutorial1c/scripts/tutorial1.py at master · fifengine/python-tutorials Expand file tree/ Copy pathtutorial1.pyMore file actions 196 lines (156 loc) · 6.93 KB / Copy pathtutorial1.pyFile metadata and controls196 lines (156 loc) · 6.93 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 #!/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 ApplicationBase. 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 ApplicationBase much more. from fife.extensions.basicapplication import ApplicationBase 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. # Dont worry too much about this yet. This will come into play once # we have a GUI in place. if event.isConsumedByWidgets(): return clickpoint = fife.ScreenPoint(event.getX(), event.getY()) # 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(ApplicationBase): """ The main application. It inherits fife.extensions.ApplicationBase 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) 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 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 _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") # 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" for this to work. self._actorlayer = self._map.getLayer("actor_layer") self._player = self._actorlayer.getInstance("player") |
| You are visitor 01402503 — 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. |