您好,登錄后才能下訂單哦!
這篇文章主要講解了java如何實現打磚塊小游戲,內容清晰明了,對此有興趣的小伙伴可以學習一下,相信大家閱讀完之后會有幫助。
源碼共包含兩個文件
文件1:play_zhuankuai.java
import java.awt.*; import javax.swing.JPanel; @SuppressWarnings("serial") public class play_zhuankuai extends JPanel implements Runnable{ boolean exit=false; boolean end=false; double x=100; //控制小球坐標 double y=100; double x1=300; //控制底板坐標 double y1=300; int score=0; int index[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; //標記,記錄磚塊是否被打中 int x2[]=new int[15]; //控制磚塊數量和坐標 int y2[]=new int[15]; double degree=3.14/3; Image fangxing; Image diban; Image zhuankuai; Image heiping; Thread thread; play_zhuankuai(){ //設置面板屬性 jiazai(); thread = new Thread(this); setSize(500,500); setLocation(0,0); setBackground(Color.black); setVisible(true); thread.start(); } void jiazai(){ //導入圖片 fangxing=Toolkit.getDefaultToolkit().getImage("images/方形.jpg").getScaledInstance(10,10,Image.SCALE_DEFAULT); diban=Toolkit.getDefaultToolkit().getImage("images/底版.jpg").getScaledInstance(100, 10, Image.SCALE_DEFAULT); zhuankuai=Toolkit.getDefaultToolkit().getImage("images/磚塊.jpg").getScaledInstance(50, 20, Image.SCALE_DEFAULT); heiping=Toolkit.getDefaultToolkit().getImage("images/黑屏.png").getScaledInstance(50, 20, Image.SCALE_DEFAULT); } public void paintComponent(Graphics g){ super.paintComponent(g); paint_zhuankuai(g); g.setColor(Color.blue); g.drawLine(0, 350, 500, 350); g.drawImage(diban,(int)x1, (int)y1, null); paint_heiping(g); g.drawImage(fangxing, (int)x, (int)y, null); paint_wenzi(g); rule(); } void rule(){ x=x+1*Math.cos(degree); y=y+1*Math.sin(degree); if(y>=500-50||score==1500){ end=true; } if(x>=500-20||x<=0) degree=3.14-degree; if(x1<=0) x1=0; if(x1>=400) x1=400; if(y1>=500-50) y1=500-50; if(y1<=350) y1=350; if(y>=y1-10&&x>=x1&&x<=x1+100||y<=0) degree=-degree; for(int i=0;i<x2.length;i++){ if((y>=y2[i]&&y<=y2[i]+20)&&((x>=x2[i]&&x<=x2[i]+50))&&index[i]==0){ index[i]=1; degree=-degree; score+=100; } } } void paint_heiping(Graphics g){ for(int i=0;i<x2.length;i++){ if(index[i]==1) g.drawImage(heiping, x2[i], y2[i], null); } } void paint_wenzi(Graphics g){ g.setColor(Color.red); g.drawString("分數:"+score, 0, 340); if(end){ if(score<1500) g.drawString("游戲結束!!!",200,250); else g.drawString("闖關成功!!!",200,250); exit=true; } } void paint_zhuankuai(Graphics g){ int p=0; int k=0; for(int i=0,j=0;i<x2.length;i++){ p=100*j; j++; if(p+100>500){ j=1; p=0; k=k+60; } x2[i]=p; y2[i]=k; g.drawImage(zhuankuai, p,k,null); } } @Override public void run() { while(!exit){ repaint(); try { Thread.sleep(5); } catch (InterruptedException e) { // TODO 自動生成的 catch 塊 System.out.println("出現錯誤:"+e+"\n"); } } } }
文件2:jframe_main.java
import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.JFrame; @SuppressWarnings("serial") public class jframe_main extends JFrame implements KeyListener{ play_zhuankuai play=new play_zhuankuai(); jframe_main(){ addKeyListener(this); add(play); setSize(500,500); setLocation(0, 0); setVisible(true); } public static void main(String[] args) { new jframe_main(); } @Override public void keyTyped(KeyEvent e) { } @Override public void keyPressed(KeyEvent e) { if(e.getKeyCode()==38) play.y1-=20; if(e.getKeyCode()==40) play.y1+=20; if(e.getKeyCode()==37) play.x1-=20; if(e.getKeyCode()==39) play.x1+=20; } @Override public void keyReleased(KeyEvent e) { } }
看完上述內容,是不是對java如何實現打磚塊小游戲有進一步的了解,如果還想學習更多內容,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。