Java 使用定时器的随机落物
我正在尝试建立一个游戏(盘子马戏团),在那里我需要创建随机形状下降,玩家应该抓住他们,以增加他们的分数。基本上,我不存在随机创建对象的问题,我只需要它们掉落 我使用对象池设计模式创建了一个包含已创建对象的池,并使用方法Java 使用定时器的随机落物,java,random,timer,Java,Random,Timer,我正在尝试建立一个游戏(盘子马戏团),在那里我需要创建随机形状下降,玩家应该抓住他们,以增加他们的分数。基本上,我不存在随机创建对象的问题,我只需要它们掉落 我使用对象池设计模式创建了一个包含已创建对象的池,并使用方法borrowShape()从池中获取一个对象 在下面的代码中,我尝试使用time.schedule一个接一个地连续制作坠落物体。问题是我不能让它们随机掉落。我想可能是计时器的问题,因为它们没有完全落下-它们在帧上的任何点停止,然后其他对象开始落下,它们也没有完全落下 public
borrowShape()
从池中获取一个对象
在下面的代码中,我尝试使用
time.schedule
一个接一个地连续制作坠落物体。问题是我不能让它们随机掉落。我想可能是计时器的问题,因为它们没有完全落下-它们在帧上的任何点停止,然后其他对象开始落下,它们也没有完全落下
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) ;
for(Shapes shape : onScreen){
g.drawImage(shape.getMushroom(), shape.getCurrentX(), shape.getCurrentY(), this);
}
repaint() ;
}
}
public类视图扩展了JFrame{
私有静态最终长serialVersionUID=1L;
公共int x1;
公共int y1;
公共int x2;
公共int y2;
公共int-Xframe;
公共国际框架;
//键盘c;
私人形象;
私人图形;
图像1;
图像2;
专用队列队列_1=新的LinkedList();
private Queue_2=新链接列表();
专用队列队列_3=新的LinkedList();
private Queue_4=新链接列表();
屏幕上的私有LinkedList=新建LinkedList();
Shapes shape_1=新的蓝蘑菇();
Shapes shape_2=新的红蘑菇();
Shapes shape_3=新的绿蘑菇();
Shapes shape_4=新的红蘑菇();
形状池;
第一州;
公共视图()抛出InterruptedException{
ImageIcon i1=新的ImageIcon(“小丑111.gif”);
小丑1=i1.getImage();
ImageIcon i2=新的ImageIcon(“小丑22.gif”);
小丑2=i2.getImage();
//创建池
在屏幕上添加(新的蓝蘑菇());
Xframe=1370;
Yframe=700;
片名(“游戏”);
设置大小(Xframe、Yframe);
可设置大小(假);
setDefaultCloseOperation(关闭时退出);
setVisible(真);
setLocationRelativeTo(空);
x1=0;
y1=Yframe-240;
x2=650;
y2=Yframe-240;
控制器k=新控制器(此,x1,y1,x2,y2,Xframe,Yframe);
这个.addKeyListener(k);
定时器时间=新定时器();
pool=ShapesPool.getInstance();
frstate=新的FirstState();
形状=新的蓝蘑菇();
对于(int i=0;i<5;i++){
shape=pool.followshape();
setCurrentX(randomX());
形状。设置电流(10);
形状设置状态(frstate);
队列_1.添加(形状);
}
对于(int i=0;i<7;i++){
shape=pool.followshape();
setCurrentX(randomX());
形状。设置电流(10);
形状设置状态(frstate);
队列2.添加(形状);
}
对于(int i=0;i<9;i++){
shape=pool.followshape();
setCurrentX(randomX());
形状。设置电流(10);
形状设置状态(frstate);
队列_3.添加(形状);
}
对于(int i=0;i<11;i++){
shape=pool.followshape();
setCurrentX(randomX());
形状。设置电流(10);
形状设置状态(frstate);
队列4.添加(形状);
}
时间表(新运行形状(),2*1000,2*1000);
时间,等等;
}
int ypos1=0;
int ypos2=0;
int ypos3=0;
int ypos4=0;
Random rand=新的Random();
公共int randomX(){
返回兰特·耐克斯汀(1700);
}
类RunShape扩展了TimerTask{
@凌驾
公开募捐{
System.out.println(“运行”);
形状=新的蓝蘑菇();
如果(!queue_1.isEmpty()){
System.out.println(“awl saaf”);
shape_1=队列_1.poll();
addObject(shape_1);
shape=pool.followshape();
setCurrentX(randomX());
形状。设置电流(10);
形状设置状态(frstate);
队列_1.添加(形状);
}
如果(!queue_2.isEmpty()){
shape_2=队列_2.poll();
addObject(shape_2);
shape=pool.followshape();
setCurrentX(randomX());
形状。设置电流(10);
形状设置状态(frstate);
队列2.添加(形状);
}
如果(!queue_3.isEmpty()){
shape_3=队列_3.poll();
addObject(shape_3);
shape=pool.followshape();
setCurrentX(randomX());
形状。设置电流(10);
形状设置状态(frstate);
队列_3.添加(形状);
}
如果(!queue_4.isEmpty()){
shape_4=队列_4.poll();
addObject(shape_4);
shape=pool.followshape();
setCurrentX(randomX());
形状。设置电流(10);
形状设置状态(frstate);
队列4.添加(形状);
}
定时器时间=新定时器();
时间表(新的TimerTask(){
公开募捐{
while(ypos1)那么你的问题和问题是什么?我不能让它们随机掉落,我想可能是计时器的问题,因为计时器没有完成下落并在帧上的任何点停止,然后其他物体开始下落,也没有完成下落。太好了,所以请将注释带到问题中以进一步澄清