为什么Generetemap没有显示?

问题描述

我正在尝试制作破砖游戏,我已经完成了背景工作,但是需要添加积木,因此请在顶部进行地图绘制。我写了代码,但是地图没有显示。你有什么想法吗?谢谢您的帮助

.......................
.......................
.......................
..............................
...............................
................................
..................................
package sample;

import java.awt.*;

public class MapGenerator
{
    public int map[][];
    public int brickWidth;
    public int brickHeight;
    public MapGenerator(int row,int col)
    {
        map = new int[row][col];
        for( int i = 0; i<map.length; i++)
        {
            for(int j=0; j<map[0].length;j++)
            {
                map[i][j] =  1;
            }
        }

        brickWidth = 540/col;
        brickWidth =150/row;

    }
    public void draw(Graphics2D g)
    {
        for( int i = 0; i<map.length; i++)
        {
            for (int j = 0; j < map[0].length; j++)
            {
                if (map[i][j] > 0)
                {
                    g.setColor(Color.black);
                    g.fillRect(j * brickWidth + 80,i*brickHeight +50,brickWidth,brickHeight);


                }
            }
        }

    }
}
package sample;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class gameplay extends JPanel implements KeyListener,ActionListener
{
    private boolean play = false;
    private int score = 0;
    private int totalBricks = 21;

    private Timer timer;
    private int delay = 8;

    private int playerX =310;

    private int ballposX = 500;
    private int ballposY = 350;
    private int ballXdir = -1;
    private int ballYdir = -2;

    private MapGenerator map;

    public gameplay()
    {
        map = new MapGenerator(3,7);

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

    public void paint(Graphics g)
    {
        //backgroung

        g.setColor(Color.GRAY);
        g.fillRect(1,1,692,692);

        //drawing map

        map.draw((Graphics2D)g);

        //border
        g.setColor(Color.yellow);
        g.fillRect(0,5,592);
        g.fillRect(0,3);
        g.fillRect(691,3,592);

        //paddle

        g.setColor(Color.GREEN);
        g.fillRect(playerX,550,100,8);

        //ball
        g.setColor(Color.YELLOW);
        g.filloval(ballposX,ballposY,20,20);

        g.dispose();
    }


    @Override
    public void actionPerformed(ActionEvent e)
    {
        timer.start();

        if(play)
        {
            if(new Rectangle(ballposX,20).intersects(new Rectangle(playerX,8)))
            {
                ballYdir =-ballYdir;
            }
            ballposX += ballXdir;
            ballposY += ballYdir;
            if(ballposX < 0 )
            {
                ballXdir = -ballXdir;
            }
            if (ballposY < 0)
            {
                ballYdir = -ballYdir;
            }
            if(ballposX > 670)
            {
                ballXdir = -ballXdir;
            }
        }

        repaint();
    }

    @Override
    public void keypressed(KeyEvent e)
    {
        if(e.getKeyCode() == KeyEvent.VK_RIGHT)
        {
            if(playerX >= 600)
            {
                playerX = 600;
            }
            else
            {
                moveRight();
            }
        }
        if(e.getKeyCode() == KeyEvent.VK_LEFT)
        {
            if(playerX < 10)
            {
                playerX = 10;
            }
            else
            {
                moveLeft();
            }
        }
    }
    public void moveRight()
    {
        play = true;
        playerX+=20;
    }

    public void moveLeft()
    {
        play = true;
        playerX-=20;
    }

    @Override
    public void keyreleased(KeyEvent e) { }

    @Override
    public void keyTyped(KeyEvent e) { }
}
package sample;
import javax.swing.JFrame;

public class Main
{
    public static void main(String[] args)
    {
        JFrame obj = new JFrame();
        gameplay Gameplay = new gameplay();
        obj.setBounds(10,10,700,600);
        obj.setTitle("Breakout Ball");
        obj.setResizable(false);

        obj.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
        obj.add(Gameplay);
        obj.setVisible(true);
    }
}

解决方法

您的砖块没有高度。在代码中,以下内容之一必须为“ brickHeight”。

  brickWidth = 540/col;
  brickWidth =150/row;

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...