Program draw in order

edited November 2016 in Questions about Code

So recently I made this little game

int enemy1x = (int)random(25, 575);
float speedfactor = 1;
int bulletx = 100000;
int bullety = 500;
int bullet1x = 0;
int bullet1 = 0;
int random1 = 0;
int enemy1y = 0;
int posx = 300;
int posy = 500;
int health = 5;
int enemy1 = 1;
int timer1 = 0;
int timer2 = 0;
int timer3 = 0;
int w = 0;
int d = 0;
int a = 0;



void setup() {
  size(600, 600);
  strokeWeight(3);
}

void keyPressed() {
  if (key == 'w' || key == 'W') w = 1;
  if (key == 'd' || key == 'D') d = 1;
  if (key == 'a' || key == 'A') a = 1;
}

void keyReleased() {
  if (key == 'w' || key == 'W') w = 0;
  if (key == 'd' || key == 'D') d = 0;
  if (key == 'a' || key == 'A') a = 0;
} 

void draw() {

  background(255);

  if (w == 1 && bullet1 == 0) {
    bullet1 = 1;
    bulletx = posx;
  }
  if (d == 1 && posx < 600) posx += (5*speedfactor); 
  if (a == 1 && posx > 0) posx -= (5*speedfactor);

  if (bullet1 == 1) {
    bullety -= (10*speedfactor);
    line(bulletx, bullety, bulletx, bullety +10);
    if (bullety < 0) {
      bullet1 = 0;
      bullety = 500;
    }
  }

  textSize(16);
  fill(0, 102, 153, 51);
  text("health:", 10, 30); 
  text(health, 65, 30);

  textSize(16);
  fill(0, 102, 153, 51);
  text("speedfactor:", 10, 45); 
  text(speedfactor, 110, 45);


  if (enemy1 == 0) {
    random1 = (int)random(0, (1000/speedfactor)); 
    enemy1 = 1;
    timer1 = millis();
  }
  timer2 = millis();
  timer3 = timer2-timer1;
  if (timer3 > random1) { 
    enemy1y += (2*speedfactor);
    if (enemy1y > 500) {
      health -= 1;
      timer1 = 0;
      timer2 = 0;
      timer3 = 0;
      bullet1 = 0;
      enemy1y = -50;
      bullety = 500;
      enemy1x = (int)random(25, 575);
      if (speedfactor > 1.5) speedfactor -= 0.5;
    }
    if ((bulletx > (enemy1x - 25)) && bulletx < (enemy1x + 25) && (bullety < enemy1y)) {
      speedfactor += 0.1;
      enemy1y = -50;
      timer1 = 0;
      timer2 = 0;
      timer3 = 0;
      enemy1 = 0;
      bullety = 500;
      bullet1 = 0;
      enemy1x = (int)random(25, 575);
    } 
    rectMode(CENTER);
    fill(255);
    rect(enemy1x, enemy1y, 50, 50);
  }

  fill(255);
  triangle(posx-15, posy, posx+15, posy, posx, posy-60);
}

but when the game runes and you shoot a "bullet" it renders the bullet over the enemy block even though I coded it to draw the bullet first and then the enemy?

Also when the speedfactor reaches 5 everything starts to glitch out for instance there some code in there that says something like this when player within the boundaries move to the right or left and when it's almost out of the boundaries it can't move in that direction but when the speedfactor reaches 5 it just stops working?

I hope someone knows the answer :)

Answers

  • edited November 2016

    Um yeah, that happens for a reason. Let me explain:
    At speed factor 5, what happens is that the size of the player is small compared to the distance it moves. In one frame it is inside the screen and in the next it is outside.

  • Is there a way to change this without chaning the speed factor? maybe add a delay

  • Actually there is, can I tell you tomorrow after testing it?
    P.S. Please mention me when you reply(with @Lord_of_the_Galaxy)

  • Thank you, have you got any idea yet of what the problem could be? @Lord_of_the_Galaxy

  • Replace

    if (d == 1 && posx < 600) posx += (5*speedfactor); 
    if (a == 1 && posx > 0) posx -= (5*speedfactor);
    

    with

    if (d == 1) posx += (5*speedfactor); 
    if (a == 1) posx -= (5*speedfactor);
    if(posx > 600)posx = 600;
    if(posx < 0)posx = 0;  
    
  • As to why the bullet appears over the enemy, I don't have any idea.

  • The bullet doesn't appear over the enemy. That is an optical illusion.

    Change to frameRate(3) and play your game (slooowly) to see what is happening.

    1. Your bullet moves to where the enemy is
    2. You draw the bullet.
    3. You check if the enemy was hit.
    4. You don't draw the enemy.

    Result -- for one frame, a bullet is hanging in midair where the enemy was.

    To fix this, update the bullet location and check for a collision first before drawing anything. If collision, draw nothing. If no collision, draw the bullet first, then the enemy.

  • Thank you @jeremydouglass

    now it renders properly :)

Sign In or Register to comment.