How to find Object in coordinate system
Hi2all !!
Well first for all im new to java and ive trying to get a job in a game-dev company;
As a test ive got a task to create a puyopuyo game.
All is goin pretty well with the development, but got some stucks.
Ok now the question:
Ive a scene 10px*10px (Example)
Ive an object with x,y coordinates
Its located at x=5 , y=5 ;
Now ive an other object (№2) x=4 , y=5;
The question is : How to find this object(number 2) ;
I mean i need a method that will check is there an object in a radius of 1px away from the Object in center;
Of course assuming that both objects are in one clollection;
Re: How to find Object in coordinate system
Ok a little bit more explanation;
The objects have one parameter with some vlaues;
I need to find objects having same values of a variable and 1px away from each other for example x or y;
Re: How to find Object in coordinate system
If I understand correctly, there are perhaps dozens of ways to do what you want. You can brute for it - loop through all the objects and find ones with the appropriate values, you could create a Map keyed with a custom object containing those values, you could create an adjacency matrix, you could maintain references to adjacent objects...
Re: How to find Object in coordinate system
Quote:
Originally Posted by
copeg
If I understand correctly, there are perhaps dozens of ways to do what you want. You can brute for it - loop through all the objects and find ones with the appropriate values, you could create a Map keyed with a custom object containing those values, you could create an adjacency matrix, you could maintain references to adjacent objects...
Okay... Here is the code
Simple PuyoPuyo GAme
Panel.class
Code :
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Panel extends JPanel{
Timer timer1,timer2;
Timer inputTimer;
int [][] grid = new int [12][7];
int vSpeed=200;
int dy=0;
List<Shape> spheres = new ArrayList<Shape>();
ArrayList toDelete = new ArrayList<Integer>();
private boolean spawn = false;
private boolean removeVertical;
private boolean removeHorizontal;
private boolean left;
private boolean right;
int objIndex;
int SpheresAmount;
//----------------------- The panel constructor
public Panel ()
{
//Init JPANEL
setFocusable(true);
setBackground(Color.BLACK);
setDoubleBuffered(true);
addKeyListener(new MyKeyAdapter());
timer1 = new Timer(vSpeed,new ListenerTimer1());
timer2 = new Timer (100,new ListenerTimer2());
timer1.start();
timer2.start();
spheres.add(new Shape());
}
/* ------------------ Main Method------------------*/
public void paint (Graphics g)
{
super.paint(g);
Graphics2D g2d= (Graphics2D) g;
Iterator<Shape> j = spheres.iterator();
while (j.hasNext())
{
Shape s = j.next();
g.drawImage(s.getImage(),s.getY(),s.getX(),null );
//-------Border Collision check-------//
if (grid[s.getGx()+1][s.getGy()]!=0||s.getGx()==10)
{
if (!j.hasNext())
{
s.setDx(0);
grid[s.getGx()][s.getGy()]=s.getColor();
spawn=true;
}
}
//-------3 vertical check-------//
if (grid[s.getGx()+1][s.getGy()]==s.getColor()&&grid[s.getGx()-1][s.getGy()]==s.getColor())
{
SpheresAmount=spheres.size();
removeVertical=true;
}
//-------3 horizontal check---
if (grid[s.getGx()][s.getGy()+1]==s.getColor()&&grid[s.getGx()][s.getGy()+2]==s.getColor())
removeHorizontal=true;
//--------END LOOP-------//
}
if (removeVertical)
{
deleteVertical();
removeVertical=false;
}
if (removeHorizontal)
{
deleteHorizontal();
removeHorizontal=false;
}
if(spawn)
{
spheres.add(new Shape());
spawn = false;
}
}
private void deleteHorizontal()
{
Iterator<Shape> j = spheres.iterator();
while (j.hasNext()){
Shape d = j.next();
if (spheres.size()+2<SpheresAmount){
grid[d.getGx()][d.getGy()]=0;
grid[d.getGx()][d.getGy()+1]=0;
grid[d.getGx()][d.getGy()+2]=0;
SpheresAmount=spheres.size();
}
}
}
private void deleteVertical() {
Iterator<Shape> j = spheres.iterator();
while (j.hasNext()){
Shape d = j.next();
if (grid[d.getGx()+1][d.getGy()]==d.getColor()&&grid[d.getGx()+2][d.getGy()]==d.getColor()){j.remove();}
if (grid[d.getGx()-1][d.getGy()]==d.getColor()&&grid[d.getGx()+1][d.getGy()]==d.getColor()){j.remove();}
if (grid[d.getGx()-1][d.getGy()]==d.getColor()&&grid[d.getGx()-2][d.getGy()]==d.getColor()){j.remove();}
if (spheres.size()+2<SpheresAmount){
grid[d.getGx()][d.getGy()]=0;
grid[d.getGx()+1][d.getGy()]=0;
grid[d.getGx()+2][d.getGy()]=0;
SpheresAmount=spheres.size();
}
}
}
// ---------------- INPUT -------------------//
private class MyKeyAdapter extends KeyAdapter
{
public void keyReleased (KeyEvent e){
int key = e.getKeyCode();
if(key==KeyEvent.VK_LEFT)left=false;
if(key==KeyEvent.VK_RIGHT)right=false;
}
public void keyPressed (KeyEvent e){
int key = e.getKeyCode();
if(key==KeyEvent.VK_LEFT)left=true;
if(key==KeyEvent.VK_RIGHT)right=true;
}
}
// ------------Action Listener---------------//
private class ListenerTimer1 implements ActionListener
{
public void actionPerformed(ActionEvent arg0)
{
Iterator <Shape> j = spheres.iterator();
while (j.hasNext())
{
Shape s = j.next();
s.move();
}
repaint();
}
}
// ------------Input movement updater-----------//
private class ListenerTimer2 implements ActionListener
{
public void actionPerformed(ActionEvent arg0)
{
Iterator <Shape> j = spheres.iterator();
while (j.hasNext())
{
Shape s = j.next();
if (s.dx!=0&&right==true){
s.moveRight();repaint();
}
if (s.dx!=0&&left==true){
s.moveLeft();repaint();
}
}
}
}
//-----------------------------------------------//
}
Shape.class
Code :
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.ImageIcon;
public class Shape {
int x,y,gx,gy;
int dx = 32;
int dy;
int i;
int colorCode=1;
Random random = new Random();
String texture="red.png";
Image image;
ImageIcon ii;
boolean delete ;
/* --------------- Constructor -------------------*/
public Shape ()
{
//colorGen ();
ii = new ImageIcon(getClass().getResource(texture));
image = ii.getImage();
}
//------The random color generator----
public void colorGen ()
{
int ran = random.nextInt(4);
switch (ran){
case 0: texture = "red.png";colorCode=1;break;
case 1 :texture = "green.png";colorCode=2;break;
case 2 : texture = "blue.png";colorCode=3;break;
case 3 : texture = "yellow.png";colorCode=4;break;
}
}
//-------------Movement-------------
public void keyPressed (KeyEvent e){
int key = e.getKeyCode();
if(dx!=0&&key==KeyEvent.VK_LEFT)dy=-32;
if(dx!=0&&key==KeyEvent.VK_RIGHT)dy=32;
System.out.println(dy);
}
public void keyReleased (KeyEvent e){
int key = e.getKeyCode();
if(key==KeyEvent.VK_LEFT)dy=0;
if(key==KeyEvent.VK_RIGHT)dy=0;
}
//-----------------------------------
public Image getImage ()
{
return image;
}
public void move ()
{
x+=dx;
}
public void moveLeft(){
y-=32;
if (y<=0)y=0;
}
public void moveRight(){
y+=32;
if (y>=160)y=160;
}
public void setDx (int dx)
{
this.dx = dx;
}
public void setDy (int dy)
{
this.dy = dy;
}
public void setX(int x)
{
this.x=x;
}
public void setY(int y)
{
this.y=y;
}
public void setDelete(boolean delete){
this.delete=delete;
}
public boolean getDelete(){
return delete;
}
public int getX(){
return x;
}
public int getY(){
return y;
}
public int getGx(){
return gx=x/32;
}
public int getGy (){
return gy=y/32;
}
public int getColor () {
return colorCode;
}
}
Well i know that im doing something terrible wrong but i just cant see it maybe some one can give a fresh look at this situtation or maybe what would be the best aproach in this case to find this spheres.
Re: How to find Object in coordinate system
Quote:
Originally Posted by
Eldarmk2
Hi2all !!
Well first for all im new to java and ive trying to get a job in a game-dev company;
As a test ive got a task to create a puyopuyo game.
All is goin pretty well with the development, but got some stucks.
Ok now the question:
Ive a scene 10px*10px (Example)
Ive an object with x,y coordinates
Its located at x=5 , y=5 ;
Now ive an other object (№2) x=4 , y=5;
The question is : ...
This presents a moral dilemma for us. If this is a test, should you be soliciting help here online? Should we be helping you?
Re: How to find Object in coordinate system
Well im allowed to use any source of information to get the test done.
Im not allowed to copy existing code.