Can someone help me with my code?

I don’t know how to make a code that makes my enemy follow the player. I have made some code but when every I run it it just freezes! Can someone help in the comments?
This is my code:

#include <SPI.h>
#include <Gamebuino.h>
Gamebuino gb;

//player variables
int player_h = 4;
int player_w = 4;
int player_x = (LCDWIDTH - player_w)/2;
int player_y = (LCDHEIGHT - player_h)/2;
int player_vy = 2;
//enemy variables
int enemy_h =6;
int enemy_w =6;
int enemy_x =(LCDWIDTH - enemy_w)/2;
int enemy_y =(LCDHEIGHT - enemy_h)/2;

void setup() {
// put your setup code here, to run once:
gb.begin();
gb.titleScreen(F(“Knight’s walk”));
gb.pickRandomSeed();
gb.battery.show = false;
gb.display.fontSize = 2;
}

void loop() {
// put your main code here, to run repeatedly:

if(player_x > enemy_x){
enemy_x = enemy_x + 1;
}
else if(enemy_x > player_x){
enemy_x = enemy_x - 1;
}
if(player_y > enemy_y){
enemy_y = enemy_y + 1;
}
else if(enemy_y > player_y){
enemy_y = enemy_y - 1;
}

 gb.display.fillRect(enemy_x, enemy_y, enemy_w, enemy_h);

if(gb.update()){

if(gb.buttons.repeat(BTN_UP, 1)){
  player_y = max(0, player_y - player_vy);
}
if(gb.buttons.repeat(BTN_DOWN, 1)){
  player_y = min(LCDHEIGHT - player_h, player_y + player_vy);
}
if(gb.buttons.repeat(BTN_LEFT, 1)){
  player_x = max(0, player_x - player_vy);
}
if(gb.buttons.repeat(BTN_RIGHT, 1)){
  player_x = min(LCDWIDTH - player_w, player_x + player_vy);
}
gb.display.fillRect(player_x, player_y, player_w, player_h);


}

}

1 Like

Is your follow enemy/player the one in the main loop?
I only get a glimpse of what you’re trying to do.
I would recommend breaking up these things into functions. Your main loop looks like a function. It also feels like it’s missing a logical step. It’s calling each step directly. That’s problematic because it’s running one line at a time. That’s why I would do a checks and balance function first with your if statements. (Add some Boolean variables maybe?)
At first, I thought your main was missing &&s. So that might be contributing to its confusion.
I had a very similar problem last week with widgets in Python. I was calling widgets and one of the last widgets couldn’t be placed correctly, so the entire window failed. That seems similar to this because your entire program is frozen. I suspect that has more to do with your characters though. It could be a similar problem to the visibility function in RPG Maker. Something could be cancelling it out or stopping a function from running.(In RPG Maker you can accidentally freeze the character by not calling on him as the actor).

I don’t really understand you because I’m not that good at programming.
I was trying to check if player(xpos) is bigger than enemy(xpos), if it is then I make enemy(xpos) bigger(+1) and if enemy(xpos) is bigger than player(xpos), then make enemy(xpos) smaller(-1).
Same for the ypos.

1 Like

