I feel like a newbie again, If functions not working

This is baffling me beyond belief, I have scryed through my code a million times.
When i run this program the IFs in MovPlayer(); don't work. The first one always comes out as true, and the second third and fourth one always come out to true if the player is pressing the desired key. By the way i am using allegro
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
 #include <allegro.h>
BITMAP* MAP;
int playerlocx = 0;
int playerlocy = 0;
int playerloctempx;
int playerloctempy;
 int Map[24][32] = {{3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                   {0,0,0,1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0},
                                   {0,0,0,0,0,1,0,0,0,0,1,0,0,0,1,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0},
                                   {0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0},
                                   {0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0},
                                   {0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                   {0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                   {0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                   {0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,2,2,0,2,2,2,2,2},
                                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,2,0,0,2},
                                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,2,0,0,2},
                                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,2},
                                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,2,0,0,2},
                                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,2,0,0,2},
                                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,2,0,0,2},
                                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,2,0,0,2},
                                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,2,2,2,2,2,0,2,2},
                                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,2,0,0,0,0,2},
                                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,2,0,0,0,0,2},
                                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,2,0,0,0,0,2},
                                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,2},
                                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,2,2,2,2,2,2,2,2}};
                                   
                     /*int ObjMap[24][32] = {{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                   {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                   {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};*/
                                   
void GameInit();
void GameInit()
{
          MAP = create_bitmap( 640, 480);
          for(int count1 = 0; count1 <=24; count1++){
                  for(int count2 = 0; count2 <= 32; count2++){
                          if( Map[count1][count2] == 0 ) rectfill( MAP, count2 * 20, count1 * 20, (count2 + 1) * 20, (count1 + 1) * 20, makecol( 255, 255, 255));
                          else if( Map[count1][count2] == 1 ) rectfill( MAP, count2 * 20, count1 * 20, (count2 + 1) * 20, (count1 + 1) * 20, makecol( 0, 255, 255));
                          else if( Map[count1][count2] == 2 ) rectfill( MAP, count2 * 20, count1 * 20, (count2 + 1) * 20, (count1 + 1) * 20, makecol( 255, 0, 255));
                          else if( Map[count1][count2] == 3 ) rectfill( MAP, count2 * 20, count1 * 20, (count2 + 1) * 20, (count1 + 1) * 20, makecol( 255, 100, 255));
                          
                          }
                          }

                          draw_sprite (screen, MAP, 0, 0);
                          } 
          
void MovPlayer();
void MovPlayer()
{

               playerloctempx = playerlocx;
          playerloctempy = playerlocy;
          if( Map[playerlocy--][playerlocx] == 0 && key[KEY_W]){playerlocy--;}
          else if( key[KEY_A] && Map[playerlocy][playerlocx--] == 0) {playerlocx--;}
          else if( key[KEY_S] && Map[playerlocy++][playerlocx] == 0) {playerlocy++;}
          else if( key[KEY_D] && Map[playerlocy][playerlocx++] == 0) {playerlocx++;}
          Map[playerloctempy][playerloctempx] = 0;
          Map[playerlocy][playerlocx] = 3;

          
                  
          for(int count1 = 0; count1 <=24; count1++){
                  for(int count2 = 0; count2 <= 32; count2++){
                          if( Map[count1][count2] == 0 ) rectfill( MAP, count2 * 20, count1 * 20, (count2 + 1) * 20, (count1 + 1) * 20, makecol( 255, 255, 255));
                          else if( Map[count1][count2] == 1 ) rectfill( MAP, count2 * 20, count1 * 20, (count2 + 1) * 20, (count1 + 1) * 20, makecol( 0, 255, 255));
                          else if( Map[count1][count2] == 2 ) rectfill( MAP, count2 * 20, count1 * 20, (count2 + 1) * 20, (count1 + 1) * 20, makecol( 255, 0, 255));                         
                          else if( Map[count1][count2] == 3 ) rectfill( MAP, count2 * 20, count1 * 20, (count2 + 1) * 20, (count1 + 1) * 20, makecol( 255, 100, 255));
                          }}
                          

                          rest(100);
                          draw_sprite (screen, MAP, 0, 0);
                          return;
                          }       


int main()
{
    allegro_init();
    install_keyboard();
    set_color_depth(16);
    set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0);
    GameInit();
    while(!key[KEY_ESC])
    { MovPlayer();
                        }
    return 0;
}
END_OF_MAIN();

Ignore the comments, I used them because i didn't want to delete the code if i needed it later.
I think you are probably doing things to your playerlocx and playerlocy variables that you don't realize.

Try to keep all modifications to those two variables out of the if conditions (and instead use foo + 1 and foo - 1 as needed), and then update the foos only when needed.

Hope this helps.
in lines 80 - 83, you change your variables during the if check, so whether map stuff is true or not, the player still moves.

the if stops checking after the first false on a line. i had no idea. so the first line was always decrementing playerlocy. the other lines gave false until you pressed a key, then it did the extra movement.

try this:
1
2
3
4
if( Map[playerlocy - 1][playerlocx] == 0 && key[KEY_W]){playerlocy--;}
else if( key[KEY_A] && Map[playerlocy][playerlocx - 1] == 0) {playerlocx--;}
else if( key[KEY_S] && Map[playerlocy + 1][playerlocx] == 0) {playerlocy++;}
else if( key[KEY_D] && Map[playerlocy][playerlocx + 1] == 0) {playerlocx++;}


Allegro4 rules!
I can't thank you enough herbert.
Topic archived. No new replies allowed.