Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Collision testing  (Read 887 times)
0 Members and 1 Guest are viewing this topic.
Offline Shazer2

Junior Duke


Medals: 3


Aspiring developer.


« Posted 2011-05-22 09:13:22 »

I've got a file which draws the position of trees (2) based off a randomly generated (seeded) number.

GamePanel.java:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
package Game;

import java.awt.Graphics;
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.Random;

import javax.imageio.ImageIO;
import javax.swing.JComponent;

public class GamePanel extends JComponent implements KeyListener {
   
   //variable declaring
   private static final long serialVersionUID = 1L;
   Game game;
   Player pl = new Player();
   Grid gr = new Grid();
   BufferedImage tree;
   BufferedImage flower;
   BufferedImage grass;
   
   int treeCount = 2;
   int flowerCount = 2;
   int[] treeX = new int[treeCount];
   int[] treeY = new int[treeCount];
   int[] flowerX = new int[flowerCount];
   int[] flowerY = new int[flowerCount];
   
   //constructor set screen up etc
   public GamePanel(Game game) {
      setFocusable(true);
      addKeyListener(this);
      this.game = game;
     
      try
      {
         pl.defPlayer = ImageIO.read(new File("C:\\Users\\Shannon\\workspace\\RPG\\src\\Images\\Players\\defPlayer.png"));
         grass = ImageIO.read(new File("C:\\Users\\Shannon\\workspace\\RPG\\src\\Images\\Backgrounds\\grass.png"));
         tree = ImageIO.read(new File("C:\\Users\\Shannon\\workspace\\RPG\\src\\Images\\Backgrounds\\tree.png"));
         flower = ImageIO.read(new File("C:\\Users\\Shannon\\workspace\\RPG\\src\\Images\\Backgrounds\\flower.png"));
      }
      catch (IOException ex)
      {
         ex.printStackTrace();
      }
     
      Random t = new Random(2934);
      Random f = new Random(3423);
      for (int i = 0; i < treeCount; i++) {
         treeX[i] = t.nextInt(480 / 32)-2;
         treeY[i] = t.nextInt(320 / 32)-2;
      }
      for (int i = 0; i < flowerCount; i++) {
         flowerX[i] = f.nextInt(480 / 32)-2;
         flowerY[i] = f.nextInt(320 / 32)-2;
      }
   }
   
   public void keyPressed(KeyEvent e) {
      int keyCode = e.getKeyCode();
      int speed = 32;
     
      if (keyCode == KeyEvent.VK_ESCAPE) {
         game.showHomePanel();
      }
     
      if (keyCode == KeyEvent.VK_P) {
         game.showPausedPanel();
      }
     
        if (keyCode == KeyEvent.VK_RIGHT) {
            pl.x += speed;
            if (pl.x > 480-32) {
                pl.x = 480-32;
            }
        }
         
        if (keyCode == KeyEvent.VK_LEFT) {
             pl.x -= speed;
             if (pl.x < 0) {
                pl.x = 0;
             }
        }
         
        if (keyCode == KeyEvent.VK_DOWN) {
            pl.y += speed;
            if (pl.y > 320-32) {
                pl.y = 320-32;
            }
        }
         
        if (keyCode == KeyEvent.VK_UP) {
            pl.y -= speed;
            if (pl.y < 0) {
                pl.y = 0;
            }
        }
        repaint();
    }

   public void keyReleased(KeyEvent e) {

   }

   public void keyTyped(KeyEvent e) {
     
   }
   
   public void paintComponent(Graphics g) {
      super.paintComponent(g);
     
      //draw grass
      for (int i = 0; i < 480; i += 32) {
         for (int x = 0; x < 320; x += 32) {
            g.drawImage(grass, i, x, null);
         }
      }
      //draw trees
      for (int i = 0; i < treeCount; i++) {
         g.drawImage(tree, treeX[i] * 32, treeY[i] * 32, null);
      }
      //draw flowers
      for (int i = 0; i < flowerCount; i++) {
         g.drawImage(flower, flowerX[i] * 32, flowerY[i] * 32, null);
      }
      gr.draw(g);
      pl.draw(g);
      requestFocusInWindow();
   }
}


Game.java (main control)
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
package Game;

import java.io.IOException;

import javax.swing.*;

public class Game {

   private static final long serialVersionUID = 1L;
   Camera camera;
   GamePanel gamePanel;
   HomePanel homePanel;
   PausedPanel pausedPanel;
   
   JFrame frame = new JFrame("RPG");
   
   public Game() {
      gamePanel = new GamePanel(this);
      homePanel = new HomePanel(this);
      pausedPanel = new PausedPanel(this);
     
      frame.add(homePanel);
      frame.setFocusable(true);
      frame.setTitle("RPG");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(800, 600);
      frame.setLocationRelativeTo(null);
      frame.setResizable(false);
      frame.setVisible(true);
   }
   
   public void showHomePanel() {
      frame.remove(gamePanel);
      frame.remove(pausedPanel);
      frame.add(homePanel);
      ((JPanel)frame.getContentPane()).revalidate();
      frame.repaint();
   }
   
   public void showGamePanel() {
      frame.remove(homePanel);
      frame.remove(pausedPanel);
      frame.add(gamePanel);
      ((JPanel)frame.getContentPane()).revalidate();
      frame.repaint();
   }
   
   public void showPausedPanel() {
      frame.remove(gamePanel);
      frame.remove(homePanel);
      frame.add(pausedPanel);
      ((JPanel)frame.getContentPane()).revalidate();
      frame.repaint();
   }

   public static void main(String[] args) throws IOException {
      new Game();
   }  
}


What I need to do is somehow get the position of the tree both X and Y then see if the Player X and Y are greater than the tree X and Y. To do so I need to get the position of the tree (X, Y) but not so sure, as it's determined in a for loop.

If you have any idea how I can do it, please leave a response.
Thanks.

"When you want to be successful as bad as you want to breathe, then you will be successful." - Eric Thomas
Offline ItsHarry

Innocent Bystander





« Reply #1 - Posted 2011-05-22 09:19:45 »

Could you show all the classes, also the one with the main loop
-EDIT-

In your repaint method, before you repaint everything, make a collision check. If the collision is true, just put x -32, otherwise don't do anything.
-EDIT-

Make a for loop, to check all collisions.
for (int i = 0; i < treeCount; i++) {
            if (treeX > pl.x)
            {
                  x -= 32;
            }
        }
Offline Shazer2

Junior Duke


Medals: 3


Aspiring developer.


« Reply #2 - Posted 2011-05-22 09:23:02 »

Updated with the main class, sorry about that.

"When you want to be successful as bad as you want to breathe, then you will be successful." - Eric Thomas
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline zoto

Senior Duke


Medals: 4



« Reply #3 - Posted 2011-05-22 11:04:57 »

Look at your code to draw the trees:
1  
treeX[i] * 32, treeY[i] * 32
is the x, y location for tree i.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (41 views)
2014-10-17 03:59:02

Norakomi (33 views)
2014-10-16 15:22:06

Norakomi (26 views)
2014-10-16 15:20:20

lcass (30 views)
2014-10-15 16:18:58

TehJavaDev (59 views)
2014-10-14 00:39:48

TehJavaDev (60 views)
2014-10-14 00:35:47

TehJavaDev (50 views)
2014-10-14 00:32:37

BurntPizza (66 views)
2014-10-11 23:24:42

BurntPizza (38 views)
2014-10-11 23:10:45

BurntPizza (80 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!