Commit 81e7c71b authored by 9731301's avatar 9731301

complete client networkPlaying

parent 90315b7f
This diff is collapsed.
......@@ -16,7 +16,7 @@ public class Client {
public void run(){
System.out.println("client connected");
try (Socket client = new Socket(InetAddress.getLocalHost().getHostName(),5757)){
try (Socket client = new Socket(InetAddress.getLocalHost().getHostName(),5758)){
OutputStream out = client.getOutputStream();
PrintWriter writer = new PrintWriter(out,true);
writer.println(user_pass);
......
package Server_Client.NetWorkGame;
import UI.GameState.*;
import UI.LogIn.Game.GameFrame;
import UI.Registration.InitialFrame;
import UI.Registration.InitialPanel;
import java.io.IOException;
import java.io.InputStream;
......@@ -15,69 +18,85 @@ public class Client1 {
private int port;
private int myX, myY, myRotate;
private ArrayList<BulletState> myBullets;
private WallState wallState;
private String serverAns;
private String[] enemyComponents;
private int numOfEnemies;
private String wallMap;
private WallState wallState;
private OutputStream out;
private InputStream in;
public Client1(String address , int port) {
this.host = address;
this.port = port;
numOfEnemies = 0;
wallState = new WallState(GameFrame.GAME_WIDTH , GameFrame.GAME_HEIGHT);
}
public void run0() throws IOException {
public void run(TankState tankState) {
try{
socket = new Socket(host,port);
OutputStream out = socket.getOutputStream();
InputStream in = socket.getInputStream();
out = socket.getOutputStream();
in = socket.getInputStream();
}
out.write("Client1".getBytes());
String clientText = "";
public void run1wall() throws IOException {
out.write(InitialPanel.userName.getText().getBytes());
byte[] buffer = new byte[2048];
int read = in.read(buffer);
wallMap = new String(buffer,0,read);
}
public void runToUpdateTank(TankState tankState) throws IOException {
try {
boolean running = true;
do {
// do {
String clientText = "";
clientText = myTankComponents();
running = !clientText.equalsIgnoreCase("over");
out.write(clientText.getBytes());
byte[] buffer = new byte[2048];
int read = in.read(buffer);
serverAns = new String(buffer,0,read);
System.out.println("- From Server : "+serverAns);
}while(running);
byte[] buffer1 = new byte[2048];
int read1 = in.read(buffer1);
serverAns = new String(buffer1, 0, read1);
System.out.println("- From Server : " + serverAns);
// }while(running);
} catch (IOException ex) {
ex.printStackTrace();
}
}
public void close() throws IOException {
out.write("over".getBytes());
System.out.println("Client disconnected.");
socket.close();
}catch (IOException e){
System.out.println("Error : "+e.getMessage());
e.printStackTrace();
}
}
private int getNumOfEnemies(){
String[] s = serverAns.split(" ");
numOfEnemies = s.length/6;
System.out.println(serverAns);
System.out.println(s.length+"ength");
numOfEnemies = (s.length)/6;
System.out.println(numOfEnemies+"num of enemies");
return numOfEnemies;
}
public ArrayList<EnemyTank> createEnemyTanks(){
ArrayList<EnemyTank> enemyTanks = new ArrayList<>();
for (int i = 0 ; i < getNumOfEnemies() ; i ++){
EnemyTank enemyTank = new EnemyTank(wallState);
EnemyTank enemyTank = new EnemyTank(getWallState());
enemyTanks.add(enemyTank);
}
if (enemyTanks == null)
enemyTanks = new ArrayList<>();
return enemyTanks;
}
public void updateEnemyComponents(ArrayList<EnemyTank> enemyTanks){//ToDO separate tanks and each tank bullets
public void updateEnemyComponents(ArrayList<EnemyTank> enemyTanks){
int j = 0;
enemyComponents = serverAns.split(" ");
wallMap = enemyComponents[0];
for (int i = 1 ; i < getNumOfEnemies() ; i++){
for (int i = 0 ; i < getNumOfEnemies() ; i++){
enemyTanks.get(i).locX = Integer.parseInt(enemyComponents[j]);j++;
enemyTanks.get(i).locY = Integer.parseInt(enemyComponents[j]);j++;
enemyTanks.get(i).rotate = Integer.parseInt(enemyComponents[j]);j++;
......@@ -99,6 +118,8 @@ public class Client1 {
private String myTankComponents(){
String tankComponent , bulletComponent = "";
tankComponent = myX +" "+ myY +" " + myRotate ;
if (myBullets == null)
return tankComponent;
for (BulletState bullet : myBullets){
bulletComponent +=" " + bullet.locX + " " + bullet.locY ;
}
......
......@@ -24,7 +24,7 @@ public class Server {
public void runRegistration() {
if (serverSocket!=null) return;
try {
serverSocket = new ServerSocket(5757);
serverSocket = new ServerSocket(5758);
System.out.println("server connected");
} catch (IOException e) {
e.printStackTrace();
......
......@@ -90,7 +90,7 @@ public class RewardState {
}
public void runReward(TankState tankState){
switch (1) {
switch (numOfReward) {
case 0:shield(tankState);break;
case 1:extraLife(tankState);break;
case 2:extraBulletDamage(tankState);break;
......
......@@ -27,7 +27,7 @@ public class TankState {
public int locX, locY, diam, rotate;
public boolean gameOver;
protected WallState walls;
protected ArrayList<BulletState> bullets = new ArrayList<>();
protected ArrayList<BulletState> bullets ;
private boolean hasShield;
private boolean hasLaser;
private int tankLife ;
......@@ -39,6 +39,7 @@ public class TankState {
// Initialize the game state and all elements ...
// setFirstLocationOfTank();
setFirstLocationOfTank();
bullets = new ArrayList<>();
rotate = 0;
diam = 32;
keyUP = false;
......@@ -67,7 +68,6 @@ public class TankState {
while (true) {
locX = randomX.nextInt(GameFrame.GAME_WIDTH-20);
locY = randomY.nextInt(GameFrame.GAME_HEIGHT-40);
System.out.println(locX +" "+locY);
if (!walls.isWallForTank(locX , locY) && locX > 10 && locY >10 && locX < GameFrame.GAME_WIDTH-10 && locY < GameFrame.GAME_HEIGHT -10)
break;
}
......@@ -80,6 +80,8 @@ public class TankState {
}
public ArrayList<BulletState> getBullets() {
if (bullets == null)
bullets = new ArrayList<BulletState>();
return bullets;
}
......
......@@ -13,7 +13,7 @@ import java.util.ArrayList;
public class WallState {
private int frameX, frameY;
private String source;
private String source = "C:\\Users\\Lenovo\\IdeaProjects\\JTankTrouble\\maps\\map3.txt";
private String[] lines;
public int numRows, numColumns, w, h;
private ArrayList<WallData> walls = new ArrayList<>();
......@@ -27,7 +27,7 @@ public class WallState {
frameY = GAME_HEIGHT;
try {
source = new String(Files.readAllBytes(Paths.get("C:\\Users\\Lenovo\\IdeaProjects\\JTankTrouble\\maps\\map3.txt")), StandardCharsets.UTF_8);
source = new String(Files.readAllBytes(Paths.get(source)), StandardCharsets.UTF_8);
wall1 = ImageIO.read(new File("C:\\Users\\Lenovo\\IdeaProjects\\JTankTrouble\\pics\\wall1.png"));
wall2 = ImageIO.read(new File("C:\\Users\\Lenovo\\IdeaProjects\\JTankTrouble\\pics\\wall2.png"));
blank = ImageIO.read(new File("C:\\Users\\Lenovo\\IdeaProjects\\JTankTrouble\\pics\\blank.png"));
......
......@@ -59,7 +59,7 @@ public class MainPanel extends JPanel {
super.mouseClicked(e);
Client1 client1 = null;
try {
client1 = new Client1(InetAddress.getLocalHost().getHostName(),5000);
client1 = new Client1(InetAddress.getLocalHost().getHostName(),5001);
run2(client1);
} catch (UnknownHostException ex) {
ex.printStackTrace();
......@@ -116,6 +116,11 @@ public class MainPanel extends JPanel {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
client1.run0();
} catch (IOException e) {
e.printStackTrace();
}
NWGFrame frame = new NWGFrame("Trouble Tank");
frame.setLocationRelativeTo(null); // put frame at center of screen
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
......@@ -123,8 +128,14 @@ public class MainPanel extends JPanel {
frame.initBufferStrategy();
// Create and execute the game-loop
NWGLoop game = new NWGLoop(frame , client1);
try {
game.init();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("222222 after init");
ThreadPool.execute(game);
System.out.println("after thearsd");
// and the game starts ...
}
});
......
......@@ -45,10 +45,11 @@ import java.util.ArrayList;
/**
* This must be called before the game loop starts.
*/
public void init() {
public void init() throws IOException {
// Perform all initializations ...
myTank = new MyTank(canvas.wallState);
client1.run(myTank);
myTank = new MyTank(client1.getWallState());
client1.run1wall();
client1.runToUpdateTank(myTank);
enemyTanks = client1.createEnemyTanks();
canvas.addKeyListener(myTank.getMyListener());
canvas.addMouseListener(myTank.getMouseListener());
......@@ -63,6 +64,7 @@ import java.util.ArrayList;
try {
long start = System.currentTimeMillis();
myTank.update();
client1.runToUpdateTank(myTank);
client1.updateEnemyComponents(enemyTanks);
client1.updateMyTank(myTank);
canvas.render(myTank , enemyTanks , client1.getWallState());
......@@ -72,6 +74,11 @@ import java.util.ArrayList;
} catch (InterruptedException | IOException ex) {
}
}
try {
client1.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package UI.LogIn;
import UI.Registration.InitialPanel;
import javax.swing.*;
import java.util.ArrayList;
public class Setting {
private float playedTime = 0;
private String userName ;
private String userName = InitialPanel.userName.getText() ;
private int numOfWinsSingle = 0;
private int numOfLoosesSingle = 0;
private int numOfWinsMulti = 0;
......@@ -29,9 +31,6 @@ public class Setting {
else numOfLoosesMulti++;
}
public void setUserName(String userName) {
this.userName = userName;
}
public int getNumOfWinsMulti() {
return numOfWinsMulti;
......
......@@ -11,7 +11,7 @@ import java.awt.event.MouseEvent;
import java.io.IOException;
public class InitialPanel extends JPanel {
public static boolean clickOnPlay;
private JTextField userName = new JTextField();
public static JTextField userName = new JTextField();
private JTextField pass = new JTextField();
public InitialPanel(){
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment