Commit ffeb34ac authored by 9731301's avatar 9731301

remove some bugs

parent 5253b190
No related merge requests found
This diff is collapsed.
File added
File deleted
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
pics/bullet.png

10.8 KB

pics/bullets.png

15.6 KB

pics/heart.png

10.8 KB

File added
pics/laser.png

1.86 KB

pics/shield.png

18.7 KB

package UI.GameState;
import UI.LogIn.Game.GameFrame;
import javax.imageio.ImageIO;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.Random;
public class RewardState {
private WallState walls ;
public int locX , locY;
private boolean drawing = false;
private Image reward;
private boolean visible ;
int numOfReward = chooseRandomReward();
public RewardState(WallState wallState){
this.walls = wallState ;
setFirstLocationOfReward();
}
private void shield(TankState tank){
}
private void laser(TankState tank){
}
private void extraLife(TankState tank){
}
private void extraBulletDamage(TankState tank){
}
private void extraBulletDamage2(TankState tank){
}
private void setFirstLocationOfReward() {
Random randomX = new Random();
Random randomY = new Random();
while (true) {
locX = randomX.nextInt(GameFrame.GAME_WIDTH);
locY = randomY.nextInt(GameFrame.GAME_HEIGHT);
if (!walls.isWallForTank(locX , locY) && locX > 10 && locY >10 && locX < GameFrame.GAME_WIDTH-10 && locY < GameFrame.GAME_HEIGHT -10)
break;
}
}
public int chooseRandomReward(){
Random random = new Random();
int x = random.nextInt(5);
System.out.println(x);
return x;
}
private Image rewardImage = null;
public Image getRewardImage() throws IOException {
this.drawing = drawing;
if (drawing = true) {
if (rewardImage!=null) return rewardImage;
int x = numOfReward;
switch (x) {
case 0:
reward = ImageIO.read(new File("C:\\Users\\Lenovo\\IdeaProjects\\JTankTrouble\\pics\\shield.png"));break;
case 1:
reward = ImageIO.read(new File("C:\\Users\\Lenovo\\IdeaProjects\\JTankTrouble\\pics\\heart.png"));break;
case 2:
reward = ImageIO.read(new File("C:\\Users\\Lenovo\\IdeaProjects\\JTankTrouble\\pics\\bullet.png"));break;
case 3:
reward = ImageIO.read(new File("C:\\Users\\Lenovo\\IdeaProjects\\JTankTrouble\\pics\\bullets.png"));break;
case 4:
reward = ImageIO.read(new File("C:\\Users\\Lenovo\\IdeaProjects\\JTankTrouble\\pics\\laser.png"));break;
}
rewardImage = reward.getScaledInstance(30, 30, java.awt.Image.SCALE_SMOOTH);
return rewardImage;
}
return null;
}
public void runReward(TankState tankState){
int x = numOfReward;
switch (x) {
case 0:shield(tankState);break;
case 1:extraLife(tankState);break;
case 2:extraBulletDamage(tankState);break;
case 3:extraBulletDamage2(tankState);break;
case 4:laser(tankState);break;
}
}
public boolean isRewardForTank (int x,int y){
//BUG
return (x>this.locX-40 && x<this.locX+40 && y>this.locY-40 && y<this.locY+40 && x < GameFrame.GAME_WIDTH - 40 && y<GameFrame.GAME_HEIGHT-30);
}
public boolean isVisible() {
return visible;
}
public void setVisible(boolean visible) {
this.visible = visible;
}
}
package UI.GameState;
public class RewarsState {
}
...@@ -31,8 +31,7 @@ public class TankState { ...@@ -31,8 +31,7 @@ public class TankState {
this.walls = walls; this.walls = walls;
// Initialize the game state and all elements ... // Initialize the game state and all elements ...
// setFirstLocationOfTank(); // setFirstLocationOfTank();
locY = 120; setFirstLocationOfTank();
locX = 120;
rotate = 0; rotate = 0;
diam = 32; diam = 32;
keyUP = false; keyUP = false;
...@@ -57,10 +56,10 @@ public class TankState { ...@@ -57,10 +56,10 @@ public class TankState {
Random randomX = new Random(); Random randomX = new Random();
Random randomY = new Random(); Random randomY = new Random();
while (true) { while (true) {
locX = randomX.nextInt(GameFrame.GAME_WIDTH); locX = randomX.nextInt(GameFrame.GAME_WIDTH-20);
locY = randomY.nextInt(GameFrame.GAME_HEIGHT); locY = randomY.nextInt(GameFrame.GAME_HEIGHT-40);
System.out.println(locX +" "+locY); System.out.println(locX +" "+locY);
if (!walls.isWallForTank(locX , locY) && locX > 10 && locY >10) if (!walls.isWallForTank(locX , locY) && locX > 10 && locY >10 && locX < GameFrame.GAME_WIDTH-10 && locY < GameFrame.GAME_HEIGHT -10)
break; break;
} }
} }
......
...@@ -7,7 +7,6 @@ import java.io.IOException; ...@@ -7,7 +7,6 @@ import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.sql.Struct;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -44,19 +43,27 @@ public class WallState { ...@@ -44,19 +43,27 @@ public class WallState {
h = (frameY-30) / (numRows); //sideY h = (frameY-30) / (numRows); //sideY
w = (frameX-10) / (numColumns); //sideX w = (frameX-10) / (numColumns); //sideX
} wall1 = wall1.getScaledInstance(w,h,Image.SCALE_SMOOTH);
wall2 = wall2.getScaledInstance(w,h,Image.SCALE_SMOOTH);
blank = blank.getScaledInstance(w,h,Image.SCALE_SMOOTH);
loadWalls();
}
public void paintComponent(Graphics g) { public void paintComponent(Graphics g) {
Image wall1scaled = wall1.getScaledInstance(w, h, java.awt.Image.SCALE_SMOOTH);
Image wall2scaled = wall2.getScaledInstance(w, h, java.awt.Image.SCALE_SMOOTH);
Image blankScaled = blank.getScaledInstance(w, h, java.awt.Image.SCALE_SMOOTH);
// Draw background
walls.clear();
g.setColor(Color.WHITE); g.setColor(Color.WHITE);
g.fillRect(0, 0, frameX, frameY); g.fillRect(0, 0, frameX, frameY);
g.setColor(Color.black); //colour of the walls g.setColor(Color.black); //colour of the walls
for (WallData d :walls){
drawWall(g,d);
}
}
public void loadWalls() {
// Draw background
walls.clear();
int x = 0; int x = 0;
int y = 0; int y = 0;
...@@ -65,15 +72,15 @@ public class WallState { ...@@ -65,15 +72,15 @@ public class WallState {
String rowChars = lines[r]; String rowChars = lines[r];
for (int c = 0; c < numColumns; c++) { for (int c = 0; c < numColumns; c++) {
if (rowChars.charAt(c) == '0') { if (rowChars.charAt(c) == '0') {
drawBlanks(g, x, y ,blankScaled); saveBlank( x, y ,3);
x += w; x += w;
} }
else if (rowChars.charAt(c) == '1') { else if (rowChars.charAt(c) == '1') {
drawAndSaveWalls(g, x, y, r , c, wall1scaled); saveWalls(x, y, r , c, 1);
x += w; x += w;
} }
else if (rowChars.charAt(c) == '2') { else if (rowChars.charAt(c) == '2') {
drawAndSaveWalls(g, x, y, r , c, wall2scaled); saveWalls( x, y, r , c, 2);
x += w; x += w;
} }
} }
...@@ -83,8 +90,8 @@ public class WallState { ...@@ -83,8 +90,8 @@ public class WallState {
private void drawAndSaveWalls(Graphics g2d,int x,int y , int r , int c ,Image image){ private void saveWalls(int x,int y , int r , int c ,int image){
WallData wall = new WallData(x , y); WallData wall = new WallData(x , y ,image,false);
if ((c<numColumns-1 && lines[r].charAt(c+1)=='1') || (c>0 && lines[r].charAt(c-1)=='1')||(c<numColumns-1 && lines[r].charAt(c+1)=='2') || (c>0 && lines[r].charAt(c-1)=='2')){ //horizontal if ((c<numColumns-1 && lines[r].charAt(c+1)=='1') || (c>0 && lines[r].charAt(c-1)=='1')||(c<numColumns-1 && lines[r].charAt(c+1)=='2') || (c>0 && lines[r].charAt(c-1)=='2')){ //horizontal
wall.setHorizontal(true); wall.setHorizontal(true);
} }
...@@ -92,11 +99,23 @@ public class WallState { ...@@ -92,11 +99,23 @@ public class WallState {
wall.setVertical(true); wall.setVertical(true);
} }
walls.add(wall); walls.add(wall);
g2d.drawImage(image , x , y , null);
} }
private void drawBlanks(Graphics g2d,int x,int y,Image image){ private void drawWall(Graphics g2d,WallData data){
g2d.drawImage(image , x , y , null); Image img;
if (data.image==1) {
img = wall1;
}else if (data.image==2){
img = wall2;
}else{
img = blank;
}
g2d.drawImage(img,data.x,data.y,null);
}
private void saveBlank(int x,int y,int image){
WallData wall = new WallData(x , y ,image,true);
walls.add(wall);
} }
public WallData getWallForBullet(int x,int y){ public WallData getWallForBullet(int x,int y){
...@@ -118,27 +137,30 @@ public class WallState { ...@@ -118,27 +137,30 @@ public class WallState {
public ArrayList<WallData> getWalls() {
return walls;
}
public class WallData { public class WallData {
private int image;
private int x,y; private int x,y;
private boolean horizontal = false; private boolean horizontal = false;
private boolean vertical = false; private boolean vertical = false;
private boolean blank;
public WallData(int x,int y){ public WallData(int x,int y,int image,boolean isBlank){
this.x = x; this.x = x;
this.y = y; this.y = y;
this.image = image;
this.blank = isBlank;
} }
public boolean isWallForTank (int x,int y){ public boolean isWallForTank (int x,int y){
//BUG //BUG
if (blank) return false;
return (x>this.x-w/2 && x<this.x+w && y>this.y-w/2 && y<this.y+h); return (x>this.x-w/2 && x<this.x+w && y>this.y-w/2 && y<this.y+h);
} }
public boolean isWallForBullet (int x,int y){ public boolean isWallForBullet (int x,int y){
//BUG //BUG
if (blank) return false;
return (x>this.x-5 && x<this.x+w+5 && y>this.y-5 && y<this.y+h+5); return (x>this.x-5 && x<this.x+w+5 && y>this.y-5 && y<this.y+h+5);
} }
......
...@@ -5,6 +5,7 @@ import UI.GameState.*; ...@@ -5,6 +5,7 @@ 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.io.IOException;
public class GameFrame extends JFrame { public class GameFrame extends JFrame {
...@@ -48,6 +49,8 @@ public class GameFrame extends JFrame { ...@@ -48,6 +49,8 @@ public class GameFrame extends JFrame {
try { try {
rendering.doRendering(graphics, myTank , enemyTank ); rendering.doRendering(graphics, myTank , enemyTank );
} catch (IOException e) {
e.printStackTrace();
} finally { } finally {
// Dispose the graphics // Dispose the graphics
graphics.dispose(); graphics.dispose();
......
...@@ -36,6 +36,7 @@ public class GameLoop implements Runnable { ...@@ -36,6 +36,7 @@ public class GameLoop implements Runnable {
public GameLoop(GameFrame frame) { public GameLoop(GameFrame frame) {
canvas = frame; canvas = frame;
} }
public static long startTime;
/** /**
* This must be called before the game loop starts. * This must be called before the game loop starts.
...@@ -47,6 +48,7 @@ public class GameLoop implements Runnable { ...@@ -47,6 +48,7 @@ public class GameLoop implements Runnable {
canvas.addKeyListener(myTank.getMyListener()); canvas.addKeyListener(myTank.getMyListener());
canvas.addMouseListener(myTank.getMouseListener()); canvas.addMouseListener(myTank.getMouseListener());
canvas.addMouseMotionListener(myTank.getMouseMotionListener()); canvas.addMouseMotionListener(myTank.getMouseMotionListener());
startTime = 0;
} }
@Override @Override
......
...@@ -3,6 +3,7 @@ package UI.LogIn.Game; ...@@ -3,6 +3,7 @@ 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.RewardState;
import UI.LogIn.Setting; import UI.LogIn.Setting;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import java.awt.*; import java.awt.*;
...@@ -12,6 +13,7 @@ import java.awt.image.BufferedImage; ...@@ -12,6 +13,7 @@ import java.awt.image.BufferedImage;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator;
public class Rendering { public class Rendering {
...@@ -22,6 +24,7 @@ public class Rendering { ...@@ -22,6 +24,7 @@ public class Rendering {
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;
private ArrayList<Float> fpsHistory; private ArrayList<Float> fpsHistory;
private ArrayList<RewardState> rewards = new ArrayList<>();
private Setting setting = new Setting(); private Setting setting = new Setting();
public Rendering(){ public Rendering(){
...@@ -33,7 +36,7 @@ public class Rendering { ...@@ -33,7 +36,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) { public void doRendering(Graphics2D g2d, MyTank myTank, EnemyTank enemyTank) throws IOException {
// Draw background // Draw background
myTank.getWalls().paintComponent(g2d); myTank.getWalls().paintComponent(g2d);
enemyTank.getWalls().paintComponent(g2d); enemyTank.getWalls().paintComponent(g2d);
...@@ -54,7 +57,7 @@ public class Rendering { ...@@ -54,7 +57,7 @@ public class Rendering {
// Drawing the rotated image at the required drawing locations // Drawing the rotated image at the required drawing locations
int lastX1 = 0; int lastX1 = 0;
int lastY1 = 0; int lastY1 = 0;
if (myTank.gameOver == false && enemyTank.gameOver == false) { if (!myTank.gameOver && !enemyTank.gameOver) {
g2d.drawImage(op.filter( image11, null),myTank.locX , myTank.locY , null); g2d.drawImage(op.filter( image11, null),myTank.locX , myTank.locY , null);
lastX1 = enemyTank.locX; lastX1 = enemyTank.locX;
lastY1 = enemyTank.locY; lastY1 = enemyTank.locY;
...@@ -78,7 +81,7 @@ public class Rendering { ...@@ -78,7 +81,7 @@ public class Rendering {
// Drawing the rotated image at the required drawing locations // Drawing the rotated image at the required drawing locations
int lastX2 = 0; int lastX2 = 0;
int lastY2 = 0; int lastY2 = 0;
if (myTank.gameOver == false && enemyTank.gameOver == false) { if (!myTank.gameOver && !enemyTank.gameOver) {
g2d.drawImage(op2.filter(image22, null), enemyTank.locX, enemyTank.locY, null); g2d.drawImage(op2.filter(image22, null), enemyTank.locX, enemyTank.locY, null);
lastX2 = enemyTank.locX; lastX2 = enemyTank.locX;
lastY2 = enemyTank.locY; lastY2 = enemyTank.locY;
...@@ -87,10 +90,12 @@ public class Rendering { ...@@ -87,10 +90,12 @@ public class Rendering {
g2d.drawImage(op2.filter( image22, null),lastX2 , lastY2 , null); g2d.drawImage(op2.filter( image22, null),lastX2 , lastY2 , null);
//Draw myTank bullet //Draw myTank bullet
if (myTank.getBullets().size() != 0 && myTank.gameOver == false && enemyTank.gameOver == false) { if (myTank.getBullets().size() != 0 && !myTank.gameOver && !enemyTank.gameOver) {
for (BulletState bullet : myTank.getBullets()) { Iterator<BulletState> it = myTank.getBullets().iterator();
while (it.hasNext()) {
BulletState bullet = it.next();
if (System.currentTimeMillis() - bullet.getCreateTime() > 4000) { if (System.currentTimeMillis() - bullet.getCreateTime() > 4000) {
// myTank.getBullets().remove(bullet); it.remove();
continue; continue;
} }
bullet.update(); bullet.update();
...@@ -100,10 +105,12 @@ public class Rendering { ...@@ -100,10 +105,12 @@ public class Rendering {
} }
//Draw enemy tank bullet //Draw enemy tank bullet
if (enemyTank.getBullets().size() != 0 && myTank.gameOver == false && enemyTank.gameOver == false) { if (enemyTank.getBullets().size() != 0 && !myTank.gameOver && !enemyTank.gameOver) {
for (BulletState bullet : enemyTank.getBullets()) { Iterator<BulletState> it = enemyTank.getBullets().iterator();
while (it.hasNext()) {
BulletState bullet = it.next();
if (System.currentTimeMillis() - bullet.getCreateTime() > 4000) { if (System.currentTimeMillis() - bullet.getCreateTime() > 4000) {
//enemyTank.getBullets().remove(bullet); it.remove();
continue; continue;
} }
bullet.update(); bullet.update();
...@@ -155,6 +162,35 @@ public class Rendering { ...@@ -155,6 +162,35 @@ public class Rendering {
} }
} }
//Draw rewards
boolean newReward = (System.currentTimeMillis() -GameLoop.startTime >= 7000);
if (newReward) {
GameLoop.startTime = System.currentTimeMillis();
RewardState reward = new RewardState(myTank.getWalls());
reward.setVisible(true);
rewards.add(reward);
}
if (rewards.size() != 0){
Iterator<RewardState> it = rewards.iterator();
while (it.hasNext()){
RewardState reward = it.next();
if (reward.isVisible() && reward.isRewardForTank(myTank.locX , myTank.locY)){
reward.runReward(myTank);
reward.setVisible(false);
it.remove();
}
else if (reward.isVisible() &&reward.isRewardForTank(enemyTank.locX , enemyTank.locY)){
reward.runReward(enemyTank);
reward.setVisible(false);
it.remove();
}
else if (reward.isVisible()){
g2d.drawImage(reward.getRewardImage(),reward.locX, reward.locY, null);
}
}
}
// Print FPS info // Print FPS info
long currentRender = System.currentTimeMillis(); long currentRender = System.currentTimeMillis();
......
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