Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 22nd, 2006, 12:48 AM   #1
bconnor
Newbie
 
Join Date: Mar 2006
Posts: 9
Rep Power: 0 bconnor is on a distinguished road
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;
}
bconnor is offline   Reply With Quote
Old May 26th, 2006, 11:13 AM   #2
free-zombie
Programmer
 
free-zombie's Avatar
 
Join Date: May 2006
Location: Bavaria, Germany
Posts: 50
Rep Power: 0 free-zombie is an unknown quantity at this point
Send a message via ICQ to free-zombie Send a message via MSN to free-zombie Send a message via Yahoo to free-zombie
( topPos < bp->x_pos || botPos > bp->x_pos)
X usually signifies left/right and not top/bottom.
"topPos" and "botPos" sound like top/bottom -> Y and not X
else if ( bp->x_pos == RIGHT_EDGE + 1 ){
you seam to be checking for GAMEOVER under the condition that the ball is beyond the right edge. This does not sound like what you want.
free-zombie is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:01 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC