为什么Generetemap没有显示?

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

Why the Generetemap is not showing?

问题

  1. package sample;
  2. import java.awt.*;
  3. public class MapGenerator
  4. {
  5. public int map[][];
  6. public int brickWidth;
  7. public int brickHeight;
  8. public MapGenerator(int row, int col)
  9. {
  10. map = new int[row][col];
  11. for( int i = 0; i < map.length; i++)
  12. {
  13. for(int j = 0; j < map[0].length; j++)
  14. {
  15. map[i][j] = 1;
  16. }
  17. }
  18. brickWidth = 540 / col;
  19. brickHeight = 150 / row;
  20. }
  21. public void draw(Graphics2D g)
  22. {
  23. for( int i = 0; i < map.length; i++)
  24. {
  25. for (int j = 0; j < map[0].length; j++)
  26. {
  27. if (map[i][j] > 0)
  28. {
  29. g.setColor(Color.black);
  30. g.fillRect(j * brickWidth + 80, i * brickHeight + 50, brickWidth, brickHeight);
  31. }
  32. }
  33. }
  34. }
  35. }
  1. package sample;
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.awt.event.KeyEvent;
  7. import java.awt.event.KeyListener;
  8. public class gameplay extends JPanel implements KeyListener, ActionListener
  9. {
  10. private boolean play = false;
  11. private int score = 0;
  12. private int totalBricks = 21;
  13. private Timer timer;
  14. private int delay = 8;
  15. private int playerX = 310;
  16. private int ballposX = 500;
  17. private int ballposY = 350;
  18. private int ballXdir = -1;
  19. private int ballYdir = -2;
  20. private MapGenerator map;
  21. public gameplay()
  22. {
  23. map = new MapGenerator(3, 7);
  24. addKeyListener(this);
  25. setFocusable(true);
  26. setFocusTraversalKeysEnabled(false);
  27. timer = new Timer(delay, this);
  28. timer.start();
  29. }
  30. public void paint(Graphics g)
  31. {
  32. //background
  33. g.setColor(Color.GRAY);
  34. g.fillRect(1, 1, 692, 692);
  35. //drawing map
  36. map.draw((Graphics2D) g);
  37. //border
  38. g.setColor(Color.yellow);
  39. g.fillRect(0, 0, 5, 592);
  40. g.fillRect(0, 0, 692, 3);
  41. g.fillRect(691, 0, 3, 592);
  42. //paddle
  43. g.setColor(Color.GREEN);
  44. g.fillRect(playerX, 550, 100, 8);
  45. //ball
  46. g.setColor(Color.YELLOW);
  47. g.fillOval(ballposX, ballposY, 20, 20);
  48. g.dispose();
  49. }
  50. @Override
  51. public void actionPerformed(ActionEvent e)
  52. {
  53. timer.start();
  54. if (play)
  55. {
  56. if (new Rectangle(ballposX, ballposY, 20, 20).intersects(new Rectangle(playerX, 550, 100, 8)))
  57. {
  58. ballYdir = -ballYdir;
  59. }
  60. ballposX += ballXdir;
  61. ballposY += ballYdir;
  62. if (ballposX < 0)
  63. {
  64. ballXdir = -ballXdir;
  65. }
  66. if (ballposY < 0)
  67. {
  68. ballYdir = -ballYdir;
  69. }
  70. if (ballposX > 670)
  71. {
  72. ballXdir = -ballXdir;
  73. }
  74. }
  75. repaint();
  76. }
  77. @Override
  78. public void keyPressed(KeyEvent e)
  79. {
  80. if (e.getKeyCode() == KeyEvent.VK_RIGHT)
  81. {
  82. if (playerX >= 600)
  83. {
  84. playerX = 600;
  85. }
  86. else
  87. {
  88. moveRight();
  89. }
  90. }
  91. if (e.getKeyCode() == KeyEvent.VK_LEFT)
  92. {
  93. if (playerX < 10)
  94. {
  95. playerX = 10;
  96. }
  97. else
  98. {
  99. moveLeft();
  100. }
  101. }
  102. }
  103. public void moveRight()
  104. {
  105. play = true;
  106. playerX += 20;
  107. }
  108. public void moveLeft()
  109. {
  110. play = true;
  111. playerX -= 20;
  112. }
  113. @Override
  114. public void keyReleased(KeyEvent e) { }
  115. @Override
  116. public void keyTyped(KeyEvent e) { }
  117. }
  1. package sample;
  2. import javax.swing.JFrame;
  3. public class Main
  4. {
  5. public static void main(String[] args)
  6. {
  7. JFrame obj = new JFrame();
  8. gameplay Gameplay = new gameplay();
  9. obj.setBounds(10, 10, 700, 600);
  10. obj.setTitle("Breakout Ball");
  11. obj.setResizable(false);
  12. obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  13. obj.add(Gameplay);
  14. obj.setVisible(true);
  15. }
  16. }
英文:

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

  1. .......................
  2. .......................
  3. .......................
  4. ..............................
  5. ...............................
  6. ................................
  7. ..................................
  1. package sample;
  2. import java.awt.*;
  3. public class MapGenerator
  4. {
  5. public int map[][];
  6. public int brickWidth;
  7. public int brickHeight;
  8. public MapGenerator(int row, int col)
  9. {
  10. map = new int[row][col];
  11. for( int i = 0; i&lt;map.length; i++)
  12. {
  13. for(int j=0; j&lt;map[0].length;j++)
  14. {
  15. map[i][j] = 1;
  16. }
  17. }
  18. brickWidth = 540/col;
  19. brickWidth =150/row;
  20. }
  21. public void draw(Graphics2D g)
  22. {
  23. for( int i = 0; i&lt;map.length; i++)
  24. {
  25. for (int j = 0; j &lt; map[0].length; j++)
  26. {
  27. if (map[i][j] &gt; 0)
  28. {
  29. g.setColor(Color.black);
  30. g.fillRect(j * brickWidth + 80, i*brickHeight +50, brickWidth,brickHeight);
  31. }
  32. }
  33. }
  34. }
  35. }
  1. package sample;
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.awt.event.KeyEvent;
  7. import java.awt.event.KeyListener;
  8. public class gameplay extends JPanel implements KeyListener, ActionListener
  9. {
  10. private boolean play = false;
  11. private int score = 0;
  12. private int totalBricks = 21;
  13. private Timer timer;
  14. private int delay = 8;
  15. private int playerX =310;
  16. private int ballposX = 500;
  17. private int ballposY = 350;
  18. private int ballXdir = -1;
  19. private int ballYdir = -2;
  20. private MapGenerator map;
  21. public gameplay()
  22. {
  23. map = new MapGenerator(3,7);
  24. addKeyListener(this );
  25. setFocusable(true);
  26. setFocusTraversalKeysEnabled(false);
  27. timer = new Timer(delay, this);
  28. timer.start();
  29. }
  30. public void paint(Graphics g)
  31. {
  32. //backgroung
  33. g.setColor(Color.GRAY);
  34. g.fillRect(1,1,692,692);
  35. //drawing map
  36. map.draw((Graphics2D)g);
  37. //border
  38. g.setColor(Color.yellow);
  39. g.fillRect(0,0,5,592);
  40. g.fillRect(0,0,692,3);
  41. g.fillRect(691,0,3,592);
  42. //paddle
  43. g.setColor(Color.GREEN);
  44. g.fillRect(playerX,550,100,8);
  45. //ball
  46. g.setColor(Color.YELLOW);
  47. g.fillOval(ballposX,ballposY,20,20);
  48. g.dispose();
  49. }
  50. @Override
  51. public void actionPerformed(ActionEvent e)
  52. {
  53. timer.start();
  54. if(play)
  55. {
  56. if(new Rectangle(ballposX, ballposY, 20, 20).intersects(new Rectangle(playerX,550,100,8)))
  57. {
  58. ballYdir =-ballYdir;
  59. }
  60. ballposX += ballXdir;
  61. ballposY += ballYdir;
  62. if(ballposX &lt; 0 )
  63. {
  64. ballXdir = -ballXdir;
  65. }
  66. if (ballposY &lt; 0)
  67. {
  68. ballYdir = -ballYdir;
  69. }
  70. if(ballposX &gt; 670)
  71. {
  72. ballXdir = -ballXdir;
  73. }
  74. }
  75. repaint();
  76. }
  77. @Override
  78. public void keyPressed(KeyEvent e)
  79. {
  80. if(e.getKeyCode() == KeyEvent.VK_RIGHT)
  81. {
  82. if(playerX &gt;= 600)
  83. {
  84. playerX = 600;
  85. }
  86. else
  87. {
  88. moveRight();
  89. }
  90. }
  91. if(e.getKeyCode() == KeyEvent.VK_LEFT)
  92. {
  93. if(playerX &lt; 10)
  94. {
  95. playerX = 10;
  96. }
  97. else
  98. {
  99. moveLeft();
  100. }
  101. }
  102. }
  103. public void moveRight()
  104. {
  105. play = true;
  106. playerX+=20;
  107. }
  108. public void moveLeft()
  109. {
  110. play = true;
  111. playerX-=20;
  112. }
  113. @Override
  114. public void keyReleased(KeyEvent e) { }
  115. @Override
  116. public void keyTyped(KeyEvent e) { }
  117. }
  1. package sample;
  2. import javax.swing.JFrame;
  3. public class Main
  4. {
  5. public static void main(String[] args)
  6. {
  7. JFrame obj = new JFrame();
  8. gameplay Gameplay = new gameplay();
  9. obj.setBounds(10,10,700,600);
  10. obj.setTitle(&quot;Breakout Ball&quot;);
  11. obj.setResizable(false);
  12. obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  13. obj.add(Gameplay);
  14. obj.setVisible(true);
  15. }
  16. }

答案1

得分: 1

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

  1. brickWidth = 540/col;
  2. brickWidth = 150/row;
英文:

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

  1. brickWidth = 540/col;
  2. 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:

确定