java实现打砖块_java实现打砖块小游戏

本文实例为大家分享了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;
【java实现打砖块_java实现打砖块小游戏】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
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
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
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经典小游戏》
更多有趣的经典小游戏实现专题,分享给大家:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

    推荐阅读