Java图形 - 游戏阶段 [英] Java graphics - Stages of a game

查看:85
本文介绍了Java图形 - 游戏阶段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在JFrame中创建游戏时,您要将图形添加到JPanel中,使用 paintComponent() -method,你如何给游戏不同的阶段?哪个最适合演出?



我可以想到的两种方式是:


  1. 添加和删除包含自己的 paintComponent()方法和计时器的面板。游戏的每个阶段(等级等)将使用不同的面板。

  2. 将所有内容保留在一个面板或框架中,并具有许多布尔值和if语句。例如,如果游戏结束的布尔值更改为true,并且在 paintComponent() -method中,则具有if语句,该语句说如果(gameover),然后在游戏的这个阶段绘制图形。

还有其他这样做的更好的方法?

解决方案

放弃所有希望,你们通过Swing进入[ !



为了回答您的问题,JPanels只是一个平面,如黑板或一张纸。他们与阶段无关,除了可以画的东西以外的其他任何东西以及其他子组件的容器。



要解决游戏 - 具体的问题,我建议你不要浪费你的时间使用Swing组件来构建游戏界面。尽管可能可能,您将遇到一些性能问题,只会让您的时间消耗。根据你的问题,你已经在想一些像Timers这样的低级别的东西来安排事情,并且覆盖了 paintComponent()方法 - 一个很常见的错误,许多一个失败的项目花时间只是为了找出它不是最好的方式。



如果你的问题是一个可以看到的森林中的路径,你会注意到这些东西:




  • 这是很好的(因为它似乎是最直接的方法)

  • 在拐角处是几个尖锐的死亡坑的表现,和模糊的,非跨平台兼容的调整

  • 你必须弄清楚如何使用Swing的绘画经理做你想要的在大多数情况下,大多数情况下,您需要对渲染管道/流程进行直接的,确定性的控制。

  • 这些项目正在试图使用Swing进行游戏,很奇怪(和愚蠢的)原因,如我想建立一个只使用的游戏标准Java库。那么标准的库是好的,所有的,但它的核心是设计来解决业务问题,并真正擅长于服务器和网络软件等等。如果您至少不使用一些gamedev库,那么您可能会错过正确的工具使用正确的工具。 Swing绝对不是游戏使用的,因为它基本上不需要游戏开发者,除非你在谈论桌面应用程序/工具,以构建和编辑级别和这种东西



而是使用 Slick2D (适用于2D游戏)或 jMonkeyEngine for 3D shtuff。这些图书馆专门针对构建游戏而设计,并且将不再需要重新发明谚语,只是为了让您的游戏开始前进。他们还有自己的社区,可以帮助您解决图书馆的问题。



除此之外,他们已经解决了一些最棘手的跨平台问题,如处理各种版本的OpenGL,以及其他技术细节,我敢打赌你不太在乎什么时候最后,你真正想要的是,嘿,只是给我一个[任意决议],图形 - 加速的表面画,不要让它复杂!


This is a topic that has been bugging me recently.

When creating a game in a JFrame, and you want to add the graphics to a JPanel with the paintComponent()-method, how would you give the game different stages? Which is best for performance?

The two ways I can think of doing it are:

  1. Adding and removing panels which contain their own paintComponent()-methods and timers. A different panel would be used for each stage of the game (level etc).
  2. Keep everything in one panel or frame, and have many boolean values and if statements. For example, if it is game over have a boolean value change to true, and within the paintComponent()-method, have an if statement which says if(gameover) and then paint the graphics for this stage of the game.

Or are there other, better ways of doing this?

解决方案

"Abandon all hope, ye who enter [through Swing to create games]!"

To answer your question, JPanels are just a surface to draw on, like a chalkboard, or a piece of paper. They have nothing to do with stages, or anything else other than being a thing you can draw on, and a container for other sub-components.

To address the game-specific question, I'd recommend you don't waste your time using Swing components to build a game interface. While it's possible, you're going to run into a bunch of performance problems that will just eat up your time. Based on your question you're already thinking about low-level stuff like Timers to schedule things, and overriding the paintComponent() method - a very common mistake, and a road that many a failed project spend time on just to find out it's not the best way.

If your question were literally a path in a forest that you could see, you would notice these things:

  • It is well trodden (as it seems to be the most direct approach)
  • Just around the corner are several spiky death pits of performance, and obscure, non-cross-platform compatible tweaks
  • You have to figure out how to use Swing's painting manager to do what you want, when in most cases for most games, you want direct, deterministic control over your rendering pipeline/process
  • It is littered with the bodies of projects that try to use Swing for games, for weird (and silly) reasons such as, "I wanted to build a game that only uses the standard Java library." Well, the standard library is nice and all, but at its heart it was designed to solve business problems, and be really good at server and networking software, among other things. If you're not using at least some gamedev libraries, you're probably missing out on using the right tool for the right job. Swing was definitely not built to be used by games, as it serves basically no needs that game developers have, unless you're talking about the desktop apps/tools to build and edit levels and that sort of thing.

Instead, use something like Slick2D (for 2D games), or jMonkeyEngine for 3D shtuff. These libraries are designed specifically for building games, and are going to save you from having to reinvent the proverbial wheel, just to get your game up and going. They also have their own communities that can help you with library-specific issues.

On top of that, they have already solved some of the trickiest cross-platform issues, like dealing with various versions of OpenGL, and other technical details that I'll bet you don't really care about when at the end of the day, what you really want is, "Hey, just give me an [arbitrary resolution], graphics-accelerated surface to draw on, and don't make it complicated!"

这篇关于Java图形 - 游戏阶段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