Commit 5253b190 authored by 9731301's avatar 9731301

set the end of the game and adjust packages

parent 1d67ac9f
This diff is collapsed.
package UI.GameState; package UI.GameState;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class EnemyTank extends TankState { public class EnemyTank extends TankState {
public EnemyTank(WallState walls) { public EnemyTank(WallState walls) {
super(walls); super(walls);
myListener.add(new KeyHandler2());
} }
public void update() { /**
* The keyboard handler.
*/
class KeyHandler2 extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_E:
keyE = true;
break;
case KeyEvent.VK_D:
keyD = true;
break;
case KeyEvent.VK_S:
keyS = true;
break;
case KeyEvent.VK_F:
keyF = true;
break;
case KeyEvent.VK_Q:
keyQ = true;
break;
case KeyEvent.VK_ESCAPE:
gameOver = true;
break;
}
}
@Override
public void keyReleased(KeyEvent e) {
switch (e.getKeyCode())
{
case KeyEvent.VK_E:
keyE = false;
break;
case KeyEvent.VK_D:
keyD = false;
break;
case KeyEvent.VK_S:
keyS = false;
break;
case KeyEvent.VK_F:
keyF = false;
break;
case KeyEvent.VK_Q:
keyQ = false;
break;
}
}
} }
} }
package UI.GameState; package UI.GameState;
import UI.LogIn.Game.MyListener;
import UI.GameFrame;
import UI.MyListener;
import java.awt.event.*; import java.awt.event.*;
public class MyTank extends TankState { public class MyTank extends TankState {
private boolean keyUP, keyDOWN, keyRIGHT, keyLEFT, keySpace;
private boolean mousePress;
private int mouseX, mouseY;
private MyListener myListener = new MyListener();
private KeyHandler1 keyHandler1 = new KeyHandler1();
private MouseHandler mouseHandler; private MouseHandler mouseHandler;
public MyTank(WallState walls) { public MyTank(WallState walls) {
super(walls); super(walls);
myListener.add(keyHandler1); myListener.add(new KeyHandler1());
keyUP = false;
keyDOWN = false;
keyRIGHT = false;
keyLEFT = false;
keySpace = false;
mousePress = false;
mouseX = 0;
mouseY = 0;
mouseHandler = new MouseHandler(); mouseHandler = new MouseHandler();
} }
/**
* The method which updates the game state.
*/
public void update() {
//
// Update the state of all game elements
// based on user input and elapsed time ...
//
int lastx = locX,lasty = locY,lastr = rotate;
if (mousePress) {
locY = mouseY - diam / 2;
locX = mouseX - diam / 2;
}
if (keyUP) {
if (rotate == 0)
rotate = 360;
locY -= 8;
if ((rotate > 180 && rotate < 270)|| rotate ==180 )
rotate +=10;
else if ( (rotate > 270 && rotate < 360) || rotate == 360)
rotate -=10;
else
rotate =270;
}
if (keyDOWN) {
rotate = rotate % 360;
locY += 8;
if ( (rotate > 0 && rotate < 90 )|| rotate == 0)
rotate +=10;
else if ( (rotate > 90 && rotate <180 )|| rotate == 180 )
rotate -=10;
else
rotate = 90;
}
if (keyLEFT ){
rotate = rotate % 360;
locX -= 8;
if ( (rotate> 90 && rotate <180) || rotate== 90 )
rotate +=10;
else if ( (rotate >180 && rotate < 270) || rotate ==270 )
rotate -=10;
else
rotate =180;
}
if (keyRIGHT ) {
rotate = rotate % 360;
locX += 8;
if ( (rotate > 270 && rotate < 360) || rotate ==270 )
rotate +=10;
else if ( (rotate < 90 && rotate > 0) || rotate == 90 )
rotate -=10;
else
rotate = 0;
}
if (keySpace){
if (bullets.size() == 0 || System.currentTimeMillis() -bullets.get(bullets.size()-1).getCreateTime()>500 ) {
BulletState newBullet = new BulletState(walls, this);
bullets.add(newBullet);
}
}
if (walls.isWallForTank(locX,locY)){
locX = lastx;
locY = lasty;
}
locX = Math.max(locX, 0);
locX = Math.min(locX, GameFrame.GAME_WIDTH - diam);
locY = Math.max(locY, 0);
locY = Math.min(locY, GameFrame.GAME_HEIGHT - diam);
}
public MouseListener getMouseListener() { public MouseListener getMouseListener() {
return mouseHandler; return mouseHandler;
} }
......
package UI.GameState;
public class RewarsState {
}
package UI.GameState; package UI.GameState;
/*** In The Name of Allah ***/ /*** In The Name of Allah ***/
import UI.GameFrame;
import UI.MyListener;
import java.awt.event.*; import UI.LogIn.Game.GameFrame;
import UI.LogIn.Game.MyListener;
import java.awt.event.KeyListener;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Random;
/** /**
...@@ -15,7 +17,10 @@ import java.util.ArrayList; ...@@ -15,7 +17,10 @@ import java.util.ArrayList;
* @author Seyede zahra seyed fatehi * @author Seyede zahra seyed fatehi
*/ */
public class TankState { public class TankState {
protected MyListener myListener = new MyListener();
protected boolean keyUP, keyDOWN, keyRIGHT, keyLEFT, keySpace , keyS , keyD, keyF , keyE , keyQ;
protected boolean mousePress;
protected int mouseX, mouseY;
public int locX, locY, diam, rotate; public int locX, locY, diam, rotate;
public boolean gameOver; public boolean gameOver;
protected WallState walls; protected WallState walls;
...@@ -25,14 +30,40 @@ public class TankState { ...@@ -25,14 +30,40 @@ public class TankState {
public TankState(WallState walls) { public TankState(WallState walls) {
this.walls = walls; this.walls = walls;
// Initialize the game state and all elements ... // Initialize the game state and all elements ...
locX = 100; // setFirstLocationOfTank();
locY = 100; locY = 120;
locX = 120;
rotate = 0; rotate = 0;
diam = 32; diam = 32;
keyUP = false;
keyDOWN = false;
keyRIGHT = false;
keyLEFT = false;
keySpace = false;
keyS = false;
keyD = false;
keyE = false;
keyF = false;
keyQ = false;
mousePress = false;
mouseX = 0;
mouseY = 0;
gameOver = false; gameOver = false;
} }
private void setFirstLocationOfTank() {
Random randomX = new Random();
Random randomY = new Random();
while (true) {
locX = randomX.nextInt(GameFrame.GAME_WIDTH);
locY = randomY.nextInt(GameFrame.GAME_HEIGHT);
System.out.println(locX +" "+locY);
if (!walls.isWallForTank(locX , locY) && locX > 10 && locY >10)
break;
}
}
...@@ -45,7 +76,84 @@ public class TankState { ...@@ -45,7 +76,84 @@ public class TankState {
} }
public MyListener getMyListener() { public MyListener getMyListener() {
return null; return myListener;
}
/**
* The method which updates the game state.
*/
public void update() {
//
// Update the state of all game elements
// based on user input and elapsed time ...
//
int lastx = locX,lasty = locY,lastr = rotate;
if (mousePress) {
locY = mouseY - diam / 2;
locX = mouseX - diam / 2;
}
if (keyUP || keyE) {
if (rotate == 0)
rotate = 360;
locY -= 8;
if ((rotate > 180 && rotate < 270)|| rotate ==180 )
rotate +=10;
else if ( (rotate > 270 && rotate < 360) || rotate == 360)
rotate -=10;
else
rotate =270;
}
if (keyDOWN || keyD) {
rotate = rotate % 360;
locY += 8;
if ( (rotate > 0 && rotate < 90 )|| rotate == 0)
rotate +=10;
else if ( (rotate > 90 && rotate <180 )|| rotate == 180 )
rotate -=10;
else
rotate = 90;
}
if (keyLEFT || keyS ){
rotate = rotate % 360;
locX -= 8;
if ( (rotate> 90 && rotate <180) || rotate== 90 )
rotate +=10;
else if ( (rotate >180 && rotate < 270) || rotate ==270 )
rotate -=10;
else
rotate =180;
}
if (keyRIGHT || keyF) {
rotate = rotate % 360;
locX += 8;
if ( (rotate > 270 && rotate < 360) || rotate ==270 )
rotate +=10;
else if ( (rotate < 90 && rotate > 0) || rotate == 90 )
rotate -=10;
else
rotate = 0;
}
if (keySpace || keyQ){
if (bullets.size() == 0 || System.currentTimeMillis() -bullets.get(bullets.size()-1).getCreateTime()>500 ) {
BulletState newBullet = new BulletState(walls, this);
bullets.add(newBullet);
}
}
if (walls.isWallForTank(locX,locY)){
locX = lastx;
locY = lasty;
}
locX = Math.max(locX, 0);
locX = Math.min(locX, GameFrame.GAME_WIDTH - diam);
locY = Math.max(locY, 0);
locY = Math.min(locY, GameFrame.GAME_HEIGHT - diam);
} }
} }
package UI; package UI.LogIn.Game;
import UI.GameState.*; import UI.GameState.*;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.image.BufferStrategy; import java.awt.image.BufferStrategy;
import java.util.ArrayList;
public class GameFrame extends JFrame { public class GameFrame extends JFrame {
...@@ -42,13 +41,13 @@ public class GameFrame extends JFrame { ...@@ -42,13 +41,13 @@ public class GameFrame extends JFrame {
* Game rendering with triple-buffering using BufferStrategy. * Game rendering with triple-buffering using BufferStrategy.
*/ */
public void render(MyTank myTank, EnemyTank enemyTank, ArrayList<BulletState> bullets) { public void render(MyTank myTank, EnemyTank enemyTank) {
do { do {
do { do {
Graphics2D graphics = (Graphics2D) bufferStrategy.getDrawGraphics(); Graphics2D graphics = (Graphics2D) bufferStrategy.getDrawGraphics();
try { try {
rendering.doRendering(graphics, myTank , enemyTank, bullets ); rendering.doRendering(graphics, myTank , enemyTank );
} finally { } finally {
// Dispose the graphics // Dispose the graphics
graphics.dispose(); graphics.dispose();
......
package UI; package UI.LogIn.Game;
/*** In The Name of Allah ***/ /*** In The Name of Allah ***/
...@@ -56,7 +57,7 @@ public class GameLoop implements Runnable { ...@@ -56,7 +57,7 @@ public class GameLoop implements Runnable {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
myTank.update(); myTank.update();
enemyTank.update(); enemyTank.update();
canvas.render(myTank , enemyTank , myTank.getBullets()); canvas.render(myTank , enemyTank);
long delay = (1000 / FPS) - (System.currentTimeMillis() - start); long delay = (1000 / FPS) - (System.currentTimeMillis() - start);
if (delay > 0) if (delay > 0)
Thread.sleep(delay); Thread.sleep(delay);
......
package UI; package UI.LogIn.Game;
import java.awt.event.KeyAdapter; import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
......
package UI; package UI.LogIn.Game;
import UI.GameState.BulletState; import UI.GameState.BulletState;
import UI.GameState.EnemyTank; import UI.GameState.EnemyTank;
import UI.GameState.MyTank; import UI.GameState.MyTank;
import UI.GameState.TankState;
import UI.LogIn.Setting; import UI.LogIn.Setting;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import java.awt.*; import java.awt.*;
...@@ -17,6 +17,7 @@ public class Rendering { ...@@ -17,6 +17,7 @@ public class Rendering {
private Image image1; private Image image1;
private Image image2; private Image image2;
private Image explosionImg;
public static final int GAME_HEIGHT = 720; // 720p game resolution public static final int GAME_HEIGHT = 720; // 720p game resolution
public static final int GAME_WIDTH = 16 * GAME_HEIGHT / 9; // wide aspect ratio public static final int GAME_WIDTH = 16 * GAME_HEIGHT / 9; // wide aspect ratio
int lastRender = -1; int lastRender = -1;
...@@ -32,7 +33,7 @@ public class Rendering { ...@@ -32,7 +33,7 @@ public class Rendering {
*/ */
// Draw all game elements according // Draw all game elements according
// to the game 'state' using 'g2d' ... // to the game 'state' using 'g2d' ...
public void doRendering(Graphics2D g2d, MyTank myTank, EnemyTank enemyTank, ArrayList<BulletState> bullets) { public void doRendering(Graphics2D g2d, MyTank myTank, EnemyTank enemyTank) {
// Draw background // Draw background
myTank.getWalls().paintComponent(g2d); myTank.getWalls().paintComponent(g2d);
enemyTank.getWalls().paintComponent(g2d); enemyTank.getWalls().paintComponent(g2d);
...@@ -43,15 +44,23 @@ public class Rendering { ...@@ -43,15 +44,23 @@ public class Rendering {
} catch (IOException e) { } catch (IOException e) {
System.out.println(e); System.out.println(e);
} }
// Rotation information // Rotation information
double rotationRequired = Math.toRadians (myTank.rotate); double rotationRequired = Math.toRadians (myTank.rotate);
BufferedImage image11 = (BufferedImage)image1; BufferedImage image11 = (BufferedImage)image1;
double locationX = image11.getWidth() / 2; double locationX = image11.getWidth() / 2;
double locationY = image11.getHeight() / 2; double locationY = image11.getHeight() / 2;
AffineTransform tx = AffineTransform.getRotateInstance(rotationRequired, locationX, locationY); AffineTransform tx = AffineTransform.getRotateInstance(rotationRequired, locationX, locationY);
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR); AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
// Drawing the rotated image at the required drawing locations // Drawing the rotated image at the required drawing locations
g2d.drawImage(op.filter( image11, null),myTank.locX , myTank.locY , null); int lastX1 = 0;
int lastY1 = 0;
if (myTank.gameOver == false && enemyTank.gameOver == false) {
g2d.drawImage(op.filter( image11, null),myTank.locX , myTank.locY , null);
lastX1 = enemyTank.locX;
lastY1 = enemyTank.locY;
}
else
g2d.drawImage(op.filter( image11, null),lastX1 , lastY1 , null);
//Draw enemy tank //Draw enemy tank
try { try {
...@@ -59,13 +68,29 @@ public class Rendering { ...@@ -59,13 +68,29 @@ public class Rendering {
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
g2d.drawImage(image2 , enemyTank.locX+30 , enemyTank.locY+30 , null); // Rotation information
double rotationEnemy = Math.toRadians (enemyTank.rotate);
BufferedImage image22 = (BufferedImage)image2;
double locationX2 = image22.getWidth() / 2;
double locationY2 = image22.getHeight() / 2;
AffineTransform tx2 = AffineTransform.getRotateInstance(rotationEnemy, locationX2, locationY2);
AffineTransformOp op2 = new AffineTransformOp(tx2, AffineTransformOp.TYPE_BILINEAR);
// Drawing the rotated image at the required drawing locations
int lastX2 = 0;
int lastY2 = 0;
if (myTank.gameOver == false && enemyTank.gameOver == false) {
g2d.drawImage(op2.filter(image22, null), enemyTank.locX, enemyTank.locY, null);
lastX2 = enemyTank.locX;
lastY2 = enemyTank.locY;
}
else
g2d.drawImage(op2.filter( image22, null),lastX2 , lastY2 , null);
//Draw bullet //Draw myTank bullet
if (bullets.size() != 0) { if (myTank.getBullets().size() != 0 && myTank.gameOver == false && enemyTank.gameOver == false) {
for (BulletState bullet : bullets) { for (BulletState bullet : myTank.getBullets()) {
if (System.currentTimeMillis() - bullet.getCreateTime() > 4000) { if (System.currentTimeMillis() - bullet.getCreateTime() > 4000) {
// bullets.remove(bullet); // myTank.getBullets().remove(bullet);
continue; continue;
} }
bullet.update(); bullet.update();
...@@ -74,6 +99,62 @@ public class Rendering { ...@@ -74,6 +99,62 @@ public class Rendering {
} }
} }
//Draw enemy tank bullet
if (enemyTank.getBullets().size() != 0 && myTank.gameOver == false && enemyTank.gameOver == false) {
for (BulletState bullet : enemyTank.getBullets()) {
if (System.currentTimeMillis() - bullet.getCreateTime() > 4000) {
//enemyTank.getBullets().remove(bullet);
continue;
}
bullet.update();
g2d.setColor(Color.GRAY);
g2d.fillOval(bullet.locX, bullet.locY, bullet.diam, bullet.diam);
}
}
//Draw explosion and ruin the tank
ArrayList<BulletState> allBullets = new ArrayList<>();
for (int i = myTank.getBullets().size() - 1 ; i >= 0 ; i -- ){
if (System.currentTimeMillis() - myTank.getBullets().get(i).getCreateTime() >4000){
break;
}
System.out.println(i);
allBullets.add(myTank.getBullets().get(i));
}
for (int i = enemyTank.getBullets().size() - 1 ; i >= 0 ; i -- ){
if (System.currentTimeMillis() - enemyTank.getBullets().get(i).getCreateTime() >4000){
break;
}
allBullets.add(enemyTank.getBullets().get(i));
}
try {
explosionImg = ImageIO.read(new File("C:\\Users\\Lenovo\\IdeaProjects\\JTankTrouble\\pics\\explosion3.png"));
} catch (IOException e) {
System.out.println(e);
}
long startExploding ;
for (BulletState bullet : allBullets){//check my tank
if (bullet.locX >myTank.locX - 20 && bullet.locX <myTank.locX + 20 && bullet.locY >myTank.locY - 10&& bullet.locY <myTank.locY + 10){
startExploding = System.currentTimeMillis();
if (System.currentTimeMillis() - startExploding < 1000) {
g2d.drawImage(op2.filter((BufferedImage) explosionImg, null), myTank.locX, myTank.locY, null);
myTank.gameOver = true;
}
}
}
for (BulletState bullet : allBullets){//check enemy tank
if (bullet.locX >enemyTank.locX - 20 && bullet.locX <enemyTank.locX + 20 && bullet.locY >enemyTank.locY - 10&& bullet.locY <enemyTank.locY + 10){
startExploding = System.currentTimeMillis();
System.out.println("ex");
if (System.currentTimeMillis() - startExploding < 1000) {
g2d.drawImage(op2.filter((BufferedImage) explosionImg, null), enemyTank.locX, enemyTank.locY, null);
enemyTank.gameOver = true;
}
}
}
// Print FPS info // Print FPS info
long currentRender = System.currentTimeMillis(); long currentRender = System.currentTimeMillis();
......
package UI; package UI.LogIn.Game;
/*** In The Name of Allah ***/ /*** In The Name of Allah ***/
......
...@@ -16,7 +16,7 @@ public class Main { ...@@ -16,7 +16,7 @@ public class Main {
// if (initialFrame.isClickOnPlay()) { // if (initialFrame.isClickOnPlay()) {
// After the player clicks 'PLAY' ... // After the player clicks 'PLAY' ...
Mainframe mainframe = new Mainframe("Trouble Tank"); MainFrame mainframe = new MainFrame("Trouble Tank");
// } // }
} }
......
...@@ -3,8 +3,8 @@ package UI.LogIn; ...@@ -3,8 +3,8 @@ package UI.LogIn;
import javax.swing.*; import javax.swing.*;
import java.io.IOException; import java.io.IOException;
class Mainframe extends JFrame { class MainFrame extends JFrame {
public Mainframe(String gameName) throws IOException { public MainFrame(String gameName) throws IOException {
setTitle(gameName); setTitle(gameName);
MainPanel mainPanel = new MainPanel(); MainPanel mainPanel = new MainPanel();
add(mainPanel); add(mainPanel);
......
package UI.LogIn; package UI.LogIn;
import UI.GameFrame; import UI.LogIn.Game.GameFrame;
import UI.GameLoop; import UI.LogIn.Game.GameLoop;
import UI.ThreadPool; import UI.LogIn.Game.ThreadPool;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import javax.swing.*; import javax.swing.*;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment