简体   繁体   English

如何使我的800 * 600游戏适合所有屏幕分辨率?

[英]How to make my 800*600 game fit all screen resolutions?

I have created a game that is 800*600. 我创建了一个800 * 600的游戏。 How can i make it so that it will fit all computer screens? 我如何才能使其适合所有计算机屏幕? How would this normally be done? 通常如何做? Other sites have said too set everything to a certain ratio depending what the screen resolution is. 其他网站也表示,将所有内容都设置为一定比例,具体取决于屏幕分辨率。 But if each image in the game changes size in comparison to the screen resolution it would mean that the images would not be in the right places and cause other problems. 但是,如果游戏中的每个图像的大小都与屏幕分辨率相比发生变化,则意味着这些图像将不在正确的位置,并且会引起其他问题。 Is it possible to just "Stretch out" my 800*600 game so it fits any size? 是否可以“拉伸”我的800 * 600游戏,使其适合任何大小? Thanks 谢谢

You need to make the game resolution-independent. 您需要使游戏独立于分辨率。 That means you create your own concept of 'game units' (which might be exactly the same as pixels relative to an 800x600 screen if you want), and then operate exclusively on those game units. 这意味着您要创建自己的“游戏机”概念(如果需要,它可以与相对于800x600屏幕的像素完全相同),然后专门在这些游戏机上运行。

Whenever you draw something to screen you convert co-ordinates and sizes from game units into pixels. 每当您在屏幕上绘制内容时,便会将游戏单位的坐标和大小转换为像素。 You will need to have functions that can convert both ways, because you may also want to translate click-events from screen space to game space. 您将需要具有可以双向转换的功能,因为您可能还希望将点击事件从屏幕空间转换为游戏空间。

When you start your game, you need to decide on the screen resolution and aspect ratio. 开始游戏时,需要确定屏幕分辨率和宽高比。 If the aspect ratio is not the same ( eg widescreen), you might choose to either stretch the game area or letterbox it. 如果宽高比不相同( 例如,宽屏),则可以选择拉伸游戏区域或在其旁边加上字母框。 By moving all the translation code to a lower level and operating only within your game units, you save yourself a lot of pain in the long run. 通过将所有翻译代码移至较低级别并仅在您的游戏单位内运行,从长远来看,您可以避免很多麻烦。

Yes, you can stretch it out. 是的,您可以扩展它。 Make the Games' main components on a JPanel that is 800 by 600, and add that JPanel to your JFrame. 在800 x 600的JPanel上制作游戏的主要组件,并将该JPanel添加到您的JFrame中。 Then the player can resize your JFrame and the components won't go offscreen, they will change shape and size based on the new size of the JFrame. 然后,播放器可以调整JFrame的大小,并且组件不会脱离屏幕,它们将根据JFrame的新大小更改形状和大小。 Remember that the mouse's position in relation to the JFrame will change a bit, but divide the mouses position by the JFrames height/width and multiply it by the JPanel height/width to get the position in relation to the JPanel. 请记住,鼠标相对于JFrame的位置会有所变化,但是将鼠标位置除以JFrames的高度/宽度,再乘以JPanel的高度/宽度,即可得出相对于JPanel的位置。 With this method you can let the user maximize your game and still have components look normal. 使用这种方法,您可以让用户最大程度地发挥游戏效果,并使组件看起来仍然正常。

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

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