gpt4 book ai didi

java - 避免不必要的检查

转载 作者:行者123 更新时间:2023-12-01 09:58:46 30 4
gpt4 key购买 nike

我有一款游戏有两种模式:限时模式和库存限制模式(有点像粉碎兄弟)。在我的游戏循环中,调用了一个名为 renderGame() 的方法,该方法更新移动对象的显示坐标,然后调用 repaint()。

@Override
public synchronized void paint(Graphics g) {
super.paintComponent(g);

Graphics2D g2d = (Graphics2D) g;

//Background Image
g2d.drawImage( displayBackground, 0, 0, null );

//Stage Image
g2d.drawImage( displayMap, 0, 0, null );

//Players Images
for( Player p: players ){
if( p.getPlayerNumber() == 1 )
g2d.drawImage( displayMario, p1XDisplayCoord, p1YDisplayCoord, null);
else
g2d.drawImage( displayLuigi, p2XDisplayCoord, p2YDisplayCoord, null);
}

//Not implemented yet
drawHUD( g2d );

g2d.dispose();
}

我的问题是:我怎样才能使drawHUD()方法根据比赛中的当前进展绘制不同的东西,而不必在每次调用renderGame()时测试定义该状态的条件我的游戏循环?有点像铁路道岔。

例如:它应该在启动期间绘制“准备就绪-开始”序列,在游戏过程中绘制玩家统计数据,并且在比赛结束后应该指示比赛获胜者。它还应该取决于游戏类型。例如:显示一个计时器,该计时器将在股票比赛期间递增,但在定时比赛期间递减。

最佳答案

大问题:

  • 您正在重写paint,但又调用了super.paintComponent——这是一件非常危险的事情。相反,paintComponent 并调用相同的 super 方法。
  • 您正在处理 JVM 提供给您的 Graphics 对象,这是另一件非常危险的事情,因为它完全破坏了图形链。不要这样做,而只处理您自己创建的 Graphics 对象。
  • Swing 是单线程的,因此没有理由同步 Paint 或 PaintComponent,如果这样做也会有风险。

回复:

My question is: how can I make it so that the drawHUD() method will draw different things based on the current progression within the match without having to test for the conditions that define that state every time I call renderGame() in my game loop? Kinda like a railway switch.

大多数状态检查应该不会很昂贵,但如果有的话,您可以使用某种 boolean 开关,该开关在状态更改时设置,绘画方法可以测试然后重置。

关于java - 避免不必要的检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36972002/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com