这篇文章主要讲解了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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。