为什么Generetemap没有显示?

huangapple go评论65阅读模式
英文:

Why the Generetemap is not showing?

问题

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;
        brickHeight = 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)
    {
        //background
        g.setColor(Color.GRAY);
        g.fillRect(1, 1, 692, 692);

        //drawing map
        map.draw((Graphics2D) g);

        //border
        g.setColor(Color.yellow);
        g.fillRect(0, 0, 5, 592);
        g.fillRect(0, 0, 692, 3);
        g.fillRect(691, 0, 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, ballposY, 20, 20).intersects(new Rectangle(playerX, 550, 100, 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);
    }
}
英文:

I am trying to make a brick breaker game and I've already done the background but need to add bricks so map on the top. I wrote the code but the map is not showing. Do you have any ideas why? Thanks for any help

.......................
.......................
.......................
..............................
...............................
................................
..................................
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&lt;map.length; i++)
{
for(int j=0; j&lt;map[0].length;j++)
{
map[i][j] =  1;
}
}
brickWidth = 540/col;
brickWidth =150/row;
}
public void draw(Graphics2D g)
{
for( int i = 0; i&lt;map.length; i++)
{
for (int j = 0; j &lt; map[0].length; j++)
{
if (map[i][j] &gt; 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,0,5,592);
g.fillRect(0,0,692,3);
g.fillRect(691,0,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, ballposY, 20, 20).intersects(new Rectangle(playerX,550,100,8)))
{
ballYdir =-ballYdir;
}
ballposX += ballXdir;
ballposY += ballYdir;
if(ballposX &lt; 0 )
{
ballXdir = -ballXdir;
}
if (ballposY &lt; 0)
{
ballYdir = -ballYdir;
}
if(ballposX &gt; 670)
{
ballXdir = -ballXdir;
}
}
repaint();
}
@Override
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode() == KeyEvent.VK_RIGHT)
{
if(playerX &gt;= 600)
{
playerX = 600;
}
else
{
moveRight();
}
}
if(e.getKeyCode() == KeyEvent.VK_LEFT)
{
if(playerX &lt; 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(&quot;Breakout Ball&quot;);
obj.setResizable(false);
obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
obj.add(Gameplay);
obj.setVisible(true);
}
}

答案1

得分: 1

你的砖块没有高度。以下两行代码中至少需要有一行包含 'brickHeight'。

  brickWidth = 540/col;
brickWidth = 150/row;
英文:

Your bricks have no height. One of the below needs to be 'brickHeight' in your code.

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

huangapple
  • 本文由 发表于 2020年10月7日 21:27:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/64245031.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定