目錄
- 游戲規則
- 主要實作方法
- 游戲流程展示
- 1. 開始游戲頁面
- 2. 加載中頁面
- 3. 選擇地圖頁面
- 4. 自定義昵稱頁面
- 5. 運行頁面
- 死亡頁面
- 發展方向
- (前面都不感興趣,快進到)原碼
- 最后
首先灰常開森在2020年下半年這學期識訓了很多知識,自己的意志也得到了鍛煉,尤其是在verilog學習中
Java課大作業訊息出來的時候我腦子一片空白,連Hello World都不會輸出,讓用Java寫個小游戲,七八個星期以來老師上課給我們講的內容我一點也沒印象,莫名有種給你一盒擦炮去炸碉堡的感覺哦,
ddl最后那三天,著實難受>_<…,
往事不可追,寒假決定把自己一點點磨出來的東西傳上來,供大家參考和指正,謝謝,
游戲規則
球球大作戰時一款多玩家實時在線對抗策略類游戲,在這款游戲中,玩家操控一個具有一定初始體積的小球在地圖中移動,為了使自己的小球的體積增大,玩家可以規劃自己的路線并吃掉沿途上的“微粒”,“微粒”具有一個很小的體積,吃掉微粒后,小球可以增加相應的體積,隨著體積不斷變大,如果當玩家控制小球的體積在直徑上明顯大于另一名玩家的小球時,如果玩家成功地使自己的小球將對方小球的超過二分之一體積覆寫,那么判定成功吃掉對方的小球,玩家控制的小球體積隨之增加,對方的小球被吃掉,需要恢復初始體積重新開始游戲,
主要實作方法
在撰寫專案前,首先對需要實作的功能進行分析,制作了如下的分析圖,

專案中較為關鍵的是Ball類,
public class Ball {
private double x; //繪制橫坐標
private double y; //繪制縱坐標
private double d; //直徑
private double real_x; //中心橫坐標
private double real_y; //中心縱坐標
private double speed; //速度
private double degree; //角度
private double m; //質量
private String name; //昵稱
private boolean alive; //存活
private Color owncolor; //顏色
private BufferedImage flag; //國家國旗
public Ball(double x, double y, double d) {
this.setX(x);
this.setY(y);
this.setD(d);
this.setOwncolor(randomcolor());
this.real_x = x + d / 2;
this.real_y = y + d / 2;
this.alive = true;
}
public Ball(double x, double y, double speed, double degree, double m) {
this.setX(x);
this.setY(y);
this.speed = speed;
this.degree = degree;
this.m = m;
FreshD();
this.alive = true;
}
public Ball(double x, double y, double speed, double degree, double m, String name) {
super();
this.x = x;
this.y = y;
this.speed = speed;
this.degree = degree;
this.m = m;
this.name = name;
this.setOwncolor(randomcolor());
FreshD();
this.alive = true;
}
public BufferedImage getFlag() {
return flag;
}
public void setFlag(BufferedImage flag) {
this.flag = flag;
}
public boolean isAlive() {
return alive;
}
public void setAlive(boolean alive) {
this.alive = alive;
}
public double getSpeed() {
return speed;
}
public void setSpeed(double speed) {
this.speed = speed;
}
public double getDegree() {
return degree;
}
public void setDegree(double degree) {
this.degree = degree;
}
public double getM() {
return m;
}
public void setM(double m) {
this.m = m;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public double getD() {
return d;
}
public void setD(double d) {
this.d = d;
}
public double getReal_x() {
return real_x;
}
public void setReal_x(double real_x) {
this.real_x = real_x;
}
public double getReal_y() {
return real_y;
}
public void setReal_y(double real_y) {
this.real_y = real_y;
}
public Color getOwncolor() {
return owncolor;
}
public void setOwncolor(Color owncolor) {
this.owncolor = owncolor;
}
public void draw(Graphics g) {
Color c = g.getColor();
g.setColor(Color.cyan);
g.fillOval((int) x, (int) y, (int) d, (int) d);
g.setColor(c);
}
public void draw(Graphics g, int windowx, int windowy) { //繪制自身
Color c = g.getColor();
g.setColor(Color.cyan);
g.fillOval((int) (x - windowx), (int) (y - windowy), (int) d, (int) d);
g.setColor(c);
}
public void move() { //移動小球
if (x > ballfight.Width || x < 0)
degree = Math.PI - degree;
if (y > ballfight.Height || y < 0)
degree = -degree;
while (degree < 0)
degree += 2 * Math.PI;
while (degree > 2 * Math.PI)
degree -= 2 * Math.PI;
if (speed > 0) {
x += speed * Math.cos(degree);
y += speed * Math.sin(degree);
}
}
public void eat(Ball b) { //吃掉別的Ball類
m += b.m;
FreshD();
}
public void FreshD() { //重繪小球中心位置
d = 30 + Math.sqrt(m) * 4;
real_x = x + d / 2;
real_y = y + d / 2;
}
public Color randomcolor() {
Random rand = new Random();
float r = rand.nextFloat();
float s = rand.nextFloat();
float t = rand.nextFloat();
return (new Color(r, s, t));
}
}
Player(玩家)類繼承Ball類,但是需要重寫draw()方法、move()方法,并添加spore()方法和my_spore_move()方法,具體如下,
public void move(double mx, double my) { //移動的邊界處理和普通Ball不同
double dis = Math.sqrt((mx - screenx) * (mx - screenx) + (my - screeny) * (my - screeny));
double Degree;
Degree = Math.acos((mx - screenx) / dis);
if (my - screeny < 0) {
Degree = -Degree;
}
setDegree(Degree);
if (dis > 2) {
if (getSpeed() > 0) {
if ((getX() + getSpeed() * Math.cos(getDegree()) < 0
|| getX() + getSpeed() * Math.cos(getDegree()) > ballfight.Width)
&& (getY() + getSpeed() * Math.sin(getDegree()) < 0
|| getY() + getSpeed() * Math.sin(getDegree()) > ballfight.Height)) {
} else if (getX() + getSpeed() * Math.cos(getDegree()) < 0
|| getX() + getSpeed() * Math.cos(getDegree()) > ballfight.Width) {
setY(getY() + getSpeed() * Math.sin(getDegree()));
FreshD();
} else if (getY() + getSpeed() * Math.sin(getDegree()) < 0
|| getY() + getSpeed() * Math.sin(getDegree()) > ballfight.Height) {
setX(getX() + getSpeed() * Math.cos(getDegree()));
FreshD();
} else {
setX(getX() + getSpeed() * Math.cos(getDegree()));
setY(getY() + getSpeed() * Math.sin(getDegree()));
FreshD();
}
}
}
}
public double getsize(double weight) {
return 10 + Math.sqrt(weight) * 4;
}
public void spore(double speed) { //吐出孢子
System.out.println(getM());
if (resttime < ballfight.timeperspore / ballfight.breaktime)
resttime++;
else {
resttime = 0;
if (getM() > 2 * ballfight.sporeweight) {
double rx, ry;
Spore s = new Spore(
getReal_x() + getD() * 0.5 * Math.cos(getDegree()) - 0.5 * getsize(ballfight.sporeweight),
getReal_y() + getD() * 0.5 * Math.sin(getDegree()) - 0.5 * getsize(ballfight.sporeweight),
speed, this.getDegree(), ballfight.sporeweight);
s.FreshD();
spores.add(s);
rx = getReal_x();
ry = getReal_y();
double weight = getM();
weight -= s.getM();
setM(weight);
FreshD();
setX(rx - getD() * 0.5);
setY(ry - getD() * 0.5);
FreshD();
}
}
}
public void my_spore_move() { //所有已吐出孢子運動
for (int i = 0; i < spores.size(); i++) {
Spore s = spores.get(i);
if (s.isAlive()) {
s.move();
s.FreshD();
}
}
}
完成基本的類的撰寫,下面是頁面以及執行緒的撰寫,
主類ballfight繼承JPanel,在主類的實體化方法中初始化JFrame作為游戲頁面的框架,
public class ballfight extends JPanel
public ballfight(String name) {
LoadImgs();
setBackground(Color.black);
InitialImages();
frame = new JFrame(name);
frame.setVisible(true);
frame.setResizable(false);
frame.add(this);
frame.setBackground(Color.black);
frame.setBounds(this.X, this.Y, ballfight.windowWidth, ballfight.windowHeight);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
接下來為frame添加KeyListener鍵盤監聽,
keylistener = new KeyListener() {
/**************/
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
/*********/
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
/*********/
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
/*********/
}
};
frame.addKeyListener(keylistener);
為ballfight類中的實體化物件添加了MouseMotionListener和MouseListener監聽,用來獲取用戶在面板上的滑鼠移動和滑鼠點擊,
game.mouse_adapter = new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
/*********/
}
};
game.addMouseListener(mouse_adapter);
game.addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
mx = e.getX();
my = e.getY();
}
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
}
});
在游戲進行中需要在地圖中隨機生成微粒,通過Runnable介面定義一個ParticleMaker類,將這個執行緒加入到主類中即可實作隨機生成的任務,
public class ParticleMaker implements Runnable {
@Override
public void run() {
// TODO Auto-generated method stub
while (player.isAlive()) {
synchronized (this) {
for (int i = 0; i < particlepertime; i++) {
MakeParticle();
}
}
try {
Thread.sleep(particletime);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
}
為了增強游戲的趣味性,添加了和用戶體重相關的排行榜功能,通過定義一個ranking類,實作對場上所有存活玩家和敵人的體重排序并生成一個實時更新的排行榜,供玩家參考自己的體重在當前場上的排名情況,
public class ranking {
Ball[] M = new Ball[10000];
Color Gold = new Color(255, 215, 0);
Color Silver = new Color(192, 192, 192);
Color Copper = new Color(244, 164, 96);
Color MediumOrchid = new Color(186, 85, 211);
Color Lavender = new Color(230, 230, 250);
Color CornflowerBlue = new Color(100, 149, 237);
public Ball[] getM() {
return M;
}
public void setM(Ball[] m) {
M = m;
}
public ranking() {
int j = 0;
for (int i = 0; i < enemy.size(); i++) {
Enemy e = enemy.get(i);
if (e != null && e.isAlive()) {
M[j] = e;
j++;
}
}
M[j] = player;
for (int i = 0; i < ranknum; i++) {
int x = i;
for (int ii = i + 1; ii <= j; ii++) {
if (M[ii].getM() > M[x].getM()) {
x = ii;
}
}
Ball y = M[i];
M[i] = M[x];
M[x] = y;
}
}
public void UpdateRankingList() {
int j = 0;
for (int i = 0; i < enemy.size(); i++) {
Enemy e = enemy.get(i);
if (e != null && e.isAlive()) {
M[j] = e;
j++;
}
}
M[j] = player;
for (int i = 0; i < ranknum; i++) {
int x = i;
for (int ii = i + 1; ii <= j; ii++) {
if (M[ii].getM() > M[x].getM()) {
x = ii;
}
}
Ball y = M[i];
M[i] = M[x];
M[x] = y;
}
}
public void DrawRankingList(Graphics g) {
UpdateRankingList();
g.setColor(MediumOrchid);
g.setFont(new Font("Comic Sans MS", Font.LAYOUT_LEFT_TO_RIGHT, 21));
g.drawString("---Ranking---", windowWidth - 200, 20);
g.setFont(new Font("楷體", Font.LAYOUT_LEFT_TO_RIGHT, 18));
g.drawString("排名", windowWidth - 200, 45);
g.drawString("昵稱", windowWidth - 140, 45);
g.drawString("地區", windowWidth - 90, 45);
g.setColor(Gold);
g.setFont(new Font("黑體", Font.LAYOUT_LEFT_TO_RIGHT, 20));
g.drawString(1 + ": ", windowWidth - 200, 70 + fontlineheight * 0);
g.setColor(Color.white);
g.drawString(M[0].getName(), windowWidth - 160, 70 + fontlineheight * 0);
g.drawImage(M[0].getFlag(), windowWidth - 85, 55 + fontlineheight * 0, 27, 18, null);
g.setColor(Silver);
g.setFont(new Font("黑體", Font.LAYOUT_LEFT_TO_RIGHT, 20));
g.drawString(2 + ": ", windowWidth - 200, 70 + fontlineheight * 1);
g.setColor(Color.white);
g.drawString(M[1].getName(), windowWidth - 160, 70 + fontlineheight * 1);
g.drawImage(M[1].getFlag(), windowWidth - 85, 55 + fontlineheight * 1, 27, 18, null);
g.setColor(Copper);
g.setFont(new Font("黑體", Font.LAYOUT_LEFT_TO_RIGHT, 20));
g.drawString(3 + ": ", windowWidth - 200, 70 + fontlineheight * 2);
g.setColor(Color.white);
g.drawString(M[2].getName(), windowWidth - 160, 70 + fontlineheight * 2);
g.drawImage(M[2].getFlag(), windowWidth - 85, 55 + fontlineheight * 2, 27, 18, null);
for (int i = 3; i < ranknum; i++) {
g.setColor(CornflowerBlue);
g.setFont(new Font("黑體", Font.LAYOUT_LEFT_TO_RIGHT, 20));
g.drawString(i + 1 + ": ", windowWidth - 200, 70 + fontlineheight * i);
g.setColor(Color.white);
g.drawString(M[i].getName(), windowWidth - 160, 70 + fontlineheight * i);
g.drawImage(M[i].getFlag(), windowWidth - 85, 55 + fontlineheight * i, 27, 18, null);
}
}
}
游戲流程展示
1. 開始游戲頁面

2. 加載中頁面

3. 選擇地圖頁面

4. 自定義昵稱頁面

5. 運行頁面

死亡頁面

發展方向
這次專案學習識訓很大,在初期因為空指標錯誤耽誤了很長時間去琢磨Java中的ArrayList和List及其方法,但是當問題經過廣泛地排查得到解決后對知識的理解也更進一步,這個專案現在雖然能夠較流暢地運行,但是其發展空間仍然巨大,正如前面在分析圖中介紹的,我希望能夠為所有的敵人類分階段地設計撰寫一種智能的演算法,讓程式控制的小球能夠在某種情境下以超過玩家的速度發育,可能要用到貪心演算法、最短路徑等理論,在真正的手機端游戲中,小球可以分裂成兩個、四個甚至更多,因為本專案未實作玩家視野大小的動態調整,所以很遺憾沒能實作分裂功能,這些都為以后這個專案的繼續發展提供了可能,
(前面都不感興趣,快進到)原碼
鏈接:https://pan.baidu.com/s/1oLecTmwYWkvFDwVWu9KFYQ
提取碼:y0v1
最后
我是第一次用Java寫游戲,所以如果有錯誤和愚蠢的地方請大家指出,也請同樣小白的同學對我的代碼不要依賴可能有錯555 ,歡迎大家在評論區交流學習!\\^ _ ^//!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/252585.html
標籤:其他
下一篇:女朋友為我寫了一個防猝死插件
