Commit c5f1d789 authored by 9731301's avatar 9731301

complete some settings

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