Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need to add a firing obejct in order for the tanks to fire. I also need the tanks to move faster, and I need

I need to add a firing obejct in order for the tanks to fire. I also need the tanks to move faster, and I need a collision file.

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package componentS;

import java.util.Observable; import java.awt.Image; import java.awt.Point; import java.awt.image.ImageObserver; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.io.File; import javax.imageio.ImageIO;

/** * * @author sstev */ public class tankObject extends gameObject implements ActionListener, KeyListener {

public Image tankR, tankU, tankL, tankD; // Timer time;

public tankObject(Point loc, Image t, Point speed) { super(loc, t, speed); try { tankR = ImageIO.read(new File("/Users/jsierra/csc413-TankGame-SteveAtSFSU/tankResources/Tank1-R.png")); tankL = ImageIO.read(new File("/Users/jsierra/csc413-TankGame-SteveAtSFSU/tankResources/Tank1-L.png")); tankU = ImageIO.read(new File("/Users/jsierra/csc413-TankGame-SteveAtSFSU/tankResources/Tank1-U.png")); tankD = ImageIO.read(new File("/Users/jsierra/csc413-TankGame-SteveAtSFSU/tankResources/Tank1-D.png")); } catch (Exception e) {

} }

public void draw(Graphics g, ImageObserver obs) { //int tWidth = imag.getWidth(obs); //int tHeight = imag.getHeight(obs);

//int numX = (int) (1100 / tWidth); //int numY = (int) (600 / tHeight); g.drawImage(imag, loc.x, loc.y, obs);

}

@Override public void update(Observable o, Object arg) {

}

public void setImage(Image dir) { this.imag = dir; }

@Override public void actionPerformed(ActionEvent e) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. }

@Override public void keyTyped(KeyEvent e) { loc.x = loc.x+0; loc.y = loc.y+0; }

@Override public void keyPressed(KeyEvent e) { int code = e.getKeyCode();

switch (code) { case KeyEvent.VK_DOWN: loc.x = loc.x + 0; loc.y += speed.y; setImage(tankD); break; case KeyEvent.VK_UP: loc.y -= speed.y; loc.x = loc.x + 0; setImage(tankU); break; case KeyEvent.VK_LEFT: loc.y = loc.y + 0; loc.x -= speed.x; setImage(tankL); break; case KeyEvent.VK_RIGHT: loc.x += speed.x; loc.y = loc.y + 0; setImage(tankR); break; }

}

@Override public void keyReleased(KeyEvent e) {

}

}

...................................................

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package componentS;

import java.awt.Graphics; import java.awt.Image; import java.awt.Point; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.image.ImageObserver; import java.io.File; import java.util.Observable; import javax.imageio.ImageIO;

/** * * @author sstev */ public class tankObject2 extends gameObject implements ActionListener, KeyListener {

public Image tankR, tankU, tankL, tankD; //Timer time; public tankObject2(Point loc, Image t, Point speed) { super(loc, t, speed); try { tankR = ImageIO.read(new File("/Users/jsierra/csc413-TankGame-SteveAtSFSU/tankResources/Tank1-R.png")); tankL = ImageIO.read(new File("/Users/jsierra/csc413-TankGame-SteveAtSFSU/tankResources/Tank1-L.png")); tankU = ImageIO.read(new File("/Users/jsierra/csc413-TankGame-SteveAtSFSU/tankResources/Tank1-U.png")); tankD = ImageIO.read(new File("/Users/jsierra/csc413-TankGame-SteveAtSFSU/tankResources/Tank1-D.png")); } catch (Exception e) {

} }

public void draw(Graphics g, ImageObserver obs) { //int tWidth = imag.getWidth(obs); //int tHeight = imag.getHeight(obs);

//int numX = (int) (1100 / tWidth); //int numY = (int) (600 / tHeight); g.drawImage(imag, loc.x, loc.y, obs);

}

@Override public void update(Observable o, Object arg) {

}

public void setImage(Image dir) { this.imag = dir; }

@Override public void actionPerformed(ActionEvent e) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. }

@Override public void keyTyped(KeyEvent e) { }

@Override public void keyPressed(KeyEvent e) { int code = e.getKeyCode();

switch (code) { case KeyEvent.VK_S: loc.x = loc.x + 0; loc.y += speed.y; setImage(tankD); break; case KeyEvent.VK_W: loc.y -= speed.y; loc.x = loc.x + 0; setImage(tankU); break; case KeyEvent.VK_A: loc.y = loc.y + 0; loc.x -= speed.x; setImage(tankL); break; case KeyEvent.VK_D: loc.x += speed.x; loc.y = loc.y + 0; setImage(tankR); break;

} }

@Override public void keyReleased(KeyEvent e) {

}

}

.........................................................

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package componentS; import java.awt.Image; import java.util.Observable; import java.awt.Point;

/** * * @author sstev */ public class wallObject extends gameObject { public wallObject(Point loc, Image img, int speed){ super(loc,img,new Point(0,0)); }

public wallObject(Point loc, Image img, Point speed){ super(loc, img, speed); } @Override public void update(Observable o, Object arg) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } }

......................................

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package componentS;

import java.net.URL; import java.awt.*; import java.awt.image.*; import java.util.ArrayList; import java.util.HashMap; import java.util.ListIterator; import java.util.Observable; import java.util.Observer; import javax.imageio.*; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.util.Random; import java.io.File; import javax.swing.*; import javax.swing.Timer;

