- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
通常此类游戏中只有 Y 轴移动可用,但我决定以同样允许 X 轴桨移动的方式进行制作。如果我在球击中桨时不在 X 轴上移动桨(在这种情况下它会直接穿过),则游戏工作得很好。如果我在球击中桨之前停止 X 轴移动,球每次都会从球上弹开。 p>
起初我认为这与像素有关,但我也尝试以多种方式改变桨碰撞,但没有取得积极的结果。无论我尝试多少次,球都不会从 Racket 上弹开。
代码非常简单,我一次只将所有内容移动 1px:
public void drawBall(Graphics g) { //ball speed, movement and collision
b.set(b.getX()+VX,b.getY()+VY); //velocity X and velocity Y both at 1px
g.setColor(Color.black);
b.draw(g);
if(b.getY()<0 || b.getY()>sizeY-b.getRadius()){ //top & bottom collision
VY = -VY;
}
if(b.getX()<0 || b.getX()>sizeX-b.getRadius()){ //left & right detection
b.center();
VX = -VX;
x = (int) (Math.random()*2+0);
if(x == 0)
VY = -VY;
}
if(b.getX() == p1.getX()+p1.width && b.getY()>p1.getY() && b.getY()<p1.getY()+p1.height){ // p1 paddle detection
VX = -VX;
}
if(b.getX()+b.getRadius()==p2.getX() && b.getY()>p2.getY() && b.getY()<p2.getY()+p2.height){ // p2 paddle detection
VX = -VX;
}
}
为了移动桨,我使用简单的标志,将按键时的移动设置为“打开”,释放时设置为“关闭”。我在不定式循环中运行它,因此它会不断执行。
这就是“run()”方法的样子:
public void run() {
while(true){
repaint();
try {
Thread.sleep(5);
playerMovement(); //moves players paddle on X and/or Y axis
playerLimits(); //limits movment of players to specific area
} catch (InterruptedException e){
}
}
}
我再次尝试以这样的方式创建 Racket 碰撞,即它不是特定于像素的“如果(球<比p1 Racket )然后改变球方向”,但没有成功。我想我在这里遗漏了一些重要的东西,但我似乎无法找出它是什么。
感谢您提前提供的任何帮助,非常感谢。
最佳答案
不要将游戏引擎代码与图形代码混合——两者必须分开。任何解决方案的关键是确保游戏引擎能够识别“碰撞”并正确且明确地处理它。不要切换速度方向,而是考虑根据碰撞发生的位置设置这些速度的绝对值。
例如,您有以下代码:
if(b.getY() < 0 || b.getY() > sizeY-b.getRadius()){ //top & bottom collision
VY = -VY;
}
但是如果 Sprite 超出了边缘,就有可能被困住。也许更好的做法是这样做:
if (b.getY() < 0) {
// note that variables should start with lower-case letters
vy = Math.abs(vy); // make sure that it is going in a positive direction
}
if (b.getY() > sizeY - b.getRadius()) {
vy = -Math.abs(vy);
}
x 方向也是如此
另请注意,如果将图形与游戏引擎分开,您的引擎将变得可单独测试并且更易于调试。
关于java - 小程序 "pong"游戏 : ball goes right through paddle if paddle is moving towards the ball,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50081836/
执行下面的代码会发生什么? Ball *ball = [[[[Ball alloc] init] autorelease] autorelease]; 最佳答案 让我们分解一下: [Ball allo
我在 Objective-C 中实现了问题“Ball to Ball Collision - Detection and Handling”中的代码。然而,只要球以一定角度碰撞,它们的速度就会急剧增加
我是 SpriteKit 的新手。 我正在尝试解决这个问题:我的场景中有两个球,当我拖动一个球并“击中”另一个球时,这个球应该使用正确的物理原理滚开。 在我的测试代码中我只能“移动”第二个球,他没有使
我有3个文件 文件 1:index.html Bouncing ball 文件 2:bounce.js var ball = document.getEle
我正在尝试使用 Swift 制作 Pong 游戏,但不使用 SpriteKit。到目前为止,我已经能够在我的 View 中成功绘制一个矩形,并且能够在屏幕上拖动它。这是我当前的代码: import U
通常此类游戏中只有 Y 轴移动可用,但我决定以同样允许 X 轴桨移动的方式进行制作。如果我在球击中桨时不在 X 轴上移动桨(在这种情况下它会直接穿过),则游戏工作得很好。如果我在球击中桨之前停止 X
我的问题几乎就在标题中,为什么我在 actionscript 3.0 中一直读到在编写代码时将“思想”与“对象”分开是个好主意? 感谢您的帮助,这让我很困惑。 最佳答案 如果你问为什么图形与定位、运动
Ball-larus“有效路径分析”算法的实现是否随处可用? [在 llvm 中实现会更有帮助] 这是原始论文的 Citeseer 链接 BL96 最佳答案 已经有一个 implementation
我是一个 JS 菜鸟。我正在进入浏览器游戏编程,并想制作一个球掉落和弹跳的快速示例以供学习。由于某种原因,当我创建 jsfiddle 时,我的代码实际上不起作用,我的 div id="ball"的 o
我一直在寻找,知道人们可以使用核心图像和 openGL 来跟踪面部。但是,我不确定从哪里开始使用 iOS 相机跟踪彩球的过程。 一旦我获得了追踪球的线索。我希望创造一些东西来检测。当球改变方向时。 抱
这是我的代码: Touch Tracker Marker body { margin: 0px; overflow
这是我的第一个图形 Java 程序,我想做的是重新创建一个简单的经典程序,其中我有多个球在 JFrame 中弹跳。窗口。 到目前为止,我已经成功地使用 run() 中的代码让一个球弹来弹去。方法。这适
因为我是 XNA 的新手,所以我正在开发一个乒乓球游戏,但我遇到了一个问题......我有 3 个类,"Game1.cs"、"Ball.cs" 和 "GreenPaddle.cs"。 GreenPad
我需要一些简单的 C 语言“球”光栅化例程。 我根据中点-Bresenham 给自己做了一些东西我首先计算的算法(在维基百科中看到)x-y屏幕平面中的圆点 // XXX // XXXXX // XXX
我儿子问我是否可以编写一个小程序让球在屏幕上弹跳,然后让我解释一下。发现一个很好的父子机会,我说“是的!没问题”。所以我挖掘了我的 python 技能并写了这个.. #!/usr/bin/python
我正在尝试更多地了解 Android 中的手势和图形,所以我正在尝试制作一种游戏,您从屏幕中间的一个球(只是一个红色圆圈)开始,如果您滑动在一个方向上,球将朝那个方向发射,屏幕弹跳等。现在,我已经成功
我有两个查询,每个查询返回一个节点 ID 列表 SELECT node.nid FROM dpf_node AS node WHERE node.type = 'image' AND node.
我正在使用 li我的菜单标签,但它一直显示圆盘球 即使我的 html 中没有它... 我怎样才能摆脱它?我查看了谷歌,但找不到答案。 谢谢! 最佳答案 CSS: li { list-style: no
此代码适用于一款名为“brick-breaker”的游戏。我试图让球在与数组相交时改变颜色。我为自定义颜色的 R、G、B 值创建了一个 int,并在与 1-255 之间的数字相交时生成这些 int i
我有一个类 mypanel 从 jpanel 扩展而来,我在其中使用图形并制作球。第二类是 Main,我在其中制作 JFrame 并将面板添加到框架。 Main 中还有另一个类 MKeyListene
我是一名优秀的程序员,十分优秀!