📄 gameserver.java
字号:
import java.net.*;
import java.util.Random;
import java.util.Vector;
import java.io.*;
public class gameServer {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ServerSocket gameS = null;
Vector playerlist = new Vector();
Vector monsterlist = new Vector();
Monster temp1 = new Monster(20,20,22,"bird1",120,180);
Monster temp2 = new Monster(30,30,22,"bird2",120,180);
Monster temp3 = new Monster(40,50,22,"bird3",120,180);
Monster temp4 = new Monster(24,23,22,"bird4",120,180);
Monster temp5 = new Monster(40,27,22,"bird5",120,180);
Monster temp6 = new Monster(2,56,22,"bird6",120,180);
Monster temp7 = new Monster(80,10,22,"bird7",120,180);
Monster temp8 = new Monster(50,15,22,"bird8",120,180);
monsterlist.addElement(temp1);
monsterlist.addElement(temp2);
monsterlist.addElement(temp3);
monsterlist.addElement(temp4);
monsterlist.addElement(temp5);
monsterlist.addElement(temp6);
monsterlist.addElement(temp7);
monsterlist.addElement(temp8);
try {
gameS = new ServerSocket(9999);
new number(playerlist).start();
while(true){
System.out.println("incoming ........");
Socket tempSocket = gameS.accept();
new HandleGame(tempSocket,playerlist,monsterlist).start();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
gameS.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class HandleGame extends Thread{
Socket itemp;
DataInputStream dio;
DataOutputStream dos;
boolean gameOver;
Vector vc;
Player2 player;
int playernumber;
Vector mons;
public HandleGame(Socket temp,Vector ivc,Vector mon){
itemp = temp;
vc = ivc;
mons = mon;
player = new Player2();
try {
dio = new DataInputStream(itemp.getInputStream());
dos = new DataOutputStream(itemp.getOutputStream());
} catch (Exception e) {
}
}
public void run(){
while(true){
try {
readSelf(dio);
writeOther(dos);
sendMonster(dos);
for(int i=0; i<mons.size();i++){
Monster temp2 = (Monster)mons.elementAt(i);
temp2.run(0, 0, 180, 200);
}
if(gameOver){
dio.close();
dos.close();
itemp.close();
}
} catch (Exception e) {
// TODO: handle exception
end();
}
}
}
public void end(){
try{
vc.removeElement(player);
dio.close();
dos.close();
itemp.close();
this.currentThread().stop();
}catch(Exception e){}
}
public void writeOther(DataOutputStream idos){
try {
idos.writeInt(vc.size() - 1);
for (int i = 0; i < vc.size(); i++) {
Player2 tempplayer = (Player2) vc.elementAt(i);
if (player != tempplayer) {
idos.writeInt(tempplayer.x);
idos.writeInt(tempplayer.y);
idos.writeInt(tempplayer.level);
idos.writeUTF(tempplayer.name);
}
}
} catch (Exception e) {
// TODO: handle exception
end();
}
}
public void sendMonster(DataOutputStream idos){
try {
idos.writeInt(mons.size());
for(int i=0;i<mons.size();i++){
Monster temp = (Monster)mons.elementAt(i);
dos.writeInt(temp.x);
dos.writeInt(temp.y);
dos.writeInt(temp.level);
dos.writeUTF(temp.name);
dos.writeInt(temp.idir);
dos.writeInt(temp.colums);
dos.writeInt(temp.rows);
//dos.writeInt(temp.idir);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void readSelf(DataInputStream idis){
try {
player.x = idis.readInt();
player.y = idis.readInt();
player.level = idis.readInt();
player.name = idis.readUTF();
System.out.println(player.name);
} catch (Exception e) {
// TODO: handle exception
end();
}
if(vc.indexOf(player) == -1)
{
vc.addElement(player);
System.out.println("add");
}
else {
System.out.println("find");
vc.setElementAt(player, vc.indexOf(player));
}
}
}
class number extends Thread{
Vector vci;
public number(Vector vc){
vci = vc;
}
public void run(){
while(true){
System.out.println("vc size = "+vci.size());
try{
Thread.sleep(500);
}catch(Exception e){}
}
}
}
class Player2{
int x;
int y;
int level;
String name;
}
class Monster{
int x;
int y;
int level;
int Hp;
int state;
String name;
int idir,colums,rows;
int viewx,viewy;
Random rnd = new Random();
public Monster(int ix, int iy,int ilevel,String tempname,int iviewx,int iviewy){
level = ilevel;
viewx = iviewx;
viewy = iviewy;
x = ix;
y = iy;
// idir = (rnd.nextInt()>>>1)%4;
name = tempname;
}
public void run(int ix,int iy,int width,int height){
//idir = (rnd.nextInt()>>>1)%4;
switch(idir){
case 0:
y++;
rows = 0;
break;
case 1:
x++;
rows = 2;
break;
case 2:
x--;
rows = 1;
break;
case 3:
y--;
rows = 3;
break;
}
colums = colums == 3? 0 : colums + 1;
if(x > ix+width){
idir = 2;
} else if(x <= ix){
idir = 1;
}
if(y < iy){
idir = 0;
}else if(y > iy+height){
idir = 3;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -