Java DxBall Oyunu

Java DxBall Oyunu

25. April 2009

Read this article in your language IT | EN | DE | ES

import java.awt.Color;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class TestFrame extends JFrame implements KeyListener{
    static JPanel content = new JPanel(null);
    static JButton btnSlider = new JButton();
    int sliderX = 360, sliderY = 551, sliderKacKayacak = 10;
    static int []vurusSayisi = new int[40];
    static Vector<JButton> mainVector = new Vector<JButton>();
    JButton btnTas ;
    static boolean[] vuruldumu = new boolean[40];

    private static final long serialVersionUID = 1L;
    public TestFrame() {

        AnimatedButton temp = new AnimatedButton("");//bu 4 satır buton oluşturma
        temp.setFocusable(false);
        content.add(temp);//
        Thread t = new Thread(temp);//
        int b=0,k=3;
        for(int i=0;i<40;i++)
        {
            if(i%10==0)
                b++;
            btnTas = new JButton();
            btnTas.setBounds((i%10)*79,(b-1)*20 , 79, 20);
            vurusSayisi[i] = k;
            content.add(btnTas);
            mainVector.add(btnTas);
            vuruldumu[i] = true;
            btnTas.setFocusable(false);
            if(i>9&&i<30)
            {
                btnTas.setBackground(Color.green);
                k=2;
            }
            if(i>29)
            {
                btnTas.setBackground(Color.yellow);
                k=1;
            }
            if(i<20&&i>=10)
                btnTas.setBackground(Color.blue);
            if(i<10&&i>=0)
                btnTas.setBackground(Color.red);

        }
        t.start();//
        btnSlider.setBackground(Color.black);
        btnSlider.setBounds(sliderX, sliderY, 80, 20);
        btnSlider.setFocusable(true);
        btnSlider.addKeyListener(this);
        content.add(btnSlider);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setContentPane(content);
        setVisible(true);
        setSize(800,600);
        setTitle("DX-Ball");

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
    public static void main(String[]args){
        new TestFrame();
    }

    public void keyPressed(KeyEvent e) {
        sliderX = btnSlider.getLocation().x;
        int bil = e.getKeyCode();        
        if(bil==39)
        {
            if(sliderX + sliderKacKayacak +80<=790)
                sliderX += sliderKacKayacak;
        }
        if(bil==37)
        {
            if(sliderX - sliderKacKayacak>=0)
                sliderX -= sliderKacKayacak;
        }
        btnSlider.setBounds(sliderX, sliderY, 80, 20);

    }
    @Override
    public void keyReleased(KeyEvent e) {


    }
    @Override
    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub

    }

}
class AnimatedButton extends JButton implements Runnable{
    /**
     *
     */
    int dx = 3;
    int dy = 3;

    private static final long serialVersionUID = 1L;

    public static boolean continueMovement = true;
    AnimatedButton(String pName){
        super(pName); // new JButton(pName)super dersem bu class dan bitane daha yaratır.
        setBounds(0, 120, 10, 10);
    }
    public void run() {


        do{
            int cx = this.getLocation().x;
            int cy = this.getLocation().y;

            if(cy+10+20>570)
            {
                int a = TestFrame.btnSlider.getLocation().x;
                if(cx+10>a && cx<a+80)
                    dy = -dy;
                else
                    continueMovement=false;
            }    
            if(cx+10>790)
                dx = -dx;
            if(cx<0)
                dx = -dx;
            if(cy<0)
                dy = -dy;
            int xVurulan = cx/79;
            int yVurulan = (cy-4)/20;
            int salla = (yVurulan*10)+xVurulan;
            if(cy<84){
                //    System.out.println(salla);
                //    System.out.println("xV="+xVurulan);
                //    System.out.println("yV="+yVurulan);
                //    System.out.println(TestFrame.vurusSayisi[salla]);
                if(TestFrame.vuruldumu[salla]){
                    TestFrame.vurusSayisi[salla]--;
                    if(TestFrame.vurusSayisi[salla]==0)
                    {
                        //System.out.println(TestFrame.vurusSayisi[salla]);
                        TestFrame.vuruldumu[salla] = false;
                        TestFrame.mainVector.get(salla).setVisible(false);
                    }
                    dy=-dy;                
                }
            }
            setBounds(cx+dx,cy+dy, 10, 10);



            try {
                Thread tr = Thread.currentThread();
                tr.sleep(18);
            }
            catch (InterruptedException e) {        
                e.printStackTrace(); }

        } while(continueMovement);}
}

Meşhur DxBall Oyununun java da yazılmış hali.

FacebookDigg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Java ,

Comments

8/7/2009 6:19:08 AM #
Bilgiler için teşekkürler
5/21/2010 5:08:24 PM #
You made numerous nice ideas there. I done a search on the issue and learnt nearly all peoples will agree with your blog.
5/22/2010 1:29:05 AM #
Have you ever considered adding more videos to your blog posts to keep the readers more entertained? I mean I just read through the entire article of yours and it was quite good but since I'm more of a visual learner,I found that to be more helpful well let me know how it turns out! I love what you guys are always up too. Such clever work and reporting! Keep up the great works guys I've added you guys to my blogroll. This is a great article thanks for sharing this informative information.. I will visit your blog regularly for some latest post.

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading



Sayfa 0.015625 saniyede yuklendi.