diff options
| author | Mauro Aranda | 2019-08-06 21:48:41 -0300 |
|---|---|---|
| committer | Eli Zaretskii | 2019-08-10 11:50:08 +0300 |
| commit | 7ff96f95d7d67fc1489fca9fd3ab4a99328a5b8a (patch) | |
| tree | c11ccf06cbb767a5574385c5fd4274329260e364 | |
| parent | e503e9d35f80ff064c9f0ef24e514b00f5e214f9 (diff) | |
| download | emacs-7ff96f95d7d67fc1489fca9fd3ab4a99328a5b8a.tar.gz emacs-7ff96f95d7d67fc1489fca9fd3ab4a99328a5b8a.zip | |
Fix pong collision detection
* lisp/play/pong.el (pong-update-game): If the ball hit the bat where
bats are positioned, draw again the bat cell in the old ball
position. (Bug#20579).
Also, avoid changing the direction of the ball right after hitting the
bats, and improve the collision detection against the borders.
| -rw-r--r-- | lisp/play/pong.el | 83 |
1 files changed, 49 insertions, 34 deletions
diff --git a/lisp/play/pong.el b/lisp/play/pong.el index 555c1939d26..759dbb404c6 100644 --- a/lisp/play/pong.el +++ b/lisp/play/pong.el | |||
| @@ -349,46 +349,61 @@ detection and checks if a player scores." | |||
| 349 | 349 | ||
| 350 | (let ((old-x pong-x) | 350 | (let ((old-x pong-x) |
| 351 | (old-y pong-y)) | 351 | (old-y pong-y)) |
| 352 | 352 | ;; Erase the last ball position. | |
| 353 | (when (and (> old-y 0) | ||
| 354 | (< old-y (- pong-height 1))) | ||
| 355 | ;; If the ball hit the bat in the column where bats are positioned, | ||
| 356 | ;; and therefore changed its x direction, draw again the bat cell. | ||
| 357 | (if (or (and (= old-x 2) (< 0 pong-xx)) | ||
| 358 | (and (= old-x (- pong-width 3)) (> 0 pong-xx))) | ||
| 359 | (gamegrid-set-cell old-x old-y pong-bat) | ||
| 360 | (gamegrid-set-cell old-x old-y pong-blank))) | ||
| 361 | |||
| 362 | ;; Update the ball position. | ||
| 353 | (setq pong-x (+ pong-x pong-xx)) | 363 | (setq pong-x (+ pong-x pong-xx)) |
| 354 | (setq pong-y (+ pong-y pong-yy)) | 364 | ;; If the ball would go out of bounds, put it against the border. |
| 355 | 365 | (cond | |
| 356 | (if (and (> old-y 0) | 366 | ((<= (+ pong-y pong-yy) 0) |
| 357 | (< old-y (- pong-height 1))) | 367 | (setq pong-yy (- pong-yy)) |
| 358 | (gamegrid-set-cell old-x old-y pong-blank)) | 368 | (setq pong-y 1)) |
| 359 | 369 | ((>= (+ pong-y pong-yy) (- pong-height 1)) | |
| 370 | (setq pong-yy (- pong-yy)) | ||
| 371 | (setq pong-y (- pong-height 2))) | ||
| 372 | (t | ||
| 373 | (setq pong-y (+ pong-y pong-yy)) | ||
| 374 | ;; Check if the ball is against the border now, | ||
| 375 | ;; and change the y direction if it is. | ||
| 376 | (when (or (<= pong-y 1) (>= pong-y (- pong-height 2))) | ||
| 377 | (setq pong-yy (- pong-yy))))) | ||
| 378 | |||
| 379 | ;; Draw the ball in its new position. | ||
| 360 | (if (and (> pong-y 0) | 380 | (if (and (> pong-y 0) |
| 361 | (< pong-y (- pong-height 1))) | 381 | (< pong-y (- pong-height 1))) |
| 362 | (gamegrid-set-cell pong-x pong-y pong-ball)) | 382 | (gamegrid-set-cell pong-x pong-y pong-ball)) |
| 363 | 383 | ||
| 384 | ;; Hit bat, score a goal, or nothing. | ||
| 364 | (cond | 385 | (cond |
| 365 | ((or (= pong-x 3) (= pong-x 2)) | 386 | ((and (or (= pong-x 3) (= pong-x 2)) |
| 366 | (if (and (>= pong-y pong-bat-player1) | 387 | (> 0 pong-xx) ; Collide with the bat if headed towards it. |
| 367 | (< pong-y (+ pong-bat-player1 pong-bat-width))) | 388 | (>= pong-y pong-bat-player1) |
| 368 | (and | 389 | (< pong-y (+ pong-bat-player1 pong-bat-width))) |
| 369 | (setq pong-yy (+ pong-yy | 390 | (setq pong-yy (+ pong-yy |
| 370 | (cond | 391 | (cond |
| 371 | ((= pong-y pong-bat-player1) -1) | 392 | ((= pong-y pong-bat-player1) -1) |
| 372 | ((= pong-y (1+ pong-bat-player1)) 0) | 393 | ((= pong-y (1+ pong-bat-player1)) 0) |
| 373 | (t 1)))) | 394 | (t 1)))) |
| 374 | (setq pong-xx (- pong-xx))))) | 395 | (setq pong-xx (- pong-xx))) |
| 375 | 396 | ||
| 376 | ((or (= pong-x (- pong-width 4)) (= pong-x (- pong-width 3))) | 397 | ((and (or (= pong-x (- pong-width 4)) (= pong-x (- pong-width 3))) |
| 377 | (if (and (>= pong-y pong-bat-player2) | 398 | (< 0 pong-xx) ; Collide with the bat if headed towards it. |
| 378 | (< pong-y (+ pong-bat-player2 pong-bat-width))) | 399 | (>= pong-y pong-bat-player2) |
| 379 | (and | 400 | (< pong-y (+ pong-bat-player2 pong-bat-width))) |
| 380 | (setq pong-yy (+ pong-yy | 401 | (setq pong-yy (+ pong-yy |
| 381 | (cond | 402 | (cond |
| 382 | ((= pong-y pong-bat-player2) -1) | 403 | ((= pong-y pong-bat-player2) -1) |
| 383 | ((= pong-y (1+ pong-bat-player2)) 0) | 404 | ((= pong-y (1+ pong-bat-player2)) 0) |
| 384 | (t 1)))) | 405 | (t 1)))) |
| 385 | (setq pong-xx (- pong-xx))))) | 406 | (setq pong-xx (- pong-xx))) |
| 386 | |||
| 387 | ((<= pong-y 1) | ||
| 388 | (setq pong-yy (- pong-yy))) | ||
| 389 | |||
| 390 | ((>= pong-y (- pong-height 2)) | ||
| 391 | (setq pong-yy (- pong-yy))) | ||
| 392 | 407 | ||
| 393 | ((< pong-x 1) | 408 | ((< pong-x 1) |
| 394 | (setq pong-score-player2 (1+ pong-score-player2)) | 409 | (setq pong-score-player2 (1+ pong-score-player2)) |