本文主要是介绍JAVA课程设计 打砖块游戏(颜色匹配版) JAVA语言,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
玩法简介:
主要玩法是:球,板子,砖块都分为红蓝两种颜色.砖块会随机分配两种颜色,球碰撞同颜色板子或者砖块会反弹,不同颜色的会直接穿过,板子会根据鼠标指针坐标确定位置.每次碰撞板子会改变小球颜色.单击鼠标左键可以改变板子上下颜色.
package ball.last;import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;public class BallPanel extends JPanel implements ActionListener,KeyListener {//球的状态int ballX;int ballY;int ballX_V;int ballY_V;boolean ballcolor;//板子(鼠标)状态int Mouse_X;int Mouse_Y;boolean Mouse_Color;//游戏状态boolean isStart = false;boolean isFail = false;//随机数Random random;//砖块类数组zhuan[] zhuan;final int wide = 100;final int high = 30;Timer timer = new Timer(10, this);//构造方法public BallPanel() {init();//鼠标移动监听this.addMouseMotionListener(new Mou());//按压监听this.addMouseListener(new Mou());this.setFocusable(true);this.addKeyListener(this);timer.start();}//初始化方法public void init() {//坐标设置ballX = 450;ballY = 450;ballX_V = 5;ballY_V = 4;ballcolor = true;//板子属性设置(鼠标坐标)Mouse_X = 500;Mouse_Y = 500;Mouse_Color = true;//随机数random = new Random();//新建砖块,并设定位置大小zhuan = new zhuan[20];for (int j = 0, i = 0; j < 5; j++) {for (int k = 0; k < 4; k++) {int result = random.nextInt(100);if (result % 2 == 0) {zhuan[i] = new zhuan(true, true, 25 + j * 130, 25 + k * 40);i++;} else {zhuan[i] = new zhuan(true, false, 25 + j * 130, 25 + k * 40);i++;}}}}//绘画类@Overrideprotected void paintComponent(Graphics g) {super.paintComponent(g);if (ballcolor) {g.setColor(Color.RED);} else {g.setColor(Color.BLUE);}g.fillOval(ballX, ballY, 20, 20);this.setBackground(Color.white);//画砖块for (int i = 0; i < 20; i++) {if (zhuan[i].isOk()) {if (zhuan[i].isColor()) {g.setColor(Color.RED);} else {g.setColor(Color.blue);}g.fillRect(zhuan[i].getX(), zhuan[i].getY(), wide, high);}}//绘制板子if (Mouse_Color) {g.setColor(Color.RED);g.fillRect(Mouse_X - 50, Mouse_Y - 10, 100, 10);g.setColor(Color.BLUE);g.fillRect(Mouse_X - 50, Mouse_Y, 100, 10);} else {g.setColor(Color.BLUE);g.fillRect(Mouse_X - 50, Mouse_Y - 10, 100, 10);g.setColor(Color.RED);g.fillRect(Mouse_X - 50, Mouse_Y, 100, 10);}// 开始和结束文字if (isStart == false) {g.setColor(Color.BLACK);g.setFont(new Font("微软雅黑", Font.BOLD, 40));g.drawString("鼠标双击开始游戏", 300, 300);}if (isFail == true) {g.setColor(Color.BLACK);g.setFont(new Font("微软雅黑", Font.BOLD, 40));g.drawString("游戏结束", 300, 300);}}//事件监听类@Overridepublic void actionPerformed(ActionEvent e) {//与墙和挡板的碰撞判定if (isStart && isFail == false) {//左右边界if (ballX >= 680 || ballX <= 0) {ballX_V *= -1;} else if (ballY <= 0 || ballY >= 650) {//上下边界ballY_V *= -1;} else if (ballY >= 660) {//演示//失败判定isFail = true;}//板子与球碰撞判定if (ballX >= Mouse_X - 50 && ballX <= Mouse_X + 50) {if (ballY >= Mouse_Y - 10 && ballY <= Mouse_Y) {//判断板子颜色和球颜色相同//只判定上方,按鼠标颜色属性...if (ballcolor == Mouse_Color) {ballY_V *= -1;ballcolor = !ballcolor;}}}//砖块与球碰撞判定for (int i = 0; i < 20; i++) {if (zhuan[i].isOk()) {if (ballX > zhuan[i].getX() && ballY > zhuan[i].getY()) {if (ballX < zhuan[i].getX() + wide && ballY < zhuan[i].getY() + high) {if (ballcolor == zhuan[i].isColor()) {ballY_V *= -1;zhuan[i].setOk(false);continue;}}}}}//结束判定条件for (int i = 0, k = 0; i < 20; i++) {if (zhuan[i].isOk() == false) {k++;if (k == 20) {isFail = true;break;}} else {k = 0;}}//运动ballX += ballX_V;ballY += ballY_V;repaint();}}@Overridepublic void keyPressed(KeyEvent e) {if(e.getKeyCode() == KeyEvent.VK_SPACE){System.out.println("1");}}//鼠标监听类private class Mou extends MouseAdapter {//点击@Overridepublic void mouseClicked(MouseEvent e) {super.mouseClicked(e);Mouse_Color = !Mouse_Color;if (e.getClickCount() == 2)isStart = !isStart;repaint();}//坐标@Overridepublic void mouseMoved(MouseEvent e) {super.mouseMoved(e);Mouse_X = e.getX();Mouse_Y = e.getY();}}@Overridepublic void keyReleased(KeyEvent e) {}@Overridepublic void keyTyped(KeyEvent e) {}
}
package ball.last;import javax.swing.*;public class Main {public static void main(String[] args) {//在主类中新建一个框架并设置属性JFrame frame = new JFrame("打砖块");frame.setResizable(false);//不可缩放frame.setBounds(300, 200, 700, 700);//窗口大小frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//用于关闭frame.add(new BallPanel());//新建BallFrame类并加入框架frame.setVisible(true);//设置可见性}
}
package ball.last;public class zhuan {private boolean ok;private boolean color;private int x;private int y;public zhuan() {}public zhuan(boolean ok, boolean color, int x, int y) {this.ok = ok;this.color = color;this.x = x;this.y = y;}public boolean isOk() {return ok;}public void setOk(boolean ok) {this.ok = ok;}public boolean isColor() {return color;}public void setColor(boolean color) {this.color = color;}public int getX() {return x;}public void setX(int x) {this.x = x;}public int getY() {return y;}public void setY(int y) {this.y = y;}
}
这篇关于JAVA课程设计 打砖块游戏(颜色匹配版) JAVA语言的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!