迷宫游戏添加玩家问题



给我一个语法错误"找不到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){}
}

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

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

你已经在Maze类中编写了MyKeyListener。 通常,如果要添加一个只引用一次的新侦听器,实际上可以在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 项中设置密钥侦听器,因此如果该项失焦,它可能不会触发。

相关内容

最新更新