Commit 3df333c1 authored by 9731301's avatar 9731301

add networkPlaying

parent b9d28866
<component name="ArtifactManager">
<artifact type="jar" name="JTankTrouble:jar">
<output-path>$PROJECT_DIR$/out/artifacts/JTankTrouble_jar</output-path>
<root id="archive" name="JTankTrouble.jar">
<element id="module-output" name="JTankTrouble" />
</root>
</artifact>
</component>
\ No newline at end of file
This diff is collapsed.
Manifest-Version: 1.0
Main-Class: UI.LogIn.Main
Manifest-Version: 1.0
Main-Class: UI.LogIn.Main
package Server_Client.NetWorkGame;
import UI.GameState.BulletState;
import UI.GameState.EnemyTank;
import UI.GameState.TankState;
import UI.GameState.WallState;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Scanner;
public class Client1 {
private Socket socket = null;
private String host;
private int port;
private int enemyX;
private int enemyY;
private int enemyRotate;
private int enemyBulletX;
private int enemyBulletY;
private boolean enemyHasLaser;
private boolean enemyHasShield;
private boolean enemyHasMoreLife;
private WallState wallState;
private ArrayList<EnemyTank> enemyTanks;
private ArrayList<BulletState> eachTankBullets;
private String serverAns;
public Client1(String address , int port) {
this.host = address;
this.port = port;
}
private String tankStateToString (TankState tank) throws IOException {
String tankComponent , bulletComponent = "";
tankComponent = tank.locX +" "+ tank.locY+" " +tank.rotate +" "+tank.hasShield() +" "+tank.hasLaser() +" "+tank.isHasMoreLife();
for (BulletState bullet : tank.getBullets()){
bulletComponent +=" " + bullet.locX + " " + bullet.locY + " " ;
}
return tankComponent + bulletComponent;
}
public void run(TankState tankState) {
try{
socket = new Socket(host,port);
OutputStream out = socket.getOutputStream();
InputStream in = socket.getInputStream();
out.write("Client1".getBytes());
String text = "";
boolean running = true;
do {
//ToDo update the tank and enemy tank x , y , ...
text = tankStateToString(tankState);
running = !text.equalsIgnoreCase("over");
out.write(text.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);
System.out.println("Client disconnected.");
socket.close();
}catch (IOException e){
System.out.println("Error : "+e.getMessage());
e.printStackTrace();
}
}
private ArrayList<EnemyTank> createEnemyTanks(String s){
ArrayList<EnemyTank> enemyTanks = new ArrayList<>();
int numOfEnemies = findNumOfEnemies(s);
for (int i = 0 ; i < numOfEnemies ; i ++){
EnemyTank enemyTank = new EnemyTank(wallState);
enemyTanks.add(enemyTank);
}
return enemyTanks;
}
//ToDo
private int findNumOfEnemies(String s){
}
private void parser(){//ToDO separate tanks and each tank bullets
}
public void updateEnemyComponents(){
for (EnemyTank enemyTank : enemyTanks){
enemyTank.locX = enemyX;
enemyTank.locY = enemyY;
enemyTank.rotate =enemyRotate;
enemyTank.setHasShield(enemyHasShield);
enemyTank.setHasLaser(enemyHasLaser);
enemyTank.setHasMoreLife(enemyHasMoreLife);
for (BulletState bullet : enemyTank.getBullets()){
bullet.locX = enemyBulletX;
bullet.locY = enemyBulletY;
}
}
}
public ArrayList<EnemyTank> getEnemyTanks() {
return enemyTanks = createEnemyTanks(serverAns);
}
public WallState getWallState() {
return wallState;
}
}
package UI.LogIn; package UI.LogIn;
import Server_Client.NetWorkGame.Client1;
import UI.LogIn.Game.GameFrame; import UI.LogIn.Game.GameFrame;
import UI.LogIn.Game.GameLoop; import UI.LogIn.Game.GameLoop;
import UI.LogIn.Game.ThreadPool; import UI.LogIn.Game.ThreadPool;
import UI.LogIn.NetWorkGame.NWGFrame;
import UI.LogIn.NetWorkGame.NWGLoop;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import javax.swing.*; import javax.swing.*;
...@@ -11,6 +14,8 @@ import java.awt.event.MouseAdapter; ...@@ -11,6 +14,8 @@ import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class MainPanel extends JPanel { public class MainPanel extends JPanel {
...@@ -48,6 +53,20 @@ public class MainPanel extends JPanel { ...@@ -48,6 +53,20 @@ public class MainPanel extends JPanel {
} }
}); });
multiPlaysBtn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
Client1 client1 = null;
try {
client1 = new Client1(InetAddress.getLocalHost().getHostName(),5000);
run2(client1);
} catch (UnknownHostException ex) {
ex.printStackTrace();
}
}
});
settingBtn.addMouseListener(new MouseAdapter() { settingBtn.addMouseListener(new MouseAdapter() {
@Override @Override
public void mouseClicked(MouseEvent e) { public void mouseClicked(MouseEvent e) {
...@@ -90,4 +109,24 @@ public class MainPanel extends JPanel { ...@@ -90,4 +109,24 @@ public class MainPanel extends JPanel {
} }
}); });
} }
private void run2(Client1 client1) {
// Initialize the global thread-pool
ThreadPool.init();
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
NWGFrame frame = new NWGFrame("Trouble Tank");
frame.setLocationRelativeTo(null); // put frame at center of screen
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
frame.initBufferStrategy();
// Create and execute the game-loop
NWGLoop game = new NWGLoop(frame , client1);
game.init();
ThreadPool.execute(game);
// and the game starts ...
}
});
}
} }
package UI.LogIn.NetWorkGame;
import UI.GameState.EnemyTank;
import UI.GameState.MyTank;
import UI.GameState.WallState;
import UI.LogIn.Game.Rendering;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferStrategy;
import java.io.IOException;
import java.util.ArrayList;
public class NWGFrame extends JFrame {
private long lastRender;
private BufferStrategy bufferStrategy;
private NWGRendering rendering = new NWGRendering();
public static final int GAME_HEIGHT = 720; // 720p game resolution
public static final int GAME_WIDTH = 16 * GAME_HEIGHT / 9; // wide aspect ratio
public WallState wallState = new WallState(GAME_WIDTH, GAME_HEIGHT);
public NWGFrame(String gameName) {
lastRender = -1;
setTitle(gameName);
pack();
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setSize(GAME_WIDTH, GAME_HEIGHT);
setResizable(false);
}
/**
* This must be called once after the JFrame is shown:
* frame.setVisible(true);
* and before any rendering is started.
*/
public void initBufferStrategy() {
// Triple-buffering
createBufferStrategy(3);
bufferStrategy = getBufferStrategy();
}
/**
* Game rendering with triple-buffering using BufferStrategy.
*/
public void render(MyTank myTank, ArrayList<EnemyTank> enemyTanks , WallState wallState) {
do {
do {
Graphics2D graphics = (Graphics2D) bufferStrategy.getDrawGraphics();
try {
rendering.doRendering(graphics, myTank, enemyTanks , wallState );
} catch (IOException e) {
e.printStackTrace();
} finally {
// Dispose the graphics
graphics.dispose();
}
// Repeat the rendering if the drawing buffer contents were restored
} while (bufferStrategy.contentsRestored());
// Display the buffer
bufferStrategy.show();
// Tell the system to do the drawing NOW;
// otherwise it can take a few extra ms and will feel jerky!
Toolkit.getDefaultToolkit().sync();
// Repeat the rendering if the drawing buffer was lost
} while (bufferStrategy.contentsLost());
}
}
package UI.LogIn.NetWorkGame;
import Server_Client.NetWorkGame.Client1;
import UI.GameState.EnemyTank;
import UI.GameState.MyTank;
import UI.LogIn.Game.GameFrame;
import java.util.ArrayList;
/*** In The Name of Allah ***/
/**
* A very simple structure for the main game loop.
* THIS IS NOT PERFECT, but works for most situations.
* Note that to make this work, none of the 2 methods
* in the while loop (update() and render()) should be
* long running! Both must execute very quickly, without
* any waiting and blocking!
*
* Detailed discussion on different game loop design
* patterns is available in the following link:
* http://gameprogrammingpatterns.com/game-loop.html
*
* @author Seyede zahra seyed fatehi
*/
public class NWGLoop implements Runnable {
/**
* Frame Per Second.
* Higher is better, but any value above 24 is fine.
*/
public static final int FPS = 30;
private NWGFrame canvas;
private MyTank myTank;
private ArrayList<EnemyTank> enemyTanks;
private Client1 client1;
public NWGLoop(NWGFrame frame , Client1 client1) {
canvas = frame;
this.client1 = client1;
}
public static long startTime;
/**
* This must be called before the game loop starts.
*/
public void init() {
// Perform all initializations ...
myTank = new MyTank(canvas.wallState);
client1.run(myTank);
enemyTanks = client1.getEnemyTanks();
canvas.addKeyListener(myTank.getMyListener());
canvas.addMouseListener(myTank.getMouseListener());
canvas.addMouseMotionListener(myTank.getMouseMotionListener());
startTime = 0;
}
@Override
public void run() {
boolean gameOver = false;
while (!gameOver) {
try {
long start = System.currentTimeMillis();
myTank.update();
client1.updateEnemyComponents();
canvas.render(myTank , enemyTanks , client1.getWallState());
long delay = (1000 / FPS) - (System.currentTimeMillis() - start);
if (delay > 0)
Thread.sleep(delay);
} catch (InterruptedException ex) {
}
}
}
}
This diff is collapsed.
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