Commit 81e7c71b authored by 9731301's avatar 9731301

complete client networkPlaying

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