我是靠谱客的博主 欢喜高山,最近开发中收集的这篇文章主要介绍java编写小游戏大球吃小球游戏简介项目结构Direction类Ball类MyBall类OtherBall类Select_Frame类AgainPanel类Frame类,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

java编写小游戏大球吃小球

  • 游戏简介
  • 项目结构
  • Direction类
  • Ball类
  • MyBall类
  • OtherBall类
  • Select_Frame类
  • AgainPanel类
  • Frame类

github源码:https://github.com/fluoritess/java-/tree/master

游戏简介

大二的时候自己写的一个java小游戏,在游戏中,玩家需要控制上下左右来操作大球吃点比自己小的球,让自己变得更大吃掉更多的小球。
在这里插入图片描述

项目结构

在这里插入图片描述

Direction类

枚举类用来标注控制小球的方向

package Ball;
public enum Direction {
	LEFT,
	UP,
	RIGHT,
	DOWN,
}

Ball类

球类,基本属性包括横纵坐标、直径、颜色。

package Ball;

import java.awt.Color;
import java.awt.Graphics;

public class Ball {
	private int diameter;
	private int x;
	private int y;
	private Color color;

	private int direction=(int) (Math.random()*8+1);
	
	public Ball(int diameter, int x, int y, Color color) {
		super();
		this.diameter = diameter;
		this.x = x;
		this.y = y;
		this.color = color;
	}

	public int getDiameter() {
		return diameter;
	}

	public void setDiameter(int diameter) {
		this.diameter = diameter;
	}

	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;
	}
	
	public void draw(Graphics g)
	{
		g.setColor(color);
		g.fillOval(x, y, diameter, diameter);  	
	}
	
	public void move(int m)
	{
		switch (direction) {
		case 1:x-=m;break;//左
		case 2:x+=m;break;//右
		case 3:y-=m;break;//上
		case 4:y+=m;break;//下
		case 5:x-=m;y-=m;break;//左上
		case 6:x-=m;y+=m;break;//左下
		case 7:x+=m;y-=m;break;//右上
		case 8:x+=m;y-=m;break;//右下
		default:
			break;
		}		
	}
}

MyBall类

玩家操作的小球

package Ball;

import java.awt.Color;

import java.awt.Graphics;

public class MyBall {
	private int diameter;
	private int x;
	private int y;
	private Color color;
	private Direction dir;
	public int getDiameter() {
		return diameter;
	}
	public int getX() {
		return x;
	}
	public int getY() {
		return y;
	}
	public Color getColor() {
		return color;
	}
	public Direction getDir() {
		return dir;
	}
	
	public void setDiameter(int diameter) {
		this.diameter = diameter;
	}
	public MyBall(int diameter, int x, int y, Color color) {
		super();
		this.diameter = diameter;
		this.x = x;
		this.y = y;
		this.color = color;
		dir=null;
	}	
	public void draw(Graphics g)
	{
		g.setColor(color);
		g.fillOval(x, y, diameter, diameter);  	
		g.setColor(Color.black);
		g.drawOval(x, y, diameter, diameter);
	}
	public void changDirection(Direction newDir)
	{		
			dir=newDir;		
	}
	public void move(int k)
	{
		if(dir!=null){
		switch(dir)
		{
		case LEFT:x-=k;break;
		case UP:y-=k;break;
		case RIGHT:x+=k;break;
		case DOWN:y+=k;break;
		}}
	}
	//吃方法
	public void eat(Ball ball,int i)
	{
		int x1=ball.getX();
		int y1=ball.getY();
		int d1=ball.getDiameter();
		if(this.diameter<600)
		{
			if(d1<this.diameter)//被吃小球直径小于玩家小球直径,玩家吃掉小球
			{
				int d=(int)Math.sqrt((x1+d1/2-this.x-this.diameter/2)*(x1+d1/2-this.x-this.diameter/2)+(y1+d1/2-this.y-this.diameter/2)*(y1+d1/2-this.y-this.diameter/2))-1;//玩家小球与该小球圆心之间的距离
				if(d<(int)(d1+this.diameter)/2)
				{				
					this.diameter+=(int)d1/i;
					ball.setX(-100);
					ball.setY(-100);
					ball.setDiameter(0);
					ball=null;
				}
				ball=null;
		}
			else if(d1>this.diameter)//被吃小球直径大于玩家小球直径,玩家死亡
			{
				int d=(int)Math.sqrt((x1+d1/2-this.x-this.diameter/2)*(x1+d1/2-this.x-this.diameter/2)+(y1+d1/2-this.y-this.diameter/2)*(y1+d1/2-this.y-this.diameter/2))-1;//����Բ��֮��ľ�����߼�һ
				if(d<(int)(d1+this.diameter)/2)
				{				
					this.x=-100;
					this.y=-100;
					this.diameter=0;
				}
		
			}
		}
	}
}

