Skip to content

Instantly share code, notes, and snippets.

@boyanov83
Created September 18, 2014 10:24
Show Gist options
  • Save boyanov83/d3569798d27f8b027410 to your computer and use it in GitHub Desktop.
Save boyanov83/d3569798d27f8b027410 to your computer and use it in GitHub Desktop.
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.Random;
class Bullets {
static int startY = 10;
static ArrayList<Integer> bullets1 = new ArrayList<Integer>();
static ArrayList<Integer> bullets2 = new ArrayList<Integer>();
static void addBullet(int y, int x, String whichPlayer)
{
if (whichPlayer.equals("player1"))
{
bullets1.add(y);
bullets1.add(x);
}
else
{
bullets2.add(y);
bullets2.add(x);
}
}
static void draw(Graphics g) throws InterruptedException {
for (int i = 0; i < bullets2.size(); i += 2)
{
g.drawImage(Toolkit.getDefaultToolkit().getImage("bullet2.png"), bullets2.get(i + 1), bullets2.get(i), null);
bullets2.set(i, (bullets2.get(i)) - 2);
if (bullets2.get(i) <= 0) {
bullets2.remove(i);
bullets2.remove(i);
i -= 2;
} else {
if (collisionCheck(bullets2.get(i)+2, bullets2.get(i+1)+2, Rain.rain))
{
bullets2.remove(i);
bullets2.remove(i);
i -= 2;
}
}
}
for (int i = 0; i < bullets1.size(); i += 2)
{
g.drawImage(Toolkit.getDefaultToolkit().getImage("bullet2.png"), bullets1.get(i + 1), bullets1.get(i), null);
bullets1.set(i, (bullets1.get(i)) - 2);
if (bullets1.get(i) <= 0) {
bullets1.remove(i);
bullets1.remove(i);
i -= 2;
} else {
if (collisionCheck(bullets1.get(i)+2, bullets1.get(i+1)+2, Rain.rain))
{
bullets1.remove(i);
bullets1.remove(i);
i -= 2;
}
}
}
}
private static boolean collisionCheck(int bulletY, int bulletX, ArrayList<Object> rain)
{
for (int i = 0; i < rain.size(); i+=4)
{
if (bulletY >= (int)rain.get(i) && bulletY <= (int)rain.get(i)+(int)rain.get(i+3) &&
bulletX >= (int)rain.get(i+1) && bulletX <= (int)rain.get(i+1)+(int)rain.get(i+3) )
{
rain.set(i, -20);
return true;
}
}
return false;
}
void update() {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment