Page 1 of 1

image goes away

Posted: April 27th, 2020, 10:29 pm
by emily
How do I keep the different images of the PacMan from disappearing when I let up on the keys.
Beginner Tutorial 4.2 Code.zip
(3.59 MiB) Downloaded 163 times

Re: image goes away

Posted: April 28th, 2020, 7:17 am
by AleksiyCODE
All of your PutPixel calls are in 'if' statements. So when you don't press any keys, all of if statements are skipped and no pixels are drawn. In this case it is better to separate moving logic from drawing logic. I think a good solution would be adding a variable, that would indicate what direction PacMan is facing and updating this variable based on if statements.
if (wnd.kbd.KeyIsPressed(VK_RIGHT))
{
x = x + 3;
faceDir = 0;//update facing direction here
}
if (wnd.kbd.KeyIsPressed(VK_LEFT))
{
x = x - 3;
faceDir = 1;//update facing direction here
}
if (wnd.kbd.KeyIsPressed(VK_DOWN))
{
y = y + 3;
faceDir = 2;//update facing direction here
}
if (wnd.kbd.KeyIsPressed(VK_UP))
{
y = y - 3;
faceDir = 3;//update facing direction here
}
and after that put your drawing code in similar if statements
if(faceDir==0)
{
//draw right facing
}
if(faceDir==1)
{
//draw left facing
}
..etc

Re: image goes away

Posted: April 29th, 2020, 12:56 am
by emily
ok thanks i'll try that method.