//改了几行代码顺序,同时遇到两个砖块可以反弹了
GameFrame.java
package cn1;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class GameFrame extends JFrame{
public static int H=400;
public static int W=313;
//Image b=Toolkit.getDefaultToolkit();
public GameFrame()
{
BallPanel bp=new BallPanel();
bp.setBackground(new Color(0,0,0));
final Controller ct=new Controller(bp);
this.addKeyListener(ct);
ActionListener task=new ActionListener()
{
public void actionPerformed(ActionEvent e) {
ct.startGame();
}
};
Timer timer=new Timer(10,task);
timer.start();
this.setSize(W, H);
this.setResizable(false);
this.setTitle("弹弹球");
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭程序
this.add(bp);
}
public static void main(String[] args) {
GameFrame gf=new GameFrame();
gf.setVisible(true);
}
}
------------------------------------------
BallPanel.java
package cn1;
import java.awt.Graphics;
import javax.swing.JPanel;
public class BallPanel extends JPanel{
private Brick[][] brick;
private Board board;
private Ball ball;
Background bg;
public void acceptObject(Brick[][] brick,Board board,Ball ball,Background bg)
{
this.brick=brick;
this.board=board;
this.ball=ball;
this.bg=bg;
this.repaint();
}
public void paint(Graphics g) {
super.paint(g);
this.board.drawMe(g);
for(int i=0;i<brick.length;i++)
{
for(int j=0;j<brick[i].length;j++)
{
this.brick[i][j].drawMe(g);
}
}
this.ball.drawMe(g);
if(ball.isOver)
{
this.bg.drawGameover(g);
}
if(ball.isWin)
{
this.bg.drawGameWin(g);
}
}
}
-----------------------------------
Controller.java
package cn1;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.*;
public class Controller extends KeyAdapter{
Brick[][] brick =new Brick[6][11];
Board board;
BallPanel bp;
Ball ball;
Background bg;
boolean isStart=false;
public Controller(BallPanel bp)
{
this.bp=bp;
this.createBricks();
this.board=new Board();
this.ball=new Ball(this.board.getH());
this.bg = new Background();
bp.acceptObject(brick,board,ball,bg);
}
public void createBricks()
{
for(int i=0;i<brick.length;i++)
{
for(int j=0;j<brick[i].length;j++)
{
brick[i][j]=new Brick(i,j);
brick[i][j].setVisible(Math.random()>0.8?false:true);
}
}
}
public void keyPressed(KeyEvent e)
{
isStart=true;
int code = e.getKeyCode();
switch (code) {
case KeyEvent.VK_LEFT:
this.board.leftMove();
break;
case KeyEvent.VK_RIGHT:
this.board.rightMove();
break;
default:
break;
}
bp.acceptObject(brick,board,ball,bg);
}
public void startGame()
{
if(!isStart||ball.isWin)
return ;
if(this.ball.y>GameFrame.H-this.ball.d-this.board.getH())
{
ball.isOver=true;
bp.acceptObject(brick,board,ball,bg);
return;
}
winGame();
this.ball.x+=this.ball.pacex;
this.ball.y-=this.ball.pacey;
bp.acceptObject(brick,board,ball,bg);
if(ball.isStuck&&ball.y==board.y-ball.d&&ball.x>=board.x-ball.d/2&&ball.x<=board.x+board.w-ball.d/2)
{
ball.pacey=-ball.pacey;
}
for(int i=0;i<brick.length;i++)
{
for(int j=0;j<brick[i].length;j++)
{
Brick bk = brick[i][j];
if(!bk.isVisible()){
continue;
}
if(this.ball.x > bk.y * bk.w -ball.d&& this.ball.x < bk.y * bk.w + bk.w){
if(this.ball.y == bk.x * bk.h -ball.d|| this.ball.y == bk.x * bk.h + bk.h){
bk.setVisible(false);
ball.pacey=-ball.pacey;
return;
}
}
if(this.ball.y > bk.x * bk.h -ball.d&& this.ball.y < bk.x * bk.h + bk.h)
{
if(this.ball.x == bk.y * bk.w -ball.d|| this.ball.x == bk.y * bk.w + bk.w)
{
bk.setVisible(false);
ball.pacex=-ball.pacex;
return;
}
}
if(this.ball.x == bk.y * bk.w -ball.d||this.ball.x == bk.y * bk.w + bk.w)
{
if(this.ball.y == bk.x * bk.h -ball.d||this.ball.y==bk.x * bk.h + bk.h)
{
bk.setVisible(false);
ball.pacex=-ball.pacex;
ball.pacey=-ball.pacey;
return;
}
}
}
}
if(this.ball.x<0||this.ball.x>GameFrame.W-this.ball.d)
{
this.ball.pacex=-this.ball.pacex;
ball.isStuck=true;
}
if(this.ball.y<0)
{
this.ball.pacey=-this.ball.pacey;
ball.isStuck=true;
}
}
public void winGame()
{
for(int i=0;i<brick.length;i++)
{
for(int j=0;j<brick[i].length;j++)
{
if(brick[i][i].isVisible())
{
return;
}
}
}
ball.isWin=true;
bp.acceptObject(brick,board,ball,bg);
}
----------------------------------
Background.java
package cn1;
import java.awt.Graphics;
import java.awt.Toolkit;
import sun.awt.image.ToolkitImage;
public class Background {
private int x;//over
private int y;
private int x1;//win
private int y1;
ToolkitImage gameover;
ToolkitImage gameWin;
public Background()
{
this.gameover=(ToolkitImage)Toolkit.getDefaultToolkit().getImage("image/over.png");
this.gameWin=(ToolkitImage)Toolkit.getDefaultToolkit().getImage("image/win.png");
this.x=(GameFrame.W-gameover.getWidth())/2;
this.y=(GameFrame.H-31-gameover.getWidth())/2;
this.x1=(GameFrame.W-gameWin.getWidth())/2;
this.y1=(GameFrame.H-31-gameWin.getWidth())/2;
}
public void drawGameover(Graphics g)
{
System.out.println("----------");
g.drawImage(gameover, x, y,
gameover.getWidth(),
gameover.getHeight(), null);
}
public void drawGameWin(Graphics g)
{
g.drawImage(gameWin, x1, y1, null);
}
}
--------------------------------------------
Ball.java
package cn1;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Ball{
public int x;
public int y;
public int d;
Image b;
public int pacey=1;
public int pacex=1;
boolean isOver=false;
boolean isStuck=false;
boolean isWin=false;
public Ball(int boardH)
{
this.x=(GameFrame.W-this.d)/2-19;
this.y=GameFrame.H-boardH-this.d-49;
try {
this.b=ImageIO.read(new File("image/ball.png"));
} catch (IOException e) {
e.printStackTrace();
}
this.d=b.getHeight(null);
}
public void drawMe(Graphics g)
{
g.drawImage(b, x, y, null);
}
}
---------------------------------
Brick.java
package cn1;
import java.awt.*;
import sun.awt.image.ToolkitImage;
public class Brick {
public int x;
public int y;
ToolkitImage b;
public int w;
public int h;
private boolean isVisible=true;
public Brick(int x,int y)
{
this.x=x;
this.y=y;
try {
this.b = (ToolkitImage)Toolkit.getDefaultToolkit().getImage("image/brick.gif");
} catch (Exception e) {
e.printStackTrace();
}
this.w=b.getWidth();
this.h=b.getHeight();
}
public void drawMe(Graphics g)
{
if(this.isVisible)
g.drawImage(b,y*this.w, x*this.h, this.w-1, this.h-1, null);
}
public void setVisible(boolean isVisible) {
this.isVisible = isVisible;
}
public boolean isVisible() {
return isVisible;
}
}
----------------------------------
Board.java
package cn1;
import java.awt.Graphics;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Board {
public int x;
public int y;
Image board;
private int h;
public int w;
private int step=10;
public Board()
{
try
{
this.board=ImageIO.read(new File("image/board.jpg"));
}catch(IOException e)
{
e.printStackTrace();
}
this.h=board.getHeight(null);
this.w=board.getWidth(null);
this.y=GameFrame.H-this.h-29;
this.x=(GameFrame.W-this.w)/2-10;
}
public void drawMe(Graphics g)
{
g.drawImage(board, x, y, w, h, null);
}
public void leftMove()
{
if(this.x>0)
this.x-=this.step;
}
public void rightMove()
{
if(this.x<GameFrame.W-this.board.getWidth(null)-this.step)
this.x+=this.step;
}
public int getH() {
return h;
}
}
弹弹球
最新推荐文章于 2022-07-05 21:20:52 发布