I got this idea from this code:
if (zombiType == 1) {
if (worldZombi[j][1] != 6) {
if (zombiTimer == 0 ) {
worldZombi[j][5] = 2; //zombiSpeed
if (zombixpos >= xposMeInTheWorld) {
worldZombi[j][2] = worldZombi[j][2] - 1;
worldZombi[j][6] = 0;
}
if (zombixpos < xposMeInTheWorld) {
worldZombi[j][2] = worldZombi[j][2] + 1;
worldZombi[j][6] = 1;
}
if (zombiypos >= yposMe && abs(zombixpos - xposMeInTheWorld) < 15) {
worldZombi[j][3] = worldZombi[j][3] - 1;
}
if (zombiypos < yposMe && abs(zombixpos - xposMeInTheWorld) < 15) {
worldZombi[j][3] = worldZombi[j][3] + 1;
}
} else {
worldZombi[j][5] = worldZombi[j][5] - 1;
if (worldZombi[j][1] == 5 && worldZombi[j][5] == 0) {
worldZombi[j][1] = 1;
}
}
}

It is on the main loop.

#include <SPI.h>
#include <Gamebuino.h>
Gamebuino gb;

//player variables
int player_h = 4;
int player_w = 4;
int player_x = (LCDWIDTH - player_w)/2;
int player_y = (LCDHEIGHT - player_h)/2;
int player_vy = 2;
//enemy variables
int enemy_h =6;
int enemy_w =6;
int enemy_x =(LCDWIDTH - enemy_w)/2;
int enemy_y =(LCDHEIGHT - enemy_h)/2;
int number_1 = 1;
//functions
int DistanceAddX(int enemy_x,int number_1)
{
int result;

result = enemy_x + number_1;

return result;

}

int DistanceRemoveX(int enemy_x,int number_1)
{
int result;

result = enemy_x - number_1;

return result;

}

int DistanceAddY(int enemy_y,int number_1)
{
int result;

result = enemy_y + number_1;

return result;

}

int DistanceRemoveY(int enemy_y,int number_1)
{
int result;

result = enemy_y - number_1; 

return result;

}
void setup() {
// put your setup code here, to run once:
gb.begin();
gb.titleScreen(F(“Knight’s walk”));
gb.pickRandomSeed();
gb.battery.show = false;
gb.display.fontSize = 2;
}

void loop() {
// put your main code here, to run repeatedly:

if(player_x > enemy_x)
{
  
return DistanceAddX;
gb.display.fillRect(enemy_x, enemy_y, enemy_w, enemy_h);

} 
else if(player_x < enemy_x)
{
  
return DistanceRemoveX;
gb.display.fillRect(enemy_x, enemy_y, enemy_w, enemy_h);

}          

  if(player_y > enemy_y)
  {
  
  return DistanceAddY;
  gb.display.fillRect(enemy_x, enemy_y, enemy_w, enemy_h);

  } 
  else if(player_y < enemy_y)
  {
  
  return DistanceRemoveY;
  gb.display.fillRect(enemy_x, enemy_y, enemy_w, enemy_h);
    
  }

if(gb.update()){

if(gb.buttons.repeat(BTN_UP, 1)){
  player_y = max(0, player_y - player_vy);
}
if(gb.buttons.repeat(BTN_DOWN, 1)){
  player_y = min(LCDHEIGHT - player_h, player_y + player_vy);
}
if(gb.buttons.repeat(BTN_LEFT, 1)){
  player_x = max(0, player_x - player_vy);
}
if(gb.buttons.repeat(BTN_RIGHT, 1)){
  player_x = min(LCDWIDTH - player_w, player_x + player_vy);
}
gb.display.fillRect(player_x, player_y, player_w, player_h);


}

}

I have added functions as you can see but it still froze. :confused:

1 Like

Sorry, it was late when I read this.

It looks like there’s no try-catch if your enemy is equal to the player’s place. They shouldn’t be in the same spot? If that still doesn’t help. I would have to say write each line down on a piece of paper and graph what you’re doing. You can additionally add comments for yourself.
When I write programs, I make the bare minimum of each asset to it. If a widget doesn’t load or a logical error occurs, I try to solve one thing at a time. I would like to review this later today. I’ll let you know if I find something.
Overall, it could be incompatible with the library unfortunately. I haven’t been able to use Makerbuino yet.

Thank you I will try!

1 Like

Still… it doesn’t make any difference, I will try your technique and if it works then good if it doesn’t I don’t know :frowning:

1 Like

No worries. Programming is a very deep and complex study. One of few things that are near impossible to fully learn on your own.
I was in the middle of fixing my Ringo last night. I will have time to peer review this program this evening. If I have anything worth noting, I’ll even take a picture.

Thank you very much. :smiley:

1 Like

#include <SPI.h> //import connection
#include <Gamebuino.h> //import arduino game library
Gamebuino gb; //build gamebuino object

//player variables
int player_h = 4; //height of 4x4 player
int player_w = 4; //width of 4x4 player
int player_x = (LCDWIDTH - player_w)/2; //player starting cords
int player_y = (LCDHEIGHT - player_h)/2; //player starting cords
int player_vy = 2; //???
//enemy variables
int enemy_h =6; //height of 6x6 enemy
int enemy_w =6; //width of 6x6 enemy
int enemy_x =(LCDWIDTH - enemy_w)/2; //enemy starting cords
int enemy_y =(LCDHEIGHT - enemy_h)/2; //enemy starting cords
int number_1 = 1; //mutable number is not UPPERCASE

//enemy functions
int DistanceAddX(int enemy_x,int number_1) //calls enemy cord + 1
{
int result;

result = enemy_x + number_1;

return result;
}

int DistanceRemoveX(int enemy_x,int number_1) //calls enemy cord -1
{
int result;

result = enemy_x - number_1;

return result;
}

int DistanceAddY(int enemy_y,int number_1) //calls enemy cord +1
{
int result;

result = enemy_y + number_1;

return result;
}

int DistanceRemoveY(int enemy_y,int number_1) //calls enemy cord - 1
{
int result;

result = enemy_y - number_1;

return result; //make this resultRY? change up result
}
void setup() {
// put your setup code here, to run once:
gb.begin(); //begin gamebuino
gb.titleScreen(F(“Knight’s walk”)); //title name
gb.pickRandomSeed(); //generate random seed
gb.battery.show = false; //turn off battery meter
gb.display.fontSize = 2; //font is set
}

void loop() {
// put your main code here, to run repeatedly:

if(player_x > enemy_x) //player is below enemy
{

return DistanceAddX; //is enemy on the screen?
gb.display.fillRect(enemy_x, enemy_y, enemy_w, enemy_h); //create enemy

}
else if(player_x < enemy_x)
{

return DistanceRemoveX;
gb.display.fillRect(enemy_x, enemy_y, enemy_w, enemy_h); //redraw enemy

}

if(player_y > enemy_y)
{

return DistanceAddY;
gb.display.fillRect(enemy_x, enemy_y, enemy_w, enemy_h); //redraw enemy

}
else if(player_y < enemy_y)
{

return DistanceRemoveY;
gb.display.fillRect(enemy_x, enemy_y, enemy_w, enemy_h); //redraw enemy

}

if(gb.update()){ //if? why not while?

if(gb.buttons.repeat(BTN_UP, 1)){
player_y = max(0, player_y - player_vy); //vy? variable y cord?
}
if(gb.buttons.repeat(BTN_DOWN, 1)){
player_y = min(LCDHEIGHT - player_h, player_y + player_vy);
}
if(gb.buttons.repeat(BTN_LEFT, 1)){
player_x = max(0, player_x - player_vy);
}
if(gb.buttons.repeat(BTN_RIGHT, 1)){
player_x = min(LCDWIDTH - player_w, player_x + player_vy);
}
gb.display.fillRect(player_x, player_y, player_w, player_h); //create player

}
}

I’m not sure what your player is doing. It confused me. I’m also not familiar with max and min methods.
I wouldn’t write it like this. I would make the player and enemy one function and call them from your buttons. I haven’t done anything like this before.
You should focus on one thing at a time. Build your player separate from things like your title. It should be a concept of its own. When you understand what your character is doing, then figure out what your enemy is doing. Even if it’s just animations.
I’m not sure how the seed is used on gamebuino. I’m sure it isn’t too related to the problem. Just work on this one step at a time. You’re trying to finish a game before the paint dries.

It was based on pong tutorial with player movements.

1 Like

Omg, I did it follows the player but still, it follows it but it is on it:
Here is the download:KNIGHTSW.HEX (29.1 KB) (Try going to up-left corner and you will see a enemy on a player!)

#include <SPI.h>
#include <Gamebuino.h>
Gamebuino gb;

//player variables
int player_h = 4;
int player_w = 4;
int player_x = (LCDWIDTH - player_w)/2;
int player_y = (LCDHEIGHT - player_h)/2;
int player_vy = 2;
//enemy variables
int enemy_h =6;
int enemy_w =6;
int enemy_x = (LCDWIDTH - enemy_w)/2;
int enemy_y = 48;
int enemy_d = 0;


void setup() {
  // put your setup code here, to run once:
  gb.begin();
  gb.titleScreen(F("Knight's walk"));
  gb.pickRandomSeed();
  gb.battery.show = false;
  gb.display.fontSize = 2;
 
}

void loop() {
  // put your main code here, to run repeatedly:
  
            if (enemy_x >= player_x) {
              delay(1000000);
              enemy_x = enemy_x - 1;
              enemy_d = 0;
              gb.display.fillRect(enemy_x, enemy_y, enemy_w, enemy_h); 
            }
            if (enemy_x < player_x) {
              delay(1000000);
              enemy_x = enemy_x + 1;
              enemy_d = 1;
              gb.display.fillRect(enemy_x, enemy_y, enemy_w, enemy_h); 
            }
            if (enemy_y >= player_y && abs(enemy_x - player_x) < 15) {
               delay(1000000);
               enemy_y = enemy_y - 1;
               gb.display.fillRect(enemy_x, enemy_y, enemy_w, enemy_h); 
            }
            if (enemy_y < player_y && abs(enemy_x - player_x) < 15) {
              delay(1000000);
               enemy_y = enemy_y + 1;
               gb.display.fillRect(enemy_x, enemy_y, enemy_w, enemy_h); 
            }
      
  if(gb.update()){

    if(gb.buttons.repeat(BTN_UP, 1)){
      player_y = max(0, player_y - player_vy);
    }
    if(gb.buttons.repeat(BTN_DOWN, 1)){
      player_y = min(LCDHEIGHT - player_h, player_y + player_vy);
    }
    if(gb.buttons.repeat(BTN_LEFT, 1)){
      player_x = max(0, player_x - player_vy);
    }
    if(gb.buttons.repeat(BTN_RIGHT, 1)){
      player_x = min(LCDWIDTH - player_w, player_x + player_vy);
    }
    gb.display.fillRect(player_x, player_y, player_w, player_h);

   
    }
  }
1 Like

That’s great!! I can’t say what you have in mind. That’s the beauty of programming. You sort of figure out what you want to do as you go along with modifications. It’s a process, and after the journey - You’re Picasso :slight_smile:
I also haven’t spent much time on this library, but I can help with other Arduino code in the future.

There a small problem because the enemy is on the player(I need to make it slower but I don’t know how :confused: )

1 Like

Can you use .5 instead of one? Also make the player faster?

Will try!
Thank you.

If I do any of these things game freezes?
Weird…

1 Like