]> git.armaanb.net Git - pong.git/commitdiff
Randomize direction of index
authorArmaan Bhojwani <me@armaanb.net>
Sat, 15 May 2021 22:37:24 +0000 (18:37 -0400)
committerArmaan Bhojwani <me@armaanb.net>
Sat, 15 May 2021 22:37:24 +0000 (18:37 -0400)
Makefile
pong.c

index a234c45070355bc1ad0ceeb62ebe85e616c34954..d07d5ecf31ec126948020259522ca9c6408f621c 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
 all:
        ${CC} -o pong pong.c ${CLAGS} -Wall -Wextra -std=c99 -pedantic \
-               -lcurses ${LDFLAGS}
+               -lcurses -lm ${LDFLAGS}
        scdoc < pong.1.scd > pong.1
 
 install: all
diff --git a/pong.c b/pong.c
index 717b1ae919752eb237e2bf451d34a8b1d646fed6..1363ae59531c37b8c90f9b1fe24ff9c4a373ab08 100644 (file)
--- a/pong.c
+++ b/pong.c
@@ -4,6 +4,7 @@
 #include <unistd.h>
 #include <sys/time.h>
 #include <time.h>
+#include <math.h>
 
 int
 die(int code)
@@ -20,6 +21,12 @@ randslope(void)
        return (rand() % (high - low + 1)) + low;
 }
 
+int
+randsign(void)
+{
+       return pow(-1, rand());
+}
+
 int
 main(void) {
        srand((unsigned int)time(NULL));
@@ -29,7 +36,7 @@ main(void) {
        int pad_h = 5;
        int a_score = 0, b_score = 0;
        int slope_x = randslope();
-       int index_dir = 1;
+       int index_dir = randsign();
        int direction = 1;
        double speed = 0.02;
        struct timeval start, stop;
@@ -126,6 +133,7 @@ main(void) {
                        x = max_x/2;
                        index = slope_x*max_y/2;
                        slope_x = randslope();
+                       index_dir = randsign();
                        speed = 0.02;
                        sleep(1);
                } else if (y == 0 || y == max_y) {