Download as pdf
Download as pdf
You are on page 1of 7
File - AU DEMY\Python Game Development™ Build 11 Total Gamesi3. 10 game section -Know about Turtelbase 1 py 34 ‘Utilities # pylint: disabLe=no-member import collections import math import os def def path(filename) : "Return full path to “filename> in freegames module." filepath = os.path.realpath(_ file) dirpath = os.path.dirname(filepath) fullpath = os.path.join(dirpath, filename) return fullpath line(a, b, x, y): “Draw Line from ~(a, b)° to ~(x, y) import turtle turtle.up() turtle.goto(a, b) turtle.down() turtle.goto(x, y) class vector(collections. Sequence) : “Two-dimensional vector. Vectors can be modified in-place. >>> v = vector(@, 1) >>> vemove(1) >>> v vector(1, 2) >>> v.rotate(98) Dov vector(-2.0, 1.0) Page tof? File - AU DEMY\Python Game Development™ Build 11 Total Gamesi3. 10 game section -Know about Turtelbase 1 py 47 48 49 50 51 52 53 54 55 56 57 58 59 co) 61 62 63 64 65 66 67 68 69 78 71 72 73 74 75 76 7 78 79 8e 81 82 83 84 85 86 87 88 89 98 91 92 # pylint: disable=invalid-name PRECISION = 6 __slots__ = ("_x', '_y', ‘_hash') def _init_(self, x, y): """Initialize vector with coordinates: x, y. >>> v = vector(1, 2) Dod Vex 1 >> wy 2 self._hash = None self._x = round(x, self.PRECISION) self._y = round(y, self.PRECISION) @property def x(self): “""X-axis component of vector. >> Vv Dd> Vex 1 doo Vx = 3 >>> VX 3 vector(1, 2) return self._x @.setter def x(self, value): if self._hash is not None: raise ValueError('cannot set x after hashing’) self._x = round(value, self.PRECISION) @property def y(self): “""Y-axis component of vector. >>> v = vector(1, 2) >>> wy Page 2 of7 File - AU DEMY\Python Game Development™ Build 11 Total Gamesi3. 10 game section -Know about Turtelbase 1 py 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 2 >> vy = 5 >> wy 5 return self._y @y.setter def def def def y(self, value: if self._hash is not None: raise ValueError('cannot set y after hashing’) self._y = round(value, self. PRECISION) __hash_(self): “"y, “hash_() -> hash(v) >>> v = vector(1, 2) >>> h = hash(v) >>> Vex = 2 Traceback (most recent call Last): ValueError: cannot set x after hashing if self._hash is None: pair = (self.x, self.y) self._hash = hash(pair) return self. hash __len_(self): vy, Len_() -> Len(v) >>> v = vector(1, 2) >>> Len(v) 2 return 2 __getitem_(self, index): “"y. getitem_(v, i) -> v[i] >>> v = vector(3, 4) >>> v[@] 3 Page 3017 File - AU DEMY\Python Game Development™ Build 11 Total Gamesi3. 10 game section -Know about Turtelbase 1 py 139 >>> v[L] 148 4 141 >>> v[2] 142 Traceback (most recent call Last): 143 se 144 IndexError 145 146 oe 147 if index == 148 return self.x 149 elif index 150 return self.y 151 else: 152 raise IndexError 153 154 def copy(self): 155 “Return copy of vector. 156 157 >>> v = vector(1, 2) 158 >>> W = v.copy() 159 >>> vis w 160 False 161 162 on 163 type_self = type(self) 164 return type_self(self.x, self.y) 165 166 def _eq_(self, other): 167 “ny eq _(w) -> v 168 169 >>> v = vector(1, 2) 178 >>> w = vector(1, 2) 171 >>> Vi == W 172 True 173 174 oe 175 if isinstance(other, vector): 176 return self.x == other.x and self.y == other.y 177 return NotImplemented 178 179 def _ne_(self, other): 188 vny, ne_(w) -> v I= 181 182 >>> v = vector(1, 2) 183 >>> w = vector(3, 4) 184 >>> v lew Page 4 of? File - AU DEMY\Python Game Development™ Build 11 Total Gamesi3. 10 game section -Know about Turtelbase 1 py 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 def hashing’ def True if isinstance(other, vector) return self.x != other.x or self.y != other.y return NotImplemented __iadd_(self, other): “my, qdadd_(w) -> v += >>> v = vector(1, 2) >>> w = vector(3, 4) D> Vv te W >>> Vv vector(4, 6) >>> vite 1 >> Vv vector(5, 7) if self._hash is not None: raise ValueError(‘cannot add vector after ) elif isinstance(other, vector): self.x += other.x self.y += other.y else: self.x += other self.y += other return self __add__(self, other): my, add_(w) -> v + >>> v = vector(1, 2) >>> w = vector(3, 4) >>> V+ Ww vector(4, 6) poo ved vector(2, 3) >>> 2.0+V vector(3.6, 4.8) copy = self.copy() return copy.__iadd__(other) Page Sof? File - AU DEMY\Python Game Development™ Build 11 Total Gamesi3. 10 game section -Know about Turtelbase 1 py 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 __radd__ = __add def def move(self, other): "Move vector by other (in-place). >>> v = vector(1, 2) >>> w = vector(3, 4) >>> vemove(w) >> v vector(4, 6) >>> v.move(3) Dov vector(7, 9) self.__iadd__(other) __isub_(self, other): "ry, “isub_(w) -> v >>> v = vector(1, 2) >>> w = vector(3, 4) >>> v-= Ww D> v vector(-2, -2) >> v-sd >>> Vv vector(-3, -3) if self._hash is not None: raise ValueError('cannot subtract vector after hashing") def elif isinstance(other, vector): self.x -= other.x self.y -= other.y else: self.x -= other self.y -= other return self __sub__(self, other): ry, sub_(w) -> v - w >>> v = vector(1, 2) Page 6 of7 File - HAU DEMY\Python Game Development™ Build 11 Total Gamesi3. 10 game section -Know about Turtelbase 1 py 275, 276 277 278 279 280 281 282 283 284 285 286 >>> w= vector(3, 4) D> v= Ww vector(-2, -2) >>> v-1 vector(@, 1) copy = self.copy() return copy.__isub__(other) Page 7 of?

You might also like