|
| 1 | +from bedlam import Game |
| 2 | +from bedlam import GameObject |
| 3 | +from bedlam import Scene |
| 4 | +from bedlam import Sprite |
| 5 | + |
| 6 | +# __pragma__('skip') |
| 7 | +document = window = Math = Date = console = 0 # Prevent complaints by optional static checker |
| 8 | + |
| 9 | +# __pragma__('noskip') |
| 10 | +# __pragma__('noalias', 'clear') |
| 11 | + |
| 12 | +BOARD_SIZE = 16 |
| 13 | +CELL_SIZE = 28 |
| 14 | +BOARD_MARGIN_LEFT = 50 |
| 15 | +BOARD_MARGIN_TOP = 5 |
| 16 | +SNAKE_TIME_STEP = 300 |
| 17 | +SNAKE_TIME_STEP_MIN = 150 |
| 18 | +GAME_BEGIN = "GAME_BEGIN" |
| 19 | +GAME_RUNNING = "GAME_RUNNING" |
| 20 | +GAME_OVER = "GAME_OVER" |
| 21 | +UP = "UP" |
| 22 | +DOWN = "DOWN" |
| 23 | +LEFT = "LEFT" |
| 24 | +RIGHT = "RIGHT" |
| 25 | +DEBUG = False |
| 26 | +COLOR_TIME = 1500 |
| 27 | +COLOR_APPLE = ['#ff0000', '#ff3333', '#ff6666', '#ff9999', '#ffcccc', '#ffffff'] |
| 28 | + |
| 29 | + |
| 30 | +class Apple(GameObject): |
| 31 | + def __init__(self, game, cx, cy): |
| 32 | + GameObject.__init__(self, game) |
| 33 | + self.cell_x = cx |
| 34 | + self.cell_y = cy |
| 35 | + |
| 36 | + def update(self, delta_time: float): |
| 37 | + GameObject.update(self, delta_time) |
| 38 | + |
| 39 | + def draw(self, ctx): |
| 40 | + GameObject.draw(self, ctx) |
| 41 | + Sprite.draw(self, ctx) |
| 42 | + ctx.save() |
| 43 | + # ctx.globalCompositeOperation = 'source-over' |
| 44 | + ctx.strokeStyle = 'red' |
| 45 | + ctx.fillStyle = COLOR_APPLE[0] |
| 46 | + ctx.lineWidth = COLOR_APPLE[0] |
| 47 | + ctx.beginPath() |
| 48 | + radius = (CELL_SIZE - 4) // 2 |
| 49 | + apple_x = self.cell_x * CELL_SIZE + BOARD_MARGIN_LEFT + CELL_SIZE // 2 |
| 50 | + apple_y = self.cell_y * CELL_SIZE + BOARD_MARGIN_TOP + CELL_SIZE // 2 |
| 51 | + ctx.arc(apple_x, apple_y, radius, 0, 2 * Math.PI) |
| 52 | + ctx.fill() |
| 53 | + ctx.stroke() |
| 54 | + ctx.restore() |
| 55 | + |
| 56 | + |
| 57 | +class Snake(GameObject): |
| 58 | + def __init__(self, game): |
| 59 | + GameObject.__init__(self, game) |
| 60 | + self.segments = [] |
| 61 | + self.time_used = 0 |
| 62 | + |
| 63 | + def append(self, snakesegment): |
| 64 | + if len(self.segments) > 0: |
| 65 | + snakesegment.prev_seg = self.segments[len(self.segments) - 1] |
| 66 | + self.segments.append(snakesegment) |
| 67 | + |
| 68 | + def reset(self): |
| 69 | + cx = 2 |
| 70 | + cy = BOARD_SIZE // 2 |
| 71 | + self.segments = [] |
| 72 | + self.append(SnakeSegment(self.game, cx, cy, RIGHT)) |
| 73 | + self.append(SnakeSegment(self.game, cx - 1, cy, RIGHT)) |
| 74 | + for snakesegment in self.segments: |
| 75 | + snakesegment.scene = self.scene |
| 76 | + snakesegment.reset() |
| 77 | + |
| 78 | + def set_direction(self, dr): |
| 79 | + snakesegment = self.segments[0] |
| 80 | + if not self.opposite(snakesegment.direction, dr): |
| 81 | + snakesegment.set_direction(dr) |
| 82 | + |
| 83 | + def opposite(self, d1, d2): |
| 84 | + return (d1 == LEFT and d2 == RIGHT) or (d1 == RIGHT and d2 == LEFT) \ |
| 85 | + or (d1 == UP and d2 == DOWN) or (d1 == DOWN and d2 == UP) |
| 86 | + |
| 87 | + def update(self, delta_time: float): |
| 88 | + global SNAKE_TIME_STEP |
| 89 | + GameObject.update(self, delta_time) |
| 90 | + if self.scene.mode == GAME_RUNNING: |
| 91 | + self.time_used = self.time_used + delta_time |
| 92 | + while self.time_used > SNAKE_TIME_STEP: |
| 93 | + self.time_used = self.time_used - SNAKE_TIME_STEP |
| 94 | + self.move_at_cell() |
| 95 | + SNAKE_TIME_STEP = Math.max(SNAKE_TIME_STEP_MIN, SNAKE_TIME_STEP-1) |
| 96 | + for snakesegment in self.segments: |
| 97 | + snakesegment.update(delta_time) |
| 98 | + |
| 99 | + def move_at_cell(self): |
| 100 | + for snakesegment in self.segments: |
| 101 | + if snakesegment.prev_seg is not None: |
| 102 | + snakesegment.next_direction = snakesegment.prev_seg.direction |
| 103 | + for snakesegment in self.segments: |
| 104 | + snakesegment.move_at_cell() |
| 105 | + seg = self.segments[0] |
| 106 | + nx, ny = seg.next_cell(seg.cell_x, seg.cell_y, seg.direction) |
| 107 | + for apple in self.scene.apples: |
| 108 | + if apple.cell_x == nx and apple.cell_y == ny: |
| 109 | + self.eat_apple(apple) |
| 110 | + break |
| 111 | + |
| 112 | + def draw(self, ctx): |
| 113 | + GameObject.draw(self, ctx) |
| 114 | + for snakesegment in self.segments: |
| 115 | + snakesegment.draw(ctx) |
| 116 | + |
| 117 | + def eat_apple(self, apple): |
| 118 | + global DEBUG |
| 119 | + seg = self.segments[0] |
| 120 | + snakesegment = SnakeSegment(self.game, apple.cell_x, apple.cell_y) |
| 121 | + snakesegment.scene = self.scene |
| 122 | + snakesegment.color_time = COLOR_TIME |
| 123 | + seg.prev_seg = snakesegment |
| 124 | + snakesegment.set_cell(apple.cell_x, apple.cell_y, seg.direction) |
| 125 | + self.scene.apples.remove(apple) |
| 126 | + self.scene.remove(apple) |
| 127 | + self.segments.insert(0, snakesegment) |
| 128 | + nx, ny = snakesegment.next_cell(snakesegment.cell_x, snakesegment.cell_y, snakesegment.direction) |
| 129 | + self.scene.sound_eat_apple.play() |
| 130 | + if nx < 0 or nx > BOARD_SIZE - 1 or ny < 0 or ny > BOARD_SIZE - 1: |
| 131 | + self.scene.collision() |
| 132 | + if DEBUG: |
| 133 | + console.log("Collision with wall at " + nx + "," + ny) |
| 134 | + if len(self.scene.apples) == 0 and self.scene.mode == GAME_RUNNING: |
| 135 | + self.schedule(self.scene.add_apple, 1000) |
| 136 | + |
| 137 | + |
| 138 | +class SnakeSegment(GameObject): |
| 139 | + def __init__(self, game, cx, cy, dr=RIGHT): |
| 140 | + GameObject.__init__(self, game) |
| 141 | + self.cell_x = cx |
| 142 | + self.cell_y = cy |
| 143 | + self.start_x = cx |
| 144 | + self.start_y = cy |
| 145 | + self.start_dir = dr |
| 146 | + self.x = 0 |
| 147 | + self.y = 0 |
| 148 | + self.direction = dr |
| 149 | + self.next_direction = dr |
| 150 | + self.prev_seg = None |
| 151 | + self.color_time = 0 |
| 152 | + |
| 153 | + def set_direction(self, dr): |
| 154 | + self.next_direction = dr |
| 155 | + if self.scene.mode == GAME_BEGIN: |
| 156 | + self.scene.mode = GAME_RUNNING |
| 157 | + self.direction = dr |
| 158 | + |
| 159 | + def set_cell(self, cx, cy, dr=None): |
| 160 | + self.cell_x = cx |
| 161 | + self.cell_y = cy |
| 162 | + self.x = self.cell_x * CELL_SIZE |
| 163 | + self.y = self.cell_y * CELL_SIZE |
| 164 | + if dr is not None: |
| 165 | + self.direction = dr |
| 166 | + self.next_direction = dr |
| 167 | + |
| 168 | + def reset(self): |
| 169 | + self.set_cell(self.start_x, self.start_y, self.start_dir) |
| 170 | + |
| 171 | + def update(self, delta_time: float): |
| 172 | + GameObject.update(self, delta_time) |
| 173 | + if self.scene.mode == GAME_RUNNING: |
| 174 | + dist = Math.round(delta_time * (CELL_SIZE / SNAKE_TIME_STEP)) |
| 175 | + if self.direction == LEFT: |
| 176 | + self.x = self.x - dist |
| 177 | + elif self.direction == RIGHT: |
| 178 | + self.x = self.x + dist |
| 179 | + elif self.direction == UP: |
| 180 | + self.y = self.y - dist |
| 181 | + elif self.direction == DOWN: |
| 182 | + self.y = self.y + dist |
| 183 | + self.color_time = Math.max(0, self.color_time - delta_time) |
| 184 | + |
| 185 | + def move_at_cell(self): |
| 186 | + global DEBUG |
| 187 | + collision = False |
| 188 | + cx = self.cell_x |
| 189 | + cy = self.cell_y |
| 190 | + if self.direction == LEFT and self.cell_x <= 1: |
| 191 | + cx = 0 |
| 192 | + if self.next_direction == self.direction: |
| 193 | + collision = True |
| 194 | + else: |
| 195 | + self.direction = self.next_direction |
| 196 | + elif self.direction == RIGHT and self.cell_x >= (BOARD_SIZE - 2): |
| 197 | + cx = BOARD_SIZE - 1 |
| 198 | + if self.next_direction == self.direction: |
| 199 | + collision = True |
| 200 | + else: |
| 201 | + self.direction = self.next_direction |
| 202 | + elif self.direction == UP and self.cell_y <= 1: |
| 203 | + cy = 0 |
| 204 | + if self.next_direction == self.direction: |
| 205 | + collision = True |
| 206 | + else: |
| 207 | + self.direction = self.next_direction |
| 208 | + elif self.direction == DOWN and self.cell_y >= (BOARD_SIZE - 2): |
| 209 | + cy = BOARD_SIZE - 1 |
| 210 | + if self.next_direction == self.direction: |
| 211 | + collision = True |
| 212 | + else: |
| 213 | + self.direction = self.next_direction |
| 214 | + else: |
| 215 | + cx, cy = self.next_cell(self.cell_x, self.cell_y, self.direction) |
| 216 | + if DEBUG and collision: |
| 217 | + console.log("collision with wall at " + cx + "," + cy) |
| 218 | + if self.prev_seg is None: |
| 219 | + nx, ny = self.next_cell(cx, cy, self.next_direction) |
| 220 | + for seg in self.scene.snake.segments: |
| 221 | + if self != seg and seg.cell_x == nx and seg.cell_y == ny: |
| 222 | + collision = True |
| 223 | + if (DEBUG): |
| 224 | + console.log("collision with snake at " + nx + "," + ny) |
| 225 | + if DEBUG: |
| 226 | + console.log(self.cell_x + ", " + self.cell_y + " : " + self.direction + " : " + self.scene.mode) |
| 227 | + if collision: |
| 228 | + self.scene.collision() |
| 229 | + else: |
| 230 | + self.set_cell(cx, cy, self.next_direction) |
| 231 | + |
| 232 | + def next_cell(self, cx, cy, dr): |
| 233 | + nx, ny = cx, cy |
| 234 | + if dr == LEFT: |
| 235 | + nx = nx - 1 |
| 236 | + elif dr == RIGHT: |
| 237 | + nx = nx + 1 |
| 238 | + elif dr == UP: |
| 239 | + ny = ny - 1 |
| 240 | + elif dr == DOWN: |
| 241 | + ny = ny + 1 |
| 242 | + return nx, ny |
| 243 | + |
| 244 | + def draw(self, ctx): |
| 245 | + GameObject.draw(self, ctx) |
| 246 | + Sprite.draw(self, ctx) |
| 247 | + ctx.save() |
| 248 | + ctx.strokeStyle = 'black' |
| 249 | + ctx.lineWidth = 3 |
| 250 | + ctx.beginPath() |
| 251 | + radius = (CELL_SIZE - 4) // 2 |
| 252 | + snake_x = self.x + BOARD_MARGIN_LEFT + CELL_SIZE // 2 |
| 253 | + snake_y = self.y + BOARD_MARGIN_TOP + CELL_SIZE // 2 |
| 254 | + if self.color_time > 0: |
| 255 | + t = Math.floor(len(COLOR_APPLE) * (COLOR_TIME - self.color_time) / COLOR_TIME) |
| 256 | + ctx.fillStyle = COLOR_APPLE[t] |
| 257 | + ctx.arc(snake_x, snake_y, radius, 0, 2 * Math.PI) |
| 258 | + ctx.fill() |
| 259 | + ctx.stroke() |
| 260 | + else: |
| 261 | + ctx.arc(snake_x, snake_y, radius, 0, 2 * Math.PI) |
| 262 | + ctx.stroke() |
| 263 | + ctx.restore() |
| 264 | + |
| 265 | + |
| 266 | +class SnakeScene(Scene): |
| 267 | + def __init__(self, game, name): |
| 268 | + Scene.__init__(self, game, name) |
| 269 | + self.sound_add_apple = None |
| 270 | + self.mode = GAME_BEGIN |
| 271 | + self.snake = Snake(game) |
| 272 | + self.append(self.snake) |
| 273 | + self.apples = [] |
| 274 | + self.add_apple(BOARD_SIZE - 3, BOARD_SIZE // 2) |
| 275 | + self.reset_board() |
| 276 | + self.sound_add_apple = self.game.load_audio('add_apple') |
| 277 | + self.sound_collision = self.game.load_audio('collision') |
| 278 | + self.sound_eat_apple = self.game.load_audio('eat_apple') |
| 279 | + |
| 280 | + def reset_board(self): |
| 281 | + self.mode = GAME_BEGIN |
| 282 | + self.snake.reset() |
| 283 | + |
| 284 | + def add_apple(self, cx=None, cy=None): |
| 285 | + while cx is None: |
| 286 | + xx = Math.floor(Math.random() * BOARD_SIZE) |
| 287 | + yy = Math.floor(Math.random() * BOARD_SIZE) |
| 288 | + if not self._occupied(xx, yy) and not self._bad(xx, yy): |
| 289 | + cx = xx |
| 290 | + cy = yy |
| 291 | + apple = Apple(self.game, cx, cy) |
| 292 | + self.apples.append(apple) |
| 293 | + self.append(apple) |
| 294 | + if self.sound_add_apple is not None: |
| 295 | + self.sound_add_apple.play() |
| 296 | + |
| 297 | + def _occupied(self, cx, cy): |
| 298 | + for apple in self.apples: |
| 299 | + if apple.cell_x == cx and apple.cell_y == cy: |
| 300 | + return True |
| 301 | + for snakesegment in self.snake.segments: |
| 302 | + if snakesegment.cell_x == cx and snakesegment.cell_y == cy: |
| 303 | + return True |
| 304 | + return False |
| 305 | + |
| 306 | + def _bad(self, cx, cy): |
| 307 | + return (cx <= 0 or cx >= BOARD_SIZE - 1) and (cy <= 0 or cy >= BOARD_SIZE - 1) |
| 308 | + |
| 309 | + def _set_direction(self, d): |
| 310 | + self.snake.set_direction(d) |
| 311 | + |
| 312 | + def handle_keydown(self, event): |
| 313 | + Scene.handle_keydown(self, event) |
| 314 | + if event.key == 'a' or event.key == 'ArrowLeft': |
| 315 | + self._set_direction(LEFT) |
| 316 | + elif event.key == 'd' or event.key == 'ArrowRight': |
| 317 | + self._set_direction(RIGHT) |
| 318 | + elif event.key == 'w' or event.key == 'ArrowUp': |
| 319 | + self._set_direction(UP) |
| 320 | + elif event.key == 's' or event.key == 'ArrowDown': |
| 321 | + self._set_direction(DOWN) |
| 322 | + |
| 323 | + def collision(self): |
| 324 | + self.sound_collision.play() |
| 325 | + self.mode = GAME_OVER |
| 326 | + |
| 327 | + def update(self, delta_time: float): |
| 328 | + Scene.update(self, delta_time) |
| 329 | + pass |
| 330 | + |
| 331 | + def draw(self, ctx): |
| 332 | + global DEBUG |
| 333 | + Scene.draw(self, ctx) |
| 334 | + ctx.save() |
| 335 | + ctx.globalCompositeOperation = 'source-over' |
| 336 | + ctx.strokeStyle = 'black' |
| 337 | + ctx.lineWidth = 2 |
| 338 | + ctx.beginPath() |
| 339 | + board_width = BOARD_SIZE * CELL_SIZE |
| 340 | + ctx.rect(BOARD_MARGIN_LEFT, BOARD_MARGIN_TOP, board_width, board_width) |
| 341 | + ctx.stroke() |
| 342 | + if DEBUG: |
| 343 | + ctx.strokeStyle = '#CCCCCC' |
| 344 | + ctx.lineWidth = 1 |
| 345 | + ctx.beginPath() |
| 346 | + for n in range(1, BOARD_SIZE): |
| 347 | + ctx.moveTo(BOARD_MARGIN_LEFT, BOARD_MARGIN_TOP + n * CELL_SIZE) |
| 348 | + ctx.lineTo(BOARD_MARGIN_LEFT + board_width, BOARD_MARGIN_TOP + n * CELL_SIZE) |
| 349 | + ctx.moveTo(BOARD_MARGIN_LEFT + n * CELL_SIZE, BOARD_MARGIN_TOP) |
| 350 | + ctx.lineTo(BOARD_MARGIN_LEFT + n * CELL_SIZE, BOARD_MARGIN_TOP + board_width) |
| 351 | + ctx.stroke() |
| 352 | + ctx.restore() |
| 353 | + |
| 354 | + |
| 355 | +class SnakeGame(Game): |
| 356 | + def __init__(self, loop_time=20): |
| 357 | + Game.__init__(self, 'Snake', loop_time) |
| 358 | + self.append(SnakeScene(self, 'MAIN')) |
| 359 | + |
| 360 | + @staticmethod |
| 361 | + def set_debug(b): |
| 362 | + global DEBUG, SNAKE_TIME_STEP |
| 363 | + if b is not None and b == 'true': |
| 364 | + DEBUG = True |
| 365 | + SNAKE_TIME_STEP = SNAKE_TIME_STEP * 2 |
0 commit comments