@Kasi;
@akarigar;
@tls;
Here is gameLoop.java where I suspect the issue is:
package main;
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;
public class gameLoop extends Applet implements Runnable, KeyListener {
static final long serialVersionUID = 1L;
public Image offscreen;
public Graphics d;
public boolean up, down, left, right;
public BufferedImage background, foreground, player;
public int counter, score, x, y, width, height, health, speed, level;
public Long lastTime;
public Box bounds;
public Player p = new Player(null);
public ArrayList<GameObject> objects = new ArrayList<GameObject>();
GameObject spike = new GameObject(200, 400, 10, 20, 20,
5000, "fire.png");
GameObject mj = new GameObject(Rand.range(200, 400), Rand.range(400, 450), 20, 20, 10,
5000, "plant.png");
public void addObjectList(GameObject... object) {
for (int i = 0; i < object.length; i++)
objects.add(object[i]);
}
public void deleteObject(int index) {
objects.remove(index);
}
public void updateObjectPositions() {
for (int i = 0; i < objects.size(); i++) {
switch (objects.get(i).fileName) {
case "fire.png": {
objects.get(i).x--;
break;
}
case "plant.png": {
objects.get(i).x--;
if (Rand.range(0, 8) == 1) {
if ((objects.get(i).y < 473) && (objects.get(i).y > 356))
objects.get(i).y = objects.get(i).y
+ (int) ((Math.sin(Rand.integer(5)) + Math
.cos(Rand.integer(5))) * 3);
}
break;
}
}
if (objects.get(i).x < -2) {
objects.remove(i);
return;
}
}
}
public void addObjectWorld(GameObject g) {
objects.add(g);
}
public void updateObjectBoxes() {
for (int i = 0; i < objects.size(); i++) {
int x1 = objects.get(i).x;
int y1 = objects.get(i).y;
int x2 = objects.get(i).x + objects.get(i).w;
int y2 = objects.get(i).y + objects.get(i).h;
GameObject.setCurrentBounds(new Box(x1, y1, x2, y2));
if (objects.get(i).fileName == "plant.png")
System.out.println(objects.get(i).getCurrentBounds().x1);
}
}
public void updatePlayerBounds() {
p.bounds = new Box(x, y, x + width, y + height);
}
public void handleScore(String objectName) {
switch(objectName) {
case "plant.png": {
health += 5;
score += Rand.range(0, 28);
break;
}
}
}
public void checkCollision() {
for (int i = 0; i < objects.size(); i++) {
if (objects.get(i).getCurrentBounds() == null)
return;
else {
if (objects.get(i).getCurrentBounds().collision(p.bounds)) {
handleScore(objects.get(i).fileName);
objects.remove(i);
return;
}
}
}
}
public void addRandomObjects() {
}
public void run() {
level = 1;
x = 100;
y = 400;
width = 20;
height = 26;
health = 10000;
addObjectList(spike, mj);
try {
background = ImageIO.read(new File("main/background.png"));
foreground = ImageIO.read(new File("main/foreground.png"));
//player = ImageIO.read(new File("main/player.png"));
p.playerSprite = ImageIO.read(new File("main/player.png"));
} catch (IOException e1) {
e1.printStackTrace();
}
while (true) {
updatePlayerBounds();
level = ((int) score / 1000) + 1;
counter++;
if (Rand.range(0, 1000000) == 5) {
addObjectList(new GameObject(Rand.range(200, 400), Rand.range(400, 450), 20, 20, 10,
5000, "plant.png"));
}
if (counter > 10) {
//health -= 1;
counter = 0;
updateObjectPositions();
updateObjectBoxes();
}
if (p.bounds != null)
checkCollision();
//if ((System.currentTimeMillis() - lastTime) > (5000) {
//}
if (y <= 356)
up = false;
if (x > 845)
right = false;
if (x < 1)
left = false;
// if ( y <= 348 && jump != true) {
// y+=4;
// }
if (left == true) {
x--;
}
if (right == true) {
x++;
}
if (up == true) {
y--;
}
if (down == true) {
y++;
}
if (y > 473) {
y = 473;
}
repaint();
try {
Thread.sleep(6 + speed);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 37) {
left = true;
}
if (e.getKeyCode() == 38) {
up = true;
}
if (e.getKeyCode() == 39) {
right = true;
}
if (e.getKeyCode() == 40) {
down = true;
}
}
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == 37) {
left = false;
}
if (e.getKeyCode() == 38) {
up = false;
counter = 4;
}
if (e.getKeyCode() == 39) {
right = false;
}
if (e.getKeyCode() == 40) {
down = false;
}
}
public void keyTyped(KeyEvent e) {
}
}
I know there is a lot of sloppy code but I am just trying to fix this bug before I go any further
[spoiler=Game.java]
package main;
import java.awt.Graphics;
public class Game extends gameLoop {
private static final long serialVersionUID = 1L;
public void init() {
setSize(854, 480);
Thread main = new Thread(this);
main.start();
offscreen = createImage(854, 480);
d = offscreen.getGraphics();
addKeyListener(this);
}
public void paint(Graphics g) {
d.clearRect(0, 0, 854, 480);
d.drawImage(background, 0, 0, this);
d.drawImage(foreground, 0, 356, this);
for (int i = 0; i < objects.size();i++) {
d.drawImage(objects.get(i).Img, objects.get(i).x, objects.get(i).y, this);
d.drawString("Object: " + objects.get(i).fileName + " " + objects.get(i).x + "," + objects.get(i).y, 2, 75 + (i *15));
}
d.drawImage(p.playerSprite, x, y, this);
d.drawString("Scores acquired: " + score, 2, 15);
//if (objects != null)
//d.drawString("Objects: " + objects.size, 2, 30);
d.drawString("Health: " + health, 2, 45);
d.drawString("Position:" + x + "," + y, 2, 60);
g.drawImage(offscreen, 0, 0, this);
}
public void update(Graphics g) {
paint(g);
}
}
[/spoiler]
[spoiler=GameObject.java]
package main;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
class GameObject {
public int x, y, w, h;
private static Box currentBounds;
public final int damage, value;
public final String fileName;
public BufferedImage Img;
public GameObject(int x, int y, int w, int h, int damage, int value, String fileName) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.damage = damage;
this.value = value;
this.fileName = fileName;
try {
this.Img = ImageIO.read(new File("main/" + fileName));
} catch (IOException e) {
this.Img = null;
System.out.println("Error loading image");
}
}
public static Box getCurrentBounds() {
return currentBounds;
}
public static void setCurrentBounds(Box currentBounds) {
GameObject.currentBounds = currentBounds;
}
}
[/spoiler]