|
| 1 | +#!/usr/bin/env python |
| 2 | +# vim: set ts=4 sw=4 sta et sts=4 ai ci: |
| 3 | +""" |
| 4 | +@ jump run - a platformer demo |
| 5 | +
|
| 6 | +Copyright 2008 Donald E. Llopis (machinezilla@gmail.com) |
| 7 | +
|
| 8 | +This program is free software; you can redistribute it and/or modify |
| 9 | +it under the terms of the GNU General Public License as published by |
| 10 | +the Free Software Foundation; either version 2 of the License, or (at |
| 11 | +your option) any later version. |
| 12 | +
|
| 13 | +This program is distributed in the hope that it will be useful, but |
| 14 | +WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | +General Public License for more details. |
| 17 | +
|
| 18 | +You should have received a copy of the GNU General Public License |
| 19 | +along with this program; if not, write to the Free Software |
| 20 | +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 |
| 21 | +USA |
| 22 | +""" |
| 23 | + |
| 24 | +import os |
| 25 | +import sys |
| 26 | +import math |
| 27 | +import pygame |
| 28 | +from pygame.locals import * |
| 29 | +from pygame.time import Clock |
| 30 | + |
| 31 | + |
| 32 | +SCREEN_WIDTH = 256 |
| 33 | +SCREEN_HEIGHT = 128 |
| 34 | +SCREEN_WIDTH_HALF = SCREEN_WIDTH / 2 |
| 35 | +SCREEN_HEIGHT_HALF = SCREEN_HEIGHT / 2 |
| 36 | +TILEMAP_WIDTH = 25 |
| 37 | +TILEMAP_HEIGHT = 25 |
| 38 | +TILE_WIDTH = 16 |
| 39 | +TILE_HEIGHT = 16 |
| 40 | +TILE_WIDTH_HALF = TILE_WIDTH / 2 |
| 41 | +TILE_HEIGHT_HALF = TILE_HEIGHT / 2 |
| 42 | +TILE_X_MAX = (SCREEN_WIDTH / TILE_WIDTH) + 1 |
| 43 | +TILE_Y_MAX = (SCREEN_HEIGHT / TILE_HEIGHT) + 1 |
| 44 | + |
| 45 | +WORLD_X_MAX = (TILEMAP_WIDTH * TILE_WIDTH) - 1 |
| 46 | +WORLD_Y_MAX = (TILEMAP_HEIGHT * TILE_HEIGHT) - 1 |
| 47 | + |
| 48 | +V_WIDTH = (SCREEN_WIDTH / TILE_WIDTH) / 2 |
| 49 | +V_HEIGHT = (SCREEN_HEIGHT / TILE_HEIGHT) / 2 |
| 50 | + |
| 51 | +GRAVITY = -1 |
| 52 | +V0 = SCREEN_HEIGHT / 2 |
| 53 | + |
| 54 | + |
| 55 | +def generate_map(): |
| 56 | + pass |
| 57 | + |
| 58 | +def main(): |
| 59 | + |
| 60 | + pygame.init() |
| 61 | + clock = Clock() |
| 62 | + screen = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT)) |
| 63 | + pygame.display.set_caption('@ Jump Run') |
| 64 | + |
| 65 | + background = pygame.Surface(screen.get_size()) |
| 66 | + background = background.convert() |
| 67 | + background.fill((0, 0, 0)) |
| 68 | + |
| 69 | + player_img = pygame.image.load("player.png").convert() |
| 70 | + tile_img = pygame.image.load("tile.png").convert() |
| 71 | + star_img = pygame.image.load("star.png").convert() |
| 72 | + |
| 73 | + tile_map = [ |
| 74 | + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 75 | + 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 76 | + 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 77 | + 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, |
| 78 | + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 , |
| 79 | + 0, 0, 0, 1, 0, 2, 2, 0, 0, 0, 0,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0, 2, 0, 2, 2, |
| 92 | + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, |
| 93 | + 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 94 | + 0, 0, 0, 1, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, |
| 95 | + 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, |
| 96 | + 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 97 | + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 98 | + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ] |
| 99 | + |
| 100 | + key = [False, False, False, False, False] |
| 101 | + |
| 102 | + wx = 0 |
| 103 | + wy = WORLD_Y_MAX - (TILE_HEIGHT * 2) |
| 104 | + TX = wx / TILE_WIDTH |
| 105 | + TY = wy / TILE_HEIGHT |
| 106 | + prev_TY = TY |
| 107 | + |
| 108 | + player_jump = False |
| 109 | + player_fall = False |
| 110 | + |
| 111 | + t = 0.0 |
| 112 | + v = 0 |
| 113 | + |
| 114 | + vy = 0 |
| 115 | + |
| 116 | + while True: |
| 117 | + |
| 118 | + delta = clock.tick(60) |
| 119 | + |
| 120 | + for event in pygame.event.get(): |
| 121 | + if event.type == QUIT: |
| 122 | + sys.exit() |
| 123 | + if event.type == KEYDOWN: |
| 124 | + if event.key == K_UP: |
| 125 | + key[0] = True |
| 126 | + if event.key == K_DOWN: |
| 127 | + key[1] = True |
| 128 | + if event.key == K_LEFT: |
| 129 | + key[2] = True |
| 130 | + if event.key == K_RIGHT: |
| 131 | + key[3] = True |
| 132 | + if event.key == K_SPACE: |
| 133 | + key[4] = True |
| 134 | + elif event.type == KEYUP: |
| 135 | + if event.key == K_UP: |
| 136 | + key[0] = False |
| 137 | + if event.key == K_DOWN: |
| 138 | + key[1] = False |
| 139 | + if event.key == K_LEFT: |
| 140 | + key[2] = False |
| 141 | + if event.key == K_RIGHT: |
| 142 | + key[3] = False |
| 143 | + if event.key == K_SPACE: |
| 144 | + key[4] = False |
| 145 | + |
| 146 | + if key[2]: |
| 147 | + wx -= 2 |
| 148 | + if wx < 0: |
| 149 | + wx = 0 |
| 150 | + # tile collision check left |
| 151 | + tx = wx / TILE_WIDTH |
| 152 | + ty = wy / TILE_HEIGHT |
| 153 | + i = (ty * TILEMAP_HEIGHT) + tx |
| 154 | + if tile_map[i] == 1: |
| 155 | + tx += 1 |
| 156 | + wx = tx * TILE_WIDTH |
| 157 | + |
| 158 | + if key[3]: |
| 159 | + prev_wx = wx |
| 160 | + wx += 2 |
| 161 | + if wx > (WORLD_X_MAX - TILE_WIDTH + 1): |
| 162 | + wx = WORLD_X_MAX - TILE_WIDTH + 1 |
| 163 | + # tile collision check right |
| 164 | + tx = (wx+TILE_WIDTH) / TILE_WIDTH |
| 165 | + ty = wy / TILE_HEIGHT |
| 166 | + i = (ty * TILEMAP_HEIGHT) + tx |
| 167 | + if tile_map[i] == 1: |
| 168 | + tx -= 1 |
| 169 | + wx = tx * TILE_WIDTH |
| 170 | + |
| 171 | + if key[4]: |
| 172 | + if not player_jump and not player_fall: |
| 173 | + player_jump = True |
| 174 | + vy = 10 |
| 175 | + |
| 176 | + if player_jump: |
| 177 | + wy -= vy |
| 178 | + vy += GRAVITY |
| 179 | + if vy <= 0: |
| 180 | + player_jump = False |
| 181 | + player_fall = True |
| 182 | + vy = 0 |
| 183 | + |
| 184 | + if player_fall: |
| 185 | + vy -= GRAVITY |
| 186 | + wy += vy |
| 187 | + prev_TY = TY |
| 188 | + |
| 189 | + # tile collision check up or down |
| 190 | + r = wx % TILE_WIDTH |
| 191 | + s = wy % TILE_HEIGHT |
| 192 | + |
| 193 | + TX = wx / TILE_WIDTH |
| 194 | + TY = wy / TILE_HEIGHT |
| 195 | + |
| 196 | + if player_jump: |
| 197 | + tx0 = TX |
| 198 | + tx1 = TX + 1 |
| 199 | + ty = TY |
| 200 | + i0 = (ty * TILEMAP_HEIGHT) + tx0 |
| 201 | + i1 = (ty * TILEMAP_HEIGHT) + tx1 |
| 202 | + |
| 203 | + a = tile_map[i0] == 1 |
| 204 | + |
| 205 | + if s: |
| 206 | + if r: |
| 207 | + b = tile_map[i1] == 1 |
| 208 | + c = a or b |
| 209 | + else: |
| 210 | + c = a |
| 211 | + |
| 212 | + if c: |
| 213 | + ty += 1 |
| 214 | + wy = ty * TILE_HEIGHT |
| 215 | + TY = ty |
| 216 | + player_jump = False |
| 217 | + player_fall = True |
| 218 | + vy = 0 |
| 219 | + s = 0 |
| 220 | + |
| 221 | + if player_fall: |
| 222 | + |
| 223 | + dy = TY - prev_TY |
| 224 | + if dy > 1: |
| 225 | + dy = dy + 2 |
| 226 | + else: |
| 227 | + dy = 2 |
| 228 | + |
| 229 | + ty = TY |
| 230 | + |
| 231 | + while dy: |
| 232 | + tx0 = TX |
| 233 | + tx1 = TX + 1 |
| 234 | + |
| 235 | + if ty >= TILEMAP_HEIGHT: |
| 236 | + ty = TILEMAP_HEIGHT-1 |
| 237 | + i0 = (ty * TILEMAP_HEIGHT) + tx0 |
| 238 | + i1 = (ty * TILEMAP_HEIGHT) + tx1 |
| 239 | + |
| 240 | + if s: |
| 241 | + a = tile_map[i0] == 1 |
| 242 | + if r: |
| 243 | + b = tile_map[i1] == 1 |
| 244 | + c = a or b |
| 245 | + else: |
| 246 | + c = a |
| 247 | + |
| 248 | + if c: |
| 249 | + ty -= 1 |
| 250 | + wy = ty * TILE_HEIGHT |
| 251 | + TY = ty |
| 252 | + prev_TY = ty |
| 253 | + player_fall = False |
| 254 | + vy = 0 |
| 255 | + break |
| 256 | + ty += 1 |
| 257 | + dy -= 1 |
| 258 | + |
| 259 | + |
| 260 | + # check for player fall |
| 261 | + if not player_fall and not player_jump: |
| 262 | + tx0 = TX |
| 263 | + tx1 = TX + 1 |
| 264 | + ty = TY + 1 |
| 265 | + if ty >= TILEMAP_HEIGHT: |
| 266 | + ty = TILEMAP_HEIGHT - 1 |
| 267 | + i0 = (ty * TILEMAP_HEIGHT) + tx0 |
| 268 | + i1 = (ty * TILEMAP_HEIGHT) + tx1 |
| 269 | + |
| 270 | + a = tile_map[i0] == 0 |
| 271 | + |
| 272 | + if r: |
| 273 | + b = tile_map[i1] == 0 |
| 274 | + c = a and b |
| 275 | + else: |
| 276 | + c = a |
| 277 | + |
| 278 | + if c: |
| 279 | + player_fall = True |
| 280 | + vy = 0 |
| 281 | + |
| 282 | + # check for star collection |
| 283 | + index = (TY * TILEMAP_HEIGHT) + TX |
| 284 | + if tile_map[index] == 2: |
| 285 | + tile_map[index] = 0 |
| 286 | + if r: |
| 287 | + index = (TY * TILEMAP_HEIGHT) + TX + 1 |
| 288 | + if tile_map[index] == 2: |
| 289 | + tile_map[index] = 0 |
| 290 | + if s: |
| 291 | + ty = TY+1 |
| 292 | + if ty >= TILEMAP_HEIGHT: |
| 293 | + ty = TILEMAP_HEIGHT-1 |
| 294 | + index = (ty * TILEMAP_HEIGHT) + TX |
| 295 | + if tile_map[index] == 2: |
| 296 | + tile_map[index] = 0 |
| 297 | + if r: |
| 298 | + index = (ty * TILEMAP_HEIGHT) + TX + 1 |
| 299 | + if tile_map[index] == 2: |
| 300 | + tile_map[index] = 0 |
| 301 | + |
| 302 | + # adjust the viewport rectangle |
| 303 | + dx_min = 0 |
| 304 | + dx_max = 0 |
| 305 | + dy_min = 0 |
| 306 | + dy_max = 0 |
| 307 | + |
| 308 | + wx_min = (wx + TILE_WIDTH_HALF) - SCREEN_WIDTH_HALF |
| 309 | + if wx_min < 0: |
| 310 | + dx_min = 0 - wx_min |
| 311 | + wx_min = 0 |
| 312 | + |
| 313 | + wx_max = wx_min + SCREEN_WIDTH - 1 |
| 314 | + if wx_max > WORLD_X_MAX: |
| 315 | + dx_max = wx_max - WORLD_X_MAX + 1 |
| 316 | + wx_max = WORLD_X_MAX |
| 317 | + wx_min -= dx_max |
| 318 | + |
| 319 | + wy_min = (wy + TILE_HEIGHT_HALF) - SCREEN_HEIGHT_HALF |
| 320 | + if wy_min < 0: |
| 321 | + dy_min = 0 - wy_min |
| 322 | + wy_min = 0 |
| 323 | + |
| 324 | + wy_max = wy_min + SCREEN_HEIGHT - 1 |
| 325 | + if wy_max > WORLD_Y_MAX: |
| 326 | + dy_max = wy_max - WORLD_Y_MAX + 1 |
| 327 | + wy_max = WORLD_Y_MAX |
| 328 | + wy_min -= dy_max |
| 329 | + |
| 330 | + # keep player image centered on the screen otherwise if at |
| 331 | + # world max/min then let player wander around the screen |
| 332 | + |
| 333 | + px = SCREEN_WIDTH_HALF - (TILE_WIDTH/2) |
| 334 | + py = SCREEN_HEIGHT_HALF - (TILE_HEIGHT/2) |
| 335 | + |
| 336 | + # moving to the left |
| 337 | + if dx_min > 0: |
| 338 | + px = wx |
| 339 | + |
| 340 | + # moving to the right |
| 341 | + elif dx_max > 0: |
| 342 | + px = SCREEN_WIDTH - (WORLD_X_MAX - wx) |
| 343 | + |
| 344 | + # moving up |
| 345 | + if dy_min > 0: |
| 346 | + py = wy |
| 347 | + |
| 348 | + # moving down |
| 349 | + elif dy_max > 0: |
| 350 | + py = SCREEN_HEIGHT - (WORLD_Y_MAX - wy) |
| 351 | + |
| 352 | + """ |
| 353 | + print "wx: %d wy: %d" % (wx, wy) |
| 354 | + print "px: %d py: %d" % (px, py) |
| 355 | + print "dx_min: %d dx_max: %d" % (dx_min, dx_max) |
| 356 | + print "dy_min: %d dy_max: %d" % (dy_min, dy_max) |
| 357 | + print "wx_min: %d wy_min: %d" % (wx_min, wy_min) |
| 358 | + print "wx_max: %d wy_max: %d" % (wx_max, wy_max) |
| 359 | + """ |
| 360 | + |
| 361 | + # draw tilemap |
| 362 | + screen.blit(background, (0, 0)) |
| 363 | + |
| 364 | + x = 0 |
| 365 | + y = 0 |
| 366 | + sy = 0 - (wy_min % TILE_HEIGHT) |
| 367 | + ty = wy_min / TILE_HEIGHT |
| 368 | + |
| 369 | + while y < TILE_Y_MAX: |
| 370 | + tx = wx_min / TILE_WIDTH |
| 371 | + sx = 0 - (wx_min % TILE_WIDTH) |
| 372 | + x = 0 |
| 373 | + while x < TILE_X_MAX: |
| 374 | + i = (ty * TILEMAP_HEIGHT) + tx |
| 375 | + if tile_map[i] == 1: |
| 376 | + screen.blit(tile_img, (sx, sy)) |
| 377 | + if tile_map[i] == 2: |
| 378 | + screen.blit(star_img, (sx, sy)) |
| 379 | + sx += TILE_WIDTH |
| 380 | + tx += 1 |
| 381 | + if tx == TILEMAP_WIDTH: |
| 382 | + break |
| 383 | + x += 1 |
| 384 | + sy += TILE_HEIGHT |
| 385 | + ty += 1 |
| 386 | + if ty == TILEMAP_HEIGHT: |
| 387 | + break |
| 388 | + y += 1 |
| 389 | + |
| 390 | + screen.blit(player_img, (px, py)) |
| 391 | + |
| 392 | + pygame.display.flip() |
| 393 | + |
| 394 | + |
| 395 | +if __name__ == '__main__': |
| 396 | + main() |
| 397 | + |
0 commit comments