Commit 05bedc2e authored by 9731301's avatar 9731301

complete shooting bullet

parent 36ee6d79
...@@ -2,21 +2,34 @@ ...@@ -2,21 +2,34 @@
<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/BulletState.java" afterDir="false" /> <change afterPath="$PROJECT_DIR$/src/UI/MyListener.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/UI/GameState/TankState.java" afterDir="false" /> <change afterPath="$PROJECT_DIR$/src/UI/Rendering.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/UI/GameState/WallState.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" afterPath="$PROJECT_DIR$/out/production/JTankTrouble/UI/GameFrame.class" 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$/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/GameState$KeyHandler.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$MouseHandler.class" beforeDir="false" /> <change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/GameState/TankState$KeyHandler.class" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/GameState.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/WallState$WallData.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/WallState.class" beforeDir="false" /> <change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/LogIn/MainPanel$1.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/JTankTrouble/UI/LogIn/MainPanel$1.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/LogIn/MainPanel$2.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/JTankTrouble/UI/LogIn/MainPanel$2.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/LogIn/MainPanel$3.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/JTankTrouble/UI/LogIn/MainPanel$3.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/LogIn/MainPanel.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/JTankTrouble/UI/LogIn/MainPanel.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/LogIn/Setting.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/JTankTrouble/UI/LogIn/Setting.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/LogIn/SettingPanel$1.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/JTankTrouble/UI/LogIn/SettingPanel$1.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/LogIn/SettingPanel$2.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/JTankTrouble/UI/LogIn/SettingPanel$2.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/LogIn/SettingPanel.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/JTankTrouble/UI/LogIn/SettingPanel.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pics/tank_blue.png" beforeDir="false" afterPath="$PROJECT_DIR$/pics/tank_blue.png" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pics/tank_dark.png" beforeDir="false" afterPath="$PROJECT_DIR$/pics/tank_dark.png" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pics/tank_green.png" beforeDir="false" afterPath="$PROJECT_DIR$/pics/tank_green.png" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pics/tank_sand.png" beforeDir="false" afterPath="$PROJECT_DIR$/pics/tank_sand.png" 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/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/GameLoop.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/UI/GameLoop.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/UI/GameState.java" 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$/src/UI/WallState.java" beforeDir="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/LogIn/MainPanel.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/UI/LogIn/MainPanel.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/UI/LogIn/Setting.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/UI/LogIn/Setting.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/UI/LogIn/SettingPanel.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/UI/LogIn/SettingPanel.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" />
...@@ -26,8 +39,8 @@ ...@@ -26,8 +39,8 @@
<component name="FileTemplateManagerImpl"> <component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES"> <option name="RECENT_TEMPLATES">
<list> <list>
<option value="Class" />
<option value="Enum" /> <option value="Enum" />
<option value="Class" />
</list> </list>
</option> </option>
</component> </component>
...@@ -47,6 +60,9 @@ ...@@ -47,6 +60,9 @@
<property name="aspect.path.notification.shown" value="true" /> <property name="aspect.path.notification.shown" value="true" />
</component> </component>
<component name="RecentsManager"> <component name="RecentsManager">
<key name="ExtractSuperBase.RECENT_KEYS">
<recent name="UI" />
</key>
<key name="CreateClassDialog.RecentsKey"> <key name="CreateClassDialog.RecentsKey">
<recent name="UI" /> <recent name="UI" />
</key> </key>
...@@ -118,7 +134,8 @@ ...@@ -118,7 +134,8 @@
<workItem from="1595889331611" duration="71000" /> <workItem from="1595889331611" duration="71000" />
<workItem from="1596026842920" duration="4069000" /> <workItem from="1596026842920" duration="4069000" />
<workItem from="1596042670145" duration="24545000" /> <workItem from="1596042670145" duration="24545000" />
<workItem from="1596134290663" duration="1190000" /> <workItem from="1596134290663" duration="37548000" />
<workItem from="1596205210217" duration="611000" />
</task> </task>
<servers /> <servers />
</component> </component>
...@@ -130,46 +147,46 @@ ...@@ -130,46 +147,46 @@
<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="1596080698011"> <state x="574" y="122" key="#com.intellij.ide.util.MemberChooser" timestamp="1596157366636">
<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="1596080698011" /> <state x="574" y="122" key="#com.intellij.ide.util.MemberChooser/0.0.1536.824@0.0.1536.824" timestamp="1596157366636" />
<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="1596028833005">
<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="1596028833005" />
<state x="611" y="310" key="#com.intellij.refactoring.move.moveClassesOrPackages.MoveClassesOrPackagesDialog.packages" timestamp="1596028822408"> <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>
<state x="611" y="310" key="#com.intellij.refactoring.move.moveClassesOrPackages.MoveClassesOrPackagesDialog.packages/0.0.1536.824@0.0.1536.824" timestamp="1596028822408" /> <state x="611" y="310" key="#com.intellij.refactoring.move.moveClassesOrPackages.MoveClassesOrPackagesDialog.packages/0.0.1536.824@0.0.1536.824" timestamp="1596137635178" />
<state x="593" y="0" key="CommitChangelistDialog2" timestamp="1595639194593"> <state x="593" y="0" key="CommitChangelistDialog2" timestamp="1595639194593">
<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="134" key="GridCell.Tab.0.bottom" timestamp="1596135091697"> <state width="1493" height="225" key="GridCell.Tab.0.bottom" timestamp="1596174690095">
<screen x="0" y="0" width="1536" height="824" /> <screen x="0" y="0" width="1536" height="824" />
</state> </state>
<state width="1493" height="134" key="GridCell.Tab.0.bottom/0.0.1536.824@0.0.1536.824" timestamp="1596135091697" /> <state width="1493" height="225" key="GridCell.Tab.0.bottom/0.0.1536.824@0.0.1536.824" timestamp="1596174690095" />
<state width="1493" height="134" key="GridCell.Tab.0.center" timestamp="1596135091697"> <state width="1493" height="225" key="GridCell.Tab.0.center" timestamp="1596174690095">
<screen x="0" y="0" width="1536" height="824" /> <screen x="0" y="0" width="1536" height="824" />
</state> </state>
<state width="1493" height="134" key="GridCell.Tab.0.center/0.0.1536.824@0.0.1536.824" timestamp="1596135091697" /> <state width="1493" height="225" key="GridCell.Tab.0.center/0.0.1536.824@0.0.1536.824" timestamp="1596174690095" />
<state width="1493" height="134" key="GridCell.Tab.0.left" timestamp="1596135091697"> <state width="1493" height="225" key="GridCell.Tab.0.left" timestamp="1596174690094">
<screen x="0" y="0" width="1536" height="824" /> <screen x="0" y="0" width="1536" height="824" />
</state> </state>
<state width="1493" height="134" key="GridCell.Tab.0.left/0.0.1536.824@0.0.1536.824" timestamp="1596135091697" /> <state width="1493" height="225" key="GridCell.Tab.0.left/0.0.1536.824@0.0.1536.824" timestamp="1596174690094" />
<state width="1493" height="134" key="GridCell.Tab.0.right" timestamp="1596135091697"> <state width="1493" height="225" key="GridCell.Tab.0.right" timestamp="1596174690095">
<screen x="0" y="0" width="1536" height="824" /> <screen x="0" y="0" width="1536" height="824" />
</state> </state>
<state width="1493" height="134" key="GridCell.Tab.0.right/0.0.1536.824@0.0.1536.824" timestamp="1596135091697" /> <state width="1493" height="225" key="GridCell.Tab.0.right/0.0.1536.824@0.0.1536.824" timestamp="1596174690095" />
<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="1596134473346"> <state x="107" y="201" key="com.intellij.ide.util.TipDialog" timestamp="1596205227078">
<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="1596134473346" /> <state x="107" y="201" key="com.intellij.ide.util.TipDialog/0.0.1536.824@0.0.1536.824" timestamp="1596205227078" />
<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>
......
pics/tank_blue.png

14.8 KB | W: | H:

pics/tank_blue.png

15 KB | W: | H:

pics/tank_blue.png
pics/tank_blue.png
pics/tank_blue.png
pics/tank_blue.png
  • 2-up
  • Swipe
  • Onion skin
pics/tank_dark.png

14.8 KB | W: | H:

pics/tank_dark.png

15 KB | W: | H:

pics/tank_dark.png
pics/tank_dark.png
pics/tank_dark.png
pics/tank_dark.png
  • 2-up
  • Swipe
  • Onion skin
pics/tank_green.png

14.8 KB | W: | H:

pics/tank_green.png

15 KB | W: | H:

pics/tank_green.png
pics/tank_green.png
pics/tank_green.png
pics/tank_green.png
  • 2-up
  • Swipe
  • Onion skin
pics/tank_sand.png

14.8 KB | W: | H:

pics/tank_sand.png

15 KB | W: | H:

pics/tank_sand.png
pics/tank_sand.png
pics/tank_sand.png
pics/tank_sand.png
  • 2-up
  • Swipe
  • Onion skin
...@@ -3,30 +3,20 @@ package UI; ...@@ -3,30 +3,20 @@ package UI;
import UI.GameState.BulletState; import UI.GameState.BulletState;
import UI.GameState.TankState; import UI.GameState.TankState;
import UI.GameState.WallState; import UI.GameState.WallState;
import UI.LogIn.Setting;
import javax.imageio.ImageIO;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferStrategy; import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
public class GameFrame extends JFrame { public class GameFrame extends JFrame {
private long lastRender; private long lastRender;
private BufferStrategy bufferStrategy; private BufferStrategy bufferStrategy;
private ArrayList<Float> fpsHistory; private Rendering rendering = new Rendering();
private Image image1;
private Image image2;
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
public WallState wallState = new WallState(GAME_WIDTH ,GAME_HEIGHT); public WallState wallState = new WallState(GAME_WIDTH ,GAME_HEIGHT);
private Setting setting = new Setting();
public GameFrame(String gameName) { public GameFrame(String gameName) {
lastRender = -1; lastRender = -1;
...@@ -35,8 +25,7 @@ public class GameFrame extends JFrame { ...@@ -35,8 +25,7 @@ public class GameFrame extends JFrame {
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setSize(GAME_WIDTH, GAME_HEIGHT); setSize(GAME_WIDTH, GAME_HEIGHT);
setResizable(false); setResizable(false);
int lastRender = -1;
fpsHistory = new ArrayList<>(100);
} }
/** /**
...@@ -54,19 +43,14 @@ public class GameFrame extends JFrame { ...@@ -54,19 +43,14 @@ public class GameFrame extends JFrame {
* Game rendering with triple-buffering using BufferStrategy. * Game rendering with triple-buffering using BufferStrategy.
*/ */
public void render(TankState tankState , BulletState bulletState) { public void render(TankState tankState , ArrayList<BulletState> bullets) {
do { do {
// The following loop ensures that the contents of the drawing buffer
// are consistent in case the underlying surface was recreated
do { do {
// Get a new graphics context every time through the loop
// to make sure the strategy is validated
Graphics2D graphics = (Graphics2D) bufferStrategy.getDrawGraphics(); Graphics2D graphics = (Graphics2D) bufferStrategy.getDrawGraphics();
try { try {
doRendering(graphics, tankState , bulletState); rendering.doRendering(graphics, tankState , bullets );
} finally { } finally {
// Dispose the graphics // Dispose the graphics
graphics.dispose(); graphics.dispose();
...@@ -84,74 +68,6 @@ public class GameFrame extends JFrame { ...@@ -84,74 +68,6 @@ public class GameFrame extends JFrame {
} while (bufferStrategy.contentsLost()); } while (bufferStrategy.contentsLost());
} }
/**
* Rendering all game elements based on the game state.
*/
// Draw all game elements according
// to the game 'state' using 'g2d' ...
private void doRendering(Graphics2D g2d, TankState tankState , BulletState bulletState) {
// Draw background
wallState.paintComponent(g2d);
// Draw tank;
try {
System.out.println("3"+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) {
System.out.println(e);
}
// Rotation information
double rotationRequired = Math.toRadians (tankState.rotate);
BufferedImage image11 = (BufferedImage)image1;
double locationX = image11.getWidth() / 2;
double locationY = image11.getHeight() / 2;
AffineTransform tx = AffineTransform.getRotateInstance(rotationRequired, locationX, locationY);
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
// Drawing the rotated image at the required drawing locations
g2d.drawImage(op.filter( image11, null),tankState.locX , tankState.locY , null);
// g2d.drawImage(image2 , state.locX+30 , state.locY+30 , null);
//Draw bullet
g2d.setColor(Color.GRAY);
g2d.fillOval(bulletState.locX, bulletState.locY, bulletState.diam, bulletState.diam);
// Print FPS info
long currentRender = System.currentTimeMillis();
if (lastRender > 0) {
fpsHistory.add(1000.0f / (currentRender - lastRender));
if (fpsHistory.size() > 100) {
fpsHistory.remove(0); // remove oldest
}
float avg = 0.0f;
for (float fps : fpsHistory) {
avg += fps;
}
avg /= fpsHistory.size();
String str = String.format("Average FPS = %.1f , Last Interval = %d ms",
avg, (currentRender - lastRender));
g2d.setColor(Color.CYAN);
g2d.setFont(g2d.getFont().deriveFont(18.0f));
int strWidth = g2d.getFontMetrics().stringWidth(str);
int strHeight = g2d.getFontMetrics().getHeight();
g2d.drawString(str, (GAME_WIDTH - strWidth) / 2, strHeight + 50);
}
lastRender = currentRender;
// Print user guide
String userGuide
= "Use the MOUSE or ARROW KEYS to move the BALL. "
+ "Press ESCAPE to end the game.";
g2d.setFont(g2d.getFont().deriveFont(18.0f));
g2d.drawString(userGuide, 10, GAME_HEIGHT - 10);
// Draw GAME OVER
if (tankState.gameOver) {
String str = "GAME OVER";
g2d.setColor(Color.WHITE);
g2d.setFont(g2d.getFont().deriveFont(Font.BOLD).deriveFont(64.0f));
int strWidth = g2d.getFontMetrics().stringWidth(str);
g2d.drawString(str, (GAME_WIDTH - strWidth) / 2, GAME_HEIGHT / 2);
}
}
} }
...@@ -29,8 +29,6 @@ public class GameLoop implements Runnable { ...@@ -29,8 +29,6 @@ public class GameLoop implements Runnable {
private GameFrame canvas; private GameFrame canvas;
private TankState tankState; private TankState tankState;
private BulletState bulletState ;
public GameLoop(GameFrame frame) { public GameLoop(GameFrame frame) {
canvas = frame; canvas = frame;
} }
...@@ -41,8 +39,7 @@ public class GameLoop implements Runnable { ...@@ -41,8 +39,7 @@ public class GameLoop implements Runnable {
public void init() { public void init() {
// Perform all initializations ... // Perform all initializations ...
tankState = new TankState(canvas.wallState); tankState = new TankState(canvas.wallState);
bulletState = new BulletState(canvas.wallState , tankState); canvas.addKeyListener(tankState.getMyListener());
canvas.addKeyListener(tankState.getKeyListener());
canvas.addMouseListener(tankState.getMouseListener()); canvas.addMouseListener(tankState.getMouseListener());
canvas.addMouseMotionListener(tankState.getMouseMotionListener()); canvas.addMouseMotionListener(tankState.getMouseMotionListener());
} }
...@@ -53,10 +50,8 @@ public class GameLoop implements Runnable { ...@@ -53,10 +50,8 @@ public class GameLoop implements Runnable {
while (!gameOver) { while (!gameOver) {
try { try {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
//
tankState.update(); tankState.update();
canvas.render(tankState , bulletState); canvas.render(tankState , tankState.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.GameFrame;
import UI.MyListener;
import java.awt.event.KeyAdapter; import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class BulletState { public class BulletState {
public static int locX, locY, diam , angle; public int locX, locY, diam , angle;
private WallState wallState; private WallState wallState;
private boolean keySpace;
private KeyHandler2 keyHandler2 = new KeyHandler2();
private long createTime;
public BulletState(WallState wallState , TankState tankState){ public BulletState(WallState wallState , TankState tankState){
...@@ -19,6 +22,36 @@ public class BulletState { ...@@ -19,6 +22,36 @@ public class BulletState {
locY = tankState.locY; locY = tankState.locY;
angle = tankState.rotate; angle = tankState.rotate;
diam = 10; diam = 10;
keySpace = false;
tankState.getMyListener().add(keyHandler2);
createTime = System.currentTimeMillis();
}
public void update(){
locY += 10*Math.sin(Math.toRadians(angle) ) ;
locX += 10*Math.cos(Math.toRadians(angle) ) ;
}
public long getCreateTime() {
return createTime;
}
class KeyHandler2 extends KeyAdapter{
@Override
public void keyPressed(KeyEvent e){
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
keySpace = true;
}
}
@Override
public void keyReleased(KeyEvent e){
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
keySpace = false;
}
}
} }
} }
...@@ -2,8 +2,10 @@ package UI.GameState; ...@@ -2,8 +2,10 @@ package UI.GameState;
/*** In The Name of Allah ***/ /*** In The Name of Allah ***/
import UI.GameFrame; import UI.GameFrame;
import UI.MyListener;
import java.awt.event.*; import java.awt.event.*;
import java.util.ArrayList;
/** /**
...@@ -17,18 +19,18 @@ public class TankState { ...@@ -17,18 +19,18 @@ public class TankState {
public int locX, locY, diam, rotate; public int locX, locY, diam, rotate;
public boolean gameOver; public boolean gameOver;
private boolean keyUP, keyDOWN, keyRIGHT, keyLEFT , keySpace; private boolean keyUP, keyDOWN, keyRIGHT, keyLEFT, keySpace;
private boolean mousePress; private boolean mousePress;
private int mouseX, mouseY; private int mouseX, mouseY;
private KeyHandler keyHandler; private MyListener myListener = new MyListener();
private KeyHandler1 keyHandler1 = new KeyHandler1();
private MouseHandler mouseHandler; private MouseHandler mouseHandler;
private WallState walls; private WallState walls;
BulletState bulletState = new BulletState(walls , TankState.this); private ArrayList<BulletState> bullets = new ArrayList<>();
public TankState(){
}
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;
...@@ -48,9 +50,6 @@ public class TankState { ...@@ -48,9 +50,6 @@ public class TankState {
mouseX = 0; mouseX = 0;
mouseY = 0; mouseY = 0;
keyHandler = new KeyHandler();
mouseHandler = new MouseHandler();
keyHandler = new KeyHandler();
mouseHandler = new MouseHandler(); mouseHandler = new MouseHandler();
} }
...@@ -70,58 +69,57 @@ public class TankState { ...@@ -70,58 +69,57 @@ public class TankState {
locX = mouseX - diam / 2; locX = mouseX - diam / 2;
} }
if (keyUP) { if (keyUP) {
rotate = rotate % 360; if (rotate == 0)
rotate = 360;
System.out.println(rotate);
locY -= 8; locY -= 8;
if ( (rotate> 90 && rotate <180) || rotate== 90 ) if ((rotate > 180 && rotate < 270)|| rotate ==180 )
rotate +=10; rotate +=10;
else if ( (rotate >180 && rotate < 270) || rotate ==270 ) else if ( (rotate > 270 && rotate < 360) || rotate == 360)
rotate -=10; rotate -=10;
else else
rotate =180; rotate =270;
} }
if (keyDOWN) { if (keyDOWN) {
rotate = rotate % 360; rotate = rotate % 360;
locY += 8; locY += 8;
if ( (rotate > 270 && rotate < 360) || rotate ==270 ) if ( (rotate > 0 && rotate < 90 )|| rotate == 0)
rotate +=10; rotate +=10;
else if ( (rotate < 90 && rotate > 0) || rotate == 90 ) else if ( (rotate > 90 && rotate <180 )|| rotate == 180 )
rotate -=10; rotate -=10;
else else
rotate = 0; rotate = 90;
} }
if (keyLEFT ){ if (keyLEFT ){
rotate = rotate % 360; rotate = rotate % 360;
locX -= 8; locX -= 8;
if ( (rotate > 0 && rotate < 90 )|| rotate == 0) if ( (rotate> 90 && rotate <180) || rotate== 90 )
rotate +=10; rotate +=10;
else if ( (rotate > 90 && rotate <180 )|| rotate == 180 ) else if ( (rotate >180 && rotate < 270) || rotate ==270 )
rotate -=10; rotate -=10;
else else
rotate = 90; rotate =180;
} }
if (keyRIGHT ) { if (keyRIGHT ) {
rotate = rotate % 360; rotate = rotate % 360;
locX += 8; locX += 8;
if ((rotate > 180 && rotate < 270)|| rotate ==180 ) if ( (rotate > 270 && rotate < 360) || rotate ==270 )
rotate +=10; rotate +=10;
else if ( (rotate > 270 && rotate < 360) || rotate == 360) else if ( (rotate < 90 && rotate > 0) || rotate == 90 )
rotate -=10; rotate -=10;
else else
rotate =270; 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);
System.out.println(newBullet.angle);
} }
if (keySpace) {
// for (int i = 0; i < 100; i++) {
bulletState.locY += 5*Math.sin(rotate) ;
System.out.println(bulletState.locY );
bulletState.locX += 5*Math.cos(rotate) ;
System.out.println("preeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeesssssssssssssedddddddddddddd");
// }
// bulletState.locX = locX;
// bulletState.locY = locY;
} }
if (walls.isWall(locX,locY)){ if (walls.isWall(locX,locY)){
locX = lastx; locX = lastx;
locY = lasty; locY = lasty;
...@@ -135,9 +133,14 @@ public class TankState { ...@@ -135,9 +133,14 @@ public class TankState {
} }
public KeyListener getKeyListener() { public WallState getWalls() {
return keyHandler; return walls;
} }
public ArrayList<BulletState> getBullets() {
return bullets;
}
public MouseListener getMouseListener() { public MouseListener getMouseListener() {
return mouseHandler; return mouseHandler;
} }
...@@ -145,12 +148,14 @@ public class TankState { ...@@ -145,12 +148,14 @@ public class TankState {
return mouseHandler; return mouseHandler;
} }
public MyListener getMyListener() {
return myListener;
}
/** /**
* The keyboard handler. * The keyboard handler.
*/ */
class KeyHandler extends KeyAdapter { class KeyHandler1 extends KeyAdapter {
@Override @Override
public void keyPressed(KeyEvent e) { public void keyPressed(KeyEvent e) {
...@@ -168,10 +173,9 @@ public class TankState { ...@@ -168,10 +173,9 @@ public class TankState {
case KeyEvent.VK_RIGHT: case KeyEvent.VK_RIGHT:
keyRIGHT = true; keyRIGHT = true;
break; break;
case KeyEvent.VK_SPACE: { case KeyEvent.VK_SPACE:
keySpace = true; keySpace = true;
break; break;
}
case KeyEvent.VK_ESCAPE: case KeyEvent.VK_ESCAPE:
gameOver = true; gameOver = true;
break; break;
...@@ -191,13 +195,12 @@ public class TankState { ...@@ -191,13 +195,12 @@ public class TankState {
case KeyEvent.VK_LEFT: case KeyEvent.VK_LEFT:
keyLEFT = false; keyLEFT = false;
break; break;
case KeyEvent.VK_SPACE: {
keySpace = false;
break;
}
case KeyEvent.VK_RIGHT: case KeyEvent.VK_RIGHT:
keyRIGHT = false; keyRIGHT = false;
break; break;
case KeyEvent.VK_SPACE:
keySpace = false;
break;
} }
} }
......
...@@ -44,6 +44,7 @@ public class MainPanel extends JPanel { ...@@ -44,6 +44,7 @@ public class MainPanel extends JPanel {
public void mouseClicked(MouseEvent e) { public void mouseClicked(MouseEvent e) {
super.mouseClicked(e); super.mouseClicked(e);
run(); run();
} }
}); });
...@@ -57,12 +58,16 @@ public class MainPanel extends JPanel { ...@@ -57,12 +58,16 @@ public class MainPanel extends JPanel {
} catch (IOException ex) { } catch (IOException ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
MainPanel.this.revalidate();
MainPanel.this.repaint();
} }
}); });
} }
private JPanel setSettingPanel() throws IOException { private JPanel setSettingPanel() throws IOException {
SettingPanel settingPanel = new SettingPanel(); SettingPanel settingPanel = new SettingPanel();
settingPanel.revalidate();
settingPanel.revalidate();
return settingPanel; return settingPanel;
} }
......
...@@ -59,12 +59,10 @@ public class Setting { ...@@ -59,12 +59,10 @@ public class Setting {
} }
public String getTankShapePath() { public String getTankShapePath() {
System.out.println(2+tankShapePath);
return tankShapePath; return tankShapePath;
} }
public void setTankShapePath(String tankShapePath) { public void setTankShapePath(String tankShapePath) {
this.tankShapePath = tankShapePath; this.tankShapePath = tankShapePath;
System.out.println(1+getTankShapePath());
} }
} }
...@@ -20,6 +20,8 @@ public class SettingPanel extends JPanel { ...@@ -20,6 +20,8 @@ public class SettingPanel extends JPanel {
setLayout(new GridLayout(1 , 2)); setLayout(new GridLayout(1 , 2));
add(informationPanel()); add(informationPanel());
add(changeableComponents()); add(changeableComponents());
this.revalidate();
this.repaint();
} }
private JPanel informationPanel(){ private JPanel informationPanel(){
...@@ -103,6 +105,8 @@ public class SettingPanel extends JPanel { ...@@ -103,6 +105,8 @@ public class SettingPanel extends JPanel {
} catch (IOException ex) { } catch (IOException ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
SettingPanel.this.revalidate();
SettingPanel.this.repaint();
setting.setTankShapePath(chosenTankPath); setting.setTankShapePath(chosenTankPath);
changeableComponents4.removeAll(); changeableComponents4.removeAll();
ImageIcon imageIcon = new ImageIcon(image); ImageIcon imageIcon = new ImageIcon(image);
...@@ -122,6 +126,8 @@ public class SettingPanel extends JPanel { ...@@ -122,6 +126,8 @@ public class SettingPanel extends JPanel {
} catch (IOException ex) { } catch (IOException ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
SettingPanel.this.revalidate();
SettingPanel.this.repaint();
} }
}); });
...@@ -136,8 +142,6 @@ public class SettingPanel extends JPanel { ...@@ -136,8 +142,6 @@ public class SettingPanel extends JPanel {
changeableComponents.add(changeableComponents3); changeableComponents.add(changeableComponents3);
changeableComponents.add(changeableComponents4); changeableComponents.add(changeableComponents4);
changeableComponents.add(back); changeableComponents.add(back);
return changeableComponents; return changeableComponents;
} }
} }
......
package UI;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
public class MyListener extends KeyAdapter {
public static ArrayList<KeyAdapter> list = new ArrayList<>();
public static void add(KeyAdapter keyAdapter){
list.add(keyAdapter);
}
@Override
public void keyPressed(KeyEvent e) {
for (KeyAdapter keyAdapter : list){
if (keyAdapter != null){
keyAdapter.keyPressed(e);
}
}
}
@Override
public void keyReleased(KeyEvent e) {
for (KeyAdapter keyAdapter : list){
if (keyAdapter != null){
keyAdapter.keyReleased(e);
}
}
}
}
package UI;
import UI.GameState.BulletState;
import UI.GameState.TankState;
import UI.LogIn.Setting;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
public class Rendering {
private Image image1;
private Image image2;
public static final int GAME_HEIGHT = 720; // 720p game resolution
public static final int GAME_WIDTH = 16 * GAME_HEIGHT / 9; // wide aspect ratio
int lastRender = -1;
private ArrayList<Float> fpsHistory;
private Setting setting = new Setting();
public Rendering(){
fpsHistory = new ArrayList<>(100);
}
/**
* Rendering all game elements based on the game state.
*/
// Draw all game elements according
// to the game 'state' using 'g2d' ...
public void doRendering(Graphics2D g2d, TankState tankState , ArrayList<BulletState> bullets) {
// Draw background
tankState.getWalls().paintComponent(g2d);
// Draw tank;
try {
image1 = ImageIO.read(new File(setting.getTankShapePath()));
// image2 = ImageIO.read(new File("C:\\Users\\Lenovo\\IdeaProjects\\JTankTrouble\\pics\\tank_sand.png"));
} catch (IOException e) {
System.out.println(e);
}
// Rotation information
double rotationRequired = Math.toRadians (tankState.rotate);
BufferedImage image11 = (BufferedImage)image1;
double locationX = image11.getWidth() / 2;
double locationY = image11.getHeight() / 2;
AffineTransform tx = AffineTransform.getRotateInstance(rotationRequired, locationX, locationY);
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
// Drawing the rotated image at the required drawing locations
g2d.drawImage(op.filter( image11, null),tankState.locX , tankState.locY , null);
// g2d.drawImage(image2 , state.locX+30 , state.locY+30 , null);
//Draw bullet
if (bullets.size() != 0) {
for (BulletState bullet : bullets) {
if (System.currentTimeMillis() - bullet.getCreateTime() > 4000) {
// bullets.remove(bullet);
continue;
}
bullet.update();
g2d.setColor(Color.GRAY);
g2d.fillOval(bullet.locX, bullet.locY, bullet.diam, bullet.diam);
}
}
// Print FPS info
long currentRender = System.currentTimeMillis();
if (lastRender > 0) {
fpsHistory.add(1000.0f / (currentRender - lastRender));
if (fpsHistory.size() > 100) {
fpsHistory.remove(0); // remove oldest
}
float avg = 0.0f;
for (float fps : fpsHistory) {
avg += fps;
}
avg /= fpsHistory.size();
String str = String.format("Average FPS = %.1f , Last Interval = %d ms",
avg, (currentRender - lastRender));
g2d.setColor(Color.CYAN);
g2d.setFont(g2d.getFont().deriveFont(18.0f));
int strWidth = g2d.getFontMetrics().stringWidth(str);
int strHeight = g2d.getFontMetrics().getHeight();
g2d.drawString(str, (GAME_WIDTH - strWidth) / 2, strHeight + 50);
}
lastRender = (int) currentRender;
// Print user guide
String userGuide
= "Use the MOUSE or ARROW KEYS to move the BALL. "
+ "Press ESCAPE to end the game.";
g2d.setFont(g2d.getFont().deriveFont(18.0f));
g2d.drawString(userGuide, 10, GAME_HEIGHT - 10);
// Draw GAME OVER
if (tankState.gameOver) {
String str = "GAME OVER";
g2d.setColor(Color.WHITE);
g2d.setFont(g2d.getFont().deriveFont(Font.BOLD).deriveFont(64.0f));
int strWidth = g2d.getFontMetrics().stringWidth(str);
g2d.drawString(str, (GAME_WIDTH - strWidth) / 2, GAME_HEIGHT / 2);
}
}
}
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