JAVA SE 架構之俄羅斯方塊的效果

來源:互聯網
上載者:User

標籤:style   blog   color   java   os   io   ar   art   

Mygame

package com.sun.c;import java.awt.event.KeyListener;import com.sun.v.MyJpanel;import com.sun.v.Myjframe;public class Mygame {    public Mygame() {        // TODO Auto-generated constructor stub    }    public static void main(String[] args) {        // TODO Auto-generated method stub        Myjframe jframe = new Myjframe();        MyJpanel panel = new MyJpanel();                jframe.add(panel);        jframe.addKeyListener(panel);    }}
Myjframe
package com.sun.v;import javax.swing.JFileChooser;import javax.swing.JFrame;/* * 這裡採用MVC的設計模式,講邏輯和頁面分開, */public class Myjframe extends JFrame{    public Myjframe(){               this.setTitle("myworld");       this.setVisible(true);       this.setSize(400,400);       this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);    }     }

MyJpanel

package com.sun.v;import java.awt.Graphics;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import javax.swing.JPanel;import com.sun.m.Dog;public class MyJpanel extends JPanel implements KeyListener,Runnable{    public MyJpanel() {        //MyJpanel jp = new MyJpanel();        Thread td = new Thread(this);        td.start();    }    Dog dog = new Dog(20,20,2);        public void paint(Graphics g){        super.paint(g);        g.draw3DRect(dog.x, dog.y,20 ,20, false);    }    @Override    public void keyTyped(KeyEvent e) {        // TODO Auto-generated method stub            }    @Override    public void keyPressed(KeyEvent e) {        // TODO Auto-generated method stub        //System.out.println("sunzhiyan");        switch (e.getKeyCode()){        case KeyEvent.VK_W:            dog.direct = 0;            dog.move(dog.direct);         break;        case KeyEvent.VK_D:            dog.direct = 1;            dog.move(dog.direct);         break;        case KeyEvent.VK_S:            dog.direct = 2;            dog.move(dog.direct);         break;        case KeyEvent.VK_A:            dog.direct = 3;            dog.move(dog.direct);         break;        }                this.repaint();    }    @Override    public void keyReleased(KeyEvent e) {        // TODO Auto-generated method stub            }    @Override    public void run() {        // TODO Auto-generated method stub        while(true){            try {                Thread.sleep(1000);                //System.out.println("我被調用了");                dog.direct = 2;                dog.move(dog.direct);                this.repaint();            } catch (InterruptedException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }                    }            }}

 

 

Dog

 


 1 package com.sun.m; 2  3 public class Dog { 4     public int x; 5     public int y; 6     public int direct ; 7     public Dog(int x,int y, int direct){ 8         this.x = x; 9         this.y = y;10         this.direct = direct;11     }12     public void move(int direct){13         switch (direct){        14         case 0:15             this.y--;16             break;17         case 1:18             this.x++;19             break;20         case 2:21             this.y++;22             break;23         case 3:24             this.x--;25             break;      26         }27         28     }29 }

 



 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.