- Joined
- Apr 9, 2010
- Messages
- 1
- Reaction score
- 0
I have 2 questions, but first, this is my source code so far:
-------------------------------------------------------------------------
import java.applet.*;
import java.applet.*;
import java.awt.*;
import java.util.Random;
public class MovingMultipleSquares extends Applet implements Runnable
{
int numberOfSquares = 5000;
int squareWidth = 10;
int squareHeight = 10;
Random randomGenerator;
int[][] squarePositions;
Color[] squareColors;
Thread tShapeMove;
static final int UP = 1;
static final int DOWN = 2;
static final int LEFT = 3;
static final int RIGHT = 4;
public void init()
{
setSize(1000,1000);
// generate random starting positions and colors for each
// of the numberOfSquares
randomGenerator = new Random();
squarePositions = generateRandomPositions(numberOfSquares);
squareColors = generateRandomColors(numberOfSquares);
// start thread to move squares
tShapeMove = new Thread(this);
tShapeMove.start();
}
public void run()
{
while(true)
{
for (int i = 0; i < numberOfSquares; i++)
{
squarePositions = move(squarePositions);
}
repaint();
try {
tShapeMove.sleep(1000/30);
} catch (InterruptedException e) { ; }
}
}
public int[] move(int[] square)
// for square, which is an array of x and y for the postion
// of the square, pick a random direction and move 1 pixel
// in that direction
{
int x = square[0];
int y = square[1];
int[] newPos = new int[2];
int iDirection;
int height = this.getSize().height;
int width = this.getSize().width;
iDirection = (int)(Math.random() * 4);
if (iDirection == UP)
y = y - 2;
if (iDirection == DOWN)
y = y + 2;
if (iDirection == LEFT)
x = x - 2;
if (iDirection == UP)
x = x + 2;
if (y < 0)
y = 0;
if (y > height - squareHeight)
y = height - squareHeight;
if (x < 0)
x = 0;
if (x > width - squareWidth)
x = width - squareWidth;
newPos[0] = x;
newPos[1] = y;
return newPos;
}
public int[][] generateRandomPositions(int number)
// For "number" of entries generate x and y position
// and return the array of poistions
{
int x;
int y;
int[][] pos = new int[number][2];
for (int i = 0; i < number; i++)
{
x = randomGenerator.nextInt(this.getSize().width);
y = randomGenerator.nextInt(this.getSize().height);
pos[0] = x;
pos[1] = y;
}
return pos;
}
public Color[] generateRandomColors(int number)
// For "number" of entries generate a random color
// composing of random RGB values between 0-255
{
int red;
int green;
int blue;
Color newColor;
Color[] colors = new Color[number];
for (int i = 0; i < number; i++)
{
red = randomGenerator.nextInt(255);
green = randomGenerator.nextInt(255);
blue = randomGenerator.nextInt(255);
newColor = new Color(red,green,blue);
colors = newColor;
}
return squareColors;
}
public void paint(Graphics g)
// paints the position and color of all the squares
{
g.drawString("Height = " + this.getSize().height , 10, 10);
g.drawString("Width = " + this.getSize().width , 10, 20);
for (int i = 0; i < numberOfSquares; i++)
{
g.setColor (squareColors);
g.fillRect (squarePositions[0], squarePositions[1], squareWidth, squareHeight);
}
}
}
-------------------------------------------------------------------------
I am not exactly new to java but i am not the most skilled with its use. I know that if you choose to compile and test this code it will seem a bit pointless to you but please bear in mind that this is just a simple test of some new concepts for me moving toward my final product.
Now for my questions:
1. How can I have the code detect collisions between the squares and then prevent them from moving over top one another?
I know this could be accomplished by adding something like this psuedo code to the move() method.
for (ieach of the other squares)
{
if (collides with another square)
dont move;
}
in the final result i think the most that will be present is around 1000 but running 1 million of these checks each tick seems like it would slow the program down a bit. Is there an easier, more efficient way of accomplishing this?
2. I also plan on having each of the squares having its own stats (health, attack, defence, etc.) and also seperate behavior algorithms. I know i could do this by creating each of the squares as objects but how can i make them all perform their intended independant actions in the same thread? I think giving them each there own thread would once again be highly inefficient.
I dont expect you to write the code for me or even to tell me how to, I can figure that part out. But if you could help point me in the right dirrection that would be extremely helpfull. Thanks.
-------------------------------------------------------------------------
import java.applet.*;
import java.applet.*;
import java.awt.*;
import java.util.Random;
public class MovingMultipleSquares extends Applet implements Runnable
{
int numberOfSquares = 5000;
int squareWidth = 10;
int squareHeight = 10;
Random randomGenerator;
int[][] squarePositions;
Color[] squareColors;
Thread tShapeMove;
static final int UP = 1;
static final int DOWN = 2;
static final int LEFT = 3;
static final int RIGHT = 4;
public void init()
{
setSize(1000,1000);
// generate random starting positions and colors for each
// of the numberOfSquares
randomGenerator = new Random();
squarePositions = generateRandomPositions(numberOfSquares);
squareColors = generateRandomColors(numberOfSquares);
// start thread to move squares
tShapeMove = new Thread(this);
tShapeMove.start();
}
public void run()
{
while(true)
{
for (int i = 0; i < numberOfSquares; i++)
{
squarePositions = move(squarePositions);
}
repaint();
try {
tShapeMove.sleep(1000/30);
} catch (InterruptedException e) { ; }
}
}
public int[] move(int[] square)
// for square, which is an array of x and y for the postion
// of the square, pick a random direction and move 1 pixel
// in that direction
{
int x = square[0];
int y = square[1];
int[] newPos = new int[2];
int iDirection;
int height = this.getSize().height;
int width = this.getSize().width;
iDirection = (int)(Math.random() * 4);
if (iDirection == UP)
y = y - 2;
if (iDirection == DOWN)
y = y + 2;
if (iDirection == LEFT)
x = x - 2;
if (iDirection == UP)
x = x + 2;
if (y < 0)
y = 0;
if (y > height - squareHeight)
y = height - squareHeight;
if (x < 0)
x = 0;
if (x > width - squareWidth)
x = width - squareWidth;
newPos[0] = x;
newPos[1] = y;
return newPos;
}
public int[][] generateRandomPositions(int number)
// For "number" of entries generate x and y position
// and return the array of poistions
{
int x;
int y;
int[][] pos = new int[number][2];
for (int i = 0; i < number; i++)
{
x = randomGenerator.nextInt(this.getSize().width);
y = randomGenerator.nextInt(this.getSize().height);
pos[0] = x;
pos[1] = y;
}
return pos;
}
public Color[] generateRandomColors(int number)
// For "number" of entries generate a random color
// composing of random RGB values between 0-255
{
int red;
int green;
int blue;
Color newColor;
Color[] colors = new Color[number];
for (int i = 0; i < number; i++)
{
red = randomGenerator.nextInt(255);
green = randomGenerator.nextInt(255);
blue = randomGenerator.nextInt(255);
newColor = new Color(red,green,blue);
colors = newColor;
}
return squareColors;
}
public void paint(Graphics g)
// paints the position and color of all the squares
{
g.drawString("Height = " + this.getSize().height , 10, 10);
g.drawString("Width = " + this.getSize().width , 10, 20);
for (int i = 0; i < numberOfSquares; i++)
{
g.setColor (squareColors);
g.fillRect (squarePositions[0], squarePositions[1], squareWidth, squareHeight);
}
}
}
-------------------------------------------------------------------------
I am not exactly new to java but i am not the most skilled with its use. I know that if you choose to compile and test this code it will seem a bit pointless to you but please bear in mind that this is just a simple test of some new concepts for me moving toward my final product.
Now for my questions:
1. How can I have the code detect collisions between the squares and then prevent them from moving over top one another?
I know this could be accomplished by adding something like this psuedo code to the move() method.
for (ieach of the other squares)
{
if (collides with another square)
dont move;
}
in the final result i think the most that will be present is around 1000 but running 1 million of these checks each tick seems like it would slow the program down a bit. Is there an easier, more efficient way of accomplishing this?
2. I also plan on having each of the squares having its own stats (health, attack, defence, etc.) and also seperate behavior algorithms. I know i could do this by creating each of the squares as objects but how can i make them all perform their intended independant actions in the same thread? I think giving them each there own thread would once again be highly inefficient.
I dont expect you to write the code for me or even to tell me how to, I can figure that part out. But if you could help point me in the right dirrection that would be extremely helpfull. Thanks.
Last edited: