Commit c5f1d789 authored by 9731301's avatar 9731301

complete some settings

parent 81e7c71b
This diff is collapsed.
1111111111111111111111111 11111111111111111111
1001001001001010100011001 10000100010000002001
1000002000000220001100001 10000100011100002001
1000111100011101110000001 10000100010000000001
1001100200000000010011111 11100000020000000001
1001000111000011110000001 10000000000000000001
1002000001000001000000001 11111111111111111111
1002000001000011000000001 \ No newline at end of file
1111111111111111111111111
\ No newline at end of file
1111111111111111111111111
1001001001001010100011001
1000002000000220001100001
1000111100011101110000001
1001100200000000010011111
1001000111000011110000001
1002000001000001000000001
1002000001000011000000001
1111111111111111111111111
\ No newline at end of file
...@@ -3,3 +3,5 @@ his2|pp ...@@ -3,3 +3,5 @@ his2|pp
1|1 1|1
4|4 4|4
2|2
...@@ -30,7 +30,6 @@ public class Client1 { ...@@ -30,7 +30,6 @@ public class Client1 {
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 { public void run0() throws IOException {
...@@ -42,26 +41,22 @@ public class Client1 { ...@@ -42,26 +41,22 @@ public class Client1 {
public void run1wall() throws IOException { public void run1wall() throws IOException {
out.write(InitialPanel.userName.getText().getBytes()); out.write(InitialPanel.userName.getText().getBytes());
byte[] buffer = new byte[2048]; byte[] buffer = new byte[2048];
int read = in.read(buffer); int read = in.read(buffer);
wallMap = new String(buffer,0,read); wallMap = new String(buffer,0,read);
setWallState();
} }
public void runToUpdateTank(TankState tankState) throws IOException { public void runToUpdateTank(TankState tankState) throws IOException {
try { try {
boolean running = true;
// do {
String clientText = ""; String clientText = "";
clientText = myTankComponents(); clientText = myTankComponents();
running = !clientText.equalsIgnoreCase("over");
out.write(clientText.getBytes()); out.write(clientText.getBytes());
byte[] buffer1 = new byte[2048]; byte[] buffer1 = new byte[2048];
int read1 = in.read(buffer1); int read1 = in.read(buffer1);
serverAns = new String(buffer1, 0, read1); serverAns = new String(buffer1, 0, read1);
System.out.println("- From Server : " + serverAns); System.out.println("- From Server : " + serverAns);
// }while(running);
} catch (IOException ex) { } catch (IOException ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
...@@ -82,10 +77,10 @@ public class Client1 { ...@@ -82,10 +77,10 @@ public class Client1 {
return numOfEnemies; return numOfEnemies;
} }
public ArrayList<EnemyTank> createEnemyTanks(){ public ArrayList<EnemyTank> createEnemyTanks() throws IOException {
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(getWallState()); EnemyTank enemyTank = new EnemyTank(wallState);
enemyTanks.add(enemyTank); enemyTanks.add(enemyTank);
} }
if (enemyTanks == null) if (enemyTanks == null)
...@@ -126,8 +121,12 @@ public class Client1 { ...@@ -126,8 +121,12 @@ public class Client1 {
return tankComponent+bulletComponent; return tankComponent+bulletComponent;
} }
public WallState getWallState() { private void setWallState() throws IOException {
wallState = new WallState(GameFrame.GAME_WIDTH , GameFrame.GAME_HEIGHT);
wallState.setSource(wallMap); wallState.setSource(wallMap);
}
public WallState getWallState() {
return wallState; return wallState;
} }
} }
...@@ -3,3 +3,5 @@ his2|pp ...@@ -3,3 +3,5 @@ his2|pp
1|1 1|1
4|4 4|4
2|2
...@@ -61,7 +61,7 @@ public class RewardState { ...@@ -61,7 +61,7 @@ public class RewardState {
public int chooseRandomReward(){ public int chooseRandomReward(){
Random random = new Random(); Random random = new Random();
int x = random.nextInt(5); int x = random.nextInt(3);
return x; return x;
} }
...@@ -77,11 +77,11 @@ public class RewardState { ...@@ -77,11 +77,11 @@ public class RewardState {
case 1: case 1:
reward = ImageIO.read(new File("C:\\Users\\Lenovo\\IdeaProjects\\JTankTrouble\\pics\\heart.png"));break; reward = ImageIO.read(new File("C:\\Users\\Lenovo\\IdeaProjects\\JTankTrouble\\pics\\heart.png"));break;
case 2: case 2:
reward = ImageIO.read(new File("C:\\Users\\Lenovo\\IdeaProjects\\JTankTrouble\\pics\\bullet.png"));break; reward = ImageIO.read(new File("C:\\Users\\Lenovo\\IdeaProjects\\JTankTrouble\\pics\\laser.png"));break;
case 3: case 3:
reward = ImageIO.read(new File("C:\\Users\\Lenovo\\IdeaProjects\\JTankTrouble\\pics\\bullets.png"));break; reward = ImageIO.read(new File("C:\\Users\\Lenovo\\IdeaProjects\\JTankTrouble\\pics\\bullets.png"));break;
case 4: case 4:
reward = ImageIO.read(new File("C:\\Users\\Lenovo\\IdeaProjects\\JTankTrouble\\pics\\laser.png"));break; reward = ImageIO.read(new File("C:\\Users\\Lenovo\\IdeaProjects\\JTankTrouble\\pics\\bullet.png"));break;
} }
rewardImage = reward.getScaledInstance(30, 30, java.awt.Image.SCALE_SMOOTH); rewardImage = reward.getScaledInstance(30, 30, java.awt.Image.SCALE_SMOOTH);
return rewardImage; return rewardImage;
...@@ -93,9 +93,9 @@ public class RewardState { ...@@ -93,9 +93,9 @@ public class RewardState {
switch (numOfReward) { 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:laser(tankState);break;
case 3:extraBulletDamage2(tankState);break; case 3:extraBulletDamage2(tankState);break;
case 4:laser(tankState);break; case 4:extraBulletDamage(tankState);break;
} }
} }
......
package UI.GameState; package UI.GameState;
import UI.LogIn.MainPanel;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import java.awt.*; import java.awt.*;
import java.io.File; import java.io.File;
...@@ -8,6 +10,7 @@ import java.nio.charset.StandardCharsets; ...@@ -8,6 +10,7 @@ import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Random;
public class WallState { public class WallState {
...@@ -27,7 +30,8 @@ public class WallState { ...@@ -27,7 +30,8 @@ public class WallState {
frameY = GAME_HEIGHT; frameY = GAME_HEIGHT;
try { try {
source = new String(Files.readAllBytes(Paths.get(source)), StandardCharsets.UTF_8); if (MainPanel.singlePlay)
setSource(findMapPath());
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"));
...@@ -137,8 +141,19 @@ public class WallState { ...@@ -137,8 +141,19 @@ public class WallState {
return false; return false;
} }
public void setSource(String source) { public String findMapPath(){
this.source = source; Random random = new Random();
int x = random.nextInt(2);
System.out.println(x);
switch (x){
case 0:return "C:\\Users\\Lenovo\\IdeaProjects\\JTankTrouble\\maps\\map1.txt";
case 1:return "C:\\Users\\Lenovo\\IdeaProjects\\JTankTrouble\\maps\\map3.txt";
}
return null;
}
public void setSource(String mapPath) throws IOException {
source = new String(Files.readAllBytes(Paths.get(mapPath)), StandardCharsets.UTF_8);
} }
public ArrayList<WallData> getWalls() { public ArrayList<WallData> getWalls() {
......
package UI.LogIn.Game; package UI.LogIn.Game;
import UI.GameState.*; import UI.GameState.*;
import UI.LogIn.MainPanel;
import UI.LogIn.Setting; import UI.LogIn.Setting;
import UI.LogIn.SettingPanel;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import java.awt.*; import java.awt.*;
import java.awt.geom.AffineTransform; import java.awt.geom.AffineTransform;
...@@ -22,7 +25,7 @@ public class Rendering { ...@@ -22,7 +25,7 @@ public class Rendering {
int lastRender = -1; int lastRender = -1;
private ArrayList<Float> fpsHistory; private ArrayList<Float> fpsHistory;
private ArrayList<RewardState> rewards = new ArrayList<>(); private ArrayList<RewardState> rewards = new ArrayList<>();
private Setting setting = new Setting(); private boolean flag = true;
public Rendering(){ public Rendering(){
fpsHistory = new ArrayList<>(100); fpsHistory = new ArrayList<>(100);
...@@ -40,7 +43,7 @@ public class Rendering { ...@@ -40,7 +43,7 @@ public class Rendering {
// Draw my tank; // Draw my tank;
try { try {
image1 = ImageIO.read(new File(setting.getTankShapePath())); image1 = ImageIO.read(new File(SettingPanel.setting.getTankShapePath()));
} catch (IOException e) { } catch (IOException e) {
System.out.println(e); System.out.println(e);
} }
...@@ -149,9 +152,14 @@ public class Rendering { ...@@ -149,9 +152,14 @@ public class Rendering {
g2d.drawImage(op2.filter((BufferedImage) explosionImg, null), myTank.locX, myTank.locY, null); g2d.drawImage(op2.filter((BufferedImage) explosionImg, null), myTank.locX, myTank.locY, null);
myTank.setTankLife(0); myTank.setTankLife(0);
myTank.gameOver = true; myTank.gameOver = true;
if (flag) {
System.out.println(MainPanel.startTimeForSetting + "********"+ System.currentTimeMillis());
SettingPanel.setting.updateSettingAfterASingleGame(MainPanel.startTimeForSetting, System.currentTimeMillis(), false);
System.out.println(MainPanel.startTimeForSetting + "********"+ System.currentTimeMillis());
flag = false;
}
} }
} else if ((bullet.locX > myTank.locX - 20 && bullet.locX < myTank.locX + 20 && bullet.locY > myTank.locY - 10 && bullet.locY < myTank.locY + 10) && !myTank.hasShield() && System.currentTimeMillis() - bullet.getCreateTime() > 100 && myTank.isHasMoreLife()) { } else if ((bullet.locX > myTank.locX - 20 && bullet.locX < myTank.locX + 20 && bullet.locY > myTank.locY - 10 && bullet.locY < myTank.locY + 10) && !myTank.hasShield() && System.currentTimeMillis() - bullet.getCreateTime() > 100 && myTank.isHasMoreLife()) {
System.out.println("has ");
myTank.setHasMoreLife(false); myTank.setHasMoreLife(false);
if (myTank.getTankLife() > 100) if (myTank.getTankLife() > 100)
myTank.setTankLife(myTank.getTankLife() - 10); myTank.setTankLife(myTank.getTankLife() - 10);
...@@ -169,6 +177,9 @@ public class Rendering { ...@@ -169,6 +177,9 @@ public class Rendering {
g2d.drawImage(op2.filter((BufferedImage) explosionImg, null), enemyTank.locX, enemyTank.locY, null); g2d.drawImage(op2.filter((BufferedImage) explosionImg, null), enemyTank.locX, enemyTank.locY, null);
enemyTank.setTankLife(0); enemyTank.setTankLife(0);
enemyTank.gameOver = true; enemyTank.gameOver = true;
if (flag){
SettingPanel.setting.updateSettingAfterASingleGame(MainPanel.startTimeForSetting , System.currentTimeMillis() ,true);}
flag = false;
} }
} }
} }
......
...@@ -20,6 +20,8 @@ import java.net.UnknownHostException; ...@@ -20,6 +20,8 @@ import java.net.UnknownHostException;
public class MainPanel extends JPanel { public class MainPanel extends JPanel {
private Image image; private Image image;
public static boolean singlePlay;
public static long startTimeForSetting;
public MainPanel() throws IOException { public MainPanel() throws IOException {
image = ImageIO.read(new File("C:\\Users\\Lenovo\\IdeaProjects\\JTankTrouble\\pics\\index.png")); image = ImageIO.read(new File("C:\\Users\\Lenovo\\IdeaProjects\\JTankTrouble\\pics\\index.png"));
...@@ -48,6 +50,9 @@ public class MainPanel extends JPanel { ...@@ -48,6 +50,9 @@ public class MainPanel extends JPanel {
@Override @Override
public void mouseClicked(MouseEvent e) { public void mouseClicked(MouseEvent e) {
super.mouseClicked(e); super.mouseClicked(e);
singlePlay =true;
long x = System.currentTimeMillis();
startTimeForSetting = x ;
run(); run();
} }
...@@ -60,6 +65,7 @@ public class MainPanel extends JPanel { ...@@ -60,6 +65,7 @@ public class MainPanel extends JPanel {
Client1 client1 = null; Client1 client1 = null;
try { try {
client1 = new Client1(InetAddress.getLocalHost().getHostName(),5001); client1 = new Client1(InetAddress.getLocalHost().getHostName(),5001);
startTimeForSetting =System.currentTimeMillis();
run2(client1); run2(client1);
} catch (UnknownHostException ex) { } catch (UnknownHostException ex) {
ex.printStackTrace(); ex.printStackTrace();
...@@ -133,9 +139,7 @@ public class MainPanel extends JPanel { ...@@ -133,9 +139,7 @@ public class MainPanel extends JPanel {
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); 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 ...
} }
}); });
......
...@@ -47,8 +47,9 @@ import java.util.ArrayList; ...@@ -47,8 +47,9 @@ import java.util.ArrayList;
*/ */
public void init() throws IOException { public void init() throws IOException {
// Perform all initializations ... // Perform all initializations ...
myTank = new MyTank(client1.getWallState());
client1.run1wall(); client1.run1wall();
System.out.println(client1.getWallState());
myTank = new MyTank(client1.getWallState());
client1.runToUpdateTank(myTank); client1.runToUpdateTank(myTank);
enemyTanks = client1.createEnemyTanks(); enemyTanks = client1.createEnemyTanks();
canvas.addKeyListener(myTank.getMyListener()); canvas.addKeyListener(myTank.getMyListener());
......
...@@ -3,6 +3,7 @@ package UI.LogIn.NetWorkGame; ...@@ -3,6 +3,7 @@ package UI.LogIn.NetWorkGame;
import Server_Client.NetWorkGame.Client1; import Server_Client.NetWorkGame.Client1;
import UI.GameState.*; import UI.GameState.*;
import UI.LogIn.Game.GameLoop; import UI.LogIn.Game.GameLoop;
import UI.LogIn.MainPanel;
import UI.LogIn.Setting; import UI.LogIn.Setting;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
...@@ -157,7 +158,6 @@ public class NWGRendering { ...@@ -157,7 +158,6 @@ public class NWGRendering {
myTank.gameOver = true; myTank.gameOver = true;
} }
} else if ((bullet.locX > myTank.locX - 20 && bullet.locX < myTank.locX + 20 && bullet.locY > myTank.locY - 10 && bullet.locY < myTank.locY + 10) && !myTank.hasShield() && System.currentTimeMillis() - bullet.getCreateTime() > 100 && myTank.isHasMoreLife()) { } else if ((bullet.locX > myTank.locX - 20 && bullet.locX < myTank.locX + 20 && bullet.locY > myTank.locY - 10 && bullet.locY < myTank.locY + 10) && !myTank.hasShield() && System.currentTimeMillis() - bullet.getCreateTime() > 100 && myTank.isHasMoreLife()) {
System.out.println("has ");
myTank.setHasMoreLife(false); myTank.setHasMoreLife(false);
if (myTank.getTankLife() > 100) if (myTank.getTankLife() > 100)
myTank.setTankLife(myTank.getTankLife() - 10); myTank.setTankLife(myTank.getTankLife() - 10);
......
...@@ -6,7 +6,7 @@ import javax.swing.*; ...@@ -6,7 +6,7 @@ import javax.swing.*;
import java.util.ArrayList; import java.util.ArrayList;
public class Setting { public class Setting {
private float playedTime = 0; private long playedTime = 0;
private String userName = InitialPanel.userName.getText() ; private String userName = InitialPanel.userName.getText() ;
private int numOfWinsSingle = 0; private int numOfWinsSingle = 0;
private int numOfLoosesSingle = 0; private int numOfLoosesSingle = 0;
...@@ -19,13 +19,14 @@ public class Setting { ...@@ -19,13 +19,14 @@ public class Setting {
private ArrayList<String> urls = new ArrayList<>(); private ArrayList<String> urls = new ArrayList<>();
public void updateSettingAfterASingleGame(float startTime , float endTime , boolean isWin ){ public void updateSettingAfterASingleGame(long startTime , long endTime , boolean isWin ){
System.out.println(endTime +" "+ startTime);
playedTime += endTime - startTime ; playedTime += endTime - startTime ;
if (isWin)numOfWinsSingle++; if (isWin)numOfWinsSingle++;
else numOfLoosesSingle++; else numOfLoosesSingle++;
} }
public void updateSettingAfterAMultiGame(float startTime , float endTime , boolean isWin ){ public void updateSettingAfterAMultiGame(long startTime , long endTime , boolean isWin ){
playedTime += endTime - startTime ; playedTime += endTime - startTime ;
if (isWin)numOfWinsMulti++; if (isWin)numOfWinsMulti++;
else numOfLoosesMulti++; else numOfLoosesMulti++;
...@@ -40,7 +41,7 @@ public class Setting { ...@@ -40,7 +41,7 @@ public class Setting {
return numOfLoosesMulti; return numOfLoosesMulti;
} }
public float getPlayedTime() { public long getPlayedTime() {
return playedTime; return playedTime;
} }
......
...@@ -12,7 +12,7 @@ import java.io.IOException; ...@@ -12,7 +12,7 @@ import java.io.IOException;
public class SettingPanel extends JPanel { public class SettingPanel extends JPanel {
private Setting setting = new Setting(); public static Setting setting = new Setting();
private String chosenTankPath ="C:\\Users\\Lenovo\\IdeaProjects\\JTankTrouble\\pics\\tank_blue.png"; private String chosenTankPath ="C:\\Users\\Lenovo\\IdeaProjects\\JTankTrouble\\pics\\tank_blue.png";
private Image image; private Image image;
......
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