Space Invaders

////Starter import javax.swing.JFrame; public class Starter extends JFrame { public Starter() { add(new Board()); setTitle("Board"); setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(500,500); setLocationRelativeTo(null); setVisible(true); setResizable(false); } public static void main(String[] args) { new Starter(); } } /////Board 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.awt.geom.AffineTransform; import java.awt.image.AffineTransformOp; import javax.swing.ImageIcon; import javax.swing.JPanel; import javax.imageio.*; import java.awt.image.*; public class Board extends JPanel implements Runnable, MouseListener { boolean ingame = true; private Dimension d; int BOARD_WIDTH=500; int BOARD_HEIGHT=500; int x = 0; BufferedImage alienImage; BufferedImage shotImage; BufferedImage playerImage; //String message = "Click Board to Start"; private Thread animator; gamChar pl = new Player(BOARD_WIDTH/2 - 50,BOARD_HEIGHT - 100); gamChar shot = new Shot(pl.x + 50,BOARD_HEIGHT - 100); Alien[][] al = new Alien[3][10]; private boolean moveRight = false; private boolean moveLeft = false; private boolean shotFired = false; int alienX; int lives = 3; int score = 0; //boolean invisible = false; int count = 0; boolean loose = false; public Board() { addKeyListener(new TAdapter()); addMouseListener(this); setFocusable(true); d = new Dimension(BOARD_WIDTH, BOARD_HEIGHT); setBackground(Color.black); moveRight = false; moveLeft = false; shotFired = false; int alx = 10; int aly = 10; //boolean dropdown = false; for(int r=0; r<al.length; r++){ for(int c = 0; c<al[0].length; c++){ al[r][c] = new Alien(alx, aly); alx += 30; } alx = 10; aly += 30; } try { alienImage = ImageIO.read(this.getClass().getResource("alien.png")); shotImage = ImageIO.read(this.getClass().getResource("ashot.png")); playerImage = ImageIO.read(this.getClass().getResource("player.png")); } catch(Exception 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.fillOval(x,y,r,r); Font small = new Font("Helvetica", Font.BOLD, 14); Font big = new Font("Helvetica", Font.BOLD, 24); FontMetrics metr = this.getFontMetrics(small); String message2 = "Lives: " + lives; String message3 = "Score : " + score; g.setColor(Color.white); g.setFont(small); //g.drawString(message, 10, d.height-60); if( ingame){ g.drawString(message2, 10, d.height-60); g.drawString(message3, 400, d.height-60); } if(loose = false){ g.setColor(Color.red); g.setFont(small); String message4 = "You Loose, Final Score : " + score; g.drawString(message4, 170, d.height-200); } if (ingame) { g.drawImage(playerImage,(int)pl.x,(int)pl.y,100,50 ,null); //} for(int r=0; r<al.length; r++){ for(int c = 0; c<al[0].length; c++){ alienX = (int)al[r][c].x; g.drawImage(alienImage,alienX, (int)al[r][c].y, 20, 20, null); alienX++; if(al[r][c].x > 500){ dropAlien(); //dropdown = true; } al[r][c].x += 0.000005; } } for(int r=0; r<al.length;r++){ for(int c=0; c<al[0].length; c++){ if((al[r][c].y-10)%60 ==0){ if((al[r][c].x + 14)>(BOARD_WIDTH-13)){ al[r][c].y += 30; }else al[r][c].x += 0.5;//was 14 }else{ if((al[r][c].x - 14)<10){ al[r][c].y += 30; }else al[r][c].x -= 0.5; ///was 14 } } } if (shotFired){ g.drawImage(shotImage,(int)shot.x,(int)shot.y,10,18 ,null); if(shot.y >= -20) shot.y -= 1; //shot.x = pl.x + 50; } ///THIS NEEDS WORK BELOW for(int r = 0; r < al.length; r++){ for(int c = 0; c < al[0].length; c++){ for(int i=0; i<shot.x; i++){ if(al[r][c].y >= shot.y - 10 && al[r][c].y <= shot.y + 10 && al[r][c].x >= shot.x - 10 && al[r][c].x <= shot.x + 10 ){ // shot.x.remove(i); //shotsy.remove(i); al[r][c].y = 1000000000; score ++; count++; }//if count is greater than 30 } } } if (moveRight && pl.x <= 450){ pl.x += 0.5; } if (moveLeft && pl.x >= -50){ pl.x -= 0.5; } if(count > 22){ ingame = false; } Toolkit.getDefaultToolkit().sync(); g.dispose(); } } private class TAdapter extends KeyAdapter { public void keyReleased(KeyEvent e) { int key = e.getKeyCode(); if(key==37){ moveLeft = false; } if(key==39){ moveRight = false; } if(key==32){ shot.y = BOARD_HEIGHT - 100; } if(key==38){ } } public void keyPressed(KeyEvent e) { //System.out.println( e.getKeyCode()); // message = "Key Pressed: " + e.getKeyCode(); int key = e.getKeyCode(); if(key==37){ moveLeft = true; } if(key==39){ moveRight = true; } if(key==32){ shotFired = true; shot.x = pl.x + 33; } if(key==13){ // invisible = true; } } } public void mousePressed(MouseEvent e) { int x = e.getX(); int y = e.getY(); } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mouseClicked(MouseEvent e) { } public void dropAlien(){ // if(dropdown=true){ for(int r=0; r<al.length; r++){ for(int c = 0; c<al[0].length; c++){ al[r][c].y = 25; } } //} } public void run() { long beforeTime, timeDiff, sleep; beforeTime = System.currentTimeMillis(); int animationDelay = 1; 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 //GamChar public class gamChar { double x; double y; public gamChar(double x, double y){ this.x = x; this.y = y; } } ///Shot public class Shot extends gamChar{ public Shot(double x, double y){ super(x,y); } } ///Player public class Player extends gamChar{ public Player(double x, double y){ super(x,y); } } ///Alien public class Alien extends gamChar{ boolean isVis; public Alien(int x, int y){ super(x,y); isVis = true; } }

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.