Commit 1d67ac9f authored by 9731301's avatar 9731301

add inheritance for tanks and adjust walls

parent 24a8caf2
No related merge requests found
...@@ -2,20 +2,26 @@ ...@@ -2,20 +2,26 @@
<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/MyTank.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$/maps/map1.txt" beforeDir="false" afterPath="$PROJECT_DIR$/maps/map1.txt" 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$/maps/map2.txt" beforeDir="false" afterPath="$PROJECT_DIR$/maps/map2.txt" afterDir="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$/maps/map3.txt" beforeDir="false" afterPath="$PROJECT_DIR$/maps/map3.txt" afterDir="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" afterPath="$PROJECT_DIR$/out/production/JTankTrouble/UI/GameState/BulletState$KeyHandler2.class" afterDir="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" afterPath="$PROJECT_DIR$/out/production/JTankTrouble/UI/GameState/BulletState.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/GameState/TankState$KeyHandler1.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/JTankTrouble/UI/GameState/TankState$KeyHandler1.class" afterDir="false" /> <change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/GameState/TankState$KeyHandler1.class" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/GameState/TankState$MouseHandler.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/JTankTrouble/UI/GameState/TankState$MouseHandler.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/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/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/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/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/Rendering.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/JTankTrouble/UI/Rendering.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/UI/GameFrame.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/UI/GameFrame.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/UI/GameLoop.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/UI/GameLoop.java" afterDir="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$/src/UI/GameState/BulletState.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/UI/GameState/BulletState.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/GameState/WallState.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/UI/GameState/WallState.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/Rendering.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/UI/Rendering.java" afterDir="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" />
...@@ -136,7 +142,8 @@ ...@@ -136,7 +142,8 @@
<workItem from="1596042670145" duration="24545000" /> <workItem from="1596042670145" duration="24545000" />
<workItem from="1596134290663" duration="37548000" /> <workItem from="1596134290663" duration="37548000" />
<workItem from="1596205210217" duration="16177000" /> <workItem from="1596205210217" duration="16177000" />
<workItem from="1596226360961" duration="600000" /> <workItem from="1596226360961" duration="5478000" />
<workItem from="1596265047938" duration="8448000" />
</task> </task>
<servers /> <servers />
</component> </component>
...@@ -152,10 +159,10 @@ ...@@ -152,10 +159,10 @@
<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="1596220903263" />
<state x="566" y="298" width="403" height="237" key="#com.intellij.refactoring.move.moveClassesOrPackages.MoveClassesOrPackagesDialog.classes" timestamp="1596028833005"> <state x="566" y="298" width="403" height="237" key="#com.intellij.refactoring.move.moveClassesOrPackages.MoveClassesOrPackagesDialog.classes" timestamp="1596270751857">
<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="1596028833005" /> <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="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>
...@@ -164,30 +171,30 @@ ...@@ -164,30 +171,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="1596222336558"> <state width="1493" height="104" key="GridCell.Tab.0.bottom" timestamp="1596274226921">
<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="1596222336558" /> <state width="1493" height="104" key="GridCell.Tab.0.bottom/0.0.1536.824@0.0.1536.824" timestamp="1596274226921" />
<state width="1493" height="104" key="GridCell.Tab.0.center" timestamp="1596222336558"> <state width="1493" height="104" key="GridCell.Tab.0.center" timestamp="1596274226921">
<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="1596222336558" /> <state width="1493" height="104" key="GridCell.Tab.0.center/0.0.1536.824@0.0.1536.824" timestamp="1596274226921" />
<state width="1493" height="104" key="GridCell.Tab.0.left" timestamp="1596222336557"> <state width="1493" height="104" key="GridCell.Tab.0.left" timestamp="1596274226920">
<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="1596222336557" /> <state width="1493" height="104" key="GridCell.Tab.0.left/0.0.1536.824@0.0.1536.824" timestamp="1596274226920" />
<state width="1493" height="104" key="GridCell.Tab.0.right" timestamp="1596222336558"> <state width="1493" height="104" key="GridCell.Tab.0.right" timestamp="1596274226921">
<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="1596222336558" /> <state width="1493" height="104" key="GridCell.Tab.0.right/0.0.1536.824@0.0.1536.824" timestamp="1596274226921" />
<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="1596226398477"> <state x="107" y="201" key="com.intellij.ide.util.TipDialog" timestamp="1596265214150">
<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="1596226398477" /> <state x="107" y="201" key="com.intellij.ide.util.TipDialog/0.0.1536.824@0.0.1536.824" timestamp="1596265214150" />
<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>
......
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
File added
File added
File added
File deleted
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
package UI; package UI;
import UI.GameState.BulletState; import UI.GameState.*;
import UI.GameState.TankState;
import UI.GameState.WallState;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.image.BufferStrategy; import java.awt.image.BufferStrategy;
...@@ -43,14 +42,13 @@ public class GameFrame extends JFrame { ...@@ -43,14 +42,13 @@ public class GameFrame extends JFrame {
* Game rendering with triple-buffering using BufferStrategy. * Game rendering with triple-buffering using BufferStrategy.
*/ */
public void render(TankState tankState , ArrayList<BulletState> bullets) { public void render(MyTank myTank, EnemyTank enemyTank, ArrayList<BulletState> bullets) {
do { do {
do { do {
Graphics2D graphics = (Graphics2D) bufferStrategy.getDrawGraphics(); Graphics2D graphics = (Graphics2D) bufferStrategy.getDrawGraphics();
try { try {
rendering.doRendering(graphics, tankState , bullets ); rendering.doRendering(graphics, myTank , enemyTank, bullets );
} finally { } finally {
// Dispose the graphics // Dispose the graphics
graphics.dispose(); graphics.dispose();
......
...@@ -3,6 +3,8 @@ package UI; ...@@ -3,6 +3,8 @@ package UI;
import UI.GameState.BulletState; import UI.GameState.BulletState;
import UI.GameState.EnemyTank;
import UI.GameState.MyTank;
import UI.GameState.TankState; import UI.GameState.TankState;
/** /**
...@@ -28,7 +30,8 @@ public class GameLoop implements Runnable { ...@@ -28,7 +30,8 @@ public class GameLoop implements Runnable {
public static final int FPS = 30; public static final int FPS = 30;
private GameFrame canvas; private GameFrame canvas;
private TankState tankState; private MyTank myTank;
private EnemyTank enemyTank;
public GameLoop(GameFrame frame) { public GameLoop(GameFrame frame) {
canvas = frame; canvas = frame;
} }
...@@ -38,10 +41,11 @@ public class GameLoop implements Runnable { ...@@ -38,10 +41,11 @@ public class GameLoop implements Runnable {
*/ */
public void init() { public void init() {
// Perform all initializations ... // Perform all initializations ...
tankState = new TankState(canvas.wallState); myTank = new MyTank(canvas.wallState);
canvas.addKeyListener(tankState.getMyListener()); enemyTank = new EnemyTank(canvas.wallState);
canvas.addMouseListener(tankState.getMouseListener()); canvas.addKeyListener(myTank.getMyListener());
canvas.addMouseMotionListener(tankState.getMouseMotionListener()); canvas.addMouseListener(myTank.getMouseListener());
canvas.addMouseMotionListener(myTank.getMouseMotionListener());
} }
@Override @Override
...@@ -50,8 +54,9 @@ public class GameLoop implements Runnable { ...@@ -50,8 +54,9 @@ public class GameLoop implements Runnable {
while (!gameOver) { while (!gameOver) {
try { try {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
tankState.update(); myTank.update();
canvas.render(tankState , tankState.getBullets()); enemyTank.update();
canvas.render(myTank , enemyTank , myTank.getBullets());
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.GameState; package UI.GameState;
import UI.MyListener;
import java.awt.event.KeyAdapter; import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
...@@ -28,17 +26,16 @@ public class BulletState { ...@@ -28,17 +26,16 @@ public class BulletState {
} }
public void update(){ public void update(){
locY += 10*Math.sin(Math.toRadians(angle) ) ; if (walls.getWallForBullet(locX , locY) != null) {
locX += 10*Math.cos(Math.toRadians(angle) ) ; if (walls.getWallForBullet(locX, locY).isHorizontal()) {
angle = 360 - angle;
for (WallState.WallData wall : walls.getWalls()){
if (wall.isWall(locX , locY) && wall.isHorizontal()) {
locY -= 10 * Math.sin(Math.toRadians(angle));
} }
if (wall.isWall(locX , locY)&& wall.isVertical()){ if (walls.getWallForBullet(locX, locY).isVertical()) {
locX-= 10*Math.cos(Math.toRadians(angle) ) ; angle = 180 - angle;
} }
} }
locY += 10*Math.sin(Math.toRadians(angle) ) ;
locX += 10*Math.cos(Math.toRadians(angle) ) ;
} }
......
package UI.GameState;
public class EnemyTank extends TankState {
public EnemyTank(WallState walls) {
super(walls);
}
public void update() {
}
}
package UI.GameState;
import UI.GameFrame;
import UI.MyListener;
import java.awt.event.*;
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;
public MyTank(WallState walls) {
super(walls);
myListener.add(keyHandler1);
keyUP = false;
keyDOWN = false;
keyRIGHT = false;
keyLEFT = false;
keySpace = false;
mousePress = false;
mouseX = 0;
mouseY = 0;
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() {
return mouseHandler;
}
public MouseMotionListener getMouseMotionListener() {
return mouseHandler;
}
public MyListener getMyListener() {
return myListener;
}
/**
* The keyboard handler.
*/
class KeyHandler1 extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
keyUP = true;
break;
case KeyEvent.VK_DOWN:
keyDOWN = true;
break;
case KeyEvent.VK_LEFT:
keyLEFT = true;
break;
case KeyEvent.VK_RIGHT:
keyRIGHT = true;
break;
case KeyEvent.VK_SPACE:
keySpace = true;
break;
case KeyEvent.VK_ESCAPE:
gameOver = true;
break;
}
}
@Override
public void keyReleased(KeyEvent e) {
switch (e.getKeyCode())
{
case KeyEvent.VK_UP:
keyUP = false;
break;
case KeyEvent.VK_DOWN:
keyDOWN = false;
break;
case KeyEvent.VK_LEFT:
keyLEFT = false;
break;
case KeyEvent.VK_RIGHT:
keyRIGHT = false;
break;
case KeyEvent.VK_SPACE:
keySpace = false;
break;
}
}
}
/**
* The mouse handler.
*/
class MouseHandler extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
mousePress = true;
}
@Override
public void mouseReleased(MouseEvent e) {
mousePress = false;
}
@Override
public void mouseDragged(MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
}
}
}
...@@ -18,19 +18,11 @@ public class TankState { ...@@ -18,19 +18,11 @@ public class TankState {
public int locX, locY, diam, rotate; public int locX, locY, diam, rotate;
public boolean gameOver; public boolean gameOver;
protected WallState walls;
private boolean keyUP, keyDOWN, keyRIGHT, keyLEFT, keySpace; protected ArrayList<BulletState> bullets = new ArrayList<>();
private boolean mousePress;
private int mouseX, mouseY;
private MyListener myListener = new MyListener();
private KeyHandler1 keyHandler1 = new KeyHandler1();
private MouseHandler mouseHandler;
private WallState walls;
private ArrayList<BulletState> bullets = new ArrayList<>();
public TankState(WallState walls) { public TankState(WallState walls) {
myListener.add(keyHandler1);
this.walls = walls; this.walls = walls;
// Initialize the game state and all elements ... // Initialize the game state and all elements ...
locX = 100; locX = 100;
...@@ -39,96 +31,9 @@ public class TankState { ...@@ -39,96 +31,9 @@ public class TankState {
diam = 32; diam = 32;
gameOver = false; gameOver = false;
keyUP = false;
keyDOWN = false;
keyRIGHT = false;
keyLEFT = false;
keySpace = false;
mousePress = false;
mouseX = 0;
mouseY = 0;
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.isWall(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 WallState getWalls() { public WallState getWalls() {
...@@ -139,94 +44,8 @@ public class TankState { ...@@ -139,94 +44,8 @@ public class TankState {
return bullets; return bullets;
} }
public MouseListener getMouseListener() {
return mouseHandler;
}
public MouseMotionListener getMouseMotionListener() {
return mouseHandler;
}
public MyListener getMyListener() { public MyListener getMyListener() {
return myListener; return null;
}
/**
* The keyboard handler.
*/
class KeyHandler1 extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
keyUP = true;
break;
case KeyEvent.VK_DOWN:
keyDOWN = true;
break;
case KeyEvent.VK_LEFT:
keyLEFT = true;
break;
case KeyEvent.VK_RIGHT:
keyRIGHT = true;
break;
case KeyEvent.VK_SPACE:
keySpace = true;
break;
case KeyEvent.VK_ESCAPE:
gameOver = true;
break;
}
}
@Override
public void keyReleased(KeyEvent e) {
switch (e.getKeyCode())
{
case KeyEvent.VK_UP:
keyUP = false;
break;
case KeyEvent.VK_DOWN:
keyDOWN = false;
break;
case KeyEvent.VK_LEFT:
keyLEFT = false;
break;
case KeyEvent.VK_RIGHT:
keyRIGHT = false;
break;
case KeyEvent.VK_SPACE:
keySpace = false;
break;
}
}
}
/**
* The mouse handler.
*/
class MouseHandler extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
mousePress = true;
}
@Override
public void mouseReleased(MouseEvent e) {
mousePress = false;
}
@Override
public void mouseDragged(MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
}
} }
} }
...@@ -7,6 +7,7 @@ import java.io.IOException; ...@@ -7,6 +7,7 @@ import java.io.IOException;
import java.nio.charset.StandardCharsets; 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.sql.Struct;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -78,75 +79,16 @@ public class WallState { ...@@ -78,75 +79,16 @@ public class WallState {
} }
y += h; y += h;
} }
/* int start = 10;
int startTop = 30;
for (int r = 0; r < numRows; r++) {
String rowChars = lines[r];
for (int c = 0; c < numColumns; c++) {
if (rowChars.charAt(c)=='0') continue;
if (c==0 && r==0 ){
if (rowChars.charAt(c+1)=='1') draw(g,start,startTop,w+(w/2),wallWidth);
if (lines[r+1].charAt(c)=='1') draw(g,start,start,wallWidth,h);
continue;
}
if (r==0){
if (lines[r+1].charAt(c)=='1'){
if (c==numColumns-1) {
draw(g,frameX-start-wallWidth,startTop,wallWidth,h);
}else{
draw(g,(c*w)+(w/2)+start,startTop,wallWidth,h);
}
}
}
if ((c<numColumns-1 && rowChars.charAt(c+1)=='1') || (c>0 && rowChars.charAt(c-1)=='1')){ //horizontal
int y = (r*h) + startTop;
int x = start;
if (r == numRows -1)
y = frameY - start - wallWidth;//last row
if (r<numRows-1 && r>0)
y += h/4;
if (c>0)
x = (w*c)+(w/2)+start;
if (r==numRows-1 && c>0)
x -=w/2;
draw(g,x,y,w,wallWidth);
}
if ((r>0 && lines[r-1].charAt(c)=='1') || (r>0 && r<numRows-1 && lines[r+1].charAt(c)=='1')) { //vertical
int x = start;
int y = r * h +start;
int th = h;
if (c>0)
x = (c*w) + (w/2) + start;
if (r==numRows-1)
th += startTop*2;
if (c==numColumns-1)//last column
x = frameX - start - wallWidth;
draw(g,x, y, wallWidth, th);
}
}
}
*/
} }
private void drawAndSaveWalls(Graphics g2d,int x,int y , int r , int c ,Image image){ private void drawAndSaveWalls(Graphics g2d,int x,int y , int r , int c ,Image image){
WallData wall = new WallData(x , y , w, h); WallData wall = new WallData(x , y);
if ((c<numColumns-1 && lines[r].charAt(c+1)=='1') || (c>0 && lines[r].charAt(c-1)=='1')){ //horizontal if ((c<numColumns-1 && lines[r].charAt(c+1)=='1') || (c>0 && lines[r].charAt(c-1)=='1')||(c<numColumns-1 && lines[r].charAt(c+1)=='2') || (c>0 && lines[r].charAt(c-1)=='2')){ //horizontal
wall.setHorizontal(true); wall.setHorizontal(true);
} }
if ((r>0 && lines[r-1].charAt(c)=='1') || (r>0 && r<numRows-1 && lines[r+1].charAt(c)=='1')) { //vertical if ((r>0 && lines[r-1].charAt(c)=='1') || (r>0 && r<numRows-1 && lines[r+1].charAt(c)=='1')||(r>0 && lines[r-1].charAt(c)=='2') || (r>0 && r<numRows-1 && lines[r+1].charAt(c)=='2')) { //vertical
wall.setVertical(true); wall.setVertical(true);
} }
walls.add(wall); walls.add(wall);
...@@ -157,33 +99,47 @@ public class WallState { ...@@ -157,33 +99,47 @@ public class WallState {
g2d.drawImage(image , x , y , null); g2d.drawImage(image , x , y , null);
} }
public boolean isWall(int x,int y){ public WallData getWallForBullet(int x,int y){
for(WallData d : walls){ for(WallData d : walls){
if (d.isWall(x,y)) return true; if (d.isWallForBullet(x,y))
return d;
}
return null;
}
public boolean isWallForTank(int x,int y){
for(WallData d : walls){
if (d.isWallForTank(x,y))
return true;
} }
return false; return false;
} }
public ArrayList<WallData> getWalls() { public ArrayList<WallData> getWalls() {
return walls; return walls;
} }
public class WallData { public class WallData {
private int x,y,w,h; private int x,y;
private boolean horizontal = false; private boolean horizontal = false;
private boolean vertical = false; private boolean vertical = false;
public WallData(int x,int y,int w,int h){ public WallData(int x,int y){
this.x = x; this.x = x;
this.y = y; this.y = y;
this.h = h;
this.w = w;
} }
public boolean isWall (int x,int y){ public boolean isWallForTank (int x,int y){
//BUG
return (x>this.x-w/2 && x<this.x+w && y>this.y-w/2 && y<this.y+h);
}
public boolean isWallForBullet (int x,int y){
//BUG //BUG
return (x>this.x && x<this.x+w && y>this.y && y<this.y+h); return (x>this.x-5 && x<this.x+w+5 && y>this.y-5 && y<this.y+h+5);
} }
public boolean isHorizontal() { public boolean isHorizontal() {
......
...@@ -82,7 +82,7 @@ public class SettingPanel extends JPanel { ...@@ -82,7 +82,7 @@ public class SettingPanel extends JPanel {
String[] options2 ={""}; String[] options2 ={""};
JComboBox comboBox2 = new JComboBox(options2); JComboBox comboBox2 = new JComboBox(options2);
JLabel tankColor = new JLabel("tank's color"); JLabel tankColor = new JLabel("tank's color");
String[] options3 ={"blue" , "green" , "black" , "white"}; String[] options3 ={"blue" , "green" ,"white"};
JComboBox comboBox3 = new JComboBox(options3); JComboBox comboBox3 = new JComboBox(options3);
image = ImageIO.read(new File(chosenTankPath)); image = ImageIO.read(new File(chosenTankPath));
ImageIcon imageIcon = new ImageIcon(image); ImageIcon imageIcon = new ImageIcon(image);
...@@ -95,7 +95,6 @@ public class SettingPanel extends JPanel { ...@@ -95,7 +95,6 @@ public class SettingPanel extends JPanel {
String s = (String) comboBox3.getSelectedItem(); String s = (String) comboBox3.getSelectedItem();
switch (s){ switch (s){
case "blue" : {chosenTankPath = "C:\\Users\\Lenovo\\IdeaProjects\\JTankTrouble\\pics\\tank_blue.png";break;} case "blue" : {chosenTankPath = "C:\\Users\\Lenovo\\IdeaProjects\\JTankTrouble\\pics\\tank_blue.png";break;}
case "black" : {chosenTankPath = "C:\\Users\\Lenovo\\IdeaProjects\\JTankTrouble\\pics\\tank_dark.png";break;}
case "green" : {chosenTankPath = "C:\\Users\\Lenovo\\IdeaProjects\\JTankTrouble\\pics\\tank_green.png";break;} case "green" : {chosenTankPath = "C:\\Users\\Lenovo\\IdeaProjects\\JTankTrouble\\pics\\tank_green.png";break;}
case "white" : chosenTankPath = "C:\\Users\\Lenovo\\IdeaProjects\\JTankTrouble\\pics\\tank_sand.png"; case "white" : chosenTankPath = "C:\\Users\\Lenovo\\IdeaProjects\\JTankTrouble\\pics\\tank_sand.png";
} }
......
package UI; package UI;
import UI.GameState.BulletState; import UI.GameState.BulletState;
import UI.GameState.EnemyTank;
import UI.GameState.MyTank;
import UI.GameState.TankState; import UI.GameState.TankState;
import UI.LogIn.Setting; import UI.LogIn.Setting;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
...@@ -30,27 +32,34 @@ public class Rendering { ...@@ -30,27 +32,34 @@ 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, TankState tankState , ArrayList<BulletState> bullets) { public void doRendering(Graphics2D g2d, MyTank myTank, EnemyTank enemyTank, ArrayList<BulletState> bullets) {
// Draw background // Draw background
tankState.getWalls().paintComponent(g2d); myTank.getWalls().paintComponent(g2d);
enemyTank.getWalls().paintComponent(g2d);
// Draw tank; // Draw my tank;
try { try {
image1 = ImageIO.read(new File(setting.getTankShapePath())); image1 = ImageIO.read(new File(setting.getTankShapePath()));
// image2 = ImageIO.read(new File("C:\\Users\\Lenovo\\IdeaProjects\\JTankTrouble\\pics\\tank_sand.png"));
} catch (IOException e) { } catch (IOException e) {
System.out.println(e); System.out.println(e);
} }
// Rotation information // Rotation information
double rotationRequired = Math.toRadians (tankState.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
g2d.drawImage(op.filter( image11, null),tankState.locX , tankState.locY , null); g2d.drawImage(op.filter( image11, null),myTank.locX , myTank.locY , null);
// g2d.drawImage(image2 , state.locX+30 , state.locY+30 , null);
//Draw enemy tank
try {
image2 = ImageIO.read(new File("C:\\Users\\Lenovo\\IdeaProjects\\JTankTrouble\\pics\\tank_dark.png"));
} catch (IOException e) {
e.printStackTrace();
}
g2d.drawImage(image2 , enemyTank.locX+30 , enemyTank.locY+30 , null);
//Draw bullet //Draw bullet
if (bullets.size() != 0) { if (bullets.size() != 0) {
...@@ -89,12 +98,12 @@ public class Rendering { ...@@ -89,12 +98,12 @@ public class Rendering {
lastRender = (int) currentRender; lastRender = (int) currentRender;
// Print user guide // Print user guide
String userGuide String userGuide
= "Use the MOUSE or ARROW KEYS to move the BALL. " = "Use the MOUSE or ARROW KEYS to move the TANK. "
+ "Press ESCAPE to end the game."; + "Press ESCAPE to end the game.";
g2d.setFont(g2d.getFont().deriveFont(18.0f)); g2d.setFont(g2d.getFont().deriveFont(18.0f));
g2d.drawString(userGuide, 10, GAME_HEIGHT - 10); g2d.drawString(userGuide, 10, GAME_HEIGHT - 10);
// Draw GAME OVER // Draw GAME OVER
if (tankState.gameOver) { if (myTank.gameOver) {
String str = "GAME OVER"; String str = "GAME OVER";
g2d.setColor(Color.WHITE); g2d.setColor(Color.WHITE);
g2d.setFont(g2d.getFont().deriveFont(Font.BOLD).deriveFont(64.0f)); g2d.setFont(g2d.getFont().deriveFont(Font.BOLD).deriveFont(64.0f));
......
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