Codigo clase newGame:
Código:
clase sprite:import java.awt.*; import java.awt.event.*; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.Timer; public class newGame extends Frame implements WindowListener{ private Graphics paper; private Defender defender; private Alien alien; private Laser laser; private Bomb bomb; Timer timer; public static void main (String[] args){ newGame Jugar = new newGame(); Jugar.setSize(600,634); Jugar.setVisible(true); } private newGame(){ setTitle("Invasores del espacio"); setLayout(new FlowLayout()); defender = new Defender(); alien = new Alien(); timer.start(); this.addWindowListener(this); } private void timer(){ if (bomb == null){ bomb = new Bomb(alien.getX(), alien.getY()); } moveAll(); drawAll(); checkHits(); } public void mouseClicked(MouseEvent event){ int initialX = defender.getX(); int initialY = defender.getY(); if (laser == null){ laser = new Laser(initialX, initialY); } } public void mouseMoved(MouseEvent event){ defender.move(event.getX()); } private void moveAll(){ alien.move(); if (bomb != null){ bomb.move(); } if (laser != null){ laser.move(); } } private void drawAll(){ Graphics paper = panel.getGraphics(); paper.setColor(Color.white); paper.fillRect(0, 0, panel.getWidth(), panel.getHeight()); paper.setColor(Color.black); defender.draw(panel); alien.draw(panel); if (laser != null){ laser.draw(panel); } if (bomb != null){ bomb.draw(panel); } } private void checkHits(){ if (collides(laser, alien)){ endGame("user"); } else{ if (collides(bomb, defender)){ endGame("alien"); } } if (bomb != null){ if (bomb.getY() > panel.getHeight()){ bomb = null; } } if (laser != null){ if (laser.getY() < 0){ laser = null; } } } private boolean collides(Sprite one, Sprite two){ if (one == null || two == null){ return false; } if ( one.getX() > two.getX() && one.getY() < (two.getY() + two.getHeight()) && (one.getX() + one.getWidth()) < (two.getX() + two.getWidth()) && (one.getY() + one.getWidth()) > (two.getY())){ return true; } else{ return false; } } private void endGame(String winner){ laser = null; bomb = null; timer.stop(); JOptionPane.showMessageDialog(null,"game over - " + winner + " wins"); } public void windowClosing(WindowEvent Event){ System.exit(0); } public void windowIconified(WindowEvent Event){ } public void windowOpened(WindowEvent Event){ } public void windowClosed(WindowEvent Event){ } public void windowDeiconified(WindowEvent Event){ } public void windowActivated(WindowEvent Event){ } public void windowDeactivated(WindowEvent Event){ } }
Código:
public class Sprite{ protected int x, y, width, height; public int getX(){ return x; } public int getY(){ return y; } public int getWidth(){ return width; } public int getHeight(){ return height; } }
Código:
clase bomb:clase alien public class Alien extends Sprite{ private int stepSize; private ImageIcon alienImage; public Alien(){ x = 0; y = 25; width = 20; height = 10; stepSize = 10; alienImage = new ImageIcon("imagddes.jpg"); } public void draw(JPanel panel){ Graphics paper = panel.getGraphics(); alienImage.paintIcon(panel, paper, x, y); } public void move(){ if (x > 200 || x < 0) { stepSize = -stepSize; } x = x + stepSize; } }
Código:
clase defensor:import java.awt.*; import javax.swing.*; import javax.swing.JPanel; public class Bomb extends Sprite { private int stepSize; private ImageIcon bombImage; public Bomb(int initialX, int initialY){ x = initialX; y = initialY; width = 5; height = 10; stepSize = 10; bombImage = new ImageIcon("bomb.jpg"); } public void draw(JPanel panel){ Graphics paper = panel.getGraphics(); bombImage.paintIcon(panel, paper, x, y); } public void move(){ y = y + stepSize; } }
Código:
clase laser:import java.awt.*; import javax.swing.*; import javax.swing.JPanel; public class Defender extends Sprite{ private int stepSize; private ImageIcon defenderImage; public Defender(){ x = 0; y = 25; width = 20; height = 10; stepSize = 10; defenderImage = new ImageIcon("imagddes.jpg"); } public void draw(JPanel panel){ Graphics paper = panel.getGraphics(); defenderImage.paintIcon(panel, paper, x, y); } public void move(){ if (x > 200 || x < 0) { stepSize = -stepSize; } x = x + stepSize; } }
Código:
import java.awt.*; import javax.swing.*; import javax.swing.JPanel; public class Laser extends Sprite{ private int stepSize; private ImageIcon laserImage; public Laser(int newX, int newY){ x = newX; y = newY; width = 5; height = 5; stepSize = 10; laserImage = new ImageIcon("imagddes.jpg"); } public void draw(JPanel panel){ Graphics paper = panel.getGraphics(); laserImage.paintIcon(panel, paper, x, y); } public void move(){ y = y - stepSize; } }