英文:
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<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,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);
}
}
答案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;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论