私は自分で簡単なプロジェクトを作成しようとしています.このプロジェクトはテトリスのゲームのように機能します.長方形オブジェクトはタイマーで作成され、3つの異なる色でランダムに作成されます.オブジェクトはリストに保持されます. moveRectangle() と control() 関数で Y の配置を確認しました。同じ色が3つ並んでいる、またはブロックの上にある場合、それらは削除され、上のブロックがそれらを置き換えます。それが私の問題です。あらゆる方法を試しましたが、できませんでした。
これは、長方形オブジェクトを作成し、オブジェクトの x 座標と y 座標を制御するために機能します。
package p1;
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.Timer;
public class DropObject {
private int yukseklik;
private int block;
private Color r;
//private static int sayac=-1;
private static int cntr;
private Rectangle object;
private static int sx=60;
private static int [] sy=new int[9];
public static int a=3;
public static Color [] colorR=new Color[25];
public DropObject(int yukseklik,int x,int y,int size,Color r)
{
x=sx;
//y=sy;
this.r=EventRect.RColor();
this.yukseklik=yukseklik;
//set();
this.object=new Rectangle(x,y,size,size);
}
/*public void set(){
colorR[cntr]=this.r;
//System.out.println("Renk: "+colorR[cntr]);
cntr++;
}*/
public static int getSX(){
return sx;
}
public static void setSX(int x){
sx=x;
}
public static int getSY(){
return sy[a];
}
public static void setSY(int y){
sy[a]=y;
}
public Color getColor(){
//colorR[++sayac]=this.r;
return this.r;
}
public int getYukseklik(){
return yukseklik;
}
public void setYukseklik(int yukseklik){
this.yukseklik=yukseklik;
}
public Rectangle getObject(){
return object;
}
public void setObject(Rectangle object){
this.object=object;
this.object.x=sx;
}
}
EventRect クラスは、オブジェクトの動きを提供し、オブジェクトの y 座標位置を指定します。
package p1;
import java.awt.Button;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
//import java.util.Timer;
import java.util.TimerTask;
import javax.swing.Timer;
import java.util.Random;
import javax.swing.JPanel;
public class EventRect extends JPanel {
private static int yukseklik=340;
private static Color BlockC;
private DropObject koordinat;
private int zheight=yukseklik;
private int fheight=yukseklik;
private int sheight=yukseklik;
private int theight=yukseklik;
private int foheight=yukseklik;
private int fiheight=yukseklik;
private int ssheight=yukseklik;
private int seheight=yukseklik;
private int seiheight=yukseklik;
private int counter=-1;
private int counter1=-1;
private int counter0=-1;
private List<DropObject> objects=new LinkedList<DropObject>();
private LinkedList<TimerTask> tasklist=new LinkedList<TimerTask>();
Timer timet;
int i=1;
public EventRect(){
addKeyListener(new InputKey());
setFocusable(true);
//System.out.println((objects.size()));
koordinat=new DropObject(yukseklik,60,-20,20,BlockC);
objects.add(koordinat);
startSampling();
//timet=new Timer(10,this);
//timet.start();
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d=(Graphics2D)g;
for(DropObject o : objects){
Rectangle r=o.getObject();
g2d.setColor(o.getColor());
//System.out.println(r.x);
g2d.fillRect(r.x, r.y, r.width+1, r.height+1);
// System.out.println(objects.size());
}
}
public void moveRectangle(){
for(int i=(objects.size()-1);i>=(objects.size()-1);i--){
DropObject o=objects.get(i);
Rectangle r=o.getObject();
if(DropObject.a==0){
DropObject.setSY(zheight);
o.setYukseklik(DropObject.getSY());
}
if(DropObject.a==1){
DropObject.setSY(fheight);
o.setYukseklik(DropObject.getSY());
}
if(DropObject.a==2){
DropObject.setSY(sheight);
o.setYukseklik(DropObject.getSY());
}
if(DropObject.a==3){
DropObject.setSY(theight);
o.setYukseklik(DropObject.getSY());
}
if(DropObject.a==4){
DropObject.setSY(foheight);
o.setYukseklik(DropObject.getSY());
}
if(DropObject.a==5){
DropObject.setSY(fiheight);
o.setYukseklik(DropObject.getSY());
}
if(DropObject.a==6){
DropObject.setSY(ssheight);
o.setYukseklik(DropObject.getSY());
}
if(DropObject.a==7){
DropObject.setSY(seheight);
o.setYukseklik(DropObject.getSY());
}
if(DropObject.a==8){
DropObject.setSY(seiheight);
o.setYukseklik(DropObject.getSY());
}
if(o.getYukseklik()>=0){
if(r.y<o.getYukseklik()){
r.y+=r.height;
o.setObject(r);
}else{
o.setYukseklik(o.getYukseklik()-r.height);
}
}
if(r.y == o.getYukseklik()){
control();
objects.add(new DropObject(o.getYukseklik(),60,-20,20,BlockC));
}
}
}
void control(){
if(DropObject.a==0){
zheight-=20;
DropObject.setSY(zheight);
}
if(DropObject.a==1){
fheight-=20;
DropObject.setSY(fheight);
}
if(DropObject.a==2){
sheight-=20;
DropObject.setSY(sheight);
}
if(DropObject.a==3){
theight-=20;
DropObject.setSY(theight);
}
if(DropObject.a==4){
foheight-=20;
DropObject.setSY(foheight);
}
if(DropObject.a==5){
fiheight-=20;
DropObject.setSY(fiheight);
}
if(DropObject.a==6){
ssheight-=20;
DropObject.setSY(ssheight);
}
if(DropObject.a==7){
seheight-=20;
DropObject.setSY(seheight);
}
if(DropObject.a==8){
seiheight-=20;
DropObject.setSY(seiheight);
}
}
void startSampling(){
TimerTask task=new TimerTask(){
public void run(){
moveRectangle();
repaint();
}
};
java.util.Timer timer=new java.util.Timer();
timer.scheduleAtFixedRate(task,0,150);
tasklist.add(task);
}
void stopSampling(){
if(tasklist.isEmpty()){
return;
}
tasklist.removeFirst().cancel();
}
public static Color RColor(){
Color [] Array={Color.blue,Color.pink,Color.gray};
Random random=new Random();
BlockC=Array[random.nextInt(Array.length)];
return BlockC;
}
}
鍵盤の動き;
package p1;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class InputKey extends KeyAdapter{
public void keyPressed(KeyEvent e){
int keys=e.getKeyCode();
if(keys==KeyEvent.VK_RIGHT){
//System.out.println("saga kaydi");
if(DropObject.getSX()<160)
DropObject.setSX(DropObject.getSX()+20);
if(DropObject.a<8){
DropObject.a++;
System.out.println("a nin degeri: "+DropObject.a);
}
}
else if(keys==KeyEvent.VK_LEFT){
if(DropObject.getSX()>0)
DropObject.setSX(DropObject.getSX()-20);
if(DropObject.a>0){
DropObject.a--;
}
}
}
}
package p1;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class Main implements Runnable{
@Override
public void run(){
JFrame frame=new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container container=frame.getContentPane();
container.setLayout(new GridLayout(0,1));
container.add(new EventRect());
frame.setSize(197,400);
frame.setVisible(true);
//container.setBackground(Color.BLUE);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Main());
}
}