summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorP. J. McDermott <pjm@nac.net>2013-05-21 17:50:37 (EDT)
committer P. J. McDermott <pjm@nac.net>2013-05-21 17:50:37 (EDT)
commitd97f66ef26c89c8bd805cdeafffa959c91ec278b (patch)
tree96bf1eabfaece5ad310210dd40ea7f08543a15e0
parenta9c64a158c369bc370c14dd87567fc030fa050e0 (diff)
downloadcursespong-d97f66ef26c89c8bd805cdeafffa959c91ec278b.zip
cursespong-d97f66ef26c89c8bd805cdeafffa959c91ec278b.tar.gz
cursespong-d97f66ef26c89c8bd805cdeafffa959c91ec278b.tar.bz2
Add (stationary and invisible) balls to the board.
-rw-r--r--src/ball.c19
-rw-r--r--src/ball.h17
-rw-r--r--src/game.c13
-rw-r--r--src/game.h6
-rw-r--r--src/local.mk3
-rw-r--r--src/main.c2
6 files changed, 56 insertions, 4 deletions
diff --git a/src/ball.c b/src/ball.c
new file mode 100644
index 0000000..7484950
--- /dev/null
+++ b/src/ball.c
@@ -0,0 +1,19 @@
+#include <stdlib.h>
+
+#include "ball.h"
+
+struct ball *
+new_ball(int x, int y)
+{
+ struct ball *b;
+
+ b = malloc(sizeof(*b));
+ if (b == NULL) {
+ return NULL;
+ }
+
+ b->x = x;
+ b->y = y;
+
+ return b;
+}
diff --git a/src/ball.h b/src/ball.h
new file mode 100644
index 0000000..c2be172
--- /dev/null
+++ b/src/ball.h
@@ -0,0 +1,17 @@
+#ifndef BALL_H
+#define BALL_H
+
+#include "board.h"
+
+static const int BALL_START_X_NORMAL = 40;
+static const int BALL_START_Y_NORMAL = 12;
+
+struct ball {
+ int x;
+ int y;
+ struct ball *next;
+};
+
+struct ball *new_ball(int x, int y);
+
+#endif
diff --git a/src/game.c b/src/game.c
index 9f7cebd..a5ae342 100644
--- a/src/game.c
+++ b/src/game.c
@@ -12,7 +12,7 @@ static void update(struct game *g);
static void draw(struct game *g);
struct game *
-new_game(void)
+new_game(char balls)
{
struct game *g;
@@ -23,6 +23,8 @@ new_game(void)
g->players[0] = new_player(0);
g->players[1] = new_player(1);
+ g->min_balls = balls;
+ g->cur_balls = 0;
return g;
}
@@ -91,10 +93,19 @@ input(struct game *g)
static void
update(struct game *g)
{
+ struct ball *b;
+
update_paddle(&g->players[0]->paddle_h);
update_paddle(&g->players[0]->paddle_v);
update_paddle(&g->players[1]->paddle_h);
update_paddle(&g->players[1]->paddle_v);
+
+ while (g->cur_balls < g->min_balls) {
+ b = new_ball(BALL_START_X_NORMAL, BALL_START_Y_NORMAL);
+ b->next = g->balls_head;
+ g->balls_head = b;
+ ++g->cur_balls;
+ }
}
static void
diff --git a/src/game.h b/src/game.h
index 9a0f096..25d509b 100644
--- a/src/game.h
+++ b/src/game.h
@@ -2,13 +2,17 @@
#define GAME_H
#include "player.h"
+#include "ball.h"
struct game {
struct player *players[2];
char running;
+ char min_balls;
+ char cur_balls;
+ struct ball *balls_head;
};
-struct game *new_game(void);
+struct game *new_game(char balls);
void free_game(struct game *g);
void run_game(struct game *g);
diff --git a/src/local.mk b/src/local.mk
index 3f3338d..778f34c 100644
--- a/src/local.mk
+++ b/src/local.mk
@@ -2,4 +2,5 @@ src_SOURCES = \
src/main.c \
src/game.c \
src/player.c \
- src/paddle.c
+ src/paddle.c \
+ src/ball.c
diff --git a/src/main.c b/src/main.c
index 408f47b..1034b2a 100644
--- a/src/main.c
+++ b/src/main.c
@@ -11,7 +11,7 @@ main(void)
struct game *g;
init_curses();
- g = new_game();
+ g = new_game(1);
run_game(g);