all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Mauro Aranda <maurooaranda@gmail.com>
To: 20579@debbugs.gnu.org
Subject: bug#20579: 24.4; Pong bat loses 1/3rd of its length if the ball hits it too quickly
Date: Wed, 7 Aug 2019 12:03:49 -0300	[thread overview]
Message-ID: <CABczVwfftHKzNHEiqR=akr8p5no9vXa0PWUny_Hk+OqjDraNOA@mail.gmail.com> (raw)
In-Reply-To: <5554F04D.6030200@creativecommons.org>


[-- Attachment #1.1: Type: text/plain, Size: 234 bytes --]

tags 20579 patch
quit

Hello.

I can reproduce this bug in Emacs 25.3, Emacs 26.2 and the current
master.

I attach a patch that fixes this bug, and also does some improvement in
the collision detection of pong.

Best regards,
Mauro.

[-- Attachment #1.2: Type: text/html, Size: 296 bytes --]

[-- Attachment #2: 0001-Fix-pong-collision-detection.patch --]
[-- Type: text/x-patch, Size: 4119 bytes --]

From d1b442f4371193b03af4c6e05119d87fc8512f31 Mon Sep 17 00:00:00 2001
From: Mauro Aranda <maurooaranda@gmail.com>
Date: Tue, 6 Aug 2019 21:48:41 -0300
Subject: [PATCH] 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.
---
 lisp/play/pong.el | 83 ++++++++++++++++++++++++++++++++-----------------------
 1 file changed, 49 insertions(+), 34 deletions(-)

diff --git a/lisp/play/pong.el b/lisp/play/pong.el
index 555c193..759dbb4 100644
--- a/lisp/play/pong.el
+++ b/lisp/play/pong.el
@@ -349,46 +349,61 @@ pong-update-game
 
     (let ((old-x pong-x)
 	  (old-y pong-y))
-
+      ;; Erase the last ball position.
+      (when (and (> old-y 0)
+                 (< old-y (- pong-height 1)))
+        ;; If the ball hit the bat in the column where bats are positioned,
+        ;; and therefore changed its x direction, draw again the bat cell.
+        (if (or (and (= old-x 2) (< 0 pong-xx))
+                (and (= old-x (- pong-width 3)) (> 0 pong-xx)))
+            (gamegrid-set-cell old-x old-y pong-bat)
+          (gamegrid-set-cell old-x old-y pong-blank)))
+
+      ;; Update the ball position.
       (setq pong-x (+ pong-x pong-xx))
-      (setq pong-y (+ pong-y pong-yy))
-
-      (if (and (> old-y 0)
-	       (< old-y (- pong-height 1)))
-	  (gamegrid-set-cell old-x old-y pong-blank))
-
+      ;; If the ball would go out of bounds, put it against the border.
+      (cond
+       ((<= (+ pong-y pong-yy) 0)
+        (setq pong-yy (- pong-yy))
+        (setq pong-y 1))
+       ((>= (+ pong-y pong-yy) (- pong-height 1))
+        (setq pong-yy (- pong-yy))
+        (setq pong-y (- pong-height 2)))
+       (t
+        (setq pong-y (+ pong-y pong-yy))
+        ;; Check if the ball is against the border now,
+        ;; and change the y direction if it is.
+        (when (or (<= pong-y 1) (>= pong-y (- pong-height 2)))
+          (setq pong-yy (- pong-yy)))))
+
+      ;; Draw the ball in its new position.
       (if (and (> pong-y 0)
 	       (< pong-y (- pong-height 1)))
 	  (gamegrid-set-cell pong-x pong-y pong-ball))
 
+      ;; Hit bat, score a goal, or nothing.
       (cond
-       ((or (= pong-x 3) (= pong-x 2))
-	(if (and (>= pong-y pong-bat-player1)
-		 (< pong-y (+ pong-bat-player1 pong-bat-width)))
-	    (and
-	     (setq pong-yy (+ pong-yy
-			      (cond
-			       ((= pong-y pong-bat-player1) -1)
-			       ((= pong-y (1+ pong-bat-player1)) 0)
-			       (t 1))))
-	     (setq pong-xx (- pong-xx)))))
-
-       ((or (= pong-x (- pong-width 4)) (= pong-x (- pong-width 3)))
-	(if (and (>= pong-y pong-bat-player2)
-		 (< pong-y (+ pong-bat-player2 pong-bat-width)))
-	    (and
-	     (setq pong-yy (+ pong-yy
-			      (cond
-			       ((= pong-y pong-bat-player2) -1)
-			       ((= pong-y (1+ pong-bat-player2)) 0)
-			       (t 1))))
-	     (setq pong-xx (- pong-xx)))))
-
-       ((<= pong-y 1)
-	(setq pong-yy (- pong-yy)))
-
-       ((>= pong-y (- pong-height 2))
-	(setq pong-yy (- pong-yy)))
+       ((and (or (= pong-x 3) (= pong-x 2))
+             (> 0 pong-xx) ; Collide with the bat if headed towards it.
+             (>= pong-y pong-bat-player1)
+             (< pong-y (+ pong-bat-player1 pong-bat-width)))
+        (setq pong-yy (+ pong-yy
+			 (cond
+			  ((= pong-y pong-bat-player1) -1)
+			  ((= pong-y (1+ pong-bat-player1)) 0)
+			  (t 1))))
+        (setq pong-xx (- pong-xx)))
+
+       ((and (or (= pong-x (- pong-width 4)) (= pong-x (- pong-width 3)))
+             (< 0 pong-xx) ; Collide with the bat if headed towards it.
+             (>= pong-y pong-bat-player2)
+             (< pong-y (+ pong-bat-player2 pong-bat-width)))
+	(setq pong-yy (+ pong-yy
+			 (cond
+			  ((= pong-y pong-bat-player2) -1)
+			  ((= pong-y (1+ pong-bat-player2)) 0)
+			  (t 1))))
+	(setq pong-xx (- pong-xx)))
 
        ((< pong-x 1)
 	(setq pong-score-player2 (1+ pong-score-player2))
-- 
2.7.4


  reply	other threads:[~2019-08-07 15:03 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-05-14 18:58 bug#20579: 24.4; Pong bat loses 1/3rd of its length if the ball hits it too quickly Matt Lee
2019-08-07 15:03 ` Mauro Aranda [this message]
2019-08-10  8:52   ` Eli Zaretskii

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CABczVwfftHKzNHEiqR=akr8p5no9vXa0PWUny_Hk+OqjDraNOA@mail.gmail.com' \
    --to=maurooaranda@gmail.com \
    --cc=20579@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.