[英]I can't get my algorithm right in java
I'm working on a small game where walls come at you and you have to dodge them by getting through gaps 我正在做一个小型游戏,墙壁碰到你,你必须通过消除缝隙来躲避它们
My problem is that I can't get my gaps equal sized 我的问题是我无法使缝隙大小相等
int x = random.nextInt(Game.WD - 200) + 100;
int y;
int HEIGHT = 10;
int WIDTH = Game.WD - x;
int gap = 100;
public void paint(Graphics g){
g.drawRect(x, y, WIDTH , HEIGHT); //right part
g.drawRect(-x, y, (Game.WD - gap) , HEIGHT); //left part
I know it has something to do with this part of the class. 我知道这与课程的这一部分有关。 when i run this, it gives me unequal gaps.
当我运行它时,它给了我不平等的差距。 i hope this is sufficient information.
我希望这是足够的信息。
game.WD is equal to 500 game.WD等于500
Here is the whole class just in case: 这是整个班级,以防万一:
import java.awt.Graphics;
import java.awt.Rectangle; 导入java.awt.Rectangle; import java.util.Random;
导入java.util.Random;
public class Border 公共课边框
Random random = new Random();
int x = random.nextInt(Game.WD - 200) + 100;
int y;
int speed = - 6;
int HEIGHT = 10;
int WIDTH = Game.WD - x;
int gap = 100;
@SuppressWarnings("unused")
private Game game;
public Border(int i) {
this.y = i;
}
public void paint(Graphics g){
g.drawRect(x, y, WIDTH , HEIGHT);
g.drawRect(-x, y, (Game.WD - gap) , HEIGHT);
}
public Rectangle getBounds3(){
return new Rectangle(x, y, WIDTH, HEIGHT);
}
public Rectangle getBounds4(){
return new Rectangle(x, y, WIDTH, HEIGHT);
}
public void move() {
y += speed;
if (y <= 0){
y = Game.HE;
x = random.nextInt(Game.WD - 200) + 100;
WIDTH = Game.WD - x;
}
}
-x
is your mistake, it makes no sense to use a value that is a mirror of x
in the y axis. -x
是您的错误,使用在y轴上x
镜像的值没有意义。
For this: 为了这:
0--a<gap>x---b
Define like so: 定义如下:
//a+gap = x so:
a = x - gap;
b = Game.WD - x;
g.drawRect(0, y, a, HEIGHT); //left part
g.drawRect(x, y, b, HEIGHT); //right part
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.