2d比赛中的蛇运动

时间:2017-02-03 13:27:29

标签: java swing 2d-games

蛇的正确运动工作正常但是当我按下任何其他键(向上或向下)改变运动的方向时,它会抛出错误并且蛇不会移动。以下是错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at GamePlay.paint(GamePlay.java:76)
    at javax.swing.JComponent.paintToOffscreen(JComponent.java:5210)
    at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:290)
    at javax.swing.RepaintManager.paint(RepaintManager.java:1272)
    at javax.swing.JComponent._paintImmediately(JComponent.java:5158)
    at javax.swing.JComponent.paintImmediately(JComponent.java:4969)
    at javax.swing.RepaintManager$4.run(RepaintManager.java:831)
    at javax.swing.RepaintManager$4.run(RepaintManager.java:814)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:814)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:789)
    at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:738)
    at javax.swing.RepaintManager.access$1200(RepaintManager.java:64)
    at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1732)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class GamePlay extends JPanel implements KeyListener, ActionListener{
    private ImageIcon titleImage;

    private int[] snakexlength= new int[750];
    private int[] snakeylength= new int[750];
    int lengthOfSnake= 3;
    int moves=0;


    private boolean left= false;
    private boolean right= false;
    private boolean up= false;
    private boolean down= false;

    private ImageIcon rightmouth;
    private ImageIcon upmouth;
    private ImageIcon downmouth;
    private ImageIcon leftmouth;

    private Timer timer;
    private int delay= 100;
    private ImageIcon snakeimage;

    public GamePlay(){
        addKeyListener(this);
        setFocusable(true);
        setFocusTraversalKeysEnabled(false);
        timer= new Timer(delay, this);
        timer.start();

    }

    public void paint(Graphics g){

        if(moves==0){
            snakexlength[2]=50;
            snakexlength[1]= 75;
            snakexlength[0]=100;

            snakeylength[2]= 100;
            snakeylength[1]=100;
            snakeylength[0]= 100;
            }

        //draw title image border
        g.setColor(Color.WHITE);
        g.drawRect(25, 100, 851, 55);

        //draw the title image
        titleImage= new ImageIcon("snaketitle.jpg");
        titleImage.paintIcon(this, g, 25, 13);

        //draw border for gameplay
        g.setColor(Color.black);
        g.drawRect(25, 75, 850, 575);

        //draw background for the gameplay
        g.setColor(Color.black);
        g.fillRect(25,75,850,580);

        rightmouth= new ImageIcon("rightmouth.png");
        rightmouth.paintIcon(this, g, snakexlength[0], snakeylength[0]);

        for(int a=0; a<lengthOfSnake; a++){

            if(a==0 && down){
                downmouth.paintIcon(this, g, snakexlength[a], snakeylength[a]);
            }

            if(a==0 && up){
                upmouth.paintIcon(this, g, snakexlength[a], snakeylength[a]);
            }

            if(a==0 && right){
                rightmouth.paintIcon(this, g, snakexlength[a], snakeylength[a]);
            }

            if(a==0 && left){
                leftmouth.paintIcon(this, g, snakexlength[a], snakeylength[a]);
            }

            if(a!=0){
            snakeimage= new ImageIcon("snakeimage.png");
            snakeimage.paintIcon(this, g,snakexlength[a], snakeylength[a]) ;
                }
            }


        g.dispose();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        timer.start();
        if(right){
            for(int r= lengthOfSnake-1; r>=0; r--){
                snakeylength[r+1]= snakeylength[r];
            }
            for(int r=lengthOfSnake; r>=0; r--){
                    if(r==0){
                        snakexlength[r]= snakexlength[r]+25;

                    }
                    else{
                    snakexlength[r]= snakexlength[r-1];

                    }

                if(snakexlength[r]> 850){
                    snakexlength[r]= 25;
                }
            }

            repaint();
        }
            if(left){
                for(int r= lengthOfSnake-1; r>=0; r--){
                    snakeylength[r+1]= snakeylength[r];
                }
                for(int r=lengthOfSnake; r>=0; r--){
                        if(r==0){
                            snakexlength[r]= snakexlength[r]-25;

                        }
                        else{
                        snakexlength[r]= snakexlength[r-1];

                        }

                    if(snakexlength[r]> 25){
                        snakexlength[r]= 850;
                    }
                }

                repaint();
            }

            if(up){
                for(int r= lengthOfSnake-1; r>=0; r--){
                    snakexlength[r+1]= snakexlength[r];
                }
                for(int r=lengthOfSnake; r>=0; r--){
                        if(r==0){
                            snakeylength[r]= snakeylength[r]+25;

                        }
                        else{
                        snakeylength[r]= snakeylength[r-1];

                        }

                    if(snakeylength[r]< 75){
                        snakeylength[r]= 625;
                    }
                }

                repaint();
            }
            if(down){
                for(int r= lengthOfSnake-1; r>=0; r--){
                    snakexlength[r+1]= snakexlength[r];
                }
                for(int r=lengthOfSnake; r>=0; r--){
                        if(r==0){
                            snakeylength[r]= snakeylength[r]+25;

                        }
                        else{
                        snakeylength[r]= snakeylength[r-1];

                        }

                    if(snakeylength[r]> 625){
                        snakeylength[r]= 75;
                    }
                }

                repaint();
            }
    }

    @Override
    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void keyReleased(KeyEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode()== KeyEvent.VK_RIGHT){
            moves++;
            right= true;
            if(!left){
                right=true;
            }
            else{
                right= false;
                left= true;
            }

        }

        if(e.getKeyCode()== KeyEvent.VK_LEFT){
            moves++;
            left= true;
            if(!right){
                left=true;
            }
                else{
                    left= false;
                    right= true;
                }
            up=false;
            down=false;
        }
        if(e.getKeyCode()== KeyEvent.VK_UP){
            moves++;
            up= true;
            if(!down){
                up=true;
            }
            else{
                up= false;
                down= true;
            }

            left=false;
            right=false;
        }

        if(e.getKeyCode()== KeyEvent.VK_DOWN){
            moves++;
            down= true;
            if(!up){
                down=true;
            }
            else{
                down= false;
                up= true;
            }

            left=false;
            right=false;
        }


    }
}

1 个答案:

答案 0 :(得分:1)

你永远不会将其他纹理初始化

private ImageIcon upmouth;
private ImageIcon downmouth;
private ImageIcon leftmouth;

添加:

rightmouth = new ImageIcon("rightmouth.png");
leftmouth= new ImageIcon("leftmouth.png");
upmouth = new ImageIcon("upmouth.png");
downmouth = new ImageIcon("downmouth.png");

最好在构造函数中,因此不要在每次paint()调用时加载图像。