打砖块JAVA_运⽤java开发打砖块游戏
运⽤java开发打砖块游戏
前⾯我们已经讲解了如何运⽤JAVA设计出贪吃蛇的游戏,那么现在我们接下来讲解第⼆个经典的游戏,称为打砖块游戏,那么这个游戏是怎么做的呢?其实,我现在讲解也只是讲解它的结构,那么具体怎么修改,还是需要⼤家去想象设计⾃⼰喜欢的游戏。
1.⾸先我们需要新建⼀个项⽬Peng,建⽴⼀个主类Peng(File-new-Class,输⼊类名Peng)。
2.再新建⼀个类MyPanel,(File-new-Class,输⼊类名MyPanel)代码如下:
import java.awt.*;
import java.awt.event.*;
public class Peng {
public Peng() {
Frame app = new Frame("打砖块,按空格开始");
//监视关闭窗体事件
app.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
}
});
app.setLocation(100, 100);
MyPanel drawB = new MyPanel(); //实例化MyPanel对象
app.add(drawB, BorderLayout.CENTER); //在窗体中间显⽰画版
app.pack();//运⾏app中的pack⽅法,动态调整frame的⼤⼩,使frame中的组件都可见app.setVisible(true);
drawB.run(); //运⾏app中的gameStart⽅法
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new Peng();
// TODO code application logic here
}
}
3.再新建⼀个类Pad,(File-new-Class,输⼊类名Pad)
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
public class Pad {
public Point location;//创建⼀个Point变量locatoin
public Point size;//创建⼀个Point变量size
private MyPanel gameP;//创建MyPanel实例,获取MyPanel中的⼀些变量值
public Pad(MyPanel gp) {
gameP = gp;//将MyPanel对象传给gameP
size = new Point(100, 20);//给size变量赋值
location = new Point((gp.width - size.x) / 2, gp.heigth - size.y);//给location变量赋值}
public void update() {
if (gameP.padMoveRight) {//如果MyPanel中padMoveRight的值是true
if (location.x + size.x < gameP.width) {//如果没到右边缘
location.x += 10;//向右移
}
}
if (gameP.padMoveLeft) {//如果MyPanel中padMoveLeft的值是true
if (location.x > 0) {//如果没到左边缘
location.x -= 10;//向左移
}
}
}
public void draw(Graphics g) {
g.setColor(Color.BLACK);//设置⿊⾊
g.fillRoundRect(location.x, location.y, size.x, size.y,10,10);//画出档板
}
}
4.再新建⼀个类Block,(File-new-Class,输⼊类名Block)
import java.awt.*;
public class Block {
private MyPanel gameP; //定义MyPanel实例gameP,⽤它来获取MyPanel中的变量值
private Point allocation;//定义Point变量
public Point location[];//定义Point数组变量
public Point size;//定义Point变量
public int num;
public boolean exist[];
public Block(MyPanel gp) {
gameP = gp;
num = 20;
allocation = new Point(4, 5);//定义Point变量
size = new Point(50, 20);//定义Point变量
location = new Point[num];//定义Point数组变量
for (int i = 0; i < allocation.y; i++) {
for (int j = 0; j < allocation.x; j++) {
location[i * allocation.x + j] = new Point((gp.width / allocation.x - size.x) / 2 + j * (gp.width / allocation.x), (i * 2 + 1) * size.y); System.out.println(location[i * allocation.x + j]);
}
}
exist = new boolean[num]; //
for (int i = 0; i < num; i++) {
exist[i] = true;
}
}
public void update() {
}
public void draw(Graphics g) {
g.ay);
for (int i = 0; i < num; i++) {
if (exist[i] == true) {//定义⼀个数组,画出⼤批量砖块,如果exist是true才画
g.fillRect(location[i].x, location[i].y, size.x, size.y);
}
}
}
}
5.最后建⼀个类Ball(File-new-Class,输⼊类名Ball)
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Administrator
*/
public class Ball {
private Point location;//定义Point变量
private int diameter;
private int dx;
private int dy;
private MyPanel gameP;//定义MyPanel实例gameP,⽤它来获取MyPanel中的变量值private Block blk;//定义Block实例,⽤它来获取Block中的变量值
private Pad pd;//定义Pad实例,⽤它来获取Pad中的变量值
private boolean padCanBounce = false;
public Ball(MyPanel gp, Pad p, Block bk) {//将其他⼏个类的值传递进来
gameP = gp;//获取MyPanel对象
blk = bk;//获取砖块对象
pd = p;//获取档板对象
diameter = 20;//⼩球的半径
location = new Point(pd.location.x + (pd.size.x - diameter) / 2, pd.location.y - pd.size.y);
//上⾯⼀⾏,是让⼩球处在档板的中间正上⽅.
dx = 5;//每次移动的偏移量
dy = -5;//每次移动的偏移量
}
public void update() {
if (gameP.ballMove) { //如果MyPanel中ballMove变量的值是true
location.x += dx;//⼩球横坐标增加
location.y += dy;//⼩球纵坐标增加
wallBounced();//碰到墙的事件
blockBounced();//碰到砖块的事件
padBounced();//碰到档板的事件
} else { //如果MyPanel中ballMove变量的值是false
location.setLocation(pd.location.x + (pd.size.x - diameter) / 2, pd.location.y - pd.size.y);
/
/让⼩球回到档板正中间,重新开球
}
}
public void draw(Graphics g) {
简单的java游戏代码g.setColor(Color.blue);//设置蓝⾊
g.fillOval(location.x, location.y, diameter, diameter);//⽤蓝⾊画球
}
public boolean Bounce(Point bk_location, Point bk_size) {//判断⼩球是否撞到砖
if ((location.x > bk_location.x - diameter) && (location.x < bk_location.x + bk_size.x) && (location.y > bk_location.y - diameter) && (location.y < bk_location.y + bk_size.y)) {
return true;//如果⼩球撞到砖,返回true
} else {
return false;//如果⼩球没有撞到砖,返回false
}
//以上是⽤边界检测法判断⼩球是不是撞到砖,这是⾮常重要的知识,参考课本P62
}
public void wallBounced() {//判断碰到墙没
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论