I am trying to build a game (circus of plates) , where I need to create random shapes falling and the player should catch them to increase the score . basically, I don't have a problem with randomly create objects, I just need them to fall.
I used objects pool design pattern to create a pool containing the created object, with a method borrowShape() to get one object form the pool.
In code bellow, I tried to use time schedule to sequentially falling object on after another.
public class view extends JFrame {
private static final long serialVersionUID = 1L;
public int x1 ;
public int y1 ;
public int x2 ;
public int y2 ;
public int Xframe ;
public int Yframe ;
// Keyboard c ;
private Image bfimage ;
private Graphics bfg ;
Image clown1 ;
Image clown2 ;
private Queue<Shapes> queue_1 = new LinkedList<Shapes>();
private Queue<Shapes> queue_2 = new LinkedList<Shapes>();
private Queue<Shapes> queue_3 = new LinkedList<Shapes>();
private Queue<Shapes> queue_4 = new LinkedList<Shapes>();
private LinkedList<Shapes> onScreen = new LinkedList<Shapes>();
Shapes shape_1 = new BlueMushroom();
Shapes shape_2 = new RedMushroom();
Shapes shape_3 = new GreenMushroom();
Shapes shape_4 = new RedMushroom();
ShapesPool pool;
FirstState frstate;
public view () throws InterruptedException{
ImageIcon i1 = new ImageIcon("clown111.gif");
clown1= i1.getImage() ;
ImageIcon i2 = new ImageIcon("clown22.gif");
clown2= i2.getImage() ;
// create pool
onScreen.add(new BlueMushroom());
Xframe= 1370 ;
Yframe = 700 ;
setTitle("game");
setSize(Xframe , Yframe);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
setLocationRelativeTo(null);
x1 = 0 ;
y1 = Yframe - 240 ;
x2 = 650 ;
y2 = Yframe - 240 ;
controller k = new controller(this , x1 , y1 , x2 , y2 , Xframe , Yframe) ;
this.addKeyListener(k);
Timer time = new Timer();
pool = ShapesPool.getInstance();
frstate = new FirstState();
Shapes shape = new BlueMushroom();
for(int i = 0 ; i < 5 ; i++){
shape = pool.borrowShape();
shape.setCurrentX(randomX());
shape.setCurrentY(10);
shape.setState(frstate);
queue_1.add(shape);
}
for(int i = 0 ; i < 7 ; i++){
shape = pool.borrowShape();
shape.setCurrentX(randomX());
shape.setCurrentY(10);
shape.setState(frstate);
queue_2.add(shape);
}
for(int i = 0 ; i < 9 ; i++){
shape = pool.borrowShape();
shape.setCurrentX(randomX());
shape.setCurrentY(10);
shape.setState(frstate);
queue_3.add(shape);
}
for(int i = 0 ; i < 11 ; i++){
shape = pool.borrowShape();
shape.setCurrentX(randomX());
shape.setCurrentY(10);
shape.setState(frstate);
queue_4.add(shape);
}
time.schedule(new RunShape(),2*1000, 2*1000);
time.wait();
}
int ypos1=0;
int ypos2=0;
int ypos3=0;
int ypos4=0;
Random rand = new Random();
public int randomX(){
return rand.nextInt(1700);
}
class RunShape extends TimerTask{
@Override
public void run(){
System.out.println("run");
Shapes shape = new BlueMushroom();
if(!queue_1.isEmpty()){
System.out.println("awl saaf ");
shape_1 = queue_1.poll();
addObject(shape_1);
shape = pool.borrowShape();
shape.setCurrentX(randomX());
shape.setCurrentY(10);
shape.setState(frstate);
queue_1.add(shape);
}
if(!queue_2.isEmpty()){
shape_2 = queue_2.poll();
addObject(shape_2);
shape = pool.borrowShape();
shape.setCurrentX(randomX());
shape.setCurrentY(10);
shape.setState(frstate);
queue_2.add(shape);
}
if(!queue_3.isEmpty()){
shape_3 = queue_3.poll();
addObject(shape_3);
shape = pool.borrowShape();
shape.setCurrentX(randomX());
shape.setCurrentY(10);
shape.setState(frstate);
queue_3.add(shape);
}
if(!queue_4.isEmpty()){
shape_4 = queue_4.poll();
addObject(shape_4);
shape = pool.borrowShape();
shape.setCurrentX(randomX());
shape.setCurrentY(10);
shape.setState(frstate);
queue_4.add(shape);
}
Timer time = new Timer();
time.schedule(new TimerTask(){
public void run(){
while(ypos1 <= 1600){
ypos1++;
shape_1.setCurrentY(ypos1);
try {
Thread.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ypos2++;
shape_2.setCurrentY(ypos2);
try {
Thread.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ypos3++;
shape_3.setCurrentY(ypos3);
try {
Thread.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ypos4++;
shape_4.setCurrentY(ypos4);
try {
Thread.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
repaint();
}
if(ypos1 == 1600 ){ ypos1 = 0;
shape_1.setCurrentY(ypos1);
}
if(ypos2 == 1600 ){ ypos2 = 0;
shape_2.setCurrentY(ypos2);
}
if(ypos3 == 1600 ){ ypos3 = 0;
shape_3.setCurrentY(ypos3);
}
if(ypos4 == 1600 ){ ypos4 = 0;
shape_4.setCurrentY(ypos4);
}
}
},1*1000 ,1*1000);
try {
time.wait(1*1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void addObject(Shapes shape){
onScreen.add(shape);
repaint();
}
public void paint(Graphics g) {
bfimage = createImage(getWidth(), getHeight()) ;
bfg = bfimage.getGraphics() ;
paintcomponent(bfg) ;
g.drawImage(bfimage, 0, 0, this) ;
}
public void paintcomponent(Graphics g){
g.drawImage(clown1, x1, y1, this) ;
g.drawImage(clown2, x2, y2, this) ;
g.drawImage(shape_1.getMushroom(), shape_1.getCurrentX(), shape_1.getCurrentY(), this);
g.drawImage(shape_2.getMushroom(), shape_2.getCurrentX(), shape_2.getCurrentY(), this);
g.drawImage(shape_3.getMushroom(), shape_3.getCurrentX(), shape_3.getCurrentY(), this);
g.drawImage(shape_4.getMushroom(), shape_4.getCurrentX(), shape_4.getCurrentY(), this);
for(Shapes shape : onScreen){
g.drawImage(shape.getMushroom(), shape.getCurrentX(), shape.getCurrentY(), this);
}
repaint() ;
}
}
Aucun commentaire:
Enregistrer un commentaire