Space Invaders

//Starter import javax.swing.JFrame; public class Starter extends JFrame { public Starter() { add(new AlienBoard()); setTitle("AlienBoard"); setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(500,500); setLocationRelativeTo(null); setVisible(true); setResizable(false); } public static void main(String[] args) { new Starter(); } } //Alienboard import java.awt.event.MouseListener; import java.awt.event.MouseEvent; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.Toolkit; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import javax.swing.Timer; import java.util.ArrayList; import java.util.Iterator; import java.util.Random; import java.util.*; import javax.swing.ImageIcon; import javax.swing.JPanel; import javax.imageio.*; import java.awt.image.*; import java.io.*; public class AlienBoard extends JPanel implements Runnable, MouseListener{ boolean startGame = false; boolean ingame = true; boolean gameOver = false; boolean goLeft = false; boolean goRight = false; boolean dropDown = false; boolean moveRight = true; boolean moveLeft = false; //boolean isHit = false; private Dimension d; int BOARD_WIDTH=500; int BOARD_HEIGHT=500; int x = 0; int speedX; int speedY; int sXValue; BufferedImage img; String message = "Click Board to Start"; private Thread animator; Alien[][] a = new Alien[3][5]; gameCharacter pl = new Player(BOARD_WIDTH/2, BOARD_HEIGHT -30); //gameCharacter s = new Shot(pl.x, pl.y +10); //Shot [] s = new Shot[10000]; ArrayList<Shot> s = new ArrayList<Shot>(); public AlienBoard(){ addKeyListener(new TAdapter()); addMouseListener(this); setFocusable(true); d = new Dimension(BOARD_WIDTH, BOARD_HEIGHT); setBackground(Color.black); int ax = 10; int ay = 10; speedX = 5; speedY = 10; for(int r=0; r<a.length; r++){ for (int c=0; c<a[0].length; c++){ a[r][c] = new Alien(ax, ay); ax += 36; } ax = 10; ay += 30; } /* try { img = ImageIO.read(this.getClass().getResource("mount.jpg")); } catch (IOException e) { System.out.println("Image could not be read"); // System.exit(1); } */ if (animator == null || !ingame) { animator = new Thread(this); animator.start(); } setDoubleBuffered(true); } public void paint(Graphics g){ super.paint(g); g.setColor(Color.black); g.fillRect(0, 0, d.width, d.height); g.setColor(new Color(127,0,255)); g.fillRect(pl.x, pl.y, 20, 20); for(int r=0; r<a.length; r++){ for (int c=0; c<a[0].length; c++){ g.fillRect(a[r][c].x, a[r][c].y, 20, 20); if(moveLeft==true){ a[r][c].x -= 5; }//aliens moving left if(moveRight==true){ a[r][c].x += 5; }//aliens moving right } } if(goLeft==true){ pl.x -= speedX; }//player moving left if(goRight==true){ pl.x += speedX; }//player moving right for(int i=0; i<s.size(); i++){ if(s.get(i).shoot==true){ g.setColor(Color.red); g.fillOval(s.get(i).x + 7,s.get(i).y,7,21); s.get(i).y -= 5; if(s.get(i).y < 0){ s.get(i).shoot=false; } }//player shooting } if(gameOver==true){ g.drawString("You Lost" ,250,250); }//gameover for(int r=0; r<a.length; r++){ for (int c=0; c<a[0].length; c++){ if(a[r][c].x > BOARD_WIDTH || a[r][c].x < 0){ dropDown = true; r = a.length; c = a[0].length; dropAlien(); } if (a[r][c].y == pl.y){ gameOver = true; }//gameover if aliens go below player } } //g.fillOval(x,y,r,r); /* for (int w=0; w < s.length; w++){ s[w] = new Shot(pl.x, pl.y + 10); g.setColor(Color.red); g.fillOval(pl.x + 7,s.y,7,21); }//attempt to turn shot into an array */ Font small = new Font("Helvetica", Font.BOLD, 14); FontMetrics metr = this.getFontMetrics(small); g.setColor(Color.white); g.setFont(small); g.drawString(message, 10, d.height-60); youHitAlien(); if (ingame) { // g.drawImage(img,0,0,200,200 ,null); } Toolkit.getDefaultToolkit().sync(); g.dispose(); }//end of paint method public void dropAlien(){ if(dropDown==true){ if(moveRight == true){ moveRight = false; moveLeft = true; }else{ moveLeft = false; moveRight = true; } //programs alien to move right and left depending on which side of the board it hits for(int r=0; r<a.length; r++){ for (int c=0; c<a[0].length; c++){ a[r][c].y += 15; } }//programs aliens to incriment down a row each time it hits a side of the board } dropDown=false; }//end of dropAlien method public void youHitAlien(){ for(int r=0; r<a.length; r++){ for (int c=0; c<a[0].length; c++){ /* if(s.y < a[r][c].y + 21 && s.y + 12 > a[r][c].y && s.x + 5 > a[r][c].x && s.x < a[r][c].x + 7){ //a[r][c].isHit = true; a[r][c].y = (-10000); s.y = -1000; }*/ } } }//end of Hit ALien method private class TAdapter extends KeyAdapter { public void keyPressed(KeyEvent e) { System.out.println( e.getKeyCode()); // message = "Key Pressed: " + e.getKeyCode(); int key = e.getKeyCode(); if (key==37){ goLeft = true; goRight = false; }//left arrow key if (key==39){ goRight = true; goLeft = false; }//right arrow key if (key==32){ sXValue = pl.x; //System.out.println("this works"); Shot t = new Shot(sXValue, pl.y + 10); if(t.y < 0){ s.add(t); } // s.get(i).x = sXValue; //new g.fillOval(s.x + 7,s.y,7,21); } //space bar shooting }//end of key press method public void keyReleased(KeyEvent e) { int key = e.getKeyCode(); if (key==37){ goLeft = false; }//left arrow key release if (key==39){ goRight = false; } // right arrow key release //if (key==32){ //if (s.y == 0){ //shoot = false; // } }//end of key release method } public void mousePressed(MouseEvent e) { int x = e.getX(); int y = e.getY(); // mouseString = "X position: " + me.getX() + " " + //"Y position: " + me.getY(); //if(startGame==false) // startGame=true; //repaint(); } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mouseClicked(MouseEvent e) { } public void run() { long beforeTime, timeDiff, sleep; beforeTime = System.currentTimeMillis(); int animationDelay = 10; long time = System.currentTimeMillis(); while (true) {//infinite loop // spriteManager.update(); repaint(); try { time += animationDelay; Thread.sleep(Math.max(0,time - System.currentTimeMillis())); }catch (InterruptedException e) { System.out.println(e); }//end catch }//end while loop }//end of run }//end of class //shot public class Shot extends gameCharacter{ int x; int y; boolean shoot = false; public Shot(int x, int y){ super(x, y); shoot = true; //System.out.println("this works"); } } //gamecharacter public class gameCharacter{ int x; int y; public gameCharacter(int x, int y){ this.x = x; this.y = y; } } //player public class Player extends gameCharacter{ boolean goRight = false; boolean goLeft = false; public Player (int x, int y){ super(x,y); /*int key = e.getKeyCode(); if (key==37){ goLeft = true; goRight = false; } if (key==39){ goRight = true; goLeft = false; }*/ } } //alien public class Alien extends gameCharacter{ boolean isVis; public Alien(int x, int y){ super(x, y); isVis = true; } }
All space invader's code so far. The shot is not working.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.