Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/main/java/org/ergasia/javaspacegame/Ammo.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class Ammo {
private Image image;
/*This boolean variable is used for the shot system*/
private boolean check = false;
private boolean updatedSpeed = false;

/**
* The Constructor.
Expand Down Expand Up @@ -87,6 +88,19 @@ public void fire(){
dy = 5;
}

public void setFirespeed() {
if (this.dy<=5) {
this.dy = this.dy * 3;
updatedSpeed=true;
}

}



public void resetSpeed() {
this.dy = 5;
}
/**
* if the player typed the right keyboard button
* the dx value increases, so the object moves right.
Expand Down
52 changes: 49 additions & 3 deletions src/main/java/org/ergasia/javaspacegame/Panel.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ public class Panel extends JPanel implements ActionListener, KeyListener {
private boolean endGame = false;
/*Image of the background */
private Image bg;
private int frameCounter = 0;

private PowerUpFuel fuel;
private int frameCounter = 0;
private int fuelEffectDuration = 300;



Expand Down Expand Up @@ -107,7 +110,8 @@ public Panel() {

}

timer = new Timer(16, this);// ~= 60fps.

timer = new Timer(16, this);// ~= 60fps.
timer.start();//Starting the thread.

}
Expand Down Expand Up @@ -190,6 +194,15 @@ private void doDrawing(Graphics g) {

g2d.drawImage(ship.getImage(), ship.getX(), ship.getY() - 25/*the height of the image*/, this);

if (fuel!= null && fuel.getImage() != null) {
if (!fuel.fuelCheck()){
g2d.drawImage(fuel.getImage(), fuel.getX(), fuel.getY(), this);
}
}
else{
//System.out.println("Fuel is "+frameCounter);

}
}

/**
Expand All @@ -200,7 +213,40 @@ private void doDrawing(Graphics g) {
*/
@Override
public void actionPerformed(ActionEvent e) {
frameCounter++;
if (fuel != null && !fuel.fuelCheck()) {
fuel.checkForCollision(ammos);

}

if (fuel != null && fuel.fuelCheck()){

// or 300 if using frames
// Set all ammo to be fast

for (Ammo ammo : ammos) {
ammo.setFirespeed();
}



fuelEffectDuration--;
System.out.println(fuelEffectDuration);
if (fuelEffectDuration <= 0) {
fuel.changefuelhit();// end the powerup

fuelEffectDuration = 300;
// Reset ammo speeds if needed
for (Ammo ammo : ammos) {
ammo.resetSpeed(); // You may need to make this method

}
}

}
if (frameCounter == 100) {
fuel= new PowerUpFuel();
}
//if stage is beaten this if statement is responsible for the setup for the next stage.
if(restart){
for(int i=0; i<numUfo; i++){
Expand Down Expand Up @@ -276,7 +322,7 @@ public void actionPerformed(ActionEvent e) {
// 2. Check collisions and respond


repaint();//Calls the paintComponent method.
repaint();//Calls the paintComponent method.
}

/**
Expand Down
89 changes: 89 additions & 0 deletions src/main/java/org/ergasia/javaspacegame/PowerUpFuel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package org.ergasia.javaspacegame;

import java.awt.Image;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Objects;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

public class PowerUpFuel {
/*X dimension of the object */
private int x;
/*Y dimension of the object */
private int y;
/*The width of the object */
private int width;
/*The height of the object */
private int height;
private Image image;
/*In this variable is saved the total score */
/*Random object */
private Random rand;
private boolean fuelhit;
private boolean isActive=false;

public PowerUpFuel() {
ImageIcon ii = null;
try {
ii = new ImageIcon(ImageIO.read(Objects.requireNonNull(getClass().getResourceAsStream("/gasolina.png"))));
} catch (IOException e) {
throw new RuntimeException(e);
}
image = ii.getImage();
rand = new Random();

do{
x = 40 + rand.nextInt(720);
y = 30 + rand.nextInt(440);
}while(x > 642 && y < 137);

width = image.getWidth(null);
height = image.getHeight(null);
}
public Image getImage() {
return image;
}
public int getX() {
return x;
}

public int getY() {
return y;
}


public void checkForCollision(ArrayList<Ammo> ammos) {
if (fuelhit) return;

for(int i=0; i<ammos.size(); i++){

int ammoX = ammos.get(i).getX();
int ammoY = ammos.get(i).getY();
int ammoWidth = ammos.get(i).getWidth();
int ammoHeight = ammos.get(i).getHeight();

if(ammoY + ammoHeight > y && ammoY + ammoHeight < y + height){
if(ammoX + ammoWidth > x && ammoX < x + width){
image = null;
fuelhit = true;
boolean isActive = true;

}
}

}

}
public boolean fuelCheck(){
return fuelhit;
}



public void changefuelhit() {
this.fuelhit = false;
;
}
}
Binary file added src/main/resources/gasolina.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.