So im making space invaders, and I cant quite figure out what I have to do in order to get the ship to move. These are the three classes that are involved in this process.. with the SIbase being the actual code that contains that ship.
SIbase and SIinvader are both subclasses of the SIpanel.
import java.applet.Applet; import java.applet.AudioClip; import java.awt.Graphics; import java.awt.Image; import java.net.URL; import java.util.Random; import javax.swing.ImageIcon; public abstract class SIinvader extends SIship { private int xpos = 0; private int ypos = 0; private int direction = 0; protected int dx; protected int x; public int mysVal(){ int value = -1; Random gen = new Random(); int random = gen.nextInt(3); if (random == 0) { value = 50; } else if(random == 1) { value = 100; } else if(random == 2) { value = 150; } else if(random == 3) { value = 300; } return value; } public void move(char udlr) { if (udlr == 'l') setXPos(xPos() - 5); else if (udlr == 'r') setXPos(xPos() + 5); else if (udlr == 'd') setYPos(yPos() + 12); } public void move(boolean movingRight, boolean movingLeft, int speed) { if (movingRight && !movingLeft) setXPos(xPos() + 5); else if (movingLeft && !movingRight) setXPos(xPos() - 5); else if(!movingLeft && !movingRight) setXPos(xPos()); } public int xPos() { return xpos; } public int yPos() { return ypos; } public void setXPos(int x) { xpos = x; } public void setYPos(int y) { ypos = y; } public int direction(int udlr) { return -1; } protected Image getImage(String filename) { URL url = getClass().getResource(filename); ImageIcon icon = new ImageIcon(url, filename); return icon.getImage(); } protected AudioClip getSound(String filename) { URL url = getClass().getResource(filename); return Applet.newAudioClip(url); } public abstract void paintComponent(Graphics g); }
import java.awt.Graphics; import java.awt.Image; import java.awt.event.KeyEvent; import javax.swing.ImageIcon; import java.awt.Event; public class SIbase extends SIinvader { public final int BOARD_WIDTH = 500; public final int BOARD_HEIGTH = 450; public int dx; public int dy; private Image base; public SIbase() { String filename; filename = "SIbase.gif"; base = getImage(filename); } public void paintComponent(Graphics g) { g.drawImage(base, xPos(), yPos(), null); } public void keyPressed(KeyEvent e) { int key = e.getKeyCode(); if (key == KeyEvent.VK_LEFT) { dx = -2; } if (key == KeyEvent.VK_RIGHT) { dx = 2; } } public void keyReleased(KeyEvent e, Graphics g) { int key = e.getKeyCode(); if (key == KeyEvent.VK_LEFT) { dx = 0; g.drawImage(base, (xPos()-2), (yPos() -2), null); } if (key == KeyEvent.VK_RIGHT) { dx = 0; g.drawImage(base, (xPos()+2), (yPos() +2), null); } } }
public class SIpanel extends JPanel implements KeyListener, ActionListener { private double timerInvaders = 40; private int timerShipAndMystery = 2; private double paceInvaders = 40; private int paceShip = 2; private boolean movingRight; private boolean movingLeft; private SImystery mystery; private ArrayList<SItop> top; private ArrayList<SImiddle> middle; private ArrayList<SIbottom> bottom; private SIbase base; private int startY = 80; private int startX = 0; private int diffYPos = 25; private int diffXPos = 35; char direction = ' '; int alt = 0; int ticks = 1; public SIpanel() { base = new SIbase(); base.setXPos(250); base.setYPos(425); startX = 95; top = new ArrayList<SItop>(); middle = new ArrayList<SImiddle>(); bottom = new ArrayList<SIbottom>(); // top created and Y set for (int i = 0; i < 10; i++) { top.add(new SItop()); top.get(i).setYPos(startY); } // middle and bottom created for (int i = 0; i < 20; i++) { middle.add(new SImiddle()); bottom.add(new SIbottom()); } // middle and bottom Y set for (int i = 0; i < 10; i++) { middle.get(i).setYPos(startY + diffYPos); middle.get(i + 10).setYPos(startY + (diffYPos * 2)); bottom.get(i).setYPos(startY + (diffYPos * 3)); bottom.get(i + 10).setYPos(startY + (diffYPos * 4)); } // top X set for (int i = 0; i < 10; i++) { top.get(i).setXPos(startX + (diffXPos * i)); // System.out.println(top.get(i).xPos()); } // middle and bottom X set for (int i = 0; i < 10; i++) { middle.get(i).setXPos(startX + (diffXPos * i)); middle.get(i + 10).setXPos(startX + (diffXPos * i)); bottom.get(i).setXPos(startX + (diffXPos * i)); bottom.get(i + 10).setXPos(startX + (diffXPos * i)); } repaint(); addKeyListener(this); new Timer(10, this).start(); if (timerShipAndMystery == 0) { base.move(movingRight, movingLeft); System.out.println("Base moving"); timerShipAndMystery = paceShip; } else timerShipAndMystery--; direction = 'r'; new Timer(10, new ActionListener() { public void actionPerformed(ActionEvent e) { if(ticks < 40 ) { ticks++; } else { ticks = 1; } for(int i = 0; i < top.size(); i++) { top.get(i).setTop(ticks); } for(int i = 0; i < middle.size(); i++) { middle.get(i).setMiddle(ticks); } for(int i = 0; i < bottom.size(); i++) { bottom.get(i).setBottom(ticks); } if (timerInvaders == 0) { if (top.get(0).xPos() <= 0 && direction == 'l') { alt++; direction = 'd'; paceInvaders = paceInvaders - (timerInvaders * .2); for (int j = 0; j < top.size(); j++) { top.get(j).move(direction); } for (int j = 0; j < middle.size(); j++) { middle.get(j).move(direction); bottom.get(j).move(direction); } direction = 'r'; for (int j = 0; j < top.size(); j++) { top.get(j).move(direction); } for (int j = 0; j < middle.size(); j++) { middle.get(j).move(direction); bottom.get(j).move(direction); } } else if (top.get(9).xPos() + 30 >= 500 && direction == 'r') { alt++; direction = 'd'; paceInvaders = paceInvaders - (timerInvaders * .2); for (int j = 0; j < top.size(); j++) { top.get(j).move(direction); } for (int j = 0; j < middle.size(); j++) { middle.get(j).move(direction); bottom.get(j).move(direction); } direction = 'l'; for (int j = 0; j < top.size(); j++) { top.get(j).move(direction); } for (int j = 0; j < middle.size(); j++) { middle.get(j).move(direction); bottom.get(j).move(direction); } } else { for (int j = 0; j < top.size(); j++) { top.get(j).move(direction); } for (int j = 0; j < middle.size(); j++) { middle.get(j).move(direction); bottom.get(j).move(direction); } } timerInvaders = paceInvaders * .5; } else timerInvaders--; repaint(); } }).start(); } public Dimension getPreferredSize() { Dimension d = new Dimension(500, 450); return d; } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); base.paintComponent(g); for (int i = 0; i < 10; i++) { top.get(i).paintComponent(g); } for (int i = 0; i < 20; i++) { middle.get(i).paintComponent(g); bottom.get(i).paintComponent(g); } } @Override public void actionPerformed(ActionEvent arg0) { base.move(movingRight, movingLeft); repaint(); } @Override public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_RIGHT) { movingRight = true; movingLeft = false; base.move(movingRight, movingLeft); System.out.println("Moving right"); } else if (e.getKeyCode() == KeyEvent.VK_LEFT) { movingRight = false; movingLeft = true; base.move(movingRight, movingLeft); System.out.println("Moving left"); } else if (e.getKeyCode() == KeyEvent.VK_SPACE) { // base.playShootSound(); } repaint(); } @Override public void keyReleased(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_RIGHT) { movingRight = false; movingLeft = false; base.move(movingRight, movingLeft); } else if (e.getKeyCode() == KeyEvent.VK_LEFT) { movingRight = false; movingLeft = false; base.move(movingRight, movingLeft); } repaint(); } @Override public void keyTyped(KeyEvent arg0) { } }