Commit ffeb34ac authored by 9731301's avatar 9731301

remove some bugs

parent 5253b190
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 {
this.walls = walls;
// Initialize the game state and all elements ...
// setFirstLocationOfTank();
locY = 120;
locX = 120;
setFirstLocationOfTank();
rotate = 0;
diam = 32;
keyUP = false;
......@@ -57,10 +56,10 @@ public class TankState {
Random randomX = new Random();
Random randomY = new Random();
while (true) {
locX = randomX.nextInt(GameFrame.GAME_WIDTH);
locY = randomY.nextInt(GameFrame.GAME_HEIGHT);
locX = randomX.nextInt(GameFrame.GAME_WIDTH-20);
locY = randomY.nextInt(GameFrame.GAME_HEIGHT-40);
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;
}
}
......
......@@ -7,7 +7,6 @@ import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.sql.Struct;
import java.util.ArrayList;
......@@ -44,19 +43,27 @@ public class WallState {
h = (frameY-30) / (numRows); //sideY
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) {
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.fillRect(0, 0, frameX, frameY);
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 y = 0;
......@@ -65,15 +72,15 @@ public class WallState {
String rowChars = lines[r];
for (int c = 0; c < numColumns; c++) {
if (rowChars.charAt(c) == '0') {
drawBlanks(g, x, y ,blankScaled);
saveBlank( x, y ,3);
x += w;
}
else if (rowChars.charAt(c) == '1') {
drawAndSaveWalls(g, x, y, r , c, wall1scaled);
saveWalls(x, y, r , c, 1);
x += w;
}
else if (rowChars.charAt(c) == '2') {
drawAndSaveWalls(g, x, y, r , c, wall2scaled);
saveWalls( x, y, r , c, 2);
x += w;
}
}
......@@ -83,8 +90,8 @@ public class WallState {
private void drawAndSaveWalls(Graphics g2d,int x,int y , int r , int c ,Image image){
WallData wall = new WallData(x , y);
private void saveWalls(int x,int y , int r , int c ,int image){
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
wall.setHorizontal(true);
}
......@@ -92,11 +99,23 @@ public class WallState {
wall.setVertical(true);
}
walls.add(wall);
g2d.drawImage(image , x , y , null);
}
private void drawBlanks(Graphics g2d,int x,int y,Image image){
g2d.drawImage(image , x , y , null);
private void drawWall(Graphics g2d,WallData data){
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){
......@@ -118,27 +137,30 @@ public class WallState {
public ArrayList<WallData> getWalls() {
return walls;
}
public class WallData {
private int image;
private int x,y;
private boolean horizontal = 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.y = y;
this.image = image;
this.blank = isBlank;
}
public boolean isWallForTank (int x,int y){
//BUG
if (blank) return false;
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){
//BUG
if (blank) return false;
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.*;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferStrategy;
import java.io.IOException;
public class GameFrame extends JFrame {
......@@ -48,6 +49,8 @@ public class GameFrame extends JFrame {
try {
rendering.doRendering(graphics, myTank , enemyTank );
} catch (IOException e) {
e.printStackTrace();
} finally {
// Dispose the graphics
graphics.dispose();
......
......@@ -36,6 +36,7 @@ public class GameLoop implements Runnable {
public GameLoop(GameFrame frame) {
canvas = frame;
}
public static long startTime;
/**
* This must be called before the game loop starts.
......@@ -47,6 +48,7 @@ public class GameLoop implements Runnable {
canvas.addKeyListener(myTank.getMyListener());
canvas.addMouseListener(myTank.getMouseListener());
canvas.addMouseMotionListener(myTank.getMouseMotionListener());
startTime = 0;
}
@Override
......
......@@ -3,6 +3,7 @@ package UI.LogIn.Game;
import UI.GameState.BulletState;
import UI.GameState.EnemyTank;
import UI.GameState.MyTank;
import UI.GameState.RewardState;
import UI.LogIn.Setting;
import javax.imageio.ImageIO;
import java.awt.*;
......@@ -12,6 +13,7 @@ import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
public class Rendering {
......@@ -22,6 +24,7 @@ public class Rendering {
public static final int GAME_WIDTH = 16 * GAME_HEIGHT / 9; // wide aspect ratio
int lastRender = -1;
private ArrayList<Float> fpsHistory;
private ArrayList<RewardState> rewards = new ArrayList<>();
private Setting setting = new Setting();
public Rendering(){
......@@ -33,7 +36,7 @@ public class Rendering {
*/
// Draw all game elements according
// 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
myTank.getWalls().paintComponent(g2d);
enemyTank.getWalls().paintComponent(g2d);
......@@ -54,7 +57,7 @@ public class Rendering {
// Drawing the rotated image at the required drawing locations
int lastX1 = 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);
lastX1 = enemyTank.locX;
lastY1 = enemyTank.locY;
......@@ -78,7 +81,7 @@ public class Rendering {
// Drawing the rotated image at the required drawing locations
int lastX2 = 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);
lastX2 = enemyTank.locX;
lastY2 = enemyTank.locY;
......@@ -87,10 +90,12 @@ public class Rendering {
g2d.drawImage(op2.filter( image22, null),lastX2 , lastY2 , null);
//Draw myTank bullet
if (myTank.getBullets().size() != 0 && myTank.gameOver == false && enemyTank.gameOver == false) {
for (BulletState bullet : myTank.getBullets()) {
if (myTank.getBullets().size() != 0 && !myTank.gameOver && !enemyTank.gameOver) {
Iterator<BulletState> it = myTank.getBullets().iterator();
while (it.hasNext()) {
BulletState bullet = it.next();
if (System.currentTimeMillis() - bullet.getCreateTime() > 4000) {
// myTank.getBullets().remove(bullet);
it.remove();
continue;
}
bullet.update();
......@@ -100,10 +105,12 @@ public class Rendering {
}
//Draw enemy tank bullet
if (enemyTank.getBullets().size() != 0 && myTank.gameOver == false && enemyTank.gameOver == false) {
for (BulletState bullet : enemyTank.getBullets()) {
if (enemyTank.getBullets().size() != 0 && !myTank.gameOver && !enemyTank.gameOver) {
Iterator<BulletState> it = enemyTank.getBullets().iterator();
while (it.hasNext()) {
BulletState bullet = it.next();
if (System.currentTimeMillis() - bullet.getCreateTime() > 4000) {
//enemyTank.getBullets().remove(bullet);
it.remove();
continue;
}
bullet.update();
......@@ -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
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