Commit 36ee6d79 authored by 9731301's avatar 9731301

add bullet class and add some classes

parent a8b5f416
...@@ -2,12 +2,21 @@ ...@@ -2,12 +2,21 @@
<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/GameState/TankState.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/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$MouseHandler.class" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/GameState.class" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/WallState$WallData.class" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/WallState.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$/src/UI/GameFrame.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/UI/GameFrame.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/GameLoop.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/UI/GameLoop.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/GameState.java" beforeDir="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/WallState.java" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/UI/Registration/InitialPanel.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/UI/Registration/InitialPanel.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" />
...@@ -18,6 +27,7 @@ ...@@ -18,6 +27,7 @@
<option name="RECENT_TEMPLATES"> <option name="RECENT_TEMPLATES">
<list> <list>
<option value="Class" /> <option value="Class" />
<option value="Enum" />
</list> </list>
</option> </option>
</component> </component>
...@@ -105,6 +115,10 @@ ...@@ -105,6 +115,10 @@
<workItem from="1595766182905" duration="14490000" /> <workItem from="1595766182905" duration="14490000" />
<workItem from="1595787964241" duration="8776000" /> <workItem from="1595787964241" duration="8776000" />
<workItem from="1595807570766" duration="19718000" /> <workItem from="1595807570766" duration="19718000" />
<workItem from="1595889331611" duration="71000" />
<workItem from="1596026842920" duration="4069000" />
<workItem from="1596042670145" duration="24545000" />
<workItem from="1596134290663" duration="1190000" />
</task> </task>
<servers /> <servers />
</component> </component>
...@@ -116,42 +130,46 @@ ...@@ -116,42 +130,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="1595827412140"> <state x="574" y="122" key="#com.intellij.ide.util.MemberChooser" timestamp="1596080698011">
<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="1595827412140" /> <state x="574" y="122" key="#com.intellij.ide.util.MemberChooser/0.0.1536.824@0.0.1536.824" timestamp="1596080698011" />
<state x="566" y="298" width="403" height="237" key="#com.intellij.refactoring.move.moveClassesOrPackages.MoveClassesOrPackagesDialog.classes" timestamp="1595774990960"> <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="1595774990960" /> <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">
<screen x="0" y="0" width="1536" height="824" />
</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="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="168" key="GridCell.Tab.0.bottom" timestamp="1595828869703"> <state width="1493" height="134" key="GridCell.Tab.0.bottom" timestamp="1596135091697">
<screen x="0" y="0" width="1536" height="824" /> <screen x="0" y="0" width="1536" height="824" />
</state> </state>
<state width="1493" height="168" key="GridCell.Tab.0.bottom/0.0.1536.824@0.0.1536.824" timestamp="1595828869703" /> <state width="1493" height="134" key="GridCell.Tab.0.bottom/0.0.1536.824@0.0.1536.824" timestamp="1596135091697" />
<state width="1493" height="168" key="GridCell.Tab.0.center" timestamp="1595828869703"> <state width="1493" height="134" key="GridCell.Tab.0.center" timestamp="1596135091697">
<screen x="0" y="0" width="1536" height="824" /> <screen x="0" y="0" width="1536" height="824" />
</state> </state>
<state width="1493" height="168" key="GridCell.Tab.0.center/0.0.1536.824@0.0.1536.824" timestamp="1595828869703" /> <state width="1493" height="134" key="GridCell.Tab.0.center/0.0.1536.824@0.0.1536.824" timestamp="1596135091697" />
<state width="1493" height="168" key="GridCell.Tab.0.left" timestamp="1595828869703"> <state width="1493" height="134" key="GridCell.Tab.0.left" timestamp="1596135091697">
<screen x="0" y="0" width="1536" height="824" /> <screen x="0" y="0" width="1536" height="824" />
</state> </state>
<state width="1493" height="168" key="GridCell.Tab.0.left/0.0.1536.824@0.0.1536.824" timestamp="1595828869703" /> <state width="1493" height="134" key="GridCell.Tab.0.left/0.0.1536.824@0.0.1536.824" timestamp="1596135091697" />
<state width="1493" height="168" key="GridCell.Tab.0.right" timestamp="1595828869703"> <state width="1493" height="134" key="GridCell.Tab.0.right" timestamp="1596135091697">
<screen x="0" y="0" width="1536" height="824" /> <screen x="0" y="0" width="1536" height="824" />
</state> </state>
<state width="1493" height="168" key="GridCell.Tab.0.right/0.0.1536.824@0.0.1536.824" timestamp="1595828869703" /> <state width="1493" height="134" key="GridCell.Tab.0.right/0.0.1536.824@0.0.1536.824" timestamp="1596135091697" />
<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="1595807634287"> <state x="107" y="201" key="com.intellij.ide.util.TipDialog" timestamp="1596134473346">
<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="1595807634287" /> <state x="107" y="201" key="com.intellij.ide.util.TipDialog/0.0.1536.824@0.0.1536.824" timestamp="1596134473346" />
<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; package UI;
import UI.GameState.BulletState;
import UI.GameState.TankState;
import UI.GameState.WallState;
import UI.LogIn.Setting; import UI.LogIn.Setting;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
...@@ -51,7 +54,7 @@ public class GameFrame extends JFrame { ...@@ -51,7 +54,7 @@ public class GameFrame extends JFrame {
* Game rendering with triple-buffering using BufferStrategy. * Game rendering with triple-buffering using BufferStrategy.
*/ */
public void render(GameState state) { public void render(TankState tankState , BulletState bulletState) {
do { do {
// The following loop ensures that the contents of the drawing buffer // The following loop ensures that the contents of the drawing buffer
...@@ -63,7 +66,7 @@ public class GameFrame extends JFrame { ...@@ -63,7 +66,7 @@ public class GameFrame extends JFrame {
Graphics2D graphics = (Graphics2D) bufferStrategy.getDrawGraphics(); Graphics2D graphics = (Graphics2D) bufferStrategy.getDrawGraphics();
try { try {
doRendering(graphics, state); doRendering(graphics, tankState , bulletState);
} finally { } finally {
// Dispose the graphics // Dispose the graphics
graphics.dispose(); graphics.dispose();
...@@ -86,7 +89,7 @@ public class GameFrame extends JFrame { ...@@ -86,7 +89,7 @@ public class GameFrame extends JFrame {
*/ */
// Draw all game elements according // Draw all game elements according
// to the game 'state' using 'g2d' ... // to the game 'state' using 'g2d' ...
private void doRendering(Graphics2D g2d, GameState state) { private void doRendering(Graphics2D g2d, TankState tankState , BulletState bulletState) {
// Draw background // Draw background
wallState.paintComponent(g2d); wallState.paintComponent(g2d);
...@@ -98,19 +101,21 @@ public class GameFrame extends JFrame { ...@@ -98,19 +101,21 @@ public class GameFrame extends JFrame {
} catch (IOException e) { } catch (IOException e) {
System.out.println(e); System.out.println(e);
} }
// Rotation information // Rotation information
double rotationRequired = Math.toRadians (state.rotate); double rotationRequired = Math.toRadians (tankState.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),state.locX , state.locY , null); g2d.drawImage(op.filter( image11, null),tankState.locX , tankState.locY , null);
// g2d.drawImage(image2 , state.locX+30 , state.locY+30 , 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 // Print FPS info
long currentRender = System.currentTimeMillis(); long currentRender = System.currentTimeMillis();
...@@ -140,7 +145,7 @@ public class GameFrame extends JFrame { ...@@ -140,7 +145,7 @@ public class GameFrame extends JFrame {
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 (state.gameOver) { if (tankState.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));
......
...@@ -2,6 +2,9 @@ package UI; ...@@ -2,6 +2,9 @@ package UI;
/*** In The Name of Allah ***/ /*** In The Name of Allah ***/
import UI.GameState.BulletState;
import UI.GameState.TankState;
/** /**
* A very simple structure for the main game loop. * A very simple structure for the main game loop.
* THIS IS NOT PERFECT, but works for most situations. * THIS IS NOT PERFECT, but works for most situations.
...@@ -14,7 +17,7 @@ package UI; ...@@ -14,7 +17,7 @@ package UI;
* patterns is available in the following link: * patterns is available in the following link:
* http://gameprogrammingpatterns.com/game-loop.html * http://gameprogrammingpatterns.com/game-loop.html
* *
* @author Seyed Mohammad Ghaffarian * @author Seyede zahra seyed fatehi
*/ */
public class GameLoop implements Runnable { public class GameLoop implements Runnable {
...@@ -25,7 +28,8 @@ public class GameLoop implements Runnable { ...@@ -25,7 +28,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 GameState state; private TankState tankState;
private BulletState bulletState ;
public GameLoop(GameFrame frame) { public GameLoop(GameFrame frame) {
canvas = frame; canvas = frame;
...@@ -36,10 +40,11 @@ public class GameLoop implements Runnable { ...@@ -36,10 +40,11 @@ public class GameLoop implements Runnable {
*/ */
public void init() { public void init() {
// Perform all initializations ... // Perform all initializations ...
state = new GameState(canvas.wallState); tankState = new TankState(canvas.wallState);
canvas.addKeyListener(state.getKeyListener()); bulletState = new BulletState(canvas.wallState , tankState);
canvas.addMouseListener(state.getMouseListener()); canvas.addKeyListener(tankState.getKeyListener());
canvas.addMouseMotionListener(state.getMouseMotionListener()); canvas.addMouseListener(tankState.getMouseListener());
canvas.addMouseMotionListener(tankState.getMouseMotionListener());
} }
@Override @Override
...@@ -49,8 +54,8 @@ public class GameLoop implements Runnable { ...@@ -49,8 +54,8 @@ public class GameLoop implements Runnable {
try { try {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
// //
state.update(); tankState.update();
canvas.render(state); canvas.render(tankState , bulletState);
// //
long delay = (1000 / FPS) - (System.currentTimeMillis() - start); long delay = (1000 / FPS) - (System.currentTimeMillis() - start);
if (delay > 0) if (delay > 0)
......
package UI.GameState;
import UI.GameFrame;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class BulletState {
public static int locX, locY, diam , angle;
private WallState wallState;
public BulletState(WallState wallState , TankState tankState){
this.wallState = wallState ;
// Initialize the game state and all elements ...
locX = tankState.locX;
locY = tankState.locY;
angle = tankState.rotate;
diam = 10;
}
}
package UI; package UI.GameState;
/*** In The Name of Allah ***/ /*** In The Name of Allah ***/
import javax.imageio.ImageIO; import UI.GameFrame;
import java.awt.*;
import java.awt.event.*; import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
/** /**
* This class holds the state of the game and all of its elements. * This class holds the state of the game and all of its elements.
* This class also handles user inputs, which affect the game state. * This class also handles user inputs, which affect the game state.
* *
* @author Seyed Mohammad Ghaffarian * @author Seyede zahra seyed fatehi
*/ */
public class GameState { 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; 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 KeyHandler keyHandler;
private MouseHandler mouseHandler; private MouseHandler mouseHandler;
private WallState walls; private WallState walls;
BulletState bulletState = new BulletState(walls , TankState.this);
public GameState(WallState walls) { public TankState(){
}
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; locX = 100;
locY = 100; locY = 100;
rotate = 4; rotate = 0;
diam = 32; diam = 32;
gameOver = false; gameOver = false;
...@@ -39,6 +42,7 @@ public class GameState { ...@@ -39,6 +42,7 @@ public class GameState {
keyDOWN = false; keyDOWN = false;
keyRIGHT = false; keyRIGHT = false;
keyLEFT = false; keyLEFT = false;
keySpace = false;
mousePress = false; mousePress = false;
mouseX = 0; mouseX = 0;
...@@ -105,6 +109,18 @@ public class GameState { ...@@ -105,6 +109,18 @@ public class GameState {
else else
rotate =270; rotate =270;
} }
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;
...@@ -139,8 +155,7 @@ public class GameState { ...@@ -139,8 +155,7 @@ public class GameState {
@Override @Override
public void keyPressed(KeyEvent e) { public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) switch (e.getKeyCode()) {
{
case KeyEvent.VK_UP: case KeyEvent.VK_UP:
keyUP = true; keyUP = true;
break; break;
...@@ -153,6 +168,10 @@ public class GameState { ...@@ -153,6 +168,10 @@ public class GameState {
case KeyEvent.VK_RIGHT: case KeyEvent.VK_RIGHT:
keyRIGHT = true; keyRIGHT = true;
break; break;
case KeyEvent.VK_SPACE: {
keySpace = true;
break;
}
case KeyEvent.VK_ESCAPE: case KeyEvent.VK_ESCAPE:
gameOver = true; gameOver = true;
break; break;
...@@ -172,6 +191,10 @@ public class GameState { ...@@ -172,6 +191,10 @@ public class GameState {
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;
...@@ -180,6 +203,10 @@ public class GameState { ...@@ -180,6 +203,10 @@ public class GameState {
} }
public void setWalls(WallState walls) {
this.walls = walls;
}
/** /**
* The mouse handler. * The mouse handler.
*/ */
......
package UI; package UI.GameState;
import java.awt.*; import java.awt.*;
import java.io.IOException; import java.io.IOException;
......
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