太空入侵者:JFrame碰撞检测和反转方向

时间:2017-02-04 19:46:36

标签: java swing jframe

我是Java初学者,试图设计太空入侵者游戏。我有5行6个外星人在屏幕上同步移动。当它们到达边缘时,它们应该向下移动一小部分然后反向。我已经尝试了几个小时无济于事,任何帮助都会非常感激。 奖励:如果有人能告诉我为什么最底层角落的最后一个外星人没有被创造出来会很棒。 主要课程:

import java.awt.*;
import java.awt.image.*;

import java.awt.event.*;
import java.io.File;

import javax.swing.*;
public class InvadersApplication extends JFrame implements
Runnable, KeyListener {
//member data
private static String workingDirectory;
private static boolean isGraphicsInitialised=false;
public static final Dimension WindowSize = new Dimension(800,600);
private static final int NUMALIENS = 30;
private double alienx, alieny;
private Sprite2D[] GeneralArray = new Sprite2D[NUMALIENS+3];
private SpaceShip PlayerShip;
private BufferStrategy strategy;


//constructor
public InvadersApplication (){

    //display the window, centred on the screen
    Dimension screensize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
    int x = screensize.width/2-WindowSize.width/2;
    int y = screensize.height/2-WindowSize.height/2;
    setBounds (x, y, WindowSize.width, WindowSize.height);
    setVisible(true);
    this.setTitle("Space Invaders!");
    createBufferStrategy(2);
    strategy = getBufferStrategy();




    //load image from disk
    ImageIcon icon = new ImageIcon("C://Users/MRDJR97/JAVAIMAGES/alien.png");
    Image alienImage = icon.getImage();

    //create and initialise some aliens, passing them each into the image we have     loaded
        int i=1;
        alienx=20; alieny=30;
        for(int j=0; j<6; j++){
            alienx=20;
            for(int k=0; k<5; k++){
                GeneralArray[i] = new Alien(alienImage);
                GeneralArray[i].setPosition(alienx, alieny);
                System.out.println(i);
                i++;
                alienx+=67;
            }
            alieny+=50;
        }




    //create and initialise the player's spaceship
    icon = new ImageIcon("C://Users/MRDJR97/JAVAIMAGES/playership.png");
    Image shipImage = icon.getImage();
    PlayerShip = new SpaceShip(shipImage);
    PlayerShip.setPosition(200, 530);
    GeneralArray[0] = PlayerShip;

    //create and start animation thread
    Thread t = new Thread(this);
    t.start();

    //send keyboard events arriving into this JFrame back to its own event handlers
    addKeyListener(this);
    isGraphicsInitialised=true; // it is now safe to paint the images

}

//thread's entry point
public void run(){
    while(1==1) {
        //1:sleep for 1/50 of a second
        try{Thread.sleep(20);
        } catch (InterruptedException e) {}

        //2:animate game objects
        for(int i=0; i<NUMALIENS; i++){
            GeneralArray[i].move();

        PlayerShip.move();

        this.repaint(); //3: force an application repaint 
        }}
    }

//Three Keyboard Event-handler functions
    public void keyPressed (KeyEvent e){
        if(e.getKeyCode()==KeyEvent.VK_LEFT)
        {PlayerShip.setXSpeed(-4);}
        else if(e.getKeyCode()==KeyEvent.VK_RIGHT)
        {PlayerShip.setXSpeed(4);}
    }

    public void keyReleased(KeyEvent e){
        if (e.getKeyCode()==KeyEvent.VK_LEFT||e.getKeyCode()==KeyEvent.VK_RIGHT){
        PlayerShip.setXSpeed(0);}
    }

    public void keyTyped(KeyEvent e) {}

    //applications paint method
    public void paint (Graphics g) {
        if (isGraphicsInitialised){
            g = strategy.getDrawGraphics();
            //don't try to draw unitialised objects
            //clear canvas w big black rectangle
            g.setColor(Color.BLACK);
            g.fillRect(0, 0, WindowSize.width, WindowSize.height);
            //redraw all game objects
            for(int i =0; i<NUMALIENS; i++){
                GeneralArray[i].paint(g);
            PlayerShip.paint(g);
            }
            g.dispose();
            strategy.show();
            }
        }
    //application entry point
        public static void main(String [] args){
            workingDirectory = System.getProperty("user.dir");
            InvadersApplication w = new InvadersApplication();      
    }
}

Alien Class(扩展到超类[Sprite2D]):

import java.awt.*;
import java.awt.image.*;
import java.lang.reflect.Constructor;

public class Alien extends Sprite2D{

public Alien(Image i) {
    super(i);
    // TODO Auto-generated constructor stub
}

Dimension screensize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
public void move(){
    x+= 5;

 }
}

0 个答案:

没有答案