Jump to content

problem about processing

Featured Replies

I have studying processing for 1 month. I would like to make a shooting game .When I run the program and shot the fire, the fire I shot recently will stay in the original place. 

When the enemy passes where I once l, they die automatically without pressing the shooting key ( 'c' || 'C').

I am confuesd with this problem two days long!Hope anyone can answer my question. Thank you!

 

 

 

//Background 
PImage universe;                             
PImage universe1;
PImage winPage;

// player
PImage buzzLightyear;  
int buzzLightyearW;
int buzzLightyearH;
float playerLeft;
float  playerRight;
float  playerTop;
float playerBottom;
int x;
int y;

// lgm
PImage littleGreenMan;
int totalNumLgm = 15;
int littleGreenManW;
int littleGreenManH;
float[] dx = new float [totalNumLgm];
float[] dy = new float [totalNumLgm];
float []lgmLeft =new float[totalNumLgm];
float []lgmRight= new float[totalNumLgm];
float []lgmTop= new float[totalNumLgm];
float []lgmBottom= new float[totalNumLgm];
float []lgmX= new float[totalNumLgm];
float []lgmY= new float[totalNumLgm];
int lgmBossPage = 4;
float []lgmX1= new float[lgmBossPage];
float []lgmY1= new float[lgmBossPage];

//fire 
int totalFireNum = 20;
PImage[] fireShape = new PImage [totalFireNum];
float [] fireW = new float[totalFireNum];
float [] fireH = new float[totalFireNum];
float distanceBetweenFireAndBuzz;
boolean [] fireAppear= new boolean[totalFireNum];
float[] fireX= new float[totalFireNum];
float[] fireY = new float[totalFireNum];
int fireSpeed = 15;
float[]fireLeft = new float[ totalFireNum];
float[]fireRight = new float[ totalFireNum];
float[]fireTop = new float[ totalFireNum];
float[]fireBottom = new float[ totalFireNum];


// boss 
PImage greenDinosaur;         
int bossHp= 30;
float a = 800.1;
float b =300.1;
float ax;
float by;
int bossW = 300;
int bossH =300;
float bossRight;
float bossLeft;
float bossTop;
float bossBottom;

//others
boolean gameOver;
int score;
int count;
boolean up, down, left, right;
int stageNum= 0;
PImage lose;

void setup()
{
  size (1000, 750);
  universe = loadImage("universe.jpg");
  buzzLightyear = loadImage("buzzLightyear.png");
  littleGreenMan = loadImage("littleGreenMan.png");
  greenDinosaur = loadImage("GreenDinosaur.png");
  universe1 = loadImage("universe1.jpg");
  winPage = loadImage("winpage.png");

  //player 
  buzzLightyearW =100;
  buzzLightyearH = 100;


  //lgm
  littleGreenManW = 100;
  littleGreenManH = 100;
  for ( int p = 0; p< totalNumLgm; p++)
  {
    lgmX[p] = random(1100, 1500);
    lgmY[p] = random(0, 750);
    dy[p] = random(0.5, 3);
    dx[p] = random(0.5, 3);
  }

  for (int v=0; v<lgmBossPage; v++)
  {
    lgmX1[v] = random(width/2, width-littleGreenManW);
    lgmY1[v] = random(150, 600);
      dy[v] = -random(0.05, 0.2);
      dx[v] =-random(0.08, 0.1);
  }

  //fire
  for (int i = 0; i < totalFireNum; i=i+1)
  {
    fireShape = loadImage("fireShape.png");
    fireW = 300;
    fireH =100;
    float scale = random(0.3, 0.5);
    fireW = fireW* scale;
    fireH =fireH * scale;
    fireAppear = false;
    fireX = -100;
    fireY = -100;
    fireAppear = false;
  }

  //others
  gameOver = false;
  up = false;
  down = false;
  left = false;
  right = false; 
  x = width/2;
  y = height/2;
}


void draw() {
  if (stageNum==0) {
    welcomePage();
  } else if (stageNum == 1) {
    gamePage();
  } else if (stageNum == 2)
  {
    gameOverPage();
  } else if (stageNum ==3)
  { 
    bossPage();
  } else if (stageNum ==4)
  {
    winPage();
  }
}

void welcomePage()
{
  stageNum =0;
  image(universe, 0, 0, width, height);
  image(buzzLightyear, 0, 250, 250, 250);
  image(littleGreenMan, 700, height/3*1.5, 300, 300);
  image(greenDinosaur, 700, 100, 300, 300);
  textSize(40);
  fill(255, 0, 0);
  text("Instruction: Press 'g' or 'G' to start", 10, 40);
  text("Survive and kill the", 10, 80);
  text("boss then win the game", 10, 120);
  text( "Press 'c' or 'C' to shoot the enemies", 10, 160);
}

void gamePage()
{
  //  stageNum = 1; 

  image(universe, 0, 0, width, height);
  textSize(50);
  fill(255, 0, 0);
  text("Time:", 10, 50);
  text("Score:" +score, 10, 100);
  count =count +1;
  text("Time:" + count/60+"s", 10, 50);
  image(buzzLightyear, x, y, buzzLightyearW, buzzLightyearH);


  if ( gameOver  == false)
  {
    for ( int p = 0; p< totalNumLgm; p++)
    {
      image(littleGreenMan, lgmX[p], lgmY[p], littleGreenManW, littleGreenManH);
      lgmY[p] = lgmY[p] - dy[p];
      lgmX[p] = lgmX[p] - dx[p];

      lgmLeft[p]= lgmX[p]+10;
      lgmRight[p] = lgmX[p]+littleGreenManW+10;
      lgmTop[p] = lgmY[p];
      lgmBottom[p] =lgmY[p]+littleGreenManH;

      if ( lgmY[p]< 0)
      { 
        dy[p] = -dy[p];
      }
      if (lgmY[p]+littleGreenManH>height)
      {
        lgmY[p]  =height- littleGreenManH;
        dy[p] = -dy[p];
      }

      if (lgmX[p]<0)
      {
        lgmX[p] = random(1100, 1700);
        lgmY[p] = random(0, 750);
        dy[p] = random(1.3, 4);
        dx[p] = random(1.3, 4);
      }
    }
    if (up)  
    {  
      y = y - 6;
    }
    if (down) 
    {
      y = y + 6;
    }
    if (left) 
    {
      x = x - 6;
    }
    if (right) 
    {
      x = x + 6;
    }

    if (y<0)                                 
    {
      y=0;
    }
    if (x<0)
    {
      x=0;
    }
    if (y>750-buzzLightyearH)
    {
      y=750-buzzLightyearH;
    }
    if (x>1000-buzzLightyearW)
    {
      x=1000-buzzLightyearW;
    }

    playerLeft=x;
    playerRight=x+buzzLightyearW;
    playerTop=y;
    playerBottom=y+buzzLightyearH;
    for (int i = 0; i < totalFireNum; i = i+1)
    {      


      if ( fireAppear==true)
      {
        image(fireShape, fireX, fireY, fireW, fireH);    
        fireX = fireX+fireSpeed;


        fireRight = fireX + fireW;
        fireLeft = fireX;
        fireTop = fireY;
        fireBottom = fireY+fireH;
      }

      if ( fireX+fireW>1000)
      {
        fireAppear = false ;
      }
    }

    for ( int p = 0; p< totalNumLgm; p++)
    {
      for (int i = 0; i < totalFireNum; i = i+1)
      {      
        if ( fireRight>lgmLeft[p] && fireLeft <lgmRight[p]&&  fireTop<lgmBottom[p] && fireBottom>lgmTop[p])

        {
          fireAppear = false ;
          lgmX[p] = random(1100, 1700);
          lgmY[p] = random(0, 750);
          dy[p] = random(0.5, 4);
          dx[p] = random(0.5, 4);

          score =score+1;
          break;
        }
      }

      if (playerRight>lgmLeft[p] && playerLeft<lgmRight[p] &&  playerTop<lgmBottom[p] && playerBottom>lgmTop[p])
      {
        gameOver = true;
        stageNum=2;
      }
      if ( score ==10)
      {
        stageNum = 3;
      }
    
    }
  }
}

void bossPage() {

  //stageNum=3;

  image(universe1, 0, 0, width, height);
  text("Time:", 10, 50);
  text("Score:" +score, 10, 100);
  count =count +1;
  text("Time:" + count/60+"s", 10, 50);
  image(buzzLightyear, x, y, buzzLightyearW, buzzLightyearH);
  image(greenDinosaur, a, b, bossW, bossH);

  for (int i = 0; i < totalFireNum; i = i+1)
  {      


     if ( fireX+fireW>1000)
      {
        fireAppear = false ;
        fireX = -300;
      }

    if (up)  
    {  
      y = y - 1;
    }
    if (down) 
    {
      y = y + 1;
    }
    if (left) 
    {
      x = x - 1;
    }
    if (right) 
    {
      x = x + 1;
    }

    if (y<0)                                 
    {
      y=0;
    }
    if (x<0)
    {
      x=0;
    }
    if (y>750-buzzLightyearH)
    {
      y=750-buzzLightyearH;
    }
    if (x>1000-buzzLightyearW)
    {
      x=1000-buzzLightyearW;
    }
  }
  for (int i = 0; i < totalFireNum; i = i+1)
  {   


    if ( fireAppear==true)
    {
      image(fireShape, fireX, fireY, fireW, fireH);    
      fireX = fireX+fireSpeed;
      fireRight = fireX + fireW;
      fireLeft = fireX;
      fireTop = fireY;
      fireBottom = fireY+fireH;
    }


    image( greenDinosaur, a, b, bossW, bossH);
    ax = -random(0.1, 0.2);
    by = random(-0.1, 0.1);
    a = a+ax;
    b = b+by;

    if (b<0)
    {
      b=0;
      by = -by;
    }
    else if (b+bossH>height)
    {
      b = height-bossH;
      by = -by;
    }
    bossRight=a+bossW;
    bossLeft=a;
    bossTop=b;
    bossBottom=b+bossH; 


    if ( fireRight>bossLeft && fireLeft <bossRight &&  fireTop<bossBottom && fireBottom>bossTop)
    {
      fireAppear = false ;
      bossHp = bossHp-1;
    }

    playerLeft=x;
    playerRight=x+buzzLightyearW;
    playerTop=y;
    playerBottom=y+buzzLightyearH;


    for (int v = 0; v<lgmBossPage; v++)
    {  

      image(littleGreenMan, lgmX1[v], lgmY1[v], littleGreenManW, littleGreenManH);
      dy[v] = -random(0.2, 0.5);
      dx[v] =-random(0.2, 0.5);
      lgmX1[v] =lgmX1[v]+dx[v];
      lgmY1[v] = lgmY1[v]+dy[v];

      if (lgmX1[v]+littleGreenManW>width)
      {
        dx[v]=-dx[v];
        lgmX1[v] = width-littleGreenManW;
      } else if (lgmX1[v]<0) {
        lgmX1[v]=0;
        dx[v]=-dx[v];
      }
      if (lgmY1[v] <0)
      {
        lgmY1[v]=0;
        dy[v]=-dy[v];
      } else if ( lgmY1[v]+littleGreenManH>height)
      {
        lgmY1[v] = height-littleGreenManH;
        dy[v] =-dy[v];
      }
    } 

    if (playerRight> bossLeft && playerLeft < bossRight && playerTop<bossBottom && playerBottom > bossTop) 
    {
      gameOver =true;
      stageNum=2;
    }
    if (a<0)
    {
      gameOver = true;
      stageNum = 2;
    }


    if ( bossHp==0)
    {  
      stageNum=4;
    }
  }
}

void gameOverPage()
{
  //  stageNum = 2;
  image(universe1, 0, 0, width, height);
  textSize(50);
  fill( 175, 130, 140);
  text("press Q or q to restart the game", width/2-350, height/2);
}


void winPage()
{
  //  stageNum = 4;
  image(winPage, 0, 0, width, height);
  fill(0);
  textSize(40);
  text("You save your friend!", width/2+80, 80);
  text("Press ' k' to restart", width/2+80, 160);
}

void keyPressed()
{
  if ((key =='G' || key == 'g') && stageNum==0)
  { 
    stageNum =1;
    gameOver =false;
  }
  if ((key == 'q'|| key == 'Q')  && stageNum ==2)
  {
    stageNum =0;
    setup();
    score=0;
    count=0;
  }
  if ((key == 'k' || key =='K') && stageNum ==4)
  {

    stageNum =0;
    setup();
    score =0;
    bossHp=30;
    count=0;
  }

  if (keyCode == UP) 
    up=true;
  if (keyCode == DOWN)
    down=true;
  if (keyCode == LEFT)
    left=true;
  if (keyCode == RIGHT)
    right=true;


  if (key =='c' || key == 'C')
  {
    for (int i = 0; i < totalFireNum; i = i+1)
    {
      if (fireAppear == false  )
      {
        fireX = x+buzzLightyearW;
        fireY = y+buzzLightyearH/2;
        fireAppear = true;
        break;
      }
    }
  }
}

void keyReleased()
{

  if (keyCode == UP)
    up=false;
  if (keyCode == DOWN)
    down=false;
  if (keyCode == LEFT)
    left=false;
  if (keyCode == RIGHT)
    right=false;
}

You declared fire parameters at the beginning as arrays:

float[] fireX= new float[totalFireNum];
float[] fireY = new float[totalFireNum];
int fireSpeed = 15;
float[]fireLeft = new float[ totalFireNum];
float[]fireRight = new float[ totalFireNum];
float[]fireTop = new float[ totalFireNum];
float[]fireBottom = new float[ totalFireNum]; 

("fire" is inappropriate name. You rather meant "missile".. ?)
but later you're treating them as regular scalar variables...

e.g.

	image(fireShape, fireX, fireY, fireW, fireH);    
        fireX = fireX+fireSpeed;
	
        fireRight = fireX + fireW;
        fireLeft = fireX;
        fireTop = fireY;
        fireBottom = fireY+fireH;
	

(addition of float/integer to array pointer (address) ?!)

 

You should make loop going through entire list of missiles, display them one by one, and change their positions independently, incrementing/changing array fields..

 

Edited by Sensei

Archived

This topic is now archived and is closed to further replies.

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.