java实现打砖块小游戏

网友投稿 238 2022-12-06


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;

if(x1<=0)

x1=0;

if(x1>=400)

x1=400;

if(y1>=500-50)

y1=500-50;

if(y1&lthttp://;=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经典小游戏》

更多有趣的经典小游戏实现专题,分享给大家:

C++经典小游戏汇总

python经典小游戏汇总

python俄罗斯方块游戏集合

javascript经典游戏 玩不停

javascript经典小游戏汇总

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经典小游戏》

更多有趣的经典小游戏实现专题,分享给大家:

C++经典小游戏汇总

python经典小游戏汇总

python俄罗斯方块游戏集合

javascript经典游戏 玩不停

javascript经典小游戏汇总

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经典小游戏》

更多有趣的经典小游戏实现专题,分享给大家:

C++经典小游戏汇总

python经典小游戏汇总

python俄罗斯方块游戏集合

javascript经典游戏 玩不停

javascript经典小游戏汇总

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经典小游戏》

更多有趣的经典小游戏实现专题,分享给大家:

C++经典小游戏汇总

python经典小游戏汇总

python俄罗斯方块游戏集合

javascript经典游戏 玩不停

javascript经典小游戏汇总


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:java编写简易贪吃蛇游戏
下一篇:Java三种IO模型原理实例详解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~