简体   繁体   中英

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"? 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:

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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