开发者

Java Game - Easiest way to draw buildings

开发者 https://www.devze.com 2023-01-17 22:09 出处:网络
It\'s my first time doing Java GUI stuff, and I have a few questions. I\'m making a simple side-scrolling game - planning it out, specifically. The window is going to be 800x800.

It's my first time doing Java GUI stuff, and I have a few questions. I'm making a simple side-scrolling game - planning it out, specifically. The window is going to be 800x800.

There's going to be buildings drawn as the game scrolls (every 40 px or so). I'm thinking that I'll make one 900px black 开发者_StackOverflowrectangle, and then set copies of it at different pixel-heights (to vary the heights of the new buildings). Then the bottom part of the rectangle would automatically be clipped.

(1) Would that work?

In the sky above the roofs, I'm going to have targets fly by that can be clicked on and shot down for points. I was thinking I'd make an ImageIcon of the target and then draw it moving across the screen. But I think it'd be hard to have to track the target coordinates and then get a mouse click coordinates and then check if they match.

(2) Is there any easy way to tell if the target is hit with a click (without a lot of coordinates and math)?

There's also skateboarder skating on top of the roofs from left to right. I was thinking I'd put a hidden rectangle behind him and then see if it intersects with a given rectangle roof to tell when he lands. But the problem is that they won't intersect until he's INSIDE the roof. And I want him to be right above the roof.

How can I find out when the skateboarder is just above the roof (without tons of coordinate math)?


There's going to be buildings drawn as the game scrolls (every 40 px or so). I'm thinking that I'll make one 900px black rectangle, and then set copies of it at different pixel-heights (to vary the heights of the new buildings). Then the bottom part of the rectangle would automatically be clipped.

That would be fine - the bottom part would be clipped.

In the sky above the roofs, I'm going to have targets fly by that can be clicked on and shot down for points. I was thinking I'd make an ImageIcon of the target and then draw it moving across the screen. But I think it'd be hard to have to track the target coordinates and then get a mouse click coordinates and then check if they match.

I cant think of any simpler way to do this. Checking a rectangular area for a hit is not too hard:

if(mouseX >= targetX && mouseX < targetX+targetWidth &&
        mouseY >= targetY && mouseY < targetY+targetHeight){
    // is a hit
}

For better collision detection you can have a mask colour on your target sprite, which is hidden (or check if the alpha is 0 if you're not using a mask). If the mouse is clicked on a non-masked pixel within the targets image area then you have a hit. For checking mouse events lookup MouseListener or MouseAdapter.

There's also skateboarder skating on top of the roofs from left to right. I was thinking I'd put a hidden rectangle behind him and then see if it intersects with a given rectangle roof to tell when he lands. But the problem is that they won't intersect until he's INSIDE the roof. And I want him to be right above the roof.

You should not need an invisible rect. The skateboarder image should have a width and height, which you should have available. Images are drawn from the top left, so the base of your skateboarder is the Y position he's drawn at plus the image height. You want to stop that value from moving beyond the roof boundary.

0

精彩评论

暂无评论...
验证码 换一张
取 消