Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Main.c
- ------------
- #include "display.h"
- #include "game.h"
- #define MIN(x,y) ((x)>(y) ? (y) : (x))
- #define DELAY 2
- #define PAD_OFFSET 4
- #define PAD_LEN_RATIO 5
- #define LEFT -1
- #define RIGHT 1
- #define UP -1
- #define DOWN 1
- /* Starting pad1 position */
- static struct position pad1_pos(int height, int width);
- /* Starting pad2 position */
- static struct position pad2_pos(int height, int width);
- /* Starting ball position */
- static struct position ball_pos(int height, int width);
- /* Starting direction position */
- static struct position ball_dir(void);
- /* Pad length */
- static int pad_len(int height);
- int main() {
- int STOP = 0, cont = 0, height, width;
- unsigned int s1, s2;
- #ifdef REG
- FILE *fp;
- fp=fopen("record.txt","w");
- #endif
- display_open(&height,&width);
- #ifdef REG
- fprintf(fp,"%d %d\n",height,width);
- #endif
- setup_game(height,width,ball_pos(height,width),ball_dir(),
- pad1_pos(height,width),pad2_pos(height,width),pad_len(height));
- display_start();
- do {
- // Add a delay to move_ball()
- if((cont = (cont + 1) % DELAY) == 0) {
- move_ball();
- display_table();
- #ifdef REG
- fprintf(fp,"M");
- #endif
- }
- switch (display_read()) {
- case P1_DOWN: move_pad1_down();
- #ifdef REG
- fprintf(fp,"z");
- #endif
- break;
- case P1_UP: move_pad1_up();
- #ifdef REG
- fprintf(fp,"a");
- #endif
- break;
- case P2_UP: move_pad2_up();
- #ifdef REG
- fprintf(fp,"u");
- #endif
- break;
- case P2_DOWN: move_pad2_down();
- #ifdef REG
- fprintf(fp,"d");
- #endif
- break;
- case PAUSE: display_pause(); break;
- case QUIT: STOP = 1; break;
- }
- display_table();
- } while(!STOP && ((s1 = get_pad1_score()) != 9 && (s2 = get_pad2_score()) != 9));
- if(s1 == 9 || s2 == 9)
- display_win(s1 == 9 ? 1 : 2);
- display_quit();
- display_close();
- #ifdef REG
- fclose(fp);
- #endif
- }
- /* Starting pad1 position */
- static struct position pad1_pos(int height, int width) {
- struct position pos;
- pos.x = PAD_OFFSET;
- pos.y = height/2;
- return pos;
- }
- /* Starting pad2 position */
- static struct position pad2_pos(int height, int width) {
- struct position pos;
- pos.x = width-PAD_OFFSET;
- pos.y = height/2;
- return pos;
- }
- /* Starting ball position */
- static struct position ball_pos(int height, int width) {
- struct position pos;
- pos.x = width/2;
- pos.y = height/2;
- return pos;
- }
- /* Starting direction position */
- static struct position ball_dir(void) {
- struct position pos = {DOWN,RIGHT};
- return pos;
- }
- /* Pad length */
- static int pad_len(int height) {
- return MIN(height/2+1,height/PAD_LEN_RATIO);
- }
- -------------------------------------------------------
- game.h
- #ifndef GAME_H
- #define GAME_H
- struct position {
- int x;
- int y;
- };
- /*
- * Setup a game with the following starting configuration:
- * - table dimension equal to heigth*width
- * - ball starting position at ball_pos
- * - ball starting direction towards ball_dir
- * - pad1 starting position at pad1_pos
- * - pad2 starting position at pad2_pos
- * - pad length equal to pad_len
- */
- void setup_game(int height, int width,
- struct position ball_pos, struct position ball_dir,
- struct position pad1_pos, struct position pad2_pos, int pad_len);
- /* Moves pad1 one position up. */
- void move_pad1_up(void);
- /* Moves pad2 one poisiton up */
- void move_pad2_up(void);
- /* Moves pad1 one position down. */
- void move_pad1_down(void);
- /* Moves pad2 one position down. */
- void move_pad2_down(void);
- /* Moves the ball in the current direction */
- void move_ball(void);
- /* Returns ball current position */
- struct position get_ball_pos(void);
- /* Returns pad1 current position */
- struct position get_pad1_pos(void);
- /* Returns pad2 current position */
- struct position get_pad2_pos(void);
- /* Returns the pad length */
- unsigned int get_pad_len(void);
- /* Returns pad1 current score */
- unsigned int get_pad1_score(void);
- /* Returns pad2 current score */
- unsigned int get_pad2_score(void);
- #endif
- -----------------------------------------
- game.c
- struct position {
- int x;
- int y;
- };
- struct position pad1;
- struct position pad2;
- static int len;
- struct position ball_position;
- struct position ball_direction;
- static int w, h;
- static int pad1_score = 0, pad2_score = 0 ;
- /*
- * Setup a game with the following starting configuration:
- * - table dimension equal to heigth*width
- * - ball starting position at ball_pos
- * - ball starting direction towards ball_dir
- * - pad1 starting position at pad1_pos
- * - pad2 starting position at pad2_pos
- * - pad length equal to pad_len
- */
- void setup_game(int height, int width,
- struct position ball_pos, struct position ball_dir,
- struct position pad1_pos, struct position pad2_pos, int pad_len) {
- pad1.x = pad1_pos.x;
- pad1.y = pad1_pos.y;
- pad2.x = pad2_pos.x;
- pad2.y = pad2_pos.y;
- len = pad_len;
- ball_position = ball_pos;
- ball_direction = ball_dir;
- w = width; h = height;
- }
- /* Moves pad1 one position up. */
- void move_pad1_up(void) {
- if (!(pad1.y == 0)) {
- if (!((ball_position.y == pad1.y || ball_position.y == pad1.y - 1) && (ball_position.x == pad1.x || ball_position.x == pad1.x + 1 || ball_position.x == pad1.x - 1))) {
- pad1.y -=1;
- }
- }
- }
- /* Moves pad2 one poisiton up */
- void move_pad2_up(void) {
- if (!(pad2.y == 0)) {
- if (!((ball_position.y == pad2.y || ball_position.y == pad2.y - 1) && (ball_position.x + 2 == pad2.x || ball_position.x + 1 == pad2.x || ball_position.x == pad2.x))) {
- pad2.y -= 1;
- }
- }
- }
- /* Moves pad1 one position down. */
- void move_pad1_down(void) {
- if (!(pad1.y + len - 1 == h)) {
- if (!(ball_position.y == pad1.y + len && (ball_position.x == pad1.x || ball_position.x == pad1.x + 1 || ball_position.x == pad1.x - 1))) {
- pad1.y += 1;
- }
- }
- }
- /* Moves pad2 one position down. */
- void move_pad2_down(void) {
- if (!(pad2.y + len - 1 == h)) {
- if (!(ball_position.y == pad2.y + len && (ball_position.x + 2 == pad2.x || ball_position.x + 1 == pad2.x + 1 || ball_position.x == pad2.x))) {
- pad2.y += 1;
- }
- }
- }
- /* Moves the ball in the current direction */
- void move_ball(void) {
- //tocco bordi
- ball_position.y += ball_direction.y;
- ball_position.x += ball_direction.x;
- //assegnazione punti
- if (ball_position.x == w) {
- pad1_score++;
- ball_position.x = w / 2;
- ball_position.y = h / 2;
- }
- if (ball_position.x == 0) {
- pad2_score++;
- ball_position.x = w / 2;
- ball_position.y = h / 2;
- }
- if (ball_position.y == h || ball_position.y == 0) ball_direction.y = -ball_direction.y;
- //pieno 1
- if (ball_position.x == pad1.x + 1 && (ball_position.y >= pad1.y && ball_position.y <= pad1.y - 1 + len)) {
- ball_direction.x = 1;
- }
- //pieno 2
- if (ball_position.x == pad2.x - 2 && (ball_position.y >= pad2.y && ball_position.y <= pad2.y - 1 + len)) {
- ball_direction.x = -1;
- }
- //tocco bordo sup pad1
- if ((ball_position.y == pad1.y - 1) && (ball_position.x == pad1.x || ball_position.x == pad1.x + 1 || ball_position.x == pad1.x + 1 || ball_position.x == pad1.x - 1)) {
- if (ball_position.y != 0) {
- ball_direction.y = -1;
- }
- ball_direction.x = 1;
- }
- //tocco bordo sup pad2
- if ((ball_position.y == pad2.y - 1) && (ball_position.x == pad2.x || ball_position.x == pad2.x - 2 || ball_position.x == pad2.x + 1 || ball_position.x == pad2.x - 1)) {
- if (ball_position.y != 0) {
- ball_direction.y = -1;
- }
- ball_direction.x = -1;
- }
- //tocco inf pad1
- if (ball_position.y == pad1.y + len && (ball_position.x == pad1.x || ball_position.x == pad1.x + 1 || ball_position.x == pad1.x - 1)) {
- if (ball_position.y != h) {
- ball_direction.y = 1;
- }
- ball_direction.x = 1;
- }
- //tocco inf pad2
- if (ball_position.y == pad2.y + len && (ball_position.x == pad2.x || ball_position.x == pad2.x - 2 || ball_position.x == pad2.x + 1 || ball_position.x == pad2.x - 1)) {
- if (ball_position.y != h) {
- ball_direction.y = 1;
- }
- ball_direction.x = -1;
- }
- }
- /* Returns ball current position */
- struct position get_ball_pos(void) { return ball_position; }
- /* Returns pad1 current position */
- struct position get_pad1_pos(void) { return pad1; }
- /* Returns pad2 current position */
- struct position get_pad2_pos(void) { return pad2; }
- /* Returns the pad length */
- unsigned int get_pad_len(void) { return len; }
- /* Returns pad1 current score */
- unsigned int get_pad1_score(void) { return pad1_score; }
- /* Returns pad2 current score */
- unsigned int get_pad2_score(void) { return pad2_score; }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement