Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在服务器和客户端之间获取和发送数据_Java_Networking_Tcp - Fatal编程技术网

Java 在服务器和客户端之间获取和发送数据

Java 在服务器和客户端之间获取和发送数据,java,networking,tcp,Java,Networking,Tcp,我们正在制作一个六人多人游戏,我负责一个网络课程。游戏采用了流畅的2D。游戏中有实体(代码如下所示) 这是战斗代码,其中包含主要的游戏代码,代码目前尚未完成,只有移动: package Game; import java.util.ArrayList; import org.lwjgl.input.*; import org.newdawn.slick.*; import org.newdawn.slick.geom.Rectangle; import org.newdawn.slick.

我们正在制作一个六人多人游戏,我负责一个网络课程。游戏采用了流畅的2D。游戏中有实体(代码如下所示)

这是战斗代码,其中包含主要的游戏代码,代码目前尚未完成,只有移动:

 package Game;

import java.util.ArrayList;

import org.lwjgl.input.*;
import org.newdawn.slick.*;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.state.*;

public class Battle extends BasicGameState {

    private ArrayList<Entity> entities = new ArrayList<Entity>();
    private Hero player;
    private Image background;
    private int midScreen, ground;

    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
        gc.setVSync(true);
        player = new Ares();
        player.setCurrent(player.getStanding());
        entities.add(player);
        background = new Image("res/Background.png");
        midScreen = 800;
        ground = 625;
    }


    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
        g.translate((float)-player.getLocationX() + midScreen, (float)-player.getLocationY() + ground);
        background.draw(0, -920);
        for(int i = 0; i < entities.size(); i++)
            entities.get(i).getCurrent().draw((float)entities.get(i).getLocationX(), (float)entities.get(i).getLocationY());
    }

    public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
            player.getCurrent().update(delta); // ensures
            Input kb = gc.getInput();

            if (kb.isKeyDown(Input.KEY_SPACE) && player.getGrounded()) {
                player.setGrounded(false);
                player.setVelocity(7);
               player.setLocation(player.getLocationX(), player.getLocationY() - player.getVelocity());
                player.setCurrent(player.getStanding());
            }
            else if (kb.isKeyDown(Input.KEY_A) && player.getLocationX() > 800) {
                player.setLocation(player.getLocationX() - player.getSpeed() * delta, player.getLocationY());
                if(player.grounded)
                    player.setCurrent(player.getRight());
            }
            else if (kb.isKeyDown(Input.KEY_D) && player.getLocationX() < 8580) {
                player.setLocation(player.getLocationX() + player.getSpeed() * delta, player.getLocationY());
                if(player.grounded)
                    player.setCurrent(player.getRight());
            }
            else
                player.setCurrent(player.getStanding());

            if (Math.abs(player.getLocationY() - ground) < .01)
                player.setGrounded(true);
            if (player.getGrounded() == false) {
                player.setVelocity(player.getVelocity() - player.getGravity());
                player.setLocation(player.getLocationX(), player.getLocationY() - player.getVelocity());
            }  
    }    

    public int getID(){
        return 1;
    }
}
打包游戏;
导入java.util.ArrayList;
导入组织lwjgl.input.*;
导入org.newdawn.slick.*;
导入org.newdawn.slick.geom.Rectangle;
导入org.newdawn.slick.state.*;
公共阶级斗争扩展到基本美国国家{
私有ArrayList实体=新ArrayList();
私人英雄玩家;
私有图像背景;
私人中屏,地面;
public void init(GameContainer gc,StateBasedGame sbg)引发异常{
gc.setVSync(真);
玩家=新战神();
player.setCurrent(player.getStanding());
实体。添加(玩家);
背景=新图像(“res/background.png”);
中屏=800;
接地=625;
}
公共void呈现(GameContainer gc、StateBasedGame sbg、Graphics g)引发异常{
g、 翻译((float)-player.getLocationX()+屏幕中间,(float)-player.getLocationY()+地面);
背景图(0,-920);
对于(int i=0;i800){
player.setLocation(player.getLocationX()-player.getSpeed()*delta,player.getLocationY());
如果(球员接地)
player.setCurrent(player.getRight());
}
else if(kb.isKeyDown(Input.KEY_D)和&player.getLocationX()<8580){
player.setLocation(player.getLocationX()+player.getSpeed()*delta,player.getLocationY());
如果(球员接地)
player.setCurrent(player.getRight());
}
其他的
player.setCurrent(player.getStanding());
if(Math.abs(player.getLocationY()-ground)<.01)
player.setGrounded(true);
if(player.getground()==false){
player.setVelocity(player.getVelocity()-player.getGravity());
player.setLocation(player.getLocationX(),player.getLocationY()-player.getVelocity());
}  
}    
公共int getID(){
返回1;
}
}
我遇到的困难是创建一个服务器和客户端,从玩家那里获取输入,例如位置,谁选择了哪个玩家,等等。我想知道,如果在战斗中,当玩家对象被制作出来时,我如何能够获取对象的位置和其他特定属性,并将它们提供给其他计算机

注意:我正在用Java编写代码,并尝试使用TCP连接来编写代码。我对UDP持开放态度,如果你认为它可能更容易。只是给一个需要做什么的基本概述,也许有一点代码,不要简单地给我答案

    package Game;

import org.newdawn.slick.Animation;

public abstract class Hero extends Entity{

    static double gravity = 0.1;
    double velocity;
    int level;
    boolean disabled, grounded = true;

    public void setLevel(int i){
        level = i;
    }
    public void setVelocity(double d){
        velocity = d;
    }
    public void setDisabled(boolean b){
        disabled = b;
    }
    public void setGrounded(boolean b){
        grounded = b;
    }
    public int getLevel(){
        return level;
    }
    public double getVelocity(){
        return velocity;
    }
    public boolean getDisabled(){
        return disabled;
    }
    public boolean getGrounded(){
        return grounded;
    }
    public double getGravity(){
        return gravity;
    }

    public boolean isAlive(){
        if(getHealth() > 0)
            return true;
        return false;
    }

    public void attack(Entity gettingAttacked, Hero attacking) {
        gettingAttacked.setHealth(gettingAttacked.getHealth() - attacking.getDamage());
    }

    public boolean isReadyToRegenerate(long delta) {
        if(pastTime < 1 * 1000){
            pastTime += delta;
            return false;
        }
        else{
            pastTime = 0;
            return true;
        }
    }   

    public static void regenerate(Hero h, long delta){
        if(h.getHealth() >= 0 && h.getHealth() < h.getMaxHealth())
            if(h.isReadyToRegenerate(delta))
                h.setHealth(h.getHealth() + (h.getMaxHealth()) * 0.01);
    }
    public abstract void levelUp();
}
   package Game;

import org.newdawn.slick.Animation;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.SpriteSheet;

public class Ares extends Hero {

    public Ares() throws SlickException {
        setHealth(500);
        setSpeed(0.4);
        setDamage(70);
        setLocation(850,625);
        setSprites(new Animation(new SpriteSheet("res/Ares.png", 191, 275), 200), new Animation(new SpriteSheet("res/AresWalk.png", 191, 275), 200));
    }

    public void levelUp() {
        level += 1;
        health *= 1.1;
        damage *= 1.1;
    }

    public double getAttackRange() {
        return 250;
    }

    public double maxHealth() {
        return 500;
    }

}
 package Game;

import java.util.ArrayList;

import org.lwjgl.input.*;
import org.newdawn.slick.*;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.state.*;

public class Battle extends BasicGameState {

    private ArrayList<Entity> entities = new ArrayList<Entity>();
    private Hero player;
    private Image background;
    private int midScreen, ground;

    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
        gc.setVSync(true);
        player = new Ares();
        player.setCurrent(player.getStanding());
        entities.add(player);
        background = new Image("res/Background.png");
        midScreen = 800;
        ground = 625;
    }


    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
        g.translate((float)-player.getLocationX() + midScreen, (float)-player.getLocationY() + ground);
        background.draw(0, -920);
        for(int i = 0; i < entities.size(); i++)
            entities.get(i).getCurrent().draw((float)entities.get(i).getLocationX(), (float)entities.get(i).getLocationY());
    }

    public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
            player.getCurrent().update(delta); // ensures
            Input kb = gc.getInput();

            if (kb.isKeyDown(Input.KEY_SPACE) && player.getGrounded()) {
                player.setGrounded(false);
                player.setVelocity(7);
               player.setLocation(player.getLocationX(), player.getLocationY() - player.getVelocity());
                player.setCurrent(player.getStanding());
            }
            else if (kb.isKeyDown(Input.KEY_A) && player.getLocationX() > 800) {
                player.setLocation(player.getLocationX() - player.getSpeed() * delta, player.getLocationY());
                if(player.grounded)
                    player.setCurrent(player.getRight());
            }
            else if (kb.isKeyDown(Input.KEY_D) && player.getLocationX() < 8580) {
                player.setLocation(player.getLocationX() + player.getSpeed() * delta, player.getLocationY());
                if(player.grounded)
                    player.setCurrent(player.getRight());
            }
            else
                player.setCurrent(player.getStanding());

            if (Math.abs(player.getLocationY() - ground) < .01)
                player.setGrounded(true);
            if (player.getGrounded() == false) {
                player.setVelocity(player.getVelocity() - player.getGravity());
                player.setLocation(player.getLocationX(), player.getLocationY() - player.getVelocity());
            }  
    }    

    public int getID(){
        return 1;
    }
}