OtherBall类

用来控制除了自己的小球之外的所有的其他小球。其他小球用List保存,统一操作。

package Ball;

import java.awt.Color;

import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class OtherBall {
	private List<Ball> balls=new ArrayList<>();
	public OtherBall(){	
		Random rand = new Random();
		//构造函数初始化产生二十个球
		while(balls.size()<20){
			int x1 = rand.nextInt(201) + -100;
			int x2 = rand.nextInt(201) + 800;
			int y1 = rand.nextInt(201) +-100;
			int y2 = rand.nextInt(201) + 800;
			Ball ball=null;
			int count=(int) (Math.random()*9+1);
			switch (count) {
			case 1:
				ball=new Ball(10,x1, y1, Color.green);balls.add(ball);
				break;
			case 2:
				ball=new Ball(20,x1, y2, Color.black);balls.add(ball);
				break;
			case 3:
				ball=new Ball(30,x2, y1, Color.blue);balls.add(ball);
				break;
			case 4:
				ball=new Ball(40,x2, y2, Color.CYAN);balls.add(ball);
				break;
			case 5:
				ball=new Ball(50,x1, y2, Color.yellow);balls.add(ball);
				break;
			case 6:
				ball=new Ball(60,x1, y1, Color.red);balls.add(ball);
				break;
			case 7:
				ball=new Ball(70,x2, y2, Color.gray);balls.add(ball);
				break;
			case 8:
				ball=new Ball(80,x2, y1, Color.PINK);balls.add(ball);
				break;
			case 9:
				ball=new Ball(14,x2, y2, Color.gray);balls.add(ball);
				break;	
			default:
				break;
			}
		}
	}
	//判断是否有球出界,如果有删除该球添加新球
	public void isAlive()
	{
		for(Ball ball:balls)
		{
			if(ball.getX()<-200||ball.getX()>1000||ball.getY()<-200||ball.getY()>1000){
				ball=null;
			}
		}	
		newball();
	}
	public void move(int m)
	{
		for(Ball ball:balls)
		{
			ball.move(m);
		}
	}
	public void iseat(MyBall mb,int i)//判断是否被吃
	{
		for(Ball ball:balls)
		{
			mb.eat(ball,i);
		}
	}
	public void draw(Graphics g)
	{
	/*
	用这里的代码会出现
	Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException
	for(Ball ball:balls)
		{
			ball.draw(g);
		}
		*/
		for(int i=0;i<balls.size();i++){
			Ball ball=balls.get(i);
			ball.draw(g);
		}
	}

	//产生新球
	public void newball()
	{
			Random rand = new Random();
			int x1 = rand.nextInt(101) + -100;
			int x2 = rand.nextInt(101) + 1000;
			int y1 = rand.nextInt(101) +-100;
			int y2 = rand.nextInt(101) + 1000;
			int x3 = rand.nextInt(1001) + 0;			
			int y3 = rand.nextInt(1001) + 0;
			Ball ball=null;
			int count=(int) (Math.random()*18+1);
			int count2=(int) (Math.random()*100+1);
			if(count2>70)
			{
			switch (count) {
			case 1:
				ball=new Ball(10,x1, y1, Color.green);balls.add(ball);
				break;
			case 2:
				ball=new Ball(20,x1, y2, Color.black);balls.add(ball);
				break;
			case 3:
				ball=new Ball(30,x2, y1, Color.blue);balls.add(ball);
				break;
			case 4:
				ball=new Ball(40,x2, y2, Color.CYAN);balls.add(ball);
				break;
			case 5:
				ball=new Ball(50,x1, y2, Color.yellow);balls.add(ball);
				break;
			case 6:
				ball=new Ball(60,x1, y1, Color.red);balls.add(ball);
				break;
			case 7:
				ball=new Ball(70,x2, y2, Color.gray);balls.add(ball);
				break;
			case 8:
				ball=new Ball(80,x2, y1, Color.PINK);balls.add(ball);
				break;
			case 9:
				ball=new Ball(100,x1, y1, Color.magenta);balls.add(ball);
				break;	
			case 10:
				ball=new Ball(14,x2, y2, Color.gray);balls.add(ball);
				break;
			case 11:
				ball=new Ball(70,x1, y3, Color.gray);balls.add(ball);
				break;
			case 12:
				ball=new Ball(80,x3, y1, Color.PINK);balls.add(ball);
				break;
			case 13:
				ball=new Ball(100,x3, y2, Color.magenta);balls.add(ball);
				break;	
			case 14:
				ball=new Ball(14,x2, y3, Color.gray);balls.add(ball);
				break;	
			case 15:
				ball=new Ball(10,x1, y3, Color.green);balls.add(ball);
				break;
			case 16:
				ball=new Ball(20,x3, y1, Color.black);balls.add(ball);
				break;
			case 17:
				ball=new Ball(30,x2, y3, Color.blue);balls.add(ball);
				break;
			case 18:
				ball=new Ball(40,x3, y2, Color.CYAN);balls.add(ball);
				break;	
			default:
				break;
			}
		}
	}
}

Select_Frame类

选择难度
在这里插入图片描述

package Ball;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Select_Frame extends JFrame implements ActionListener{
	JButton jb1=null;
	JButton jb2=null;
	JButton jb3=null;
	JButton jb4=null;
	JButton jb5=null;
	Select_Frame()
	{
		jb1=new JButton("简单");
		jb2=new JButton("普通");
		jb3=new JButton("困难");
		jb4=new JButton("极难");
		jb5=new JButton("炼狱");
		this.add(jb1);
		this.add(jb2);
		this.add(jb3);
		this.add(jb4);
		this.add(jb5);
		jb1.addActionListener(this);
		jb2.addActionListener(this);
		jb3.addActionListener(this);
		jb4.addActionListener(this);
		jb5.addActionListener(this);
		jb1.setActionCommand("jb1");
		jb2.setActionCommand("jb2");
		jb3.setActionCommand("jb3");
		jb4.setActionCommand("jb4");
		jb5.setActionCommand("jb5");
		this.setTitle("选择难度");
		this.setVisible(true);
		this.setLocation(700,300);
		this.setSize(400,400);
		this.setLayout(new GridLayout(5, 1));
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	public static void main(String[] args)
	{
		Select_Frame sf=new Select_Frame();
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getActionCommand().equals("jb1")){			
			this.setVisible(false);
			Frame frame=new Frame(2,2,1,10);
		}
		else if(e.getActionCommand().equals("jb2")){			
			this.setVisible(false);
			Frame frame=new Frame(4,2,1,10);
		}
		else if(e.getActionCommand().equals("jb3")){			
			this.setVisible(false);
			Frame frame=new Frame(6,2,1,10);
		}
		else if(e.getActionCommand().equals("jb4")){			
			this.setVisible(false);
			Frame frame=new Frame(6,2,1,8);
		}
		else if(e.getActionCommand().equals("jb5")){			
			this.setVisible(false);
			Frame frame=new Frame(6,2,1,6);
		}
	}
}

AgainPanel类

重新开始

package Ball;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import org.omg.CORBA.PUBLIC_MEMBER;

public class AgainPanel extends JFrame implements ActionListener{
	JButton jb1=null;
	JButton jb2=null;
	JButton jb3=null;
	public AgainPanel()
	{
		jb1=new JButton("重新开始");
		jb2=new JButton("结束游戏");
		jb3=new JButton("查看排名");
		this.add(jb1);
		this.add(jb2);
		this.add(jb3);
		jb1.addActionListener(this);
		jb1.setActionCommand("jb1");
		jb2.addActionListener(this);
		jb2.setActionCommand("jb2");
		jb3.addActionListener(this);
		jb3.setActionCommand("jb3");
		this.setVisible(true);
		this.setLayout(new GridLayout(3,1));
		this.setLocation(800, 300);
		this.setSize(300, 400);
		
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getActionCommand().equals("jb1"))
		{
			Select_Frame sFrame=new Select_Frame();
		}
		else if(e.getActionCommand().equals("jb2"))
		{
			System.exit(0);
		}
	}
}

Frame类

游戏运行的核心类

package Ball;

import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Color;


import java.awt.Graphics;
import java.awt.List;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.lang.invoke.SwitchPoint;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.Random;

import javax.print.attribute.standard.MediaSize.Other;
import javax.swing.*;



public class Frame extends JFrame{
		private BufferedImage image=new BufferedImage(1000, 1000, 1);
		private OtherBall otherBall=new OtherBall();
		private MyBall mb=new MyBall(15, 500, 500, Color.white);
		private boolean isAnimate=true;
		JPanel jp1=null;
		JButton jb1=null;
		JLabel jlb1=null;
		String string="当前直径:";
		public Frame(int i,int k,int m,int j)//i控制增长速度,k我的控制速度,m控制敌方速度,j控制线程刷新
		{
			
			jp1=new JPanel();
			jb1=new JButton("重新开始");
			jp1.add(jb1);
			jlb1=new JLabel(string);
			jlb1.setBounds(10, 10, 80, 20);
			
			this.add(jlb1);
			this.setLayout(null);
			this.setVisible(true);
			this.setTitle("大球吃小球");
			this.setSize(1000, 1000);
			this.setLocation(400, 0);
			this.setDefaultCloseOperation(EXIT_ON_CLOSE);
			this.setBackground(Color.white);
			this.addKeyListener(new suspend());
			
			addKeyListener(new KeyAdapter() {
				@Override
				public void keyPressed(KeyEvent e)
				{
						Direction newDir=mb.getDir();
						switch(e.getKeyCode())
						{
						case KeyEvent.VK_A:
							newDir=Direction.LEFT;break;
						case KeyEvent.VK_W:
							newDir=Direction.UP;break;
						case KeyEvent.VK_D:
							newDir=Direction.RIGHT;break;
						case KeyEvent.VK_S:
							newDir=Direction.DOWN;break;
						
						}
						if(newDir!=mb.getDir())
						{
							mb.changDirection(newDir);
						}
				}/*
				public void keyReleased(KeyEvent e)
				{
					Direction newDir=mb.getDir();
					switch(e.getKeyCode())
					{
					case KeyEvent.VK_A:
						newDir=null;break;
					case KeyEvent.VK_W:
						newDir=null;break;
					case KeyEvent.VK_D:
						newDir=null;break;
					case KeyEvent.VK_S:
						newDir=null;break;
					
					}
					if(newDir!=mb.getDir())
					{
						mb.changDirection(newDir);
					}
				}*/
			});
			
			
	
			
			new Thread(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					
					while(true)
					{			
						if(isAnimate){
						otherBall.iseat(mb,i);
						otherBall.isAlive();
						string="当前直径:"+mb.getDiameter();
						jlb1.setText(string);
						if(mb.getDiameter()!=0&&mb.getX()>-100&&mb.getX()<1100&&mb.getY()>-100&&mb.getY()<1100){
						otherBall.move(m);
						mb.move(k);
						repaint();
						}
						else{
							try {
								Thread.sleep(3000);
								setVisible(false);
								
								
							} catch (InterruptedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							System.out.println("游戏失败");
							AgainPanel againPanel=new AgainPanel();
							break;
						}
						}
						try {
							Thread.sleep(j);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
			}).start();
			
			
		}
		
		public void paint(Graphics g)
		{
			Graphics g2=image.getGraphics();
			super.paint(g2);			
			otherBall.draw(g2);
			mb.draw(g2);
			g.drawImage(image, 0, 0, null);
		}
		private class suspend extends KeyAdapter{
			public void keyReleased(KeyEvent e){
				if((e.getKeyChar())==' '){
					isAnimate=!isAnimate;
				}
		} 
		}
}

最后

以上就是欢喜高山为你收集整理的java编写小游戏大球吃小球游戏简介项目结构Direction类Ball类MyBall类OtherBall类Select_Frame类AgainPanel类Frame类的全部内容,希望文章能够帮你解决java编写小游戏大球吃小球游戏简介项目结构Direction类Ball类MyBall类OtherBall类Select_Frame类AgainPanel类Frame类所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(37)

评论列表共有 0 条评论

立即
投稿
返回
顶部