![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2006
Posts: 9
Rep Power: 0
![]() |
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;
}
} |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 874
Rep Power: 4
![]() |
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 hereI am not sure what these lines of code are for (in red): if ( c == 'a' ) {
temp++;
}
else {
moveA = moveA +1;
}
|
|
|
|
|
|
#3 | |
|
Newbie
Join Date: Mar 2006
Posts: 9
Rep Power: 0
![]() |
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:
move((TOP_ROW + 5) + padPosition,RIGHT_EDGE+1);
vline(' ', (BOT_ROW-TOP_ROW)/3);*Sorry its just one of those days... |
|
|
|
|
|
|
#4 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 874
Rep Power: 4
![]() |
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.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|