Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Circles followng player

This thread is locked; no one can reply to it. rss feed Print
Circles followng player
Sadako
Member #17,047
March 2019

So basically I made 2 characters. one is my player and another one is An AI.
If I press A my character fires up the circle. I used the same method for the AI but basically the circle is staying on his body and not moving even though I did this

``` if (fireballFollow2)
{
cY2 = Y2; // set position of circle to player's Y
cX2 = X2; // set position of circle to player's X
cY2 += 50; // move the circle from player's Y
cX2 += 50; // move the circle from player's X
fireballFollow2 = false;
}```

Chris Katko
Member #1,881
January 2002
avatar

Is that the exact code from your game? Because if you hand typed it, you may have missed a typo that is the source of the bug. Like if you did if(fireballfollow2 = true){} you're setting it true every time and setting it to player x/y every time, and then adding +50 so it's always player_x + 50 and player_y + 50.

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

Peter Hull
Member #1,136
March 2001

I think it's because the block of code you have on lines 107..140 (which I believe is intended to reset the position of player 2's fireball) gets called every timer tick, instead of just when player 2 is going to fire. Hence the fireball never gets anywhere.

Also, do you know about switch?

Hope that helps.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Welcome to a.cc and hello. ;)

Did you know that an enum is another data type? You can declare variables of enum type.

#SelectExpand
1enum DIRECTION { 2 DIR_NONE = -1, 3 DIR_NORTH = 0, 4 DIR_EAST = 1, 5 DIR_SOUTH = 2, 6 DIR_WEST = 3 7}; 8 9DIRECTION playerdir = DIR_NORTH; 10 11int* playery = &py1; 12int* playerx = &px1; 13 14if (playerdir != DIR_NONE) { 15 if (playerdir % 2 == 0) { 16 // NS 17 *playery += yspeed*(playerdir == DIR_NORTH)?-1:1; 18 } 19 else { 20 // EW 21 *playerx += xspeed*(playerdir == DIR_EAST)?1:-1; 22 } 23}

See how simple your code can be? Look into using pointers and arrays and bit flags.

Peter Hull
Member #1,136
March 2001

Also please don't make 2 topics with the same title. Life is confusing enough without this.
Pete

Sadako
Member #17,047
March 2019

So I've fixed the code like u guys said however so on "level 2" of game when I pressed enter the player2 is only firing circle on left side when it should fire 2 circle at the same time. Left and Right

Peter Hull
Member #1,136
March 2001

OK this is just advice so don't take it the wrong way. I looked at the code and I found it hard to follow. I think it will become unmanageable as your game gets more complex. Without adding any more language features just now (structs and whatever will help in the future..), try to identify all the 'things' in the program and what variables each one needs. Then group these together and name them consistently.
Then look at everything that needs to happen and group those into sections in the main loop with comments to explain what is happening.
Then, I think, you will find it easier to see the patterns and where the code does not match your intention.
For example, I'd say you have two players and two (later three) fireballs - so put the variables for each one of those together. And, in terms of actions, you've got things like 'Process input', 'check if on screen', 'move player', 'move fireballs' and so on.

In practical terms I noticed that when destroyBG gets set to true it never gets set back to false and the code reloads the 'bg2.png' bitmap every cycle which looks wrong to me.

Also, you can start a new topic every time you have a new question, or continue on the old one but don't do both!

[edit] Line 367-389, this doesn't look right;

#SelectExpand
1 if (fireballUpLV2) 2 { 3 al_draw_circle(cX3, cY3, 20, al_map_rgb(200, 200, 50), 5); 4 cY3 -= speed + 5; 5 6 } 7 else if (fireballDownLV2) 8 { 9 al_draw_circle(cX3, cY3, 20, al_map_rgb(200, 200, 50), 5); 10 cY3 += speed + 5; 11 12 } 13 else if (fireballLeftLV2) 14 { 15 al_draw_circle(cX3, cY3, 20, al_map_rgb(200, 200, 50), 5); 16 cX3 -= speed + 5; 17 18 } 19 else if (fireballRight2) 20 { 21 al_draw_circle(cX3, cY3, 20, al_map_rgb(200, 200, 50), 5); 22 cX3 += speed + 5; 23 }

fireballRight2 should be fireballRightLV2 ? This is what I mean about 'patterns'

Go to: