Secondary game programming question...
in
Programming Questions
•
2 years ago
Hi everyone!
I posted here a few days ago and was promptly advised on how to solve my problem, which was creating bullets for a 'shoot-em-up' style game that i'm attempting to make. (thank you phi.lho!)
I have since had to learn a different method for storing, displaying and removing the individual bullets on a condition. (due to the fact that the other method didn't work on processing.js for reasons i can't remember)
I've hit another series of problems that I sincerely can not see how to solve...
As you would see by running the code, the bullets conditionally disappear properly, but only are affected by one of the enemies (i'm assuming it's either the first or last created), and only that same enemy can be 'damaged' and 'destroyed'.
Also, the enemy is only affected damage-wise by the first bullet created onscreen...
EDIT: This version doesn't do any of those things... looks like I messed up the code before I posted it. =(
(another thing that bugs me is the wonky and buggy controls which i'm sure you'll notice, but that's not extremely high priority and i would probably be able to fix it on my own. any advice about it is extremely welcome, though!)
Again thanks SO much to whoever takes the time to read this and help, honestly. =)
I posted here a few days ago and was promptly advised on how to solve my problem, which was creating bullets for a 'shoot-em-up' style game that i'm attempting to make. (thank you phi.lho!)
I have since had to learn a different method for storing, displaying and removing the individual bullets on a condition. (due to the fact that the other method didn't work on processing.js for reasons i can't remember)
I've hit another series of problems that I sincerely can not see how to solve...
As you would see by running the code, the bullets conditionally disappear properly, but only are affected by one of the enemies (i'm assuming it's either the first or last created), and only that same enemy can be 'damaged' and 'destroyed'.
Also, the enemy is only affected damage-wise by the first bullet created onscreen...
EDIT: This version doesn't do any of those things... looks like I messed up the code before I posted it. =(
(another thing that bugs me is the wonky and buggy controls which i'm sure you'll notice, but that's not extremely high priority and i would probably be able to fix it on my own. any advice about it is extremely welcome, though!)
Again thanks SO much to whoever takes the time to read this and help, honestly. =)
- Player ship;
- float plXpos;
- float plYpos;
- float shipXSpd;
- float shipYSpd;
- ArrayList bullets;
- int bNum;
- ArrayList enemies;
- int eNum;
- int stream1x() {return int(plXpos);}
- //int stream2x() {return int(plXpos + 10);}
- int streamy() {return int(plYpos - 25);}
- int randclr() {return int(random(100,255));}
- int randx() {return int(random(20,580));}
- int randhund() {return int(random(0,100));}
- int frameCounter;
- boolean bShooting;
- boolean bulletHit;
- boolean enemyHit;
- boolean enemyDie;
- int score;
- void setup() {
- frameRate(60);
- size(600,800);
- ship = new Player(300,700);
- bullets = new ArrayList();
- enemies = new ArrayList();
- }
- void draw() {
- background(20);
- noCursor();
- fill(255);
- text("Score: "+score, 0, 10);
- ship.movement();
- ship.display();
-
- for (int eNum = enemies.size()-1; eNum >= 0; eNum--) {
- Enemy enemy = (Enemy) enemies.get(eNum);
- enemy.move();
- enemy.display();
- enemy.eDeathChk();
-
- if (enemy.offscreen() || enemyDie == true) {
- enemies.remove(eNum);
- }
-
- if (enemyDie == true) {
- score = score + 100;
- }
- }
- for (int h = enemies.size(); h < 2; h++) {
- enemies.add(new Enemy(randx(), 0, 15, 30));
- break;
- }
-
- for (int bNum = bullets.size()-1; bNum >= 0; bNum--) {
- Prjktl bullet = (Prjktl) bullets.get(bNum);
- bullet.move();
- bullet.display();
- hitDetect();
-
- if (bullet.offscreen() || enemyHit == true) {
- bullets.remove(bNum);
- }
- }
- if (bShooting == true) {
- for (int j = bullets.size(); j < 100; j++) {
- frameCounter++;
- if (frameCounter % 10 == 0) {
- bullets.add(new Prjktl(stream1x(),streamy(),randclr()));
- //bullets.add(new Prjktl(stream2x(),streamy(),randclr()));
- break;
- }
- }
- }
- }
- void keyPressed() {
- if (key == CODED) {
- if(keyCode==UP){shipYSpd=-4;}
- else if (keyCode==DOWN) {shipYSpd=4;}
- else if (keyCode==RIGHT) {shipXSpd=4;}
- else if (keyCode==LEFT) {shipXSpd=-4;}
- }
- if(key=='z'){bShooting=true;}
- }
- void keyReleased() {
- if (key == CODED) {
- if(keyCode==UP){shipYSpd=0;}
- else if (keyCode==DOWN) {shipYSpd=0;}
- else if (keyCode==RIGHT) {shipXSpd=0;}
- else if (keyCode==LEFT) {shipXSpd=0;}
- }
- if(key=='z'){bShooting=false;}
- }
- void hitDetect() {
- Enemy enemy = (Enemy) enemies.get(eNum);
- Prjktl bullet = (Prjktl) bullets.get(bNum);
- if ((bullet.x >= enemy.x-15) && (bullet.x <= enemy.x+15) &&
- (bullet.y <= enemy.y+35) && (bullet.y >= enemy.y-35)) {
- enemyHit = true;
- } else {
- enemyHit = false;
- }
- }
- //void enemyHappen() {
-
- //}
- class Player {
- Player(float startX, float startY) {
- plXpos = startX;
- plYpos = startY;
- }
- void display() {
- stroke(100,100,255);
- fill(200,200,200);
- rectMode(CENTER);
- rect(plXpos,plYpos,30,50);
- }
- void movement() {
- plXpos = plXpos+shipXSpd;
- plYpos = plYpos+shipYSpd;
- }
- }
- class Prjktl {
-
- float x;
- float y;
- float speed;
- float c;
-
- Prjktl(float tempX, float tempY, float tempColor) {
- x = tempX;
- y = tempY;
- speed = 20;
- c = tempColor;
- }
-
- void move() {
- y=y-speed;
- }
-
- boolean offscreen() {
- if (y < height-height) {
- return true;
- } else {
- return false;
- }
- }
-
- void display() {
- noStroke();
- fill(0, c, c);
- ellipse(x,y,5,17);
- }
- }
-
- class Enemy {
-
- float w;
- float h;
- float x;
- float y;
- float speed;
- int life;
-
- Enemy (float startX, float startY, float tempW, float tempH ) {
- x = startX;
- y = startY;
- w = tempW;
- h = tempH;
- speed = 1;
- life = 1;
- }
-
- void move() {
- y = y + speed;
- }
-
- boolean offscreen() {
- if (y > height) {
- return true;
- } else {
- return false;
- }
- }
-
- void eDeathChk() {
- if (enemyHit == true) {
- life--;
- }
- if (life == 0) {
- enemyDie = true;
- } else {
- enemyDie = false;
- }
- }
-
- void display() {
- stroke(255,100,100);
- fill(200,200,200);
- rectMode(CENTER);
- rect(x,y,w,h);
- }
- }
1