如何添加玩家动作

时间:2016-01-25 22:19:36

标签: java maze

我做了一个迷宫并添加了一个红色方块来代表玩家。为了实现并允许用户在我的迷宫中移动,我该怎么办?我之前做的是创建一个单独的类,扩展JPanel并实现actionListener但是没有用。所以我正在寻找一种更容易建立的替代方法

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

    //     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){}

}

1 个答案:

答案 0 :(得分:1)

您的www类是JFrame(扩展名),因此您可以像这样添加keyListener

首先创建一个名为MyKeyListener的新类:

class MyKeyListener extends KeyAdapter {
    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;
             }
         }
}

(你写的是正确的)

然后在www类的初始化中执行此操作:

setKeyListener(new MyKeyListener());

这将为整个JFrame添加一个键监听器,它将keylistener应用于整个网格。

编辑:将代码更改为:

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