迷宫游戏添加玩家问题

时间:2016-01-26 00:18:30

标签: java

  

给我一​​个语法错误,"找不到MyKeyListener"。我试图添加它,所以玩家类可以实现到网格中。到目前为止,我已经创建了播放器和迷宫,但由于这种语法错误,似乎无法将播放器添加到迷宫中。任何人都可以指出我正在犯的错误。

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;  // Needed for ActionListener
import javax.swing.event.*;  // Needed for ActionListener
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.Timer;

public class www extends JFrame
{
    static Maze maze = new Maze ();
    static Timer t;

    //======================================================== constructor
    public www ()
    {
        // 1... Create/initialize components
        // 2... Create content pane, set layout
        JPanel content = new JPanel ();        // Create a content pane
        content.setLayout (new BorderLayout ()); // Use BorderLayout for panel
        JPanel north = new JPanel ();
        north.setLayout (new FlowLayout ()); // Use FlowLayout for input area

        DrawArea board = new DrawArea (500, 500);

        // 3... Add the components to the input area.

        content.add (north, "North"); // Input area
        content.add (board, "South"); // Output area

        // 4... Set this window's attributes.
        setContentPane (content);
        pack ();
        setTitle ("MAZE");
        setSize (490, 500);
        setKeyListener(new MyKeylistener());
        setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo (null);           // Center window.
    }

    public static void main (String[] args)
    {
        www window = new www ();
        window.setVisible (true);

        //jf.setTitle("Tutorial");
        //jf.setSize(600,400);

    }

    class DrawArea extends JPanel
    {
        public DrawArea (int width, int height)
        {
            this.setPreferredSize (new Dimension (width, height)); // size
        }

        public void paintComponent (Graphics g)
        {
            maze.show (g); // display current state of colony
        }
    }
}

class Maze 
{
    private int grid [][];

    public Maze ()
    {
        int [][] maze = 
            { {1,0,1,1,1,1,1,1,1,1,1,1,1},
                {1,0,1,0,1,0,1,0,0,0,0,0,1},
                {1,0,1,0,0,0,1,0,1,1,1,0,1},
                {1,0,0,0,1,1,1,0,0,0,0,0,1},
                {1,0,1,0,0,0,0,0,1,1,1,0,1},
                {1,0,1,0,1,1,1,0,1,0,0,0,1},
                {1,0,1,0,1,0,0,0,1,1,1,0,1},
                {1,0,1,0,1,1,1,0,1,0,1,0,1},
                {1,0,0,0,0,0,0,0,0,0,1,0,1},
                {1,1,1,1,1,1,1,1,1,1,1,0,1}};

        grid = maze;

    }

    public void show (Graphics g)
    {
        for (int row = 0 ; row < grid.length ; row++)
            for (int col = 0 ; col < grid [0].length ; col++)
            {
                if (grid [row] [col] == 1) // life
                    g.setColor (Color.black);
                else
                    g.setColor (Color.white);
                g.fillRect (col * 30 + 30, row * 30 + 30, 30, 30); // draw life form

            }
        //g.setColor(Color.RED);
        //g.fillRect(60,30,30,50);

    }

    class MyKeyListener extends KeyAdapter {

        int x =  0, y = 0,velX = 0, velY = 0;
        public void keyPressed(KeyEvent e)
        {
            int c =  e.getKeyCode();
            if(c == KeyEvent.VK_LEFT)
            {
                velX = -1;
                velY = 0;
            }
            if (c == KeyEvent.VK_UP)
            {
                velX = 0;
                velY = -1;
            }
            if( c==KeyEvent.VK_RIGHT)
            {
                velX = 1;
                velY = 0;
            }
            if(c==KeyEvent.VK_DOWN)
            {
                velX = 0;
                velY = 1;
            }
        }

        public MyKeyListener ()
        {
            tm.start ();    
            addKeyListener(this);
            setFocusable(true);
            setFocusTraversalKeysEnabled(false);
        }

        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            g.setColor(Color.RED);
            g.fillRect(x,y,50,30);

        }

        public void actionPerformed(ActionEvent e)
        {
            x = x+velX;
            y = y +velY;
            repaint();
        }


    }

    //     public void player ()
    //     {
    // 
    //         Timer tm = new Timer(5,this);
    //          int x = 0; 
    //           int y = 0;
    //          int velX = 0 ;
    //          int velY = 0;tm.start();
    //         addKeyListener(this);
    //         setFocusable(true);
    //         setFocusTraversalKeysEnabled(false);
    //     }

    //     public void actionPerformed(ActionEvent e)
    //     {
    //         x = x+velX;
    //         y = y +velY;
    //         repaint();
    //     }

    //     public void keyPressed(KeyEvent e)
    //     {
    //         int c =  e.getKeyCode();
    //         if(c == KeyEvent.VK_LEFT)
    //         {
    //             velX = -1;
    //             velY = 0;
    //         }
    //         if (c == KeyEvent.VK_UP)
    //         {
    //             velX = 0;
    //             velY = -1;
    //         }
    //         if( c==KeyEvent.VK_RIGHT)
    //         {
    //             velX = 1;
    //             velY = 0;
    //         }
    //         if(c==KeyEvent.VK_DOWN)
    //         {
    //             velX = 0;
    //             velY = 1;
    //         }
    //     }
    // 
    //     public void keyTyped(KeyEvent e){}
    // 
    //     public void keyReleased(KeyEvent e){}

}

2 个答案:

答案 0 :(得分:2)

MyKeyListenerMaze的内部类。您可以通过Maze.MyKeyListener引用它。

请注意,这似乎也不是一个好主意:www类是您的UI组件,应该是定义键侦听器的类,而不是Maze模型。

答案 1 :(得分:0)

您已在MyKeyListener课程内写了Maze。通常,如果要添加一个仅被引用一次的新侦听器,您实际上可以在setKeyListener()方法的参数中编写侦听器。

新的setKeyListener()行看起来像这样,

setKeyListener(new Keylistener(){

    int x =  0, y = 0,velX = 0, velY = 0;
    public void keyPressed(KeyEvent e)
    {
        int c =  e.getKeyCode();
        if(c == KeyEvent.VK_LEFT)
        {
            velX = -1;
            velY = 0;
        }
        if (c == KeyEvent.VK_UP)
        {
            velX = 0;
            velY = -1;
        }
        if( c==KeyEvent.VK_RIGHT)
        {
            velX = 1;
            velY = 0;
        }
        if(c==KeyEvent.VK_DOWN)
        {
            velX = 0;
            velY = 1;
        }
    }

    public MyKeyListener ()
    {
        tm.start ();    
        addKeyListener(this);
        setFocusable(true);
        setFocusTraversalKeysEnabled(false);
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.setColor(Color.RED);
        g.fillRect(x,y,50,30);

    }

    public void actionPerformed(ActionEvent e)
    {
        x = x+velX;
        y = y +velY;
        repaint();
    }
});

然后可以删除MyKeyListener类。

值得注意的是,您通常希望在顶级UI对象(如窗格)中定义关键侦听器,或者在本例中为Maze模型。现在,您正在子UI项目中设置关键侦听器,因此如果该项目失焦,则可能无法触发。