Related
For my class I need to create a one player pong game. We need to use what we've learned so its simple code and the problem is, while the "while loop" is running, my Keydown thread doesn't seem to work. My teacher isn't helpful at all so that's why i'm here. Please help by only using methods and such that are used in my program.
I've tried moving the ball code into a run thread but i can never seem to actually get that thread running.
public class PongGame extends Applet {
int startup = 0;
int x = 400;
int y = 500;
int x2 = (int) (Math.random() * (600 + 1));
int y2 = (int) (Math.random() * (600 + 1));
int xVelocity = 1;
int yVelocity = 1;
int x2Velocity = 1;
int y2Velocity = 1;
int curry = 700;
int currx = 600;
int counter = 2;
#Override
public void init() {
setSize(1440, 900);
setBackground(Color.black);
setFont(new Font("Helvetica", Font.BOLD, 36));
}
#Override
public boolean keyDown(Event evt, int key) {
startup = startup + 1;
repaint();
if (key == Event.RIGHT) {
currx += 10;
}
if (key == Event.LEFT) {
currx -= 10;
}
if (currx == 1400) {
currx = 10;
}
if (currx == 20) {
currx = 1399;
}
repaint();
return false;
}
public void run(Graphics g) {
startup = 0;
while (counter > 0) {
try {
Thread.sleep(30);
} catch (InterruptedException e) {
}
x += xVelocity;
y += yVelocity;
g.setColor(Color.blue);
g.fillRect(currx, curry, 300, 25);
g.setColor(Color.red);
g.fillOval(x, y, 30, 30);
for (int j = 0; j < 20000000; j++);
g.setColor(Color.black);
g.fillOval(x, y, 30, 30);
//Bounce the ball off the wall or paddle
if (x >= 1400 || x <= 0) {
xVelocity = -xVelocity;
}
if (y >= 800 || y <= 0) {
yVelocity = -yVelocity;
}
if (((y == 675)) && ((currx <= x) && (currx + 300) >= x)) {
yVelocity = -yVelocity;
y = y - 10;
repaint();
}
}
}
public void paint(Graphics g) {
if (startup == 0) {
g.setColor(Color.white);
g.drawString("Welcome To PONG", 0, 100);
g.drawString("Created by Caden Anton", 0, 200);
g.drawString("copyright May 3rd, 2019 ©", 0, 300);
g.drawString("Press any key to continue", 0, 400);
}
if (startup == 1) {
g.setColor(Color.white);
g.drawString("RULES:", 0, 100);
g.drawString("Press the arrwow keys to move the paddle", 0, 200);
g.drawString("Your objective is to keep the ball from touching the ground", 0, 300);
g.drawString("Once the ball hits the ground you lose the game", 0, 400);
}
if (startup >= 2) {
//Input Run thread start here.
}
}
}
private JPanel contentPane;
private KeyListener myKeyListener;
JLabel lblUp;
JLabel lblMiddle;
JLabel lblDown;
JButton btnStart;
JLabel lblScore;
int lblu = 0;
int x = 0;
int y = 50;
int u = 1;
int w = 1;
int rxx = 0;
int ryy = 0;
int s = 0;
Timer timer;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Game_1 frame = new Game_1();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Game_1() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblScore = new JLabel("0");
lblScore.setHorizontalAlignment(SwingConstants.CENTER);
lblScore.setForeground(Color.GREEN);
lblScore.setBounds(388, 0, 46, 14);
contentPane.add(lblScore);
addKeyListener(this);
}
public void keyPressed(KeyEvent arg0) {
Graphics pen = this.contentPane.getGraphics();
int maxh = contentPane.getHeight();
int maxw = contentPane.getWidth();
if(y < 0){
y = maxh -50;
}
if(y > maxh-45){
y = 0;
}
if (arg0.getKeyCode() == KeyEvent.VK_UP) {
if(u ==0){
pen.setColor(Color.BLACK);
pen.fillRect(0, 0, maxw, maxh);
y = y - 10;
pen.setColor(Color.GREEN);
pen.fillRect(x, y, 50, 50);
pen.setColor(Color.BLACK);
pen.fillRect(x - 50, y, 50, 50);
x = x + 1;
if (x >= maxw) {
pen.fillRect(x - 30, y, 50, 50);
x = 0;
}
}
} else if (arg0.getKeyCode() == KeyEvent.VK_RIGHT) {
if(u ==1){
pen.setColor(Color.BLACK);
pen.fillRect(0, 0, maxw, maxh);
pen.setColor(Color.BLACK);
pen.fillRect(0, 0, maxw, maxh);
Timer timer = new Timer(100, this);
timer.start();
u = 0;
}else if(u ==0){
x = x +10;
}
} else if (arg0.getKeyCode() == KeyEvent.VK_DOWN) {
if(u ==0){
pen.setColor(Color.BLACK);
pen.fillRect(0, 0, maxw, maxh);
pen.setColor(Color.BLACK);
y = y+ 10;
if (x >= maxw) {
pen.fillRect(x - 30, y, 50, 50);
x = 0;
}
}
} else if (arg0.getKeyCode() == KeyEvent.VK_LEFT) {
if(u ==0){
pen.setColor(Color.BLACK);
pen.fillRect(0, 0, maxw, maxh);
pen.setColor(Color.BLACK);
x = x- 10;
if (x < 0) {
pen.fillRect(x - 30, y, 50, 50);
x = maxw;
}
}
}
}
public void keyReleased(KeyEvent arg0) {
}
public void keyTyped(KeyEvent arg0) {
}
public void run() {
}
#Override
public void actionPerformed(ActionEvent arg0) {
Graphics pen = this.contentPane.getGraphics();
int maxh = contentPane.getHeight();
int maxw = contentPane.getWidth();
pen.setColor(Color.BLACK);
pen.fillRect(0, 0, maxw, maxh);
pen.setColor(Color.GREEN);
pen.fillRect(x, y, 50, 50);
pen.setColor(Color.BLACK);
pen.fillRect(x - 50, y, 50, 50);
x = x + 1;
if (x >= maxw) {
pen.fillRect(x - 30, y, 50, 50);
x = 0;
}
if(w ==1){
Random r = new Random();
int ry = r.nextInt(maxh - 0) + 100;
int rx = r.nextInt(maxw - 0) + 100;
rxx = rx;
ryy = ry;
pen.setColor(Color.RED);
pen.fillRect(rx, ry, 10, 10);
w = 0;
}
pen.setColor(Color.RED);
pen.fillRect(rxx, ryy, 10, 10);
if(x-50 <= rxx && x > rxx && y > ryy && y-50 <= ryy){
s ++;
System.out.println("PUNKT");
pen.setColor(Color.BLACK);
pen.fillRect(rxx, ryy, 10, 10);
w = 1;
}
}
}
here is the Problem: it only detects that they touch in the left top >corner(the wrong code is at the end, the last if)
you can start the game by pressing the Right arrow Button on you're Keyboard.Moving upwards: up Key on you're Keyboard.Moving downwards:down Key on your're Keyboard.The right Button also lets you move to the right, the left Button to the left.Picture of the Game
I want, that the Rect detects that it touches the other in every part
of it, not only in the left top corner
Let's assume you've got first rect with: x1, y1, width1, height1 and second rect with x2, y2, width2, height2. The first rect touched the second in every part of it (so the second one is contained in the first one) when x1 <= x2 && x2+width2 <= x1+width2 && y1 <= y2 && y2+height <= y1+height
so
if (x1 <= x2 && x2+width2 <= x1+width2 && y1 <= y2 && y2+height <= y1+height) {
// the second rect is contained in the first rect
}
So I finally fixed it. For those who are interested, here is the fixed part of the code:
if (x+d >= x2 && x <= x2 && y+d >=y2 && y <= y2) {
s++;
d = d + s*10;
System.out.println("PUNKT");
pen.setColor(Color.BLACK);
pen.fillRect(x2, y2, 10, 10);
w = 1;
if (s == 10) {
JOptionPane.showMessageDialog(frame, "Achievement get: " + s + "Punkte!");
}
The program was running fine before I put the Rectangle2D objects in an ArrayList, except that whenever I changed directions, the snake would just rotate rather than bend. Now the snake does not even show up, and my console gives me the error, as provided by Mad Programmer:
Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException:
Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:638)
at java.util.ArrayList.get(ArrayList.java:414)
at javaapplication968.JavaApplication968$Display.actionPerformed(JavaApplication968.java:110)
I know the issue is related to the ArrayLists, but how do I fix it?
Here is my class:
public class Display extends JPanel implements ActionListener, KeyListener {
Timer t = new Timer(1, this);
double xCoord = 50;
double yCoord = 50;
double xvel = 0;
double yvel = .01;
double ranx, xtemp;
double rany, ytemp;
int eaten = 0;
ArrayList<Rectangle2D> rects = new ArrayList<Rectangle2D>();
ArrayList<Double> xloc = new ArrayList<Double>();
ArrayList<Double> yloc = new ArrayList<Double>();
public Display(int xsize, int ysize) {
t.start();
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
xtemp = xsize;
ranx = (xtemp - 15) * Math.random();
ytemp = ysize;
rany = (ytemp - 15) * Math.random();
formSnake();
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
// Adds snake
g2.setColor(Color.YELLOW);
for (int i = 0; i < rects.size(); i++) {
g2.fill(rects.get(i));
}
// Adds fruit at random location
g2.setColor(Color.BLUE);
g2.fill(new Ellipse2D.Double(ranx, rany, 15, 15));
setBackground(Color.BLACK);
}
#Override
public void actionPerformed(ActionEvent e) {
repaint();
// Replaces each rectangle location with one before it
for (int i = rects.size() - 1; i > 0; i--) {
xloc.set(i, xloc.get(i - 1));
yloc.set(i, yloc.get(i - 1));
}
// Sets new head location
xloc.set(0, xloc.get(0) + xvel);
yloc.set(0, yloc.get(0) + yvel);
getEaten();
}
// Adds initial 3 2DRectangles to snake at start of game
public void formSnake() {
for (int i = 0; i < rects.size(); i++) {
xloc.add(xCoord);
yloc.add(yCoord - 16 * i);
rects.add(rects.size(),
new Rectangle2D.Double(xloc.get(i), yloc.get(i), 15, 15));
}
}
// Adds 1 2DRectangle to the front of the snake every time it eats
public void extend() {
// Vertical
if (xvel == 0) {
if (yvel <= 0) { // Up
xloc.add(xloc.get(xloc.size() - 1));
yloc.add(yloc.get(yloc.size() - 1) - 16);
rects.add(new Rectangle2D.Double(xloc.get(xloc.size() - 1),
yloc.get(yloc.size() - 1), 15, 15));
} else
// Down
xloc.add(xloc.get(xloc.size() - 1));
yloc.add(yloc.get(yloc.size() - 1) + 16);
rects.add(new Rectangle2D.Double(xloc.get(xloc.size() - 1), yloc
.get(yloc.size() - 1), 15, 15));
}
// Horizontal
else if (yvel == 0) {
if (xvel <= 0) { // Left
xloc.add(xloc.get(xloc.size() - 1) - 16);
yloc.add(yloc.get(yloc.size() - 1));
rects.add(new Rectangle2D.Double(xloc.get(xloc.size() - 1),
yloc.get(yloc.size() - 1), 15, 15));
} else
// Right
xloc.add(xloc.get(xloc.size() - 1) + 16);
yloc.add(yloc.get(yloc.size() - 1));
rects.add(new Rectangle2D.Double(xloc.get(xloc.size() - 1), yloc
.get(yloc.size() - 1), 15, 15));
}
}
public void getEaten() {
if (Math.abs(xCoord - ranx) < 15 && Math.abs(yCoord - rany) < 15) {
rany = ytemp * Math.random();
ranx = xtemp * Math.random();
eaten++;
extend();
}
}
//Directions
public void up() {
yvel = -0.5;
xvel = 0;
}
public void down() {
yvel = 0.5;
xvel = 0;
}
public void left() {
xvel = -0.5;
yvel = 0;
}
public void right() {
xvel = 0.5;
yvel = 0;
}
//Just for testing purposes
public void stop() {
xvel = 0;
yvel = 0;
}
//Direction implementation
#Override
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
if (code == KeyEvent.VK_UP || code == KeyEvent.VK_NUMPAD8) {
up();
}
if (code == KeyEvent.VK_DOWN || code == KeyEvent.VK_NUMPAD2) {
down();
}
if (code == KeyEvent.VK_RIGHT || code == KeyEvent.VK_NUMPAD6) {
right();
}
if (code == KeyEvent.VK_LEFT || code == KeyEvent.VK_NUMPAD4) {
left();
}
}
#Override
public void keyTyped(KeyEvent e) {
}
#Override
public void keyReleased(KeyEvent e) {
}
}
You seem to be getting a
Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:638)
at java.util.ArrayList.get(ArrayList.java:414)
at javaapplication968.JavaApplication968$Display.actionPerformed(JavaApplication968.java:110)
At xloc.set(0, xloc.get(0) + xvel);, because there are no elements the xloc List
Change your formSnake method to do exactly what it says...// Adds initial 3 2DRectangles to snake at start of game instead of using rects.size(), which will be 0 when it's called...
// Adds initial 3 2DRectangles to snake at start of game
public void formSnake() {
for (int i = 0; i < 3; i++) {
xloc.add(xCoord);
yloc.add(yCoord - 16 * i);
rects.add(rects.size(),
new Rectangle2D.Double(xloc.get(i), yloc.get(i), 15, 15));
}
}
Hello I have a game where you walk around looking for clues as to what happened.
I have a map.txt with numbers on it representing each block. if the number is equal to 5 a table is created. I want to have clues on the table. I want to find out how many tables are created in the map and assign each table an id but none of the ids can be the same and the id number cant be bigger than the amount of tables for that map. Here is my code so far:
table class
public class TableWithClue {
public int x, y;
public boolean search = false;
public Rectangle r, r1;
public int time = 0;
public int timer = 100;
public int aftertime = 0;
public int aftertimer = 100;
public boolean foundsomething = false;
public TableWithClue(int x, int y,int id) {
this.x = x;
this.y = y;
r = new Rectangle(x - play.camx, y - play.camy, 20, 30);
}
public void tick() {
r = new Rectangle(x - play.camx, y - play.camy, 20, 30);
if (r.intersects(play.p.r)) {
if(Keys.isPressed(Keys.e)){
search = true;
Sound.snap.play();
}
}
}
public void render(Graphics g) {
Image img;
if (search) {
ImageIcon i62 = new ImageIcon("res/tiles/table1.png");
img = i62.getImage();
g.drawImage(img, x - play.camx, y - play.camy, null);
} else {
ImageIcon i62 = new ImageIcon("res/tiles/table1.png");
img = i62.getImage();
g.drawImage(img, x - play.camx, y - play.camy, null);
if(r.intersects(play.p.r)){
ImageIcon i162 = new ImageIcon("res/gui/openstuff.png");
img = i162.getImage();
g.drawImage(img, Comp.size.width /2 - 95, 50, null);
g.setColor(Color.white);
g.setFont(new Font("italic", Font.BOLD,15 ));
g.drawString(" E to search ", Comp.size.width /2 - 32, 88);
}
}
if(search && r.intersects(play.p.r)){
if(time >=timer ){
if(aftertime >= aftertimer){
search = false;
time= 0;
aftertime = 0;
}else{
aftertime++;
}
}else {
time++;
}
g.setColor(Color.white);
g.fillRect((int) play.p.x - 55, (int) play.p.y - 70, 120, 40);
g.setColor(Color.black);
g.drawRect((int) play.p.x - 55, (int) play.p.y - 70, 120, 40);
if(time <timer && time != 0){
g.setColor(Color.black);
g.setFont(new Font("italic", Font.BOLD,15 ));
g.drawString("Searching",(int) play.p.x - 30, (int) play.p.y - 55);
}
g.setColor(Color.gray);
g.fillRect((int) play.p.x - 35, (int) play.p.y - 50, (80 * time) / timer, 10);
g.setColor(new Color(0, 0, 0));
g.drawRect((int) play.p.x - 35, (int) play.p.y - 50, 80, 10);
if(aftertime<=aftertimer && time >= timer){
g.setColor(Color.black);
g.setFont(new Font("italic", Font.BOLD,12 ));
g.drawString("Found Nothing",(int) play.p.x - 34, (int) play.p.y - 55);
}
}else{
search = false;
}
if(play.debug){
g.setColor(Color.red);
g.drawRect(r.x, r.y, r.width, r.height);
g.setColor(Color.yellow);
}else{
}
}
}
The tables are created here in a different class called block:
public void interactables() {
if (id == 5) {
screens.interactables.t1.add(new TableWithClue(x ,y));
}
}
Here is the map class:
public class map {
private String path;
private int width, height;
private static block[][] blocks;
private boolean open = false;;
public Image i2;
public map(String path) {
this.path = path;
loadMap();
}
public void loadMap() {
InputStream is = this.getClass().getResourceAsStream(path);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
try {
width = Integer.parseInt(br.readLine());
height = Integer.parseInt(br.readLine());
blocks = new block[height][width];
for (int y = 0; y < height; y++) {
String line = br.readLine();
String[] tokens = line.split("\\s+");
for (int x = 0; x < width; x++) {
blocks[y][x] = new block(x * block.blocksize, y * block.blocksize, Integer.parseInt(tokens[x]));
}
}
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void draw(Graphics g) {
for (int i = 0; i < blocks.length; i++) {
for (int j = 0; j < blocks[0].length; j++) {
if (blocks[i][j].x + 50 > play.camx && blocks[i][j].x - 50 < play.camx + Comp.size.width + 10 && blocks[i][j].y + 50 > play.camy && blocks[i][j].y - 50 < play.camy + Comp.size.height + 10) {
blocks[i][j].render(g);
}
}
}
}
public static block[][] getBlocks() {
return blocks;
}
public void interact() {
for (int i = 0; i < blocks.length; i++) {
for (int j = 0; j < blocks[0].length; j++) {
blocks[i][j].interactables();
}
}
}
public void addNPC() {
for (int i = 0; i < blocks.length; i++) {
for (int j = 0; j < blocks[0].length; j++) {
blocks[i][j].NPC();
}
}
}
}
Thank you in advance:)
First, you will want to store the table's ID in the TableWithClue class. Then, you will want a static int in your block class. This variable will store the next table ID; each time, the next ID is passed to the table's constructor, then the variable is incremented. So the first table loaded would be table 0, the second would be table 1, etc.
So I'm trying to implement collision detection in my game and for some reason the collision isnt working properly.
public class ArenaKeys extends KeyAdapter {
arenaScreenBuild arena;
int xPos = 0, playerFace = 4;
int xPPos = 200, yPPos = 150;
int pX = 40, pY = 30;
AttackAshe aAtk = new AttackAshe();
int[][] mask = new int[400][92];
#SuppressWarnings("static-access")
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();// Get key pressed
if (keyCode == e.VK_RIGHT) {
playerFace = 4;
xPos += 5;
pX = (xPos + xPPos) / 5;
if (checkBoundary(pX, pY) == (false))
xPos -= 5;
} else if (keyCode == e.VK_LEFT) {
playerFace = 3;
xPos -= 5;
pX = (xPos + xPPos) / 5;
if (checkBoundary(pX, pY) == (false))
xPos += 5;
} else if (keyCode == e.VK_UP) {
playerFace = 2;
yPPos -= 5;
pY = yPPos / 5;
if (checkBoundary(pX, pY) == (false))
yPPos += 5;
} else if (keyCode == e.VK_DOWN) {
playerFace = 1;
yPPos += 5;
pY = yPPos / 5;
if (checkBoundary(pX, pY) == (false))
yPPos -= 5;
}
if (keyCode == e.VK_SPACE) {
aAtk.regArrow(arena.xPosition(), arena.yPosition());
arena.shoot(playerFace);
arena.xArrow = xPPos;
arena.yArrow = yPPos;
} else if (keyCode == e.VK_ESCAPE)
System.exit(0);
arena.moveArena(xPos);
arena.turnPlayer(playerFace);
arena.updateScreen(xPPos, yPPos);
}
public boolean checkBoundary(int x, int y) {
Rectangle t1 = new Rectangle(Turret.x, Turret.y, Turret.WIDTH,
Turret.HEIGHT);
Rectangle p = new Rectangle(pX, pY, Player.WIDTH, Player.HEIGHT);
if (t1.intersects(p))
// if (mask[x][y] == 0)
return false;
else
return true;
}
public static class Turret {
static int x = 168;
static int y = 40;
static final int WIDTH = 50;
static final int HEIGHT = 50;
}
public static class Player {
static final int WIDTH = 25;
static final int HEIGHT = 25;
}
public ArenaKeys(arenaScreenBuild arena) throws Exception {
this.arena = arena;
}
}
Approximately 20 units before the actual turret, the sprite stops being able to move any further. The sprite cannot go above or below the turret even if you go really high or really low.
What seems to be going wrong is that the sprite is colliding into the turret rectangle too early but I don't understand how that it possible. I draw the turret exactly 50 wide, 50 high at 168,40. The player is moving so it's x, y is different everytime but it's dimensions are 25 wide and 25 high.
The original turret is 126x111 approximately but I draw it as 50x50
25x25
public class arenaScreenBuild extends JPanel implements ActionListener {
String picPath = "pictures/";
String[] fileName = { "stageBridge.png", "turret.png", "Ashe.png",
"regArrow.png", "arenaScreen.png" };
ClassLoader cl = arenaScreenBuild.class.getClassLoader();
URL imgURL[] = new URL[5];
Toolkit tk = Toolkit.getDefaultToolkit();
Image imgBG, imgTurret, imgPlayer, imgRegArrow, imgBorder;
Boolean[] ptFunc = new Boolean[3];
int PLAYER_INITIAL_X = 200, PLAYER_INITIAL_Y = 150;
int xPos = 0, xPFace = 150, yPFace = 0;
int xPPos = 200, yPPos = 150;
int xVal, yVal, xArrow, yArrow, xTemp, yTemp;
Timer space;
int counter, facePosition = 1;
public arenaScreenBuild() throws Exception {
for (int x = 0; x < 5; x++) {
imgURL[x] = cl.getResource(picPath + fileName[x]);
}
imgBG = tk.createImage(imgURL[0]);
imgTurret = tk.createImage(imgURL[1]);
imgPlayer = tk.createImage(imgURL[2]);
imgRegArrow = tk.createImage(imgURL[3]);
imgBorder = tk.createImage(imgURL[4]);
for (int x = 0; x < 3; x++)
ptFunc[x] = true;
space = new Timer(100, this);
}
public void updateScreen() {
repaint();
}
public void moveArena(int x) {
xPos = x;
}
public void updateScreen(int x, int y) {
xPPos = x;
yPPos = y;
repaint();
}
public boolean arrow() {
return (true);
}
public void turnPlayer(int face) {
if (face == 4) {
xPFace = 150;
} else if (face == 3) {
xPFace = 100;
} else if (face == 2) {
xPFace = 50;
} else if (face == 1) {
xPFace = 0;
}
if (yPFace == 50)
yPFace = 0;
else if (yPFace == 0)
yPFace = 50;
}
public void paintComponent(Graphics g) {
g.drawImage(imgBG, 10, 30, 610, 490, xPos, 0, xPos + 600, 460, this);
g.drawImage(imgTurret, 850 - xPos, 200, 950 - xPos, 300, 0, 0, 126,
110, this);
g.drawImage(imgTurret, 1350 - xPos, 200, 1450 - xPos, 300, 0, 0, 126,
110, this);
g.drawImage(imgPlayer, xPPos, yPPos, 50 + (xPPos),
50 + (yPPos), xPFace, yPFace, xPFace + 50, yPFace + 50, this);
if (counter <= 5000 && counter > 0)
g.drawImage(imgRegArrow, xArrow, yArrow, this);
g.drawImage(imgBorder, 0, 0, 620, 600, 0, 0, 620, 600, this);
g.setColor(Color.WHITE);
g.drawString("x:" + (xPPos + xPos), 535, 525);
g.drawString("y:" + yPPos, 535, 545);
}
public int xPosition() {
xVal = xPPos + xPos;
return (xVal);
}
public int yPosition() {
yVal = yPPos;
return (yVal);
}
public void shoot(int i) {
facePosition = i;
xTemp = xPosition();
yTemp = yPosition();
space.start();
}
public void actionPerformed(ActionEvent e) {
counter++;
if (facePosition == 4) {
if (counter <= 5000) {
xArrow += 50;
}
}
else if (facePosition == 3) {
if (counter <= 5000) {
xArrow -= 50;
}
}
else if (facePosition == 2) {
if (counter <= 5000) {
yArrow -= 50;
}
}
else if (facePosition == 1) {
if (counter <= 5000) {
yArrow += 50;
}
}
if (xArrow == (xTemp + 100)) {
counter = 0;
space.stop();
}
updateScreen();
}
}
Turns out that my values for the x position were wrong. Also due to my previous boundary code, there were still remenants of it which messed me up therefore not allowing me to see the problem sooner.
For any one looking for how to make the collision detection boundary, just make 2 rectangles and use the
Rectangle rect1 = new Rectangle(topLeftX, topLeftY, rectangleWidth,rectangleHeight);
Rectangle rect2 = new Rectangle(topLeftX, topLeftY, rectangleWidth, rectangleHeight);
if (rect1.intersects(rect2))
//enter code to do if it intersects here