/** * * @author sstev */ public final class gameEnvironment extends JPanel implements Runnable, Observer {

private Thread thread;

private static final gameEnvironment tankGame = new gameEnvironment();

private BufferedImage buffImag;

Image minMap;

Graphics g2;

Point set = new Point(0, 0);

int mapWidth = 1200; int mapHeight = 700; public Image background, wall, tankR, tankL; ImageObserver obs; public tankObject tank1; public tankObject2 tank2; keyEvents ke1, ke2; //keyEvents keys = new keyEvents();

private ArrayList backgroundL; private ArrayList solidWallsL;

//public static HashMap sprites; //public static HashMap bSprites; // public static HashMap boolean gameVerdict, gg;

public gameEnvironment() { this.setFocusable(true);

// backgroundL = new ArrayList<>(); // solidWallsL = new ArrayList<>(); //sprites = new HashMap<>(); //bSprites = new HashMap<>(); }

public static gameEnvironment getInstance() { return tankGame; }

public void init() { this.setFocusable(true); setBackground(Color.white); //loadSprites(); obs = this; try { background = ImageIO.read(new File("/Users/jsierra/csc413-TankGame-SteveAtSFSU/tankResources/Background.png")); wall = ImageIO.read(new File("/Users/jsierra/csc413-TankGame-SteveAtSFSU/tankResources/Wall2.png")); tankR = ImageIO.read(new File("/Users/jsierra/csc413-TankGame-SteveAtSFSU/tankResources/Tank1-R.png")); tankL = ImageIO.read(new File("/Users/jsierra/csc413-TankGame-SteveAtSFSU/tankResources/Tank1-L.png")); } catch (Exception e) { System.out.println("error loading images"); }

tank2 = new tankObject2(new Point(50, 325), tankR, new Point(5, 5)); tank1 = new tankObject(new Point(1050, 325), tankL, new Point(5, 5)); //drawTank(tank, g2);

//ge2 = new gameEvents(); this.addKeyListener(tank2); this.addKeyListener(tank1);

//ge2.addObserver(tank2); //ctrl key2 = new ctrl(ge2); //addKeyListener(key2); gameVerdict = false; //int tWidth = background.getWidth(this); // int tHeight = background.getHeight(this);

// backgroundL.add(new backGround(background, set));

/* for (int i = 0; i < mapHeight; i += wall.getHeight(this)) { solidWallsL.add(new solidWall(new Point(0, i), wall, set)); } for (int i = 0; i < mapHeight; i += wall.getHeight(this)) { solidWallsL.add(new solidWall(new Point(mapWidth - 30, i), wall, set)); } for (int i = 0; i < mapWidth; i += wall.getWidth(this)) { solidWallsL.add(new solidWall(new Point(i, 0), wall, set)); } for (int i = 0; i < mapWidth; i += wall.getWidth(this)) { solidWallsL.add(new solidWall(new Point(i, mapHeight - 60), wall, set)); } */ }

/* private void loadSprites() { sprites.put("background", getSprite("/tankResources/Background.png")); sprites.put("wall", getSprite("/tankResources/Wall2.png"));

} */

/* public Image getSprite(String file) { URL url = gameEnvironment.class.getResource(file); Image imag = java.awt.Toolkit.getDefaultToolkit().getImage(url); try { MediaTracker tracker = new MediaTracker(this); tracker.addImage(imag, 0); tracker.waitForID(0); } catch (Exception e) {

} return imag; } */

/* public BufferedImage convertToBuffered(Image imag) { int w3 = imag.getWidth(null); int h3 = imag.getHeight(null);

BufferedImage buffImag2 = new BufferedImage(w3, h3, BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = buffImag2.createGraphics(); g2.drawImage(buffImag2, null, x, y); g2.dispose(); return buffImag2; } */ public void addBackground(backGroundObject... ob) { for (backGroundObject backGround : ob) { backgroundL.add(backGround); } }

public void addSolidWalls(solidWall... ob) { for (solidWall solidwall : ob) { solidWallsL.add(solidwall); } }

public ListIterator getBackgroundObjects() { return backgroundL.listIterator(); }

public ListIterator getSolidWalls() { return solidWallsL.listIterator(); }

public void setDimensions(int w, int h) { this.mapWidth = w; this.mapHeight = h; }

public void drawGameFrame() { /* ListIterator iterator = getBackgroundObjects(); while (iterator.hasNext()) { backGroundObject bgo = (backGroundObject) iterator.next();

bgo.update(w, h); if (bgo.getY() > h || !bgo.show) { iterator.remove(); } bgo.draw(g, this); }

iterator = getSolidWalls(); while (iterator.hasNext()) { wallObject wo = (wallObject) iterator.next(); wo.update(w, h); wo.draw(g, this); } */ //drawBackground(); }

public void drawBackground(Image b, Image w, Graphics g) { int tWidth = b.getWidth(this); int tHeight = b.getHeight(this);

//int numX = (int) (1100 / tWidth); //int numY = (int) (600 / tHeight); for (int i = 0; i <= mapHeight; i++) { for (int j = 0; j <= mapWidth; j++) { // g2.drawImage(background, j * tWidth, i * tHeight, tWidth, tHeight, this); g.drawImage(b, j * tWidth, i * tHeight, tWidth, tHeight, obs); } }

for (int i = 0; i <= mapHeight + 320; i += wall.getHeight(this)) { g.drawImage(w, 0, i, obs); } for (int i = 0; i <= mapHeight + 320; i += wall.getHeight(this)) { g.drawImage(w, 1170, i, obs); } for (int i = 0; i <= mapWidth + 180; i += wall.getWidth(this)) { g.drawImage(w, i, 0, obs); } for (int i = 0; i <= mapWidth + 180; i += wall.getWidth(this)) { g.drawImage(w, i, 650, obs); }

}

public void drawTank(tankObject t1, Graphics g) { //int tWidth = t1.imag.getWidth(obs); // int tHeight = t1.imag.getHeight(obs);

g.drawImage(t1.imag, t1.loc.x, t1.loc.y, obs);

}

public Graphics2D createGraphics(int w, int h) { Graphics2D g2 = null; if (buffImag == null || buffImag.getWidth() != w || buffImag.getHeight() != h) { buffImag = (BufferedImage) createImage(w, h); }

g2 = buffImag.createGraphics(); g2.setBackground(getBackground()); g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g2.clearRect(0, 0, w, h); return g2; }

@Override public void paint(Graphics g) { super.paintComponent(g); Dimension windowSize = getSize(); Graphics2D g2 = createGraphics(windowSize.width, windowSize.height); //drawGameFrame(windowSize.width, windowSize.height, g2); drawBackground(background, wall, g); //drawTank(tank1,g); //drawTank(tank2,g); tank1.draw(g, obs); tank2.draw(g, obs);

//addKeyListener(); }

public void start() { thread = new Thread(this); thread.setPriority(Thread.MIN_PRIORITY); thread.start(); }

@Override public void run() { Thread game = Thread.currentThread(); while (thread == game) { this.requestFocusInWindow(); repaint();

try { thread.sleep(2); } catch (InterruptedException e) { break; } } }

@Override public void update(Observable o, Object arg) {

} }

....................

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package componentS; import java.awt.Graphics; import java.awt.Image; import java.awt.Point; import java.awt.image.ImageObserver; /** * * @author sstev */ public class backGround extends backGroundObject { //int next, w2, h2; public backGround(Image img, Point speed){ super(new Point(0,0),img,speed); this.imag = img; } @Override public void draw(Graphics g, ImageObserver obs){ int tWidth = imag.getWidth(obs); int tHeight = imag.getHeight(obs); int numX = (int) (1100/tWidth); int numY = (int) (600/tHeight); for(int i = -1; i <= numY; i++){ for(int j = 0; j <= numX; j++){ g.drawImage(imag, j * tWidth, i * tHeight, tWidth, tHeight, obs); } }

} }

..........................

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package componentS; import java.awt.Image; import java.util.Observable; import java.awt.Point; //import java.awt.Point;

/** * * @author sstev */ public class backGroundObject extends gameObject { public backGroundObject(Image img, Point loc){ super(loc,img,new Point(0,1)); } public backGroundObject(Point loc, Image img, Point speed){ super(loc, img, speed); }

@Override public void update(Observable o, Object arg) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } }

...................

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package componentS; import java.awt.event.*; /** * * @author sstev */ public class ctrl extends KeyAdapter { private keyEvents ge; //public ctrl(){ //} public ctrl(keyEvents ge){ this.ge = ge; } public void keyReleased(keyEvents e){ ge.setValue(e); } }

...........................

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package componentS;

import java.awt.event.*; import java.util.Observer; import java.util.Observable; /** * * @author sstev */ public class gameEvents extends Observable{ private int type; final int keyE = 1; final int col = 2; public Object event; public KeyAdapter key; public void setValue(keyEvents e){ type = keyE; this.event = e; setChanged(); notifyObservers(this); } public void setValue(String msg){ type = col; event = msg; setChanged(); notifyObservers(this); } public int getType(){ return type; } public void setType(int newType){ this.type = newType; } public Object getEvent(){ return event; } public KeyAdapter getKey(){ return this.key; } }

...............................

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package componentS;

import java.awt.*; import java.awt.image.*; import java.util.Observable; import java.util.Observer; import java.awt.Point;

import componentS.keyEvents;

/** * * @author sstev */ abstract public class gameObject implements Observer { protected Point loc; protected Point speed; public int w; public int h; public Image imag; ImageObserver eye; private boolean finished; public boolean show; public gameObject(Point loc,Image imag, Point speed){ this.loc = loc; this.imag = imag; w = imag.getWidth(eye); h = imag.getHeight(eye); show = false; finished = false; this.speed = speed; } // public gameObject(Point loc, Image imag){ // this(loc,imag,new Point(0,0)); // } public void draw(Graphics graphs, ImageObserver eye){ //int w = imag.getWidth(eye); // int h = imag.getHeight(eye); graphs.drawImage(imag,loc.x,loc.y, eye); } public void setImg(Image imag){ this.imag = imag; this.h = imag.getHeight(eye); this.w = imag.getWidth(eye); } /*private BufferedImage convertToBuffered(Image imag){ int w = imag.getWidth(null); int h = imag.getHeight(null); BufferedImage buffImag = new BufferedImage(w,h,BufferedImage.TYPE_INT_ARGB); Graphics2D g = buffImag.createGraphics(); g.drawImage(buffImag, null, x, y); g.dispose(); return buffImag; } */ public void update(int w, int h){ loc.x += speed.x; loc.y += speed.y; if(loc.y < w-100 || loc.y == h+100){ this.show = false; } } public int getX(){ return loc.x; } public int getY(){ return loc.y; } public int getW(){ return w; } public int getH(){ return h; } public Point getSpeed(){ return speed; } public void setLoc(Point newLoc){ loc.setLocation(newLoc); } public void move(int dx, int dy){ loc.translate(speed.x, speed.y); } public void move(){ loc.translate(speed.x, speed.y); } public Point getLoc(){ return new Point(loc.x,loc.y); } public void hide(){ this.show = false; } public void show(){ this.show = true; } public void finished(){ this.finished = true; } public void notFinished(){ this.finished = false; } }

............................

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package componentS;

import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.util.Observable;

import javax.swing.Timer; /** * * @author sstev */ public class keyEvents extends Observable implements ActionListener,KeyListener{ Timer t; int x, y, sX, sY; public keyEvents(){ t = new Timer(1, this); t.start(); }

public void keyPressed(KeyEvent e){ int code = e.getKeyCode(); switch(code){ case KeyEvent.VK_DOWN: sX = 0; sY = 5; break; case KeyEvent.VK_UP: sY = -5; sX = 0; break; case KeyEvent.VK_LEFT: sY = 0; sX = -5; break; case KeyEvent.VK_RIGHT: sX = 5; sY = 0; break; } } public void keyTyped(KeyEvent e){ } public void keyReleased(KeyEvent e){ sX = 0; sY = 0; } @Override public void actionPerformed(ActionEvent ae){ this.x += this.sX; this.y += this.sY; if(x < 0){ x = 0; } if(y < 0){ y = 0; } } }

.........................

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package componentS; import java.awt.Graphics; import java.awt.Image; import java.awt.image.ImageObserver; import java.util.Observable; import java.awt.Point; /** * * @author sstev */ public class solidWall extends wallObject{ int w, h, next; public solidWall(Point loc, Image img, Point speed){ super(loc,img,speed); } @Override public void draw(Graphics g, ImageObserver obs){ int tWidth = imag.getWidth(obs); int tHeight = imag.getHeight(obs); int numX = (int) (w / tWidth); int numY = (int) (h / tHeight); for(int i = -1; i <= numY; i++){ for(int j = 0; j <= numX; j++){ g.drawImage(imag, i, j, obs); } } } }

...............................

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package componentS;

import java.awt.Dimension; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JPanel; import java.awt.*;

import javax.swing.JFrame;

/** * * @author sstev */ public class tankGame { gameEnvironment game; Thread thread; public static void main(String args[]){ final gameEnvironment game = gameEnvironment.getInstance(); JFrame frame = new JFrame("Tank Wars"); frame.addWindowListener(new WindowAdapter(){ @Override public void windowGainedFocus(WindowEvent e) { game.requestFocusInWindow(); } }); frame.getContentPane().add("Center", game); frame.pack(); frame.setSize(new Dimension(1200, 700)); game.setDimensions(1100, 600); game.init(); frame.setVisible(true); frame.setResizable(false); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); game.start(); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Microsoft SQL Server 2012 Administration

Authors: Adam Jorgensen, Steven Wort

1st Edition

1118106881, 9781118106884

More Books

Students also viewed these Databases questions