I wanted to make the enemy follow the player. I run the code on my makerbuino it just froze.
Here is that 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;
int number_1 = 1;
//functions
int DistanceAddX(int enemy_x,int number_1)
{
int result;
result = enemy_x + number_1;
gb.display.fillRect(enemy_x, enemy_y, enemy_w, enemy_h);
return result;
}
int DistanceRemoveX(int enemy_x,int number_1)
{
int result;
result = enemy_x - number_1;
gb.display.fillRect(enemy_x, enemy_y, enemy_w, enemy_h);
return result;
}
int DistanceAddY(int enemy_y,int number_1)
{
int result;
result = enemy_y + number_1;
gb.display.fillRect(enemy_x, enemy_y, enemy_w, enemy_h);
return result;
}
int DistanceRemoveY(int enemy_y,int number_1)
{
int result;
result = enemy_y - number_1;
gb.display.fillRect(enemy_x, enemy_y, enemy_w, enemy_h);
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;
}
else if(player_x < enemy_x)
{
return DistanceRemoveX;
}
if(player_y > enemy_y)
{
return DistanceAddY;
}
else if(player_y < enemy_y)
{
return DistanceRemoveY;
}
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);
}
}