简体   繁体   English

在Android上,如何绘制像pygame Rect这样的Rect? (x,y,w,h)

[英]On Android, how to draw a Rect like pygame Rect? (x, y, w, h)

Is there any way of drawing a Rect in Android in the following pattern, instead of "left, top, right, bottom"? 有什么办法可以按照以下模式在Android中绘制Rect,而不是“左,上,右,下”? The problem is that I'm using random coordinates: 问题是我正在使用随机坐标:

Rect e = new Rect(random.nextInt(300)+20,20,random.nextInt(300)+45,70);

And it's hard to keep track of the first left random position to include in the right one to the width. 而且很难跟踪左边的第一个随机位置以包括在右边的宽度中。 In the "x, y, w, h", it's easy to apply my collision test: 在“ x,y,w,h”中,很容易应用碰撞测试:

if ((((p.left+p.width())>e.left)&&(p.left<(e.left+e.width())))&&
       (((p.top+p.height())>e.top)&&(p.top<(e.top+e.height())))) {
    gameScreen = 2;
}

Just move the random numbers outside of the constructor for your rect. 只需将随机数移到构造函数的外部即可。

int left = random.nextInt(300)+20;
int right = left + 20;
int top = random.nextInt(300)+45;
int bottom =  top + 70;

Rect e = new Rect(left, top, right, bottom);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM