Quantcast
Channel: Android Mode - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 941

Sketch running slow

$
0
0

So I made this little game with pictures (they are hosted online so anyone can run this script) on my computer it runs fine but when I move to a device with lower specs it starts to drop to like 5fps how would I solve this? also when running this code it could look clitchy since I didn't keep aspect ratios in mind when making it.

EDIT: I made the stupid mistake of not mentioning that when I when I said "a lower spec system" I meant an andriod phone

ArrayList <Bullet> bullets;
int background_y;

PImage background;
PImage player;
PImage enemy;
PImage bullet;

int bullet_traveling;
int bullet_timer1;
int bullet_timer2;
int bullet_shot;
int bullet_side;
int bullet_x;
int bullet_y;

int enemy_health;
int enemy_x;
int enemy_y;

int player_x;
int player_y;

void setup() {
  fullScreen();
  frameRate(60);
  bullets = new ArrayList();

  background = loadImage("https://" + "s21.postimg.org/oubg978af/background.png");
  player = loadImage("https://" + "s13.postimg.org/hy84r98mv/player.png");
  bullet = loadImage("https://" + "s23.postimg.org/45ev766kr/bullet.png");
  enemy = loadImage("https://" + "s27.postimg.org/6njql63gz/enemy.png");

  bullet.resize(bullet.width/3, bullet.height/3);
  background.resize(width, height);

  player_y = height/4*3;
  player_x = width/2;
}

void draw() {
  background();
  for (Bullet temp : bullets) temp.update();
  bullet_enemy();
  player();
  enemy();

  if (mousePressed) {
    if (bullet_timer1 == 0) {
      bullet_timer1 = 1;
      bullet_timer2 = millis();
    }
    if (millis() - bullet_timer2 >= 200) {
      bullet_timer1 = 0;
      if (bullet_side == 1) {
        Bullet temp = new Bullet(player_x+55, player_y);
        bullets.add(temp);
        bullet_side = 0;
      } else {
        Bullet temp = new Bullet(player_x-55, player_y);
        bullets.add(temp);
        bullet_side = 1;
      }
    }
  }
}

void background() {
  image(background, 0, background_y);
  image(background, 0, background_y-height);
  background_y += 1;
  if (background_y >= height) background_y = 0;
}

void enemy() {
  if (enemy_health == 0) {
    enemy_health = 1;
    enemy_y = -enemy.width/2;
    enemy_x = int(random(enemy.width/2, width-enemy.width/2));
  }
  if (enemy_y-enemy.width/2 >= height) {
    enemy_health = 0;
  }
  enemy_y += 3;

  image(enemy, enemy_x-enemy.width/2, enemy_y-enemy.height/2);
}

void player() {
  if (mousePressed && player_x < mouseX) player_x = player_x + 12;
  if (mousePressed && player_x > mouseX) player_x = player_x - 12;
  image(player, -player.width/2+player_x, -player.height/2+player_y);
}

void bullet_enemy() {
  if (player_x <= enemy_x+100 && player_x >= enemy_x-100 || bullet_traveling == 1) {
    bullet_traveling = 1;
    if (bullet_shot == 0) {
      bullet_x = enemy_x;
      bullet_y = enemy_y;
      bullet_shot = 1;
    }
    rect(bullet_x, bullet_y, 10, 10);
    bullet_y = bullet_y + 30 ;
  }

  if (bullet_x <= player_x+player.width/2 && bullet_x >= player_x-player.width/2 && bullet_y >= player_y) {
    bullet_traveling = 0;
    bullet_shot = 0;
  }
}

class Bullet {
  float x;
  float y;
  float speed;

  Bullet(float tx, float ty) {
    x = tx;
    y = ty;
  }

  void update() {
    image(bullet, -bullet.width/2+x, -bullet.height/2+y);
    y -= 25;
    if (x <= enemy_x+enemy.width/2 && x >= enemy_x-enemy.width/2 && y <= enemy_y) {
      x = -10000;
      enemy_health = 0;
    }
  }
}

Viewing all articles
Browse latest Browse all 941

Trending Articles