⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 server.java

📁 使用Java 编制的小游戏
💻 JAVA
字号:
import java.io.*;
import java.net.*;
import java.util.*;
import java.math.*;

public class Server {

	private DataInputStream in;

	private DataOutputStream out;

	public static final int PORT = 8181;

	ServerSocket ser;

	Socket s;

	map m;

	StringBuffer StrB;

	String Str;

	player p;

	String ss;

	boolean vec = false;

	Server() {
		startServer();
	}

	//  :dos下不能正常运行, 因为有多个类,无法全部载入;
	void startServer() {
		try {
			System.out.println("游戏服务器启动……");
			ser = new ServerSocket(8181);
			s = ser.accept();

			in = new DataInputStream(s.getInputStream());

			out = new DataOutputStream(s.getOutputStream());

			out.writeUTF("服务器连接成功,请输入您要选择的地图大小,big,small,or mid");
			mapInput();
			m = new map(Str);
			m.init();

			System.out.println("地图初始化完成");
			out.writeUTF("地图生成完毕,请输入你要选择的游戏难度,easy or hard");
			nanduInput();
			p = new player(Str, m);// 生成玩家属性;
			out.writeUTF("请输入角色姓名汉字或者字母,没有限制");

			ss = in.readUTF();

			Str = "主角" + ss + p.getProperties() + " 地图大小为" + m.getMaxX() + "乘以"
					+ m.getMaxY() + "  游戏开始:GoodLuck,boy!";

			out.writeUTF(Str);
			System.out.println("服务器初始化完成,游戏正式开始");

		} catch (IOException e) {

			e.printStackTrace();
		}

	}

	void mapInput() {
		try {
			do {

				Str = in.readUTF();// 主动与被动的关系;始终是客户端为主动的,服务器被动响应;

				if (Str.equals("big") || Str.equals("small")
						|| Str.equals("mid")) {
					System.out.println("玩家选择的地图大小为" + Str);
					out.writeUTF("生成大地图");
					break;
				} else
					out.writeUTF("重新输入");
			} while (true);
		} catch (IOException e) {

			e.printStackTrace();
		}
	}

	void nanduInput() {
		try {
			do {

				Str = in.readUTF();// 主动与被动的关系;始终是客户端为主动的,服务器被动响应;

				if (Str.equals("easy") || Str.equals("hard")) {
					System.out.println("玩家选择的地图大小为" + Str);
					out.writeUTF("设定了难度" + Str);
					break;
				} else
					out.writeUTF("重新输入");
			} while (true);
		} catch (IOException e) {

			e.printStackTrace();
		}
	}

	void play() {
		try {
			out.writeUTF("请您使用w,s,d,a,键控制角色移动分别为上下左右,请不要直接回车输入 " + "boss的位置为:"
					+ m.i + "," + m.j);

			re: do { // 循环条件为真,当碰到大boss的时候,就会胜利或者失败跳出退出;

				Str = in.readUTF();

				System.out.println("玩家输入的移动为:");
				System.out.println(Str);
				if (Str.equals("")) {
					out.writeUTF("输入不正确,请重新输入");
					continue;
				}
				if(Str.equals("exit"))
					break;
				char c = Str.charAt(0);//如果空字符串,就会发生空指针错误,找不到任何字符;
				if (!(c == 'w' || c == 's' || c == ('a') || c == 'd')
						|| c == (char) 13) {
					out.writeUTF("输入不正确,请重新输入");
					out.flush();
					continue;
				}

				// 以上对玩家的坐标移动作出响应;

				if (true) {
					if (p.x == 0) {
						out.writeUTF("不能左移了,坐标" + p.x + "," + p.y);
					} else if (p.y == 0) {
						out.writeUTF("不能上移了.坐标为:" + p.x + " ," + p.y);
					} else if (p.x == m.getMaxX() - 1)
						out.writeUTF("不能右移了,坐标" + p.x + "," + p.y);
					else if (p.y == m.getMaxY() - 1)
						out.writeUTF("不能下移了,坐标" + p.x + "," + p.y);
					else
						out.writeUTF("可以移动,目前坐标为" + p.x + "," + p.y);
				}
				out.flush();

				p.move(Str, m);

				switch (m.getChar()[p.x][p.y]) {
				case 'a': {
					p.addAt();

					out.writeUTF("Lucky,你的的攻击力上升了" + p.getProperties());
					System.out.println("Lucky,玩家的攻击力上升了,继续移动吧");
					out.flush();
					m.getChar()[p.x][p.y] = 'z';
					out.writeUTF("很好");
					out.flush();
					break;// 必须注意这个条件,
				}

				case 'd': {
					p.addDf();
					out.writeUTF("Lucky,你的防御力上升了" + p.getProperties());
					System.out.println("Lucky,玩家的防御力上升了 继续移动吧");
					out.flush();
					out.writeUTF("很好");
					out.flush();
					m.getChar()[p.x][p.y] = 'z';
					break;
				}
				case 'h': {
					p.addHp();
					out.writeUTF("Lucky,你的生命上限上升了" + p.getProperties());
					System.out.println("Lucky,玩家的生命力上升了,继续移动吧");
					out.flush();
					out.writeUTF("很好");
					m.getChar()[p.x][p.y] = 'z';
					out.flush();

					break;
				}
				case 'm': {
					p.addMp();

					out.writeUTF("Lucky,你的魔力上升了" + p.getProperties());
					System.out.println("Lucky,玩家的魔力上升了,继续移动吧");
					out.flush();
					out.writeUTF("很好");
					out.flush();
					m.getChar()[p.x][p.y] = 'z';
					break;
				}
				case 'z': {
					p.atc();

					out.writeUTF("来到这里,发现又产生了一个怪物,经过一番努力你干掉了它,继续移动吧"
							+ p.getProperties());
					System.out.println("玩家杀怪数量增加1");
					if (p.levUp())
						out.writeUTF("玩家等级提升 " + p.getProperties());

					out.flush();
					out.writeUTF("很好");
					out.flush();
					break;
				}

				case 'B': {

					out.writeUTF("嘿嘿,你碰到了最终boss,觉悟吧!");
					out.flush();

					Str = "玩家目前属性为:" + "主角" + ss + p.getProperties() + "等级"
							+ p.lev;

					out.writeUTF(Str);
					out.flush();
					out.writeUTF("boss 攻击力为100,防御力为200,生命20000");
					out.flush();
					if (p.df <=100) {
						out.writeUTF("抱歉,你的能力过低,被boss秒杀了,原地复活,请按方向键离开 努力后再挑战一次");
						out.flush();
						out.writeUTF("false");
						break ;
					}
					if (p.df >=100) {
						out.writeUTF("恭喜你,你的能力远超过boss,游戏胜利了,请输入exit键退出或者方向键继续游戏");
						out.writeUTF("false");
						 
						vec = true;
						break ;

					}
				}

				}

			} while (!(Str).equals("exit"));

			if (vec) {
				out.writeUTF("游戏胜利,服务器断开");
				out.flush();
				s.close();

				ser.close();
			} else {
				out.writeUTF("游戏失败,服务器中断连接");
				out.flush();

			}

		} catch (IOException e) {

			e.printStackTrace();
		}

	}

	public static void main(String[] args) {

		Server ser = new Server();

		ser.play();
	}

}

class player {

	int hp;

	int mp;

	int at;// 攻击力

	int df;// 防御力

	int x;

	int y;// 坐标

	int lev = 0;// 等级;

	int count;// 杀怪数量;

	player() {
	}

	player(String s, map m) {
		int k = 0;
		if (s.equalsIgnoreCase("easy"))
			k = 20;
		if (s.equalsIgnoreCase("hard"))
			k = 10;
		hp = (int) (Math.random() * k * 100);
		mp = (int) (Math.random() * k * 50);
		at = (int) (Math.random() * k * 15);
		df = (int) (Math.random() * k * 5);

		x = (int) (Math.random() * m.getMaxX());

		y = (int) (Math.random() * m.getMaxY());

	}

	String getProperties() {
		return "目前玩家属性为: 生命" + hp + " 魔力" + mp + " 攻击力" + at + " 防御力" + df
				+ " 坐标" + x + "," + y;
	}

	void getCoordinate() {
		System.out.println("人物目前位置在" + x + "," + y);
	}

	void addAt() {
		at++;

	}

	void addDf() {

		df++;
	}

	void addHp() {

		hp += 10;
	}

	void addMp() {
		mp += 5;
	}

	void move(String s, map m) {

		switch (s.charAt(0)) {
		case 'a': {
			if (x > 0)
				x--;

			break;
		}
		case 'd': {
			if (x < m.getMaxX() - 1)
				x++;
			break;
		}
		case 'w': {
			if (y > 0)
				y--;
			break;
		}
		case 's': {
			if (y < m.getMaxY() - 1)
				y++;
			break;

		}

		}

	}

	boolean levUp() {
		if (count % 5 == 0) {
			at += 15;
			df += 5;
			hp += 100;
			mp += 50;
			lev++;
			return true;
		}
		return false;

	}

	void atc() {
		count++;
	}

	void df() {
	}

	void hpRestore() {
		hp += 10;
	}

	int getX() {
		return x;
	}

	int getY() {
		return y;
	}

}

class map {

	private int MaxX;

	private int MaxY;

	private char a[][];// 使用二维数组来表示地图,数组中的内容来表示地图中的内容;

	private int n;// 根据这个实例变量来确定地图大小的选择;

	protected int i;

	protected int j;

	map(String s) {

		if (s.equalsIgnoreCase("big"))
			n = 20;
		if (s.equalsIgnoreCase("small"))
			n = 10;
		if (s.equalsIgnoreCase("mid"))
			n = 15;

	}

	void init() {// 随即初始化地图,根据玩家选择大小;

		double x = Math.random() + 1;
		double y = Math.random() + 1;
		MaxX = (int) (x * n);

		MaxY = (int) (y * n);

		a = new char[MaxX][MaxY];

		int s = 0;
		for (int i = 0; i < MaxX; i++) {
			for (int j = 0; j < MaxY; j++)

			{
				if (a[i][j] == '\u0000') {
					s = (int) (Math.random() * 100) % 5;

					switch (s) {

					case 0:
						a[i][j] = 'a';
						break;
					case 1:
						a[i][j] = 'd';
						break;
					case 2:
						a[i][j] = 'h';
						break;
					case 3:
						a[i][j] = 'm';
						break;
					default:
						a[i][j] = 'z';
						break;
					}

				}
			}
		}

		i = (int) (Math.random() * 100) % MaxX;
		j = (int) (Math.random() * 100) % MaxY;

		a[i][j] = 'B';// 产生boss所在地

	}

	protected int getMaxX() {
		return MaxX;
	}

	protected int getMaxY() {
		return MaxY;
	}

	char[][] getChar() {
		return a;// 返回地图数组;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -