|
Newbie
Join Date: Mar 2006
Posts: 9
Rep Power: 0 
|
collision detection
My collision detection for my pong game isnt working so well, instead of ending game when it misses the paddle, it ends game anytime it hits the right edge, my brain is fried at the moment ... anyone help me out ..
#include <ncurses.h>
#include <signal.h>
#include "bounce.h"
#include "set_ticker.c"
#include <stdlib.h>
struct ppball the_ball ;
void set_up();
void wrap_up();
int topPos, botPos;
int main()
{
int c, padPosition, spCount;
spCount = 0;
set_up();
while ( ( c = getchar()) != 'q'){
if ( c == 'a')
if (topPos > TOP_ROW)
padPosition--;
if (c == 'z')
if (botPos - 1 < BOT_ROW)
padPosition++;
clear();
attron(A_REVERSE);
move(0,LEFT_EDGE+20);
addstr(" Single Player Pong ");
move(2,LEFT_EDGE+2);
addstr(" INSTRUCTIONS: (move paddle) A - up, Z - down (quit) - q ");
move(TOP_ROW-1,LEFT_EDGE-1);
hline(' ', LEFT_EDGE + 53); /* Top Border */
vline(' ', TOP_ROW + 13); /* Left Side */
move(BOT_ROW+1,LEFT_EDGE); /* Bottom Border */
hline(' ', LEFT_EDGE + 52);
topPos = ((TOP_ROW + 5) + padPosition);
botPos = topPos + ((BOT_ROW - TOP_ROW)/3);
move((TOP_ROW + 5) + padPosition,RIGHT_EDGE+1);
vline(' ', (BOT_ROW - TOP_ROW)/3);
attroff(A_REVERSE);
}
for ( spCount = 0; spCount++; spCount<10)the_ball.x_ttm++ && the_ball.y_ttm++;
for ( spCount = 10; spCount++; spCount<20)the_ball.x_ttm+50 && the_ball.y_ttm+50;
for ( spCount = 20; spCount++; spCount>20)the_ball.x_ttm+100 && the_ball.y_ttm+100;
wrap_up();
}
void set_up()
{
void ball_move(int);
the_ball.y_pos = Y_INIT;
the_ball.x_pos = X_INIT;
the_ball.y_ttg = the_ball.y_ttm = Y_TTM ;
the_ball.x_ttg = the_ball.x_ttm = X_TTM ;
the_ball.y_dir = 1 ;
the_ball.x_dir = 1 ;
the_ball.symbol = DFL_SYMBOL ;
initscr();
noecho();
crmode();
attron(A_REVERSE);
move(0,LEFT_EDGE+20);
addstr(" Single Player Pong ");
move(2,LEFT_EDGE+2);
addstr(" INSTRUCTIONS: (move paddle) A - up, Z - down (quit) - q ");
move(TOP_ROW-1,LEFT_EDGE-1);
hline(' ', LEFT_EDGE + 53); /* Top Border */
vline(' ', TOP_ROW + 13); /* Left Side */
move(BOT_ROW+1,LEFT_EDGE); /* Bottom Border */
hline(' ', LEFT_EDGE + 52);
move(TOP_ROW + 5,RIGHT_EDGE+1); /* Paddle (Right Side) */
vline(' ', (BOT_ROW-TOP_ROW)/3 );
attroff(A_REVERSE);
signal( SIGINT , SIG_IGN );
mvaddch( the_ball.y_pos, the_ball.x_pos, the_ball.symbol );
refresh();
signal( SIGALRM, ball_move );
set_ticker( 1000 / TICKS_PER_SEC ); /* send millisecs per tick */
}
void wrap_up()
{
set_ticker( 0 );
endwin(); /* put back to normal */
}
void ball_move(int signum)
{
int y_cur, x_cur, moved;
signal( SIGALRM , SIG_IGN ); /* dont get caught now */
y_cur = the_ball.y_pos ; /* old spot */
x_cur = the_ball.x_pos ;
moved = 0 ;
if ( the_ball.y_ttm > 0 && the_ball.y_ttg-- == 1 ){
the_ball.y_pos += the_ball.y_dir ; /* move */
the_ball.y_ttg = the_ball.y_ttm ; /* reset*/
moved = 1;
}
if ( the_ball.x_ttm > 0 && the_ball.x_ttg-- == 1 ){
the_ball.x_pos += the_ball.x_dir ; /* move */
the_ball.x_ttg = the_ball.x_ttm ; /* reset*/
moved = 1;
}
if ( moved ){
mvaddch( y_cur, x_cur, BLANK );
mvaddch( y_cur, x_cur, BLANK );
mvaddch( the_ball.y_pos, the_ball.x_pos, the_ball.symbol );
bounce_or_lose( &the_ball );
move(LINES-1,COLS-1);
refresh();
}
signal( SIGALRM, ball_move); /* for unreliable systems */
}
int bounce_or_lose(struct ppball *bp)
{
int return_val = 0 ;
if ( bp->y_pos == TOP_ROW ){
bp->y_dir = 1 ;
return_val = 1 ;
} else if ( bp->y_pos == BOT_ROW ){
bp->y_dir = -1 ;
return_val = 1;
}
if ( bp->x_pos == LEFT_EDGE ){
bp->x_dir = 1 ;
return_val = 1 ;
} else if ( bp->x_pos == RIGHT_EDGE + 1 ){
if ( topPos < bp->x_pos || botPos > bp->x_pos){
printf("%s", " GAMEOVER!! Now quitting... \n\n");
exit(1); }
else{
bp->x_dir = -1;
return_val = 1;
}
}
return return_val;
}
|