Using gotoxy() how would I reduce the flickering in my game? I know that part of my problem is cause by redrawing the whole screen all the time instead parts of it.
Using gotoxy() how would I reduce the flickering in my game? I know that part of my problem is cause by redrawing the whole screen all the time instead parts of it.
you can make an extra function called updatescreen(); add a xold, yold variable to hold the position pacman is on when the position is changed called updatescreen(); and in there draw the pacman using x and y coordinates and reprint 1 tile of the map which are xold and yold so that everytime you move the whole screen isn't redrawn only 2 tiles.
so basically like this:
hope that helped.Code:int main() { int quit = 0; // loadMap(); drawscreen(); while(!quit) { // if(kbhit()) player.oldy = player.y; player.oldx = player.x; quit = getInput(); collision(); updatescreen(); } return 0; } void updatescreen(void) { gotoxy(player.x,player.y); map[player.y][player.x] = 2; gotoxy(player.xold,player.yold) switch(map[player.yold][player.xold]) { case 0: {textcolor(2); cprintf("."); break;} case 1: {textcolor(9); cprintf("\xdb"); break;} case 2: {textcolor(14); cprintf("\x02"); break;} case 3: {textcolor(11); cprintf("\x02"); break; } case 4: {printf(" "); break;} } }
¿Red Baron?
"Imagination is more important than knowledge"
-Albert Einstein (1879-1955)
Check out my games!
[code] /* dont forget code tags! */ [/code]
OK, thanks for your help. So you're suggestion that I repeat the code that I have in drawscreen() in your updatescreen()?
yes/no call drawscreen before entering your loop, then everytime a key is pressed use updatescreen. drawscreen and updatescreen aren't suppose to have the same stuff, what i put in the example function is all you need dont add anything to it, unless you get an error or mistake.
¿Red Baron?
"Imagination is more important than knowledge"
-Albert Einstein (1879-1955)
Check out my games!
[code] /* dont forget code tags! */ [/code]
Okay, I've added your update function and now my drawscreen() contains:
The problem now is for some reason the cursor gets positioned at 1,1 instead of 1,2 and input response isn't the same.Code:void drawscreen(void) { int i,j, x = 1, y = 1; map[player.y][player.x] = 2; for(i =0; i < MAXY; i++) { for(j = 0; j < MAXX; j++) { gotoxy(x,y); switch(map[i][j]) { case 0: {textcolor(2); cprintf("."); break;} case 1: {textcolor(9); cprintf("\xdb"); break;} case 2: {textcolor(14); cprintf("\x02"); break;} case 3: {textcolor(11); cprintf("\x01"); break; } case 4: {printf(" "); break;} } x++; } x = 1; y++; } gotoxy(50,1); cprintf("X: %d Y: %d", player.x, player.y); } void updatescreen(void) { map[player.oldy][player.oldx] = 4; gotoxy(player.oldy,player.oldx); switch(map[player.oldy][player.oldx]) { case 0: {textcolor(2); cprintf("."); break;} case 1: {textcolor(9); cprintf("\xdb"); break;} case 2: {textcolor(14); cprintf("\x02"); break;} case 3: {textcolor(11); cprintf("\x02"); break; } case 4: {printf(" "); break;} } gotoxy(50,1); cprintf("X: %d Y: %d", player.x, player.y); }
can't see anything wrong with your program, put the whole code up and i'll try to help
¿Red Baron?
"Imagination is more important than knowledge"
-Albert Einstein (1879-1955)
Check out my games!
[code] /* dont forget code tags! */ [/code]
here's the source
wow was there a lot of errors, since i'm a nice guy i fixed it all for you...
Code:int main() { int quit = 0; drawscreen(); while(!quit) { player.oldy = player.y; player.oldx = player.x; quit = getInput(); updatescreen(); } return 0; } void drawscreen(void) { int i,j, x = 1, y = 1; player.x=1; player.y=1; for(i =0; i < MAXY; i++) { for(j = 0; j < MAXX; j++) { gotoxy(j,i); switch(map[i][j]) { case 0: {textcolor(2); cprintf("."); break;} //dot case 1: {textcolor(9); cprintf("\xdb"); break;} //block case 2: {textcolor(14); cprintf("\x02"); break;} //person case 3: {textcolor(11); cprintf("\x01"); break; } //nme case 4: {printf(" "); break;} //empty } } gotoxy(player.x,player.y); textcolor(14); cprintf("\x02"); } gotoxy(50,1); cprintf("X: %d Y: %d", player.x, player.y); } void updatescreen(void) { map[player.oldy][player.oldx] = 4; gotoxy(player.oldx,player.oldy); switch(map[player.oldy][player.oldx]) { case 0: {textcolor(2); cprintf("."); break;} //dot case 1: {textcolor(9); cprintf("\xdb"); break;} //block case 2: {textcolor(14); cprintf("\x02"); break;} //person case 3: {textcolor(11); cprintf("\x02"); break; } //nme case 4: {printf(" "); break;} //empty } gotoxy(player.x,player.y); textcolor(14); cprintf("\x02"); gotoxy(50,1); cprintf("X: %d Y: %d", player.x, player.y); } int getInput(void) { int move; int quit = 0; move = getch(); if(move==KEY_UP && map[player.y-1][player.x]!=1) player.y--; else if(move==KEY_DOWN && map[player.y+1][player.x]!=1) player.y++; else if(move==KEY_LEFT && map[player.y][player.x-1]!=1) player.x--; else if(move==KEY_RIGHT && map[player.y][player.x+1]!=1) player.x++; else if(move==KEY_ESC) quit=1; return (quit); }
Last edited by red_baron; 07-23-2002 at 08:01 PM.
¿Red Baron?
"Imagination is more important than knowledge"
-Albert Einstein (1879-1955)
Check out my games!
[code] /* dont forget code tags! */ [/code]
One more problem. In your code you have gotoxy(j,i) which on first loop produces gotoxy(0,0) while the first on screen coordinate is 1,1. The 0th elements do print it just that they aren't on screen. I was thinking of just ignoring the 0th elements. Do you think thats right? Besides all this I greatly appreciate you taking your time to help me.
sure i think i missed it didn't notice it started at 0 but go ahead and do that also the colission method you can add again i just had a problem while fixing your code so i added the function to key input but if you want you can put back the function again it willl work, anyway good luck with whatever your gonna do.
¿Red Baron?
"Imagination is more important than knowledge"
-Albert Einstein (1879-1955)
Check out my games!
[code] /* dont forget code tags! */ [/code]