Commit 5253b190 authored by 9731301's avatar 9731301

set the end of the game and adjust packages

parent 1d67ac9f
...@@ -2,26 +2,45 @@ ...@@ -2,26 +2,45 @@
<project version="4"> <project version="4">
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="0b3d586d-9f0f-4ca4-b09d-03a200e1e684" name="Default Changelist" comment=""> <list default="true" id="0b3d586d-9f0f-4ca4-b09d-03a200e1e684" name="Default Changelist" comment="">
<change afterPath="$PROJECT_DIR$/src/UI/GameState/EnemyTank.java" afterDir="false" /> <change afterPath="$PROJECT_DIR$/src/UI/GameState/RewarsState.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/UI/GameState/MyTank.java" afterDir="false" /> <change afterPath="$PROJECT_DIR$/src/UI/LogIn/Game/GameFrame.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/UI/LogIn/Game/GameLoop.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/UI/LogIn/Game/MyListener.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/UI/LogIn/Game/Rendering.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/UI/LogIn/Game/ThreadPool.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/GameFrame.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/JTankTrouble/UI/GameFrame.class" afterDir="false" /> <change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/GameFrame.class" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/GameLoop.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/JTankTrouble/UI/GameLoop.class" afterDir="false" /> <change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/GameLoop.class" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/GameState/BulletState$KeyHandler2.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/JTankTrouble/UI/GameState/BulletState$KeyHandler2.class" afterDir="false" /> <change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/GameState/BulletState$KeyHandler2.class" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/GameState/BulletState.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/JTankTrouble/UI/GameState/BulletState.class" afterDir="false" /> <change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/GameState/BulletState.class" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/GameState/TankState$KeyHandler1.class" beforeDir="false" /> <change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/GameState/EnemyTank.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/JTankTrouble/UI/GameState/EnemyTank.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/GameState/TankState$MouseHandler.class" beforeDir="false" /> <change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/GameState/MyTank$KeyHandler1.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/JTankTrouble/UI/GameState/MyTank$KeyHandler1.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/GameState/MyTank$MouseHandler.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/JTankTrouble/UI/GameState/MyTank$MouseHandler.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/GameState/MyTank.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/JTankTrouble/UI/GameState/MyTank.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/GameState/TankState.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/JTankTrouble/UI/GameState/TankState.class" afterDir="false" /> <change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/GameState/TankState.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/JTankTrouble/UI/GameState/TankState.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/GameState/WallState$WallData.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/JTankTrouble/UI/GameState/WallState$WallData.class" afterDir="false" /> <change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/LogIn/Main.class" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/GameState/WallState.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/JTankTrouble/UI/GameState/WallState.class" afterDir="false" /> <change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/LogIn/MainPanel$1.class" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/Rendering.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/JTankTrouble/UI/Rendering.class" afterDir="false" /> <change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/LogIn/MainPanel$2.class" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/UI/GameFrame.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/UI/GameFrame.java" afterDir="false" /> <change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/LogIn/MainPanel$3.class" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/UI/GameLoop.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/UI/GameLoop.java" afterDir="false" /> <change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/LogIn/MainPanel.class" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/UI/GameState/BulletState.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/UI/GameState/BulletState.java" afterDir="false" /> <change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/LogIn/Mainframe.class" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/LogIn/SettingPanel$1.class" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/LogIn/SettingPanel$2.class" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/LogIn/SettingPanel.class" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/MyListener.class" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/Rendering.class" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/ThreadPool.class" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/UI/GameFrame.java" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/UI/GameLoop.java" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/UI/GameState/EnemyTank.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/UI/GameState/EnemyTank.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/UI/GameState/MyTank.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/UI/GameState/MyTank.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/UI/GameState/TankState.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/UI/GameState/TankState.java" afterDir="false" /> <change beforePath="$PROJECT_DIR$/src/UI/GameState/TankState.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/UI/GameState/TankState.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/UI/GameState/WallState.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/UI/GameState/WallState.java" afterDir="false" /> <change beforePath="$PROJECT_DIR$/src/UI/LogIn/Main.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/UI/LogIn/Main.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/UI/LogIn/SettingPanel.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/UI/LogIn/SettingPanel.java" afterDir="false" /> <change beforePath="$PROJECT_DIR$/src/UI/LogIn/MainFrame.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/UI/LogIn/MainFrame.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/UI/Rendering.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/UI/Rendering.java" afterDir="false" /> <change beforePath="$PROJECT_DIR$/src/UI/LogIn/MainPanel.java" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/UI/MyListener.java" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/UI/Rendering.java" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/UI/ThreadPool.java" beforeDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
...@@ -143,7 +162,8 @@ ...@@ -143,7 +162,8 @@
<workItem from="1596134290663" duration="37548000" /> <workItem from="1596134290663" duration="37548000" />
<workItem from="1596205210217" duration="16177000" /> <workItem from="1596205210217" duration="16177000" />
<workItem from="1596226360961" duration="5478000" /> <workItem from="1596226360961" duration="5478000" />
<workItem from="1596265047938" duration="8448000" /> <workItem from="1596265047938" duration="12842000" />
<workItem from="1596295633892" duration="11516000" />
</task> </task>
<servers /> <servers />
</component> </component>
...@@ -155,14 +175,14 @@ ...@@ -155,14 +175,14 @@
<screen x="0" y="0" width="1536" height="824" /> <screen x="0" y="0" width="1536" height="824" />
</state> </state>
<state x="517" y="216" key="#com.intellij.fileTypes.FileTypeChooser/0.0.1536.824@0.0.1536.824" timestamp="1595580808418" /> <state x="517" y="216" key="#com.intellij.fileTypes.FileTypeChooser/0.0.1536.824@0.0.1536.824" timestamp="1595580808418" />
<state x="574" y="122" key="#com.intellij.ide.util.MemberChooser" timestamp="1596220903263"> <state x="574" y="122" key="#com.intellij.ide.util.MemberChooser" timestamp="1596308911976">
<screen x="0" y="0" width="1536" height="824" /> <screen x="0" y="0" width="1536" height="824" />
</state> </state>
<state x="574" y="122" key="#com.intellij.ide.util.MemberChooser/0.0.1536.824@0.0.1536.824" timestamp="1596220903263" /> <state x="574" y="122" key="#com.intellij.ide.util.MemberChooser/0.0.1536.824@0.0.1536.824" timestamp="1596308911976" />
<state x="566" y="298" width="403" height="237" key="#com.intellij.refactoring.move.moveClassesOrPackages.MoveClassesOrPackagesDialog.classes" timestamp="1596270751857"> <state x="566" y="298" width="403" height="237" key="#com.intellij.refactoring.move.moveClassesOrPackages.MoveClassesOrPackagesDialog.classes" timestamp="1596307776243">
<screen x="0" y="0" width="1536" height="824" /> <screen x="0" y="0" width="1536" height="824" />
</state> </state>
<state x="566" y="298" width="403" height="237" key="#com.intellij.refactoring.move.moveClassesOrPackages.MoveClassesOrPackagesDialog.classes/0.0.1536.824@0.0.1536.824" timestamp="1596270751857" /> <state x="566" y="298" width="403" height="237" key="#com.intellij.refactoring.move.moveClassesOrPackages.MoveClassesOrPackagesDialog.classes/0.0.1536.824@0.0.1536.824" timestamp="1596307776243" />
<state x="611" y="310" key="#com.intellij.refactoring.move.moveClassesOrPackages.MoveClassesOrPackagesDialog.packages" timestamp="1596137635178"> <state x="611" y="310" key="#com.intellij.refactoring.move.moveClassesOrPackages.MoveClassesOrPackagesDialog.packages" timestamp="1596137635178">
<screen x="0" y="0" width="1536" height="824" /> <screen x="0" y="0" width="1536" height="824" />
</state> </state>
...@@ -171,30 +191,30 @@ ...@@ -171,30 +191,30 @@
<screen x="0" y="0" width="1536" height="824" /> <screen x="0" y="0" width="1536" height="824" />
</state> </state>
<state x="593" y="0" key="CommitChangelistDialog2/0.0.1536.824@0.0.1536.824" timestamp="1595639194593" /> <state x="593" y="0" key="CommitChangelistDialog2/0.0.1536.824@0.0.1536.824" timestamp="1595639194593" />
<state width="1493" height="104" key="GridCell.Tab.0.bottom" timestamp="1596274226921"> <state width="1493" height="83" key="GridCell.Tab.0.bottom" timestamp="1596308973864">
<screen x="0" y="0" width="1536" height="824" /> <screen x="0" y="0" width="1536" height="824" />
</state> </state>
<state width="1493" height="104" key="GridCell.Tab.0.bottom/0.0.1536.824@0.0.1536.824" timestamp="1596274226921" /> <state width="1493" height="83" key="GridCell.Tab.0.bottom/0.0.1536.824@0.0.1536.824" timestamp="1596308973864" />
<state width="1493" height="104" key="GridCell.Tab.0.center" timestamp="1596274226921"> <state width="1493" height="83" key="GridCell.Tab.0.center" timestamp="1596308973864">
<screen x="0" y="0" width="1536" height="824" /> <screen x="0" y="0" width="1536" height="824" />
</state> </state>
<state width="1493" height="104" key="GridCell.Tab.0.center/0.0.1536.824@0.0.1536.824" timestamp="1596274226921" /> <state width="1493" height="83" key="GridCell.Tab.0.center/0.0.1536.824@0.0.1536.824" timestamp="1596308973864" />
<state width="1493" height="104" key="GridCell.Tab.0.left" timestamp="1596274226920"> <state width="1493" height="83" key="GridCell.Tab.0.left" timestamp="1596308973864">
<screen x="0" y="0" width="1536" height="824" /> <screen x="0" y="0" width="1536" height="824" />
</state> </state>
<state width="1493" height="104" key="GridCell.Tab.0.left/0.0.1536.824@0.0.1536.824" timestamp="1596274226920" /> <state width="1493" height="83" key="GridCell.Tab.0.left/0.0.1536.824@0.0.1536.824" timestamp="1596308973864" />
<state width="1493" height="104" key="GridCell.Tab.0.right" timestamp="1596274226921"> <state width="1493" height="83" key="GridCell.Tab.0.right" timestamp="1596308973864">
<screen x="0" y="0" width="1536" height="824" /> <screen x="0" y="0" width="1536" height="824" />
</state> </state>
<state width="1493" height="104" key="GridCell.Tab.0.right/0.0.1536.824@0.0.1536.824" timestamp="1596274226921" /> <state width="1493" height="83" key="GridCell.Tab.0.right/0.0.1536.824@0.0.1536.824" timestamp="1596308973864" />
<state x="589" y="114" key="RollbackChangesDialog" timestamp="1595702094731"> <state x="589" y="114" key="RollbackChangesDialog" timestamp="1595702094731">
<screen x="0" y="0" width="1536" height="824" /> <screen x="0" y="0" width="1536" height="824" />
</state> </state>
<state x="589" y="114" key="RollbackChangesDialog/0.0.1536.824@0.0.1536.824" timestamp="1595702094731" /> <state x="589" y="114" key="RollbackChangesDialog/0.0.1536.824@0.0.1536.824" timestamp="1595702094731" />
<state x="107" y="201" key="com.intellij.ide.util.TipDialog" timestamp="1596265214150"> <state x="107" y="201" key="com.intellij.ide.util.TipDialog" timestamp="1596295681139">
<screen x="0" y="0" width="1536" height="824" /> <screen x="0" y="0" width="1536" height="824" />
</state> </state>
<state x="107" y="201" key="com.intellij.ide.util.TipDialog/0.0.1536.824@0.0.1536.824" timestamp="1596265214150" /> <state x="107" y="201" key="com.intellij.ide.util.TipDialog/0.0.1536.824@0.0.1536.824" timestamp="1596295681139" />
<state x="209" y="33" key="new project wizard" timestamp="1593723788909"> <state x="209" y="33" key="new project wizard" timestamp="1593723788909">
<screen x="0" y="0" width="1536" height="824" /> <screen x="0" y="0" width="1536" height="824" />
</state> </state>
......
package UI.GameState; package UI.GameState;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class EnemyTank extends TankState { public class EnemyTank extends TankState {
public EnemyTank(WallState walls) { public EnemyTank(WallState walls) {
super(walls); super(walls);
myListener.add(new KeyHandler2());
}
/**
* The keyboard handler.
*/
class KeyHandler2 extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_E:
keyE = true;
break;
case KeyEvent.VK_D:
keyD = true;
break;
case KeyEvent.VK_S:
keyS = true;
break;
case KeyEvent.VK_F:
keyF = true;
break;
case KeyEvent.VK_Q:
keyQ = true;
break;
case KeyEvent.VK_ESCAPE:
gameOver = true;
break;
}
}
@Override
public void keyReleased(KeyEvent e) {
switch (e.getKeyCode())
{
case KeyEvent.VK_E:
keyE = false;
break;
case KeyEvent.VK_D:
keyD = false;
break;
case KeyEvent.VK_S:
keyS = false;
break;
case KeyEvent.VK_F:
keyF = false;
break;
case KeyEvent.VK_Q:
keyQ = false;
break;
}
} }
public void update() {
} }
} }
package UI.GameState; package UI.GameState;
import UI.LogIn.Game.MyListener;
import UI.GameFrame;
import UI.MyListener;
import java.awt.event.*; import java.awt.event.*;
public class MyTank extends TankState { public class MyTank extends TankState {
private boolean keyUP, keyDOWN, keyRIGHT, keyLEFT, keySpace;
private boolean mousePress;
private int mouseX, mouseY;
private MyListener myListener = new MyListener();
private KeyHandler1 keyHandler1 = new KeyHandler1();
private MouseHandler mouseHandler; private MouseHandler mouseHandler;
public MyTank(WallState walls) { public MyTank(WallState walls) {
super(walls); super(walls);
myListener.add(keyHandler1); myListener.add(new KeyHandler1());
keyUP = false;
keyDOWN = false;
keyRIGHT = false;
keyLEFT = false;
keySpace = false;
mousePress = false;
mouseX = 0;
mouseY = 0;
mouseHandler = new MouseHandler(); mouseHandler = new MouseHandler();
} }
/**
* The method which updates the game state.
*/
public void update() {
//
// Update the state of all game elements
// based on user input and elapsed time ...
//
int lastx = locX,lasty = locY,lastr = rotate;
if (mousePress) {
locY = mouseY - diam / 2;
locX = mouseX - diam / 2;
}
if (keyUP) {
if (rotate == 0)
rotate = 360;
locY -= 8;
if ((rotate > 180 && rotate < 270)|| rotate ==180 )
rotate +=10;
else if ( (rotate > 270 && rotate < 360) || rotate == 360)
rotate -=10;
else
rotate =270;
}
if (keyDOWN) {
rotate = rotate % 360;
locY += 8;
if ( (rotate > 0 && rotate < 90 )|| rotate == 0)
rotate +=10;
else if ( (rotate > 90 && rotate <180 )|| rotate == 180 )
rotate -=10;
else
rotate = 90;
}
if (keyLEFT ){
rotate = rotate % 360;
locX -= 8;
if ( (rotate> 90 && rotate <180) || rotate== 90 )
rotate +=10;
else if ( (rotate >180 && rotate < 270) || rotate ==270 )
rotate -=10;
else
rotate =180;
}
if (keyRIGHT ) {
rotate = rotate % 360;
locX += 8;
if ( (rotate > 270 && rotate < 360) || rotate ==270 )
rotate +=10;
else if ( (rotate < 90 && rotate > 0) || rotate == 90 )
rotate -=10;
else
rotate = 0;
}
if (keySpace){
if (bullets.size() == 0 || System.currentTimeMillis() -bullets.get(bullets.size()-1).getCreateTime()>500 ) {
BulletState newBullet = new BulletState(walls, this);
bullets.add(newBullet);
}
}
if (walls.isWallForTank(locX,locY)){
locX = lastx;
locY = lasty;
}
locX = Math.max(locX, 0);
locX = Math.min(locX, GameFrame.GAME_WIDTH - diam);
locY = Math.max(locY, 0);
locY = Math.min(locY, GameFrame.GAME_HEIGHT - diam);
}
public MouseListener getMouseListener() { public MouseListener getMouseListener() {
return mouseHandler; return mouseHandler;
} }
......
package UI.GameState;
public class RewarsState {
}
package UI.GameState; package UI.GameState;
/*** In The Name of Allah ***/ /*** In The Name of Allah ***/
import UI.GameFrame;
import UI.MyListener;
import java.awt.event.*; import UI.LogIn.Game.GameFrame;
import UI.LogIn.Game.MyListener;
import java.awt.event.KeyListener;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Random;
/** /**
...@@ -15,7 +17,10 @@ import java.util.ArrayList; ...@@ -15,7 +17,10 @@ import java.util.ArrayList;
* @author Seyede zahra seyed fatehi * @author Seyede zahra seyed fatehi
*/ */
public class TankState { public class TankState {
protected MyListener myListener = new MyListener();
protected boolean keyUP, keyDOWN, keyRIGHT, keyLEFT, keySpace , keyS , keyD, keyF , keyE , keyQ;
protected boolean mousePress;
protected int mouseX, mouseY;
public int locX, locY, diam, rotate; public int locX, locY, diam, rotate;
public boolean gameOver; public boolean gameOver;
protected WallState walls; protected WallState walls;
...@@ -25,14 +30,40 @@ public class TankState { ...@@ -25,14 +30,40 @@ public class TankState {
public TankState(WallState walls) { public TankState(WallState walls) {
this.walls = walls; this.walls = walls;
// Initialize the game state and all elements ... // Initialize the game state and all elements ...
locX = 100; // setFirstLocationOfTank();
locY = 100; locY = 120;
locX = 120;
rotate = 0; rotate = 0;
diam = 32; diam = 32;
keyUP = false;
keyDOWN = false;
keyRIGHT = false;
keyLEFT = false;
keySpace = false;
keyS = false;
keyD = false;
keyE = false;
keyF = false;
keyQ = false;
mousePress = false;
mouseX = 0;
mouseY = 0;
gameOver = false; gameOver = false;
} }
private void setFirstLocationOfTank() {
Random randomX = new Random();
Random randomY = new Random();
while (true) {
locX = randomX.nextInt(GameFrame.GAME_WIDTH);
locY = randomY.nextInt(GameFrame.GAME_HEIGHT);
System.out.println(locX +" "+locY);
if (!walls.isWallForTank(locX , locY) && locX > 10 && locY >10)
break;
}
}
...@@ -45,7 +76,84 @@ public class TankState { ...@@ -45,7 +76,84 @@ public class TankState {
} }
public MyListener getMyListener() { public MyListener getMyListener() {
return null; return myListener;
}
/**
* The method which updates the game state.
*/
public void update() {
//
// Update the state of all game elements
// based on user input and elapsed time ...
//
int lastx = locX,lasty = locY,lastr = rotate;
if (mousePress) {
locY = mouseY - diam / 2;
locX = mouseX - diam / 2;
}
if (keyUP || keyE) {
if (rotate == 0)
rotate = 360;
locY -= 8;
if ((rotate > 180 && rotate < 270)|| rotate ==180 )
rotate +=10;
else if ( (rotate > 270 && rotate < 360) || rotate == 360)
rotate -=10;
else
rotate =270;
}
if (keyDOWN || keyD) {
rotate = rotate % 360;
locY += 8;
if ( (rotate > 0 && rotate < 90 )|| rotate == 0)
rotate +=10;
else if ( (rotate > 90 && rotate <180 )|| rotate == 180 )
rotate -=10;
else
rotate = 90;
}
if (keyLEFT || keyS ){
rotate = rotate % 360;
locX -= 8;
if ( (rotate> 90 && rotate <180) || rotate== 90 )
rotate +=10;
else if ( (rotate >180 && rotate < 270) || rotate ==270 )
rotate -=10;
else
rotate =180;
}
if (keyRIGHT || keyF) {
rotate = rotate % 360;
locX += 8;
if ( (rotate > 270 && rotate < 360) || rotate ==270 )
rotate +=10;
else if ( (rotate < 90 && rotate > 0) || rotate == 90 )
rotate -=10;
else
rotate = 0;
}
if (keySpace || keyQ){
if (bullets.size() == 0 || System.currentTimeMillis() -bullets.get(bullets.size()-1).getCreateTime()>500 ) {
BulletState newBullet = new BulletState(walls, this);
bullets.add(newBullet);
}
}
if (walls.isWallForTank(locX,locY)){
locX = lastx;
locY = lasty;
}
locX = Math.max(locX, 0);
locX = Math.min(locX, GameFrame.GAME_WIDTH - diam);
locY = Math.max(locY, 0);
locY = Math.min(locY, GameFrame.GAME_HEIGHT - diam);
} }
} }
package UI; package UI.LogIn.Game;
import UI.GameState.*; import UI.GameState.*;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.image.BufferStrategy; import java.awt.image.BufferStrategy;
import java.util.ArrayList;
public class GameFrame extends JFrame { public class GameFrame extends JFrame {
...@@ -42,13 +41,13 @@ public class GameFrame extends JFrame { ...@@ -42,13 +41,13 @@ public class GameFrame extends JFrame {
* Game rendering with triple-buffering using BufferStrategy. * Game rendering with triple-buffering using BufferStrategy.
*/ */
public void render(MyTank myTank, EnemyTank enemyTank, ArrayList<BulletState> bullets) { public void render(MyTank myTank, EnemyTank enemyTank) {
do { do {
do { do {
Graphics2D graphics = (Graphics2D) bufferStrategy.getDrawGraphics(); Graphics2D graphics = (Graphics2D) bufferStrategy.getDrawGraphics();
try { try {
rendering.doRendering(graphics, myTank , enemyTank, bullets ); rendering.doRendering(graphics, myTank , enemyTank );
} finally { } finally {
// Dispose the graphics // Dispose the graphics
graphics.dispose(); graphics.dispose();
......
package UI; package UI.LogIn.Game;
/*** In The Name of Allah ***/ /*** In The Name of Allah ***/
...@@ -56,7 +57,7 @@ public class GameLoop implements Runnable { ...@@ -56,7 +57,7 @@ public class GameLoop implements Runnable {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
myTank.update(); myTank.update();
enemyTank.update(); enemyTank.update();
canvas.render(myTank , enemyTank , myTank.getBullets()); canvas.render(myTank , enemyTank);
long delay = (1000 / FPS) - (System.currentTimeMillis() - start); long delay = (1000 / FPS) - (System.currentTimeMillis() - start);
if (delay > 0) if (delay > 0)
Thread.sleep(delay); Thread.sleep(delay);
......
package UI; package UI.LogIn.Game;
import java.awt.event.KeyAdapter; import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
......
package UI; package UI.LogIn.Game;
import UI.GameState.BulletState; import UI.GameState.BulletState;
import UI.GameState.EnemyTank; import UI.GameState.EnemyTank;
import UI.GameState.MyTank; import UI.GameState.MyTank;
import UI.GameState.TankState;
import UI.LogIn.Setting; import UI.LogIn.Setting;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import java.awt.*; import java.awt.*;
...@@ -17,6 +17,7 @@ public class Rendering { ...@@ -17,6 +17,7 @@ public class Rendering {
private Image image1; private Image image1;
private Image image2; private Image image2;
private Image explosionImg;
public static final int GAME_HEIGHT = 720; // 720p game resolution public static final int GAME_HEIGHT = 720; // 720p game resolution
public static final int GAME_WIDTH = 16 * GAME_HEIGHT / 9; // wide aspect ratio public static final int GAME_WIDTH = 16 * GAME_HEIGHT / 9; // wide aspect ratio
int lastRender = -1; int lastRender = -1;
...@@ -32,7 +33,7 @@ public class Rendering { ...@@ -32,7 +33,7 @@ public class Rendering {
*/ */
// Draw all game elements according // Draw all game elements according
// to the game 'state' using 'g2d' ... // to the game 'state' using 'g2d' ...
public void doRendering(Graphics2D g2d, MyTank myTank, EnemyTank enemyTank, ArrayList<BulletState> bullets) { public void doRendering(Graphics2D g2d, MyTank myTank, EnemyTank enemyTank) {
// Draw background // Draw background
myTank.getWalls().paintComponent(g2d); myTank.getWalls().paintComponent(g2d);
enemyTank.getWalls().paintComponent(g2d); enemyTank.getWalls().paintComponent(g2d);
...@@ -43,15 +44,23 @@ public class Rendering { ...@@ -43,15 +44,23 @@ public class Rendering {
} catch (IOException e) { } catch (IOException e) {
System.out.println(e); System.out.println(e);
} }
// Rotation information // Rotation information
double rotationRequired = Math.toRadians (myTank.rotate); double rotationRequired = Math.toRadians (myTank.rotate);
BufferedImage image11 = (BufferedImage)image1; BufferedImage image11 = (BufferedImage)image1;
double locationX = image11.getWidth() / 2; double locationX = image11.getWidth() / 2;
double locationY = image11.getHeight() / 2; double locationY = image11.getHeight() / 2;
AffineTransform tx = AffineTransform.getRotateInstance(rotationRequired, locationX, locationY); AffineTransform tx = AffineTransform.getRotateInstance(rotationRequired, locationX, locationY);
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR); AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
// Drawing the rotated image at the required drawing locations // Drawing the rotated image at the required drawing locations
int lastX1 = 0;
int lastY1 = 0;
if (myTank.gameOver == false && enemyTank.gameOver == false) {
g2d.drawImage(op.filter( image11, null),myTank.locX , myTank.locY , null); g2d.drawImage(op.filter( image11, null),myTank.locX , myTank.locY , null);
lastX1 = enemyTank.locX;
lastY1 = enemyTank.locY;
}
else
g2d.drawImage(op.filter( image11, null),lastX1 , lastY1 , null);
//Draw enemy tank //Draw enemy tank
try { try {
...@@ -59,13 +68,29 @@ public class Rendering { ...@@ -59,13 +68,29 @@ public class Rendering {
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
g2d.drawImage(image2 , enemyTank.locX+30 , enemyTank.locY+30 , null); // Rotation information
double rotationEnemy = Math.toRadians (enemyTank.rotate);
BufferedImage image22 = (BufferedImage)image2;
double locationX2 = image22.getWidth() / 2;
double locationY2 = image22.getHeight() / 2;
AffineTransform tx2 = AffineTransform.getRotateInstance(rotationEnemy, locationX2, locationY2);
AffineTransformOp op2 = new AffineTransformOp(tx2, AffineTransformOp.TYPE_BILINEAR);
// Drawing the rotated image at the required drawing locations
int lastX2 = 0;
int lastY2 = 0;
if (myTank.gameOver == false && enemyTank.gameOver == false) {
g2d.drawImage(op2.filter(image22, null), enemyTank.locX, enemyTank.locY, null);
lastX2 = enemyTank.locX;
lastY2 = enemyTank.locY;
}
else
g2d.drawImage(op2.filter( image22, null),lastX2 , lastY2 , null);
//Draw bullet //Draw myTank bullet
if (bullets.size() != 0) { if (myTank.getBullets().size() != 0 && myTank.gameOver == false && enemyTank.gameOver == false) {
for (BulletState bullet : bullets) { for (BulletState bullet : myTank.getBullets()) {
if (System.currentTimeMillis() - bullet.getCreateTime() > 4000) { if (System.currentTimeMillis() - bullet.getCreateTime() > 4000) {
// bullets.remove(bullet); // myTank.getBullets().remove(bullet);
continue; continue;
} }
bullet.update(); bullet.update();
...@@ -74,6 +99,62 @@ public class Rendering { ...@@ -74,6 +99,62 @@ public class Rendering {
} }
} }
//Draw enemy tank bullet
if (enemyTank.getBullets().size() != 0 && myTank.gameOver == false && enemyTank.gameOver == false) {
for (BulletState bullet : enemyTank.getBullets()) {
if (System.currentTimeMillis() - bullet.getCreateTime() > 4000) {
//enemyTank.getBullets().remove(bullet);
continue;
}
bullet.update();
g2d.setColor(Color.GRAY);
g2d.fillOval(bullet.locX, bullet.locY, bullet.diam, bullet.diam);
}
}
//Draw explosion and ruin the tank
ArrayList<BulletState> allBullets = new ArrayList<>();
for (int i = myTank.getBullets().size() - 1 ; i >= 0 ; i -- ){
if (System.currentTimeMillis() - myTank.getBullets().get(i).getCreateTime() >4000){
break;
}
System.out.println(i);
allBullets.add(myTank.getBullets().get(i));
}
for (int i = enemyTank.getBullets().size() - 1 ; i >= 0 ; i -- ){
if (System.currentTimeMillis() - enemyTank.getBullets().get(i).getCreateTime() >4000){
break;
}
allBullets.add(enemyTank.getBullets().get(i));
}
try {
explosionImg = ImageIO.read(new File("C:\\Users\\Lenovo\\IdeaProjects\\JTankTrouble\\pics\\explosion3.png"));
} catch (IOException e) {
System.out.println(e);
}
long startExploding ;
for (BulletState bullet : allBullets){//check my tank
if (bullet.locX >myTank.locX - 20 && bullet.locX <myTank.locX + 20 && bullet.locY >myTank.locY - 10&& bullet.locY <myTank.locY + 10){
startExploding = System.currentTimeMillis();
if (System.currentTimeMillis() - startExploding < 1000) {
g2d.drawImage(op2.filter((BufferedImage) explosionImg, null), myTank.locX, myTank.locY, null);
myTank.gameOver = true;
}
}
}
for (BulletState bullet : allBullets){//check enemy tank
if (bullet.locX >enemyTank.locX - 20 && bullet.locX <enemyTank.locX + 20 && bullet.locY >enemyTank.locY - 10&& bullet.locY <enemyTank.locY + 10){
startExploding = System.currentTimeMillis();
System.out.println("ex");
if (System.currentTimeMillis() - startExploding < 1000) {
g2d.drawImage(op2.filter((BufferedImage) explosionImg, null), enemyTank.locX, enemyTank.locY, null);
enemyTank.gameOver = true;
}
}
}
// Print FPS info // Print FPS info
long currentRender = System.currentTimeMillis(); long currentRender = System.currentTimeMillis();
......
package UI; package UI.LogIn.Game;
/*** In The Name of Allah ***/ /*** In The Name of Allah ***/
......
...@@ -16,7 +16,7 @@ public class Main { ...@@ -16,7 +16,7 @@ public class Main {
// if (initialFrame.isClickOnPlay()) { // if (initialFrame.isClickOnPlay()) {
// After the player clicks 'PLAY' ... // After the player clicks 'PLAY' ...
Mainframe mainframe = new Mainframe("Trouble Tank"); MainFrame mainframe = new MainFrame("Trouble Tank");
// } // }
} }
......
...@@ -3,8 +3,8 @@ package UI.LogIn; ...@@ -3,8 +3,8 @@ package UI.LogIn;
import javax.swing.*; import javax.swing.*;
import java.io.IOException; import java.io.IOException;
class Mainframe extends JFrame { class MainFrame extends JFrame {
public Mainframe(String gameName) throws IOException { public MainFrame(String gameName) throws IOException {
setTitle(gameName); setTitle(gameName);
MainPanel mainPanel = new MainPanel(); MainPanel mainPanel = new MainPanel();
add(mainPanel); add(mainPanel);
......
package UI.LogIn; package UI.LogIn;
import UI.GameFrame; import UI.LogIn.Game.GameFrame;
import UI.GameLoop; import UI.LogIn.Game.GameLoop;
import UI.ThreadPool; import UI.LogIn.Game.ThreadPool;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import javax.swing.*; import javax.swing.*;
......
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