JAVA实现经典《JAVA打砖块》游戏(JAVA 小虚竹)

您所在的位置:网站首页 超级豪华打砖块 JAVA实现经典《JAVA打砖块》游戏(JAVA 小虚竹)

JAVA实现经典《JAVA打砖块》游戏(JAVA 小虚竹)

2023-10-08 13:19| 来源: 网络整理| 查看: 265

「这是我参与2022首次更文挑战的第24天,活动详情查看:2022首次更文挑战」

❤️作者主页:小虚竹

❤️作者简介:大家好,我是小虚竹。Java领域优质创作者🏆,CSDN博客专家🏆,华为云享专家🏆,掘金年度人气作者🏆,阿里云专家博主🏆

❤️技术活,该赏

❤️点赞 👍 收藏 ⭐再看,养成习惯

前言

《JAVA打砖块》游戏是自制的游戏。玩家操作一根萤幕上水平的“棒子”,让一颗不断弹来弹去的“球”在撞击作为过关目标消去的“砖块”的途中不会落到萤幕底下。。

主要设计 设计游戏界面,用swing实现 设计砖块,砖块类, 设计小球,满屏乱跑的小球类,负责打碎砖块 设计棒子,左右移动的木头板类 球碰到砖块、棒子与底下以外的三边会反弹,落到底下会失去一颗球,把砖块全部消去就可以破关。 小球碰到砖块的回调算法设计 小球碰到棒子的回调算法设计 设计碰撞特效,一个负责显示爆炸效果的类 设计音效类,碰撞时发出音效。 功能截图

游戏开始:

image-20220223213023941

碰撞效果:

代码实现

游戏核心类

public class BricksGame extends BaseGame { private ArrayList list = new ArrayList(); private RenderTask render; private Random random = new Random(); private boolean over; private Image memImage; private Graphics memG;// 双缓冲的画布 public BricksGame(int width, int height, String title) throws HeadlessException { super(width, height, title); super.setBgColor(new Color(0x23,0x30,0x41)); initGame(width, height); } private void initGame(int width, int height) { setFps(60); over=false; // 获取窗口的内外大小 Container contentPane = this.getContentPane(); Dimension size = contentPane.getSize(); int contentWidth = size.width; int contentHeight = size.height; this.setContentWidth(contentWidth); this.setContentHeight(contentHeight); //System.out.println(contentPane instanceof JPanel);// true //System.out.println(size.width);//582 //System.out.println(size.height);//403 // 窗口四个方向的边界值 Insets insets = getInsets(); //System.out.println(insets.top); //System.out.println(insets.bottom); //System.out.println(insets.left); //System.out.println(insets.right); Scene env = new Scene(width, height, new Margin(insets.left, insets.right, insets.top, insets.bottom)); ImageIcon woodIcon = new ImageIcon("image/wood.png"); int w = woodIcon.getIconWidth(); int h = woodIcon.getIconHeight(); Wood wood = new Wood(w, h, new Color(0x97, 0x5B, 0x12)); wood.setScene(env); wood.setX(getWidth()/2 - w/2); wood.setY(getHeight()-50); wood.setImage(woodIcon.getImage()); list.add(wood); Ball ball = new Ball(10, Color.WHITE); ImageIcon iconBall = new ImageIcon("image/ball2.png"); ball.setImage(iconBall.getImage()); ball.setScene(env); ball.setCoordinator(width / 2 - ball.getRadius(), wood.getY()-ball.getRadius()*2); ball.setWoodBar(wood); list.add(ball); Color[] colors = new Color[]{ new Color(0xAA, 0xCF, 0x51), new Color(0xFC, 0xA9, 0x4B), new Color(0x73, 0xC7, 0xFF), Color.PINK, Color.GRAY }; //ImageIcon brickIcon = new ImageIcon("image/brick_1.png"); int brW = 60; int brH = 30; int start = 25; int brickX = start; int brickY = 100; int brickAreaWidth = getWidth() - start *2; int count = brickAreaWidth / brW - 1; int remainWidth = brickAreaWidth - count * brW; int intervalHort = brW + 3; start = brickX = (getWidth() - intervalHort * (count)) / 2; int intervalVert = brH + 3; HitBrick hitBrick = new HitBrick(); for (int j = 0; j < 3; j++) {// brick line for(int i = 0; i< count; i++){// brick columns Brick brick = new Brick(); brick.setColor(colors[i % colors.length]); //brick.setImage(brickIcon.getImage()); brick.setWidth(brW); brick.setHeight(brH); brick.setX(brickX); brick.setY(brickY); brick.setBall(ball); brick.setHitListener(hitBrick); brickX += intervalHort; list.add(brick); } brickX = start; brickY += intervalVert; } // 双缓冲,在内存里面创建一个和窗口JFrame一样大小的Image memImage = createImage(getWidth(), getHeight()); memG = memImage.getGraphics(); GameOver gameOver = new GameOver(memG); ball.setGameOverListener(gameOver); // 键盘事件的监听 Input input = new Input(); input.init(); addKeyListener(input); // 重新渲染画面任务 render = new RenderTask(this); render.start(); addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { //super.mousePressed(e); System.out.println(String.format("x:%d, y:%d", e.getX(), e.getY())); //x:218,y:305,w:164,h:45 if ((e.getX() >= 218 && e.getX() = 305 && e.getY()


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3