Java编程,突破游戏球物理不符合预期(Java programming, breakout game ball physics not behaving as expected)
我正在学习Java,书中有“Java的艺术与科学:计算机科学概论”。 其中一个练习程序是创建一个简单的Breakout游戏克隆。
我目前能够加载游戏,但我遇到了球物理问题。 我正在使用最简单的物理,并且无法理解为什么它不起作用。
当球击中墙壁时,它会正常弹跳,但是当它击中桨或砖时,它不会。 我在碰撞检测中使用相同的代码来改变我在墙壁上使用的方向。 检测到碰撞,我添加了一个println并在通过碰撞事件时观察了控制台,但方向改变没有发生。
package chapter10; /* * This program creates a clone of the classic Breakout Game, * where a player bounces a ball with a paddle to "break" bricks * along the top of the screen. * * Controls: Left: left button, Right: right button */ import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.RepaintManager; import javax.swing.Timer; import acm.program.*; import acm.graphics.*; public class BreakoutClone extends GraphicsProgram { /* components */ private GRect paddle; private GRect brick; private GOval ball; /* static variables */ private static final double PADDLE_HEIGHT = 5; private static final double BALL_SPEED = 2; private static final double PADDLE_SPEED = 2; private static final double ROWS_BRICKS = 6; private static final double COLUMNS_BRICKS = 10; private static final double TOP_GAP = 50; /* variables */ private int numTurns = 3; private double paddleWidth = 50; private int dx = 2; private int dy = 2; public void init() { setSize(700, 600); paddle = new GRect(0, getHeight() - 30, paddleWidth, PADDLE_HEIGHT); paddle.setFilled(true); add(paddle); addBricks(); ball = new GOval(getWidth() / 2, 175, 5, 5); ball.setFilled(true); add(ball); } public void run() { animateBall(); // TODO animate paddle } public void addBricks() { double gap = 20; double brickWidth = getWidth() / COLUMNS_BRICKS; for (int r = 0; r < ROWS_BRICKS; r++) { for (int b = 0; b < COLUMNS_BRICKS; b++) { brick = new GRect(b * brickWidth, gap * r + TOP_GAP, brickWidth, 10); brick.setFilled(true); add(brick); } } } public void endGame() { // TODO write end game method } public void animateBall() { while (numTurns > 0) { ball.move(dx, dy); pause(15); /* Look for Object Collision */ GObject topRightObject = getElementAt(ball.getX() + 5, ball.getY()); GObject topLeftObject = getElementAt(ball.getX(), ball.getY()); GObject botRightObject = getElementAt(ball.getX() + 5, ball.getY() + 5); GObject botLeftObject = getElementAt(ball.getX(), ball.getY() + 5); /* Bounce off walls */ if ((ball.getX() >= getWidth() - 5) || ball.getX() <= 0) { dx = -dx; } if (ball.getY() <= 0) { dy = -dy; } if ((ball.getY() >= getHeight() - 5)) { dy = -dy; // numTurns--; // if (numTurns == 0) { // endGame(); // } else { // run(); // } } /* Bounce off objects, remove bricks */ if (topRightObject != null) { dy = -dy; hasCollided(topRightObject); } if (topLeftObject != null) { dy = -dy; hasCollided(topLeftObject); } if (botRightObject != null) { dy = -dy; hasCollided(botRightObject); } if (botLeftObject != null) { dy = -dy; hasCollided(botLeftObject); } } } private void hasCollided(GObject obj) { if (obj.equals(paddle)) { System.out.println("detecting paddle"); } else { System.out.println("detecting brick"); remove(obj); } } }
I am studying java with the book "The Art and Science of Java: An Introduction to Computer Science". One of the practice programs is to create a simple clone of the Breakout game.
I am currently able to load the game, but am having issues with the ball physics. I'm using the simplest physics possible, and can't see why it isn't working.
When the ball hits a wall, it bounces normally, but when it hits the paddle or a brick, it doesn't. I'm using the same code in the collision detection to change the direction that I used with the walls. The collisions are detected, I added a println and watched the console as it went through the collision events, but the direction change is not happening.
package chapter10; /* * This program creates a clone of the classic Breakout Game, * where a player bounces a ball with a paddle to "break" bricks * along the top of the screen. * * Controls: Left: left button, Right: right button */ import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.RepaintManager; import javax.swing.Timer; import acm.program.*; import acm.graphics.*; public class BreakoutClone extends GraphicsProgram { /* components */ private GRect paddle; private GRect brick; private GOval ball; /* static variables */ private static final double PADDLE_HEIGHT = 5; private static final double BALL_SPEED = 2; private static final double PADDLE_SPEED = 2; private static final double ROWS_BRICKS = 6; private static final double COLUMNS_BRICKS = 10; private static final double TOP_GAP = 50; /* variables */ private int numTurns = 3; private double paddleWidth = 50; private int dx = 2; private int dy = 2; public void init() { setSize(700, 600); paddle = new GRect(0, getHeight() - 30, paddleWidth, PADDLE_HEIGHT); paddle.setFilled(true); add(paddle); addBricks(); ball = new GOval(getWidth() / 2, 175, 5, 5); ball.setFilled(true); add(ball); } public void run() { animateBall(); // TODO animate paddle } public void addBricks() { double gap = 20; double brickWidth = getWidth() / COLUMNS_BRICKS; for (int r = 0; r < ROWS_BRICKS; r++) { for (int b = 0; b < COLUMNS_BRICKS; b++) { brick = new GRect(b * brickWidth, gap * r + TOP_GAP, brickWidth, 10); brick.setFilled(true); add(brick); } } } public void endGame() { // TODO write end game method } public void animateBall() { while (numTurns > 0) { ball.move(dx, dy); pause(15); /* Look for Object Collision */ GObject topRightObject = getElementAt(ball.getX() + 5, ball.getY()); GObject topLeftObject = getElementAt(ball.getX(), ball.getY()); GObject botRightObject = getElementAt(ball.getX() + 5, ball.getY() + 5); GObject botLeftObject = getElementAt(ball.getX(), ball.getY() + 5); /* Bounce off walls */ if ((ball.getX() >= getWidth() - 5) || ball.getX() <= 0) { dx = -dx; } if (ball.getY() <= 0) { dy = -dy; } if ((ball.getY() >= getHeight() - 5)) { dy = -dy; // numTurns--; // if (numTurns == 0) { // endGame(); // } else { // run(); // } } /* Bounce off objects, remove bricks */ if (topRightObject != null) { dy = -dy; hasCollided(topRightObject); } if (topLeftObject != null) { dy = -dy; hasCollided(topLeftObject); } if (botRightObject != null) { dy = -dy; hasCollided(botRightObject); } if (botLeftObject != null) { dy = -dy; hasCollided(botLeftObject); } } } private void hasCollided(GObject obj) { if (obj.equals(paddle)) { System.out.println("detecting paddle"); } else { System.out.println("detecting brick"); remove(obj); } } }
原文:https://stackoverflow.com/questions/28397996
最满意答案
选择所需的范围并输入以下ex命令:
:'<,'>s/\s*\\$/\=repeat(' ', 80-col('.')).'\'
用行重尾处的空格和\替换表达式,该表达式在第80列之前重复一个空格,然后附加一个
\
字符。 如果您的行超过80个字符,它将附加0个空格,这可能不是您想要的,在这种情况下将80更改为79并为字符串添加空格:' \'
select the range you want and enter the following ex command:
:'<,'>s/\s*\\$/\=repeat(' ', 80-col('.')).'\'
Which substitutes the whitespace and \ at the end of the line with an expression which repeats a space up until column 80 and then appends a
\
character. It'll append 0 spaces if your line is > 80 characters, which may not be what you want, in which case change the 80 to 79 and add a space to the string:' \'
相关问答
更多-
电脑等级考试一级的报名费是多少[2022-05-08]
80 -
常州会计继续教育学习卡多少钱一张?[2021-12-25]
80 -
HTTP所用的默认端口号是什么[2022-11-08]
80 -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
停止emacs在80列终端中封装80列列线?(Stop emacs from wrapping 80 column lines in an 80 column terminal?)[2023-11-11]
如果我正确理解这个问题,这不是关于逻辑行换行(文件中的行是如何分段的),而是关于可视化换行(如何根据窗口宽度显示行)。 如果您只是希望显示器在窗口边界之前直观地包装多个零个字符,并且避免了任何地方的反斜线 ,但无论您的逻辑线路真的长,您可以使用延线模式 : 与可视线模式不同,长线模式会在填充列(请参阅填充命令)中打破较长的线条,而不是右边的窗口边缘。 要启用长线模式,请键入Mx延线模式。 如果文本中有很长的一行,这也会立即“包装”它们。 然后,只需使用全局设置( .emacs ,尽管您可能希望针对特定情况使 ... -
以下代码段将解决您的问题。 public class Test { public static void main( String[] args ) throws Exception { HashMap
personMap = new HashMap (); try { BufferedReader in = new BufferedReader( new File ... -
选择所需的范围并输入以下ex命令: :'<,'>s/\s*\\$/\=repeat(' ', 80-col('.')).'\' 用行重尾处的空格和\替换表达式,该表达式在第80列之前重复一个空格,然后附加一个\字符。 如果您的行超过80个字符,它将附加0个空格,这可能不是您想要的,在这种情况下将80更改为79并为字符串添加空格: ' \' select the range you want and enter the following ex command: :'<,'>s/\s*\\$/\=repea ...
-
您不必逐个读取行。 string[][] words = null; private void btnInput_Click(object sender, EventArgs e) { string line = "", contents = ""; try { using (StreamReader file = new StreamReader("pers.txt")) { contents = file.ReadToEnd(); ...
-
我认为你最好只是自己打印每个字符,并在添加换行符之前检查空格。 就像是: ((charCount > 60) && (currentChar == ' ')) { // print newline and reset counter printf("\n"); charCount = 0; } 编辑:在任何事情之前检查当前的char是否已经是换行符并跳过它。 检查回车\r\n因为在Windows换行符是\r\n 。 #include
int main(void ... -
使用virtualedit选项: :set ve=all 然后,您可以使用此命令将光标移动到行尾后的任何位置,例如: 81| ; 然后你只需要使用Ctrl-V +向上/向下箭头,然后: r\在每一行上添加一些反斜杠。 Use the virtualedit option: :set ve=all Then you can move your cursor anywhere after the end of the line, with this command for example: 81|; the ...