Ver Mensaje Individual
  #1 (permalink)  
Antiguo 15/10/2013, 03:09
Avatar de CMushroom
CMushroom
 
Fecha de Ingreso: diciembre-2011
Ubicación: Morelos
Mensajes: 99
Antigüedad: 13 años
Puntos: 1
Pregunta Juego chico clases 8 errores

Ayuda no puedo acabar este juego ya estan las 4 clases que heredan de clase Sprite y newGame las llama a las clase y sus metodos pero tengo 9 errores 6 de los cuales son en el metodo "drawAll" en "panel" y 1 en metodo checkHits por "panel" tambien ( ---- en linea: if (bomb.getY() > panel.getHeight()){---- ) y en metodo mouseMoved en defender.move(event.getX(); ...........En las clases Defensor, Alien, Laser y Bomba se utiliza una imagen pueden utilizar la que quieran de 20 px para todos para probarlo.

Codigo clase newGame:

Código:
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){
    }
}
clase sprite:

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 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;
	}
}
clase bomb:

Código:
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;
	}
}
clase defensor:

Código:
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;
	}
}
clase laser:

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;
	}
}

Última edición por CMushroom; 15/10/2013 a las 03:14