java课程结课后老师要求做一个课程设计,但是本人学艺不精,高级点的不会做,然后又想做些好玩的,于是就产生了做一个球球大作战小游戏的想法,过程中查了很多资料,并且程序功能还不是很完备,但是勉勉强强还是能够入眼并且可以使用的,现在把代码分享给大家,有需要的可以自行摘取,有问题的可以在评论区留言哦,如果有大佬帮我再优化下就更好了【手动狗头】
废话就不多说了,接下来给大家整活了~
可通过键盘上的↑↓←→控制玩家球
有背景音乐和胜利以及失败的音效
玩法和球球大作战是一样的,核心思想就是大球吃小球
运行效果
源代码如下(全部代码)
Ball.java
package ballball;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
public class Ball {
/* 小球的基本属性 */
int x, y;//定义x, y坐标
int d;//直径
Color ballColor;//小球的颜色
int speed;//小球的运动速度
int position;//小球的运动方向
/*小球的运动方向*/
public static final int LEFT_UP = 0;//左上
public static final int RIGHT_UP = 1;//右上
public static final int LEFT_DOWN = 2;//左下
public static final int RIGHT_DOWN = 3;//右下
/*构造方法*/
public Ball(int x, int y, int position, int d, int speed, Color ballColor){
this.x = x;
this.y = y;
this.position = position;
this.d = d;
this.speed = speed;
this.ballColor = ballColor;
}
//构造玩家球
public Ball(int x, int y, int d, int speed, Color ballColor){
this.x = x;
this.y = y;
this.d = d;
this.speed = speed;
this.ballColor = ballColor;
}
//画小球
public void drawBall(Graphics g){
g.setColor(ballColor);
g.fillOval(x, y, d, d);
}
public void drawBall2(Graphics g){
g.setColor(ballColor);
g.fillOval(x, y, d, d);
//球加文字
g.setColor(Color.white);
//设置字体大小
Font font = new Font(Font.DIALOG, Font.BOLD, 15);
g.setFont(font);
g.drawString("✧٩(ˊωˋ*)و✧", x+d/8, y+d/2);
}
//小球的运动方向,遇到边界回弹
public void ballMove(){
switch (this.position) {
case LEFT_UP:
x -= speed;
y -= speed;
if (x <= 0) {
this.position = RIGHT_UP;
}else if (y <= 0) {
this.position = LEFT_DOWN;
}
break;
case RIGHT_UP:
x += speed;
y -= speed;
if (x >= BallMain.SCREEN_WIDTH - d) {
this.position = LEFT_UP;
}else if (y <= 0) {
this.position = RIGHT_DOWN;
}
break;
case LEFT_DOWN:
x -= speed;
y += speed;
if (x <= 0) {
this.position = RIGHT_DOWN;
}else if (y >= BallMain.SCREEN_HEIGHT - d) {
this.position = LEFT_UP;
}
break;
case RIGHT_DOWN:
x += speed;
y += speed;
if (x >= BallMain.SCREEN_WIDTH - d) {
this.position = LEFT_DOWN;
}else if (y >= BallMain.SCREEN_HEIGHT - d) {
this.position = RIGHT_UP;
}
break;
}
}
}
BallAndBall.java
package ballball;
public class BallAndBall {
//检查是否碰撞上
public boolean isBallCrach(Ball b1, Ball b2){
boolean flag = false;
int x1 = b1.x + b1.d/2;
int y1 = b1.y + b1.d/2;
int x2 = b2.x + b2.d/2;
int y2 = b2.y + b2.d/2;
//计算圆心距
double e = Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
if (e <= b1.d/2 + b2.d/2) {
return true;
}
return false;
}
}
BallJPanel.java
package ballball;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import com.sun.javafx.embed.swing.Disposer;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
/*画小球*/
public class BallJPanel extends JPanel implements KeyListener{
//存储小球的集合
List<Ball> foodList = new ArrayList<Ball>();
List<Ball> ballList = new ArrayList<Ball>();
int x1 = 450;
int y1 = 450;
int d1 = 20;
private Color white;
//玩家球
Ball game = new Ball(x1, y1, d1, 50, white);
//小球的数量
private int foodNumber = 100;
private int ballnumber =4;
public BallJPanel(){
addBall();
startBalls();
}
//产生小球的方法
public void addBall(){
for (int i = 0; i < foodNumber; i++) {
//随机产生100个食物
int x = (int)(Math.random()*BallMain.SCREEN_WIDTH);
int y = (int)(Math.random()*BallMain.SCREEN_HEIGHT);
int position = (int)(Math.random()*4);
int d = 5;
int speed=0;
//颜色 三原色 R G B
int red = (int)(Math.random()*255 + 1);
int green = (int)(Math.random()*255 + 1);
int blue = (int)(Math.random()*255 + 1);
Color ballColor = new Color(red, green, blue);
Ball a = new Ball(x, y, position, d,speed, ballColor);
//将食物添加到集合中
foodList.add(a);
}
for (int i = 0; i < ballnumber; i++) {
//随机产生4个小球
int x = (int)(Math.random()*BallMain.SCREEN_WIDTH);
int y = (int)(Math.random()*BallMain.SCREEN_HEIGHT);
int position = (int)(Math.random()*4);
int d = 8;
int speed =1;
//颜色 三原色 R G B
int red = (int)(Math.random()*255 + 1);
int green = (int)(Math.random()*255 + 1);
int blue = (int)(Math.random()*255 + 1);
Color ballColor = new Color(red, green, blue);
Ball b = new Ball(x, y, position, d, speed, ballColor);
//将小球添加到集合中
ballList.add(b);
}
}
public void paint(Graphics g){
super.paint(g);
BufferedImage img =null;
//添加图片
try {
img = ImageIO.read(new File("D:\\workspace\\game\\src\\ballball\\素材\\background.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
g.drawImage(img, 0, 0, 1360, 760, this);
this.setBackground(Color.white);
for (int i = 0; i < foodList.size(); i++) {
Ball ball = foodList.get(i);
ball.drawBall(g);
}
for (int i = 0; i < ballList.size(); i++) {
Ball ball = ballList.get(i);
ball.drawBall(g);
}
//玩家
game.drawBall2(g);
}
public void startBalls(){
//启动线程-----匿名内部类
new Thread(){
public void run() {
//遍历食物集合
while(true){
for (int i = 0; i < ballList.size(); i++) {
//取出小球
Ball b=ballList.get(i);
//让每一个小球进行移动
b.ballMove();
}
Ball b1;
Ball b2;
for (int i = 0; i < ballList.size(); i++) {
//取小球
b1 = ballList.get(i);
for (int j = 0; j < foodList.size(); j++) {
//取食物
b2 = foodList.get(j);
//大球吃小球
BallAndBall bad = new BallAndBall();
if(bad.isBallCrach(b1, b2)){
if (b1.d >= b2.d) {
b1.d += b2.d/5;
foodList.remove(b2);
continue;
}
}
if(bad.isBallCrach(b2, game)){
if(b2.d < game.d){
game.d += b2.d/5;
foodList.remove(b2);
continue;
}
}
if (bad.isBallCrach(b1, game)) {
if (b1.d > game.d) {
try {
FileInputStream fail=new FileInputStream("D:\\workspace\\game\\src\\ballball\\素材\\失败.wav");
AudioStream a=new AudioStream(fail);
AudioPlayer.player.start(a);
JOptionPane.showMessageDialog(null, "GAME OVER !","游戏失败",1);
System.exit(0);
} catch (Exception e) {
e.printStackTrace();
}
}else if(b1.d < game.d)
game.d+=b1.d/10;
ballList.remove(b1);
if (ballList.isEmpty()) {
try {
FileInputStream victory=new FileInputStream("D:\\workspace\\game\\src\\ballball\\素材\\胜利.wav");
AudioStream b=new AudioStream(victory);
AudioPlayer.player.start(b);
JOptionPane.showMessageDialog(null, "GAME VICTORY !","游戏胜利",1);
System.exit(0);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
repaint();//重绘
try {
Thread.sleep(5);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
}.start();
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if (e.getKeyCode() == KeyEvent.VK_UP) {
game.y -= 20;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
game.y += 20;
}
if (e.getKeyCode() == KeyEvent.VK_LEFT ) {
game.x -= 15;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
game.x += 15;
}
if (e.getKeyCode() == KeyEvent.VK_1) {
game.x += 10;
game.y -= 10;
}
if (e.getKeyCode() == KeyEvent.VK_2) {
game.x -= 10;
game.y -= 10;
}
if (e.getKeyCode() == KeyEvent.VK_3) {
game.x -= 10;
game.y += 10;
}
if (e.getKeyCode() == KeyEvent.VK_4) {
game.x += 10;
game.y += 10;
}
repaint();
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
}
BallMain.java
package ballball;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.JFrame;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
/*创建窗体*/
public class BallMain extends JFrame{
//窗体的宽高
public static final int SCREEN_WIDTH = 900;
public static final int SCREEN_HEIGHT = 600;
//全屏
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int width = (int)d.getWidth();
int height = (int)d.getHeight();
public BallMain(){
this.setTitle("球球大作战");
//设置位置
this.setBounds(0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
//添加小球到窗体
BallJPanel bj = new BallJPanel();
getContentPane().add(bj);
//添加键盘的监听事件
this.addKeyListener(bj);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
//主方法
public static void main(String[] args) {
BallMain b = new BallMain();
//添加音乐
try {
FileInputStream f =new FileInputStream("D:\\workspace\\game\\src\\ballball\\素材\\卡农.wav");
AudioStream as = new AudioStream(f);
AudioPlayer.player.start(as);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
background.png
背景音乐和音效戳这里
提取码:6666