Commit 05bedc2e authored by 9731301's avatar 9731301

complete shooting bullet

parent 36ee6d79
This diff is collapsed.
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 (keySpace){
if (bullets.size() == 0 || System.currentTimeMillis() -bullets.get(bullets.size()-1).getCreateTime()>500 ) {
// for (int i = 0; i < 100; i++) { BulletState newBullet = new BulletState(walls, this);
bulletState.locY += 5*Math.sin(rotate) ; bullets.add(newBullet);
System.out.println(bulletState.locY ); System.out.println(newBullet.angle);
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