java編寫的簡單移動方塊小遊戲代碼_java

來源:互聯網
上載者:User

本文執行個體講述了java編寫的簡單移動方塊小遊戲代碼。分享給大家供大家參考,具體如下:

運行效果截圖如下:

第一次用java編寫圖形化的介面,還是有些青澀。。以後繼續努力!!具體代碼如下:

//Little Box Game by AlexYui//Game.java By 1093710210@ HITimport javax.swing.*;import java.awt.event.*;import java.awt.geom.*;import java.awt.*;import java.util.*;public class Game{ public static void main(String[] agrs){  MyFrame gameframe = new MyFrame();  gameframe.setTitle("Java Box Game By AlexYui");  gameframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  gameframe.setVisible(true); }}//架構class MyFrame extends JFrame { private int flag = 0; protected double x[] = {0,0,270,0,270}; protected double y[] = {0,0,0,270,270};  protected int winner[]={0,0,0,0,0}; public static final int DEFAULT_WIDTH = 317; public static final int DEFAULT_HEIGHT = 339; public MyFrame(){ MyPanel a = new MyPanel(); addKeyListener(new MyKeyListener()); this.add(a); setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT); }class MyPanel extends JPanel{ public MyPanel(){} public void paint(Graphics g){ super.paint(g); Graphics2D g2 = (Graphics2D)g; Rectangle2D rect1 = new Rectangle2D.Double(x[1],y[1],30f,30f); Rectangle2D rect2 = new Rectangle2D.Double(x[2],y[2],30f,30f); Rectangle2D rect3 = new Rectangle2D.Double(x[3],y[3],30f,30f); Rectangle2D rect4 = new Rectangle2D.Double(x[4],y[4],30f,30f); //畫中間的四個方框 g2.setPaint(Color.RED); g2.drawRect(120,120 ,30,30); g2.drawRect(120,150 ,30,30); g2.drawRect(150,120 ,30,30); g2.drawRect(150,150 ,30,30); //填充四個角上的方塊 g2.setPaint(Color.BLUE); if(winner[1] == 0)g2.fill(rect1); if(winner[2] == 0)g2.fill(rect2); if(winner[3] == 0)g2.fill(rect3); if(winner[4] == 0)g2.fill(rect4); g2.setPaint(Color.RED); //將已經選擇的方塊塗成紅色 if(flag == 1) g2.fill(rect1);     if(flag == 2) g2.fill(rect2);   if(flag == 3) g2.fill(rect3);   if(flag == 4)   g2.fill(rect4);   g2.setPaint(Color.BLUE); if(winner[1] == 1)g2.fill(rect1); if(winner[2] == 1)g2.fill(rect2); if(winner[3] == 1)g2.fill(rect3); if(winner[4] == 1)g2.fill(rect4);  g2.setPaint(Color.RED);   if(winner[1] == 1 && winner[2] == 1 && winner[3] == 1 && winner[4] == 1) g2.drawString("Succes! You Won!",100,75); //保持邊框的顏色 g2.drawRect(120,120 ,30,30); g2.drawRect(120,150 ,30,30); g2.drawRect(150,120 ,30,30); g2.drawRect(150,150 ,30,30);   repaint(); }} class MyKeyListener implements KeyListener{   //overide   public void keyPressed(KeyEvent event){   int keyCode = event.getKeyCode();     if(keyCode == KeyEvent.VK_1){flag = 1;}     if(keyCode == KeyEvent.VK_2){flag = 2;}     if(keyCode == KeyEvent.VK_3){flag = 3;}     if(keyCode == KeyEvent.VK_4){flag = 4;}     if(keyCode == KeyEvent.VK_UP){move(0,-30,flag);win();}     if(keyCode == KeyEvent.VK_DOWN){move(0,30,flag);win();}     if(keyCode == KeyEvent.VK_LEFT){move(-30,0,flag);win();}     if(keyCode == KeyEvent.VK_RIGHT){move(30,0,flag);win();}     }   public void keyReleased(KeyEvent event){}   public void keyTyped(KeyEvent event){}   }   //用於判別基本規則,移動方塊   void move(int mx,int my,int mflag){     int i;     boolean teller = false;     x[mflag] +=mx;     y[mflag] +=my;     if(! ((x[flag] >= 0 && x[flag]<=270)&&(y[flag] >= 0 && y[flag]<=270))){     x[mflag] -=mx;     y[mflag] -=my;       }     for(i = 1;i<5;i++){       if(mflag != i)       if(x[mflag] == x[i] && y[mflag] == y[i])       teller = true;       }     if(teller == true){    x[mflag] -= mx;     y[mflag] -= my;       }     }     //用於判別已經進入符合條件地區的方塊     void win(){       int j1,j2;       for(j1=1;j1<5;j1++)       if((x[j1]<=150&&x[j1]>=120)&&(y[j1]<=150&&y[j1]>=120))         winner[j1]=1;       for(j2=1;j2<5;j2++)       if(!((x[j2]<=150&&x[j2]>=120)&&(y[j2]<=150&&y[j2]>=120)))         winner[j2]=0;     }}

希望本文所述對大家Java程式設計有所協助。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.