Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old May 18th, 2006, 9:25 PM   #1
bconnor
Newbie
 
Join Date: Mar 2006
Posts: 9
Rep Power: 0 bconnor is on a distinguished road
Help with logic?

So basically im trying to work on a single player pong game ...

What im struggling with the logic behind the movement of the paddle..

If i just keep pressing the one direction, say a ... that moves fine, though the issue arises when i than press z, rather than moving down just one space, it moves far more than that...

	

/* a = up, z = down */


	int	c, temp, moveA, moveZ;
	temp = 1; 
	moveA, moveZ = 5; 
	
	set_up();

	while ( ( c = getchar()) != 'q' ){
		if ( c == 'a' ){
		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);
		
		moveA = (TOP_ROW + 5)-temp;
		move(moveA,RIGHT_EDGE+1);		  
		vline(' ', (BOT_ROW-TOP_ROW)/3);
		attroff(A_REVERSE);
		getch();	
		if ( c == 'a' )	{
			temp++;
			}
		else {
			moveA = moveA +1;
			}
		}
		else if ( c == 'z' ){
		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);
		
		moveZ = (TOP_ROW + 5)+ temp;
		move(moveZ,RIGHT_EDGE+1);	  
		vline(' ', (BOT_ROW-TOP_ROW)/3);
		attroff(A_REVERSE);
		getch();
		if ( c == 'z' ){
			temp++;
			}
		else{
			moveZ = moveZ -1;
			}
		}
bconnor is offline   Reply With Quote
Old May 18th, 2006, 11:06 PM   #2
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 893
Rep Power: 4 The Dark is on a distinguished road
It looks like your variable "temp" is the position of the bat, when you press "a", you are subtracting from (TOP_ROW + 5), but when you press "z" you are adding it to (TOP_ROW + 5), this means it will jump all over the place. Especially since temp never gets decreased.

My suggestion:
Change the name of "temp" to "batPosition" (or similar). Increment it when the users presses "a" and decrement it when they press "z" then you can always use the same calculation for where to draw the bat. This also means that your two if statements can be merged together. Something like:
  if ( c == 'a')
    batPosition++;
  else if (c == 'z')
    batPosition--;
  ... Drawing code here

I am not sure what these lines of code are for (in red):
    if ( c == 'a' )	{
      temp++;
    }
    else {
      moveA = moveA +1;
    }
It will never get in there, as c is always 'a' at this point, even if it did get in there, moveA is never used again after this point, so there is no point in modifying it.
The Dark is offline   Reply With Quote
Old May 18th, 2006, 11:53 PM   #3
bconnor
Newbie
 
Join Date: Mar 2006
Posts: 9
Rep Power: 0 bconnor is on a distinguished road
Yeah, not sure myself what i was trying to with the code in red, i think possibly it was meant to be elseif statement, though i get the sense my head is somewhere else today, if anywhere at all ...

Quote:
always use the same calculation for where to draw the bat
This going to sound dumb i get the sense, though in what sense do you mean "same", the same as i had, or a calculation that will be same as both..

move((TOP_ROW + 5) + padPosition,RIGHT_EDGE+1);		  
vline(' ', (BOT_ROW-TOP_ROW)/3);



*Sorry its just one of those days...
bconnor is offline   Reply With Quote
Old May 19th, 2006, 2:30 AM   #4
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 893
Rep Power: 4 The Dark is on a distinguished road
Same calculation for both, it doesn't matter whether you are going up or down when you are drawing the bat, it is the location that counts.
The Dark 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 3:17 AM.

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