我的 Java 游戏的评分系统
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21476923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Score sytem for my java game
提问by user3256734
I'm making a basic 2D side scroller for a college assignment. I'm looking to add a scoring system to it, to ultimately have a high score system. I'm having trouble getting the score to display, I have a block, that when the player touches it, they get + 1 score, I used:
我正在为大学作业制作一个基本的 2D 侧卷轴。我希望为它添加一个评分系统,最终拥有一个高分系统。我无法显示分数,我有一个块,当玩家触摸它时,他们会得到 + 1 分,我使用了:
JOptionPane.showMessageDialog(null, "Your score : " + Score);
This confirmed that the score was increasing properly. I would like to display the score in the top right of the window constantly, how would I go about it?
这证实了分数正在适当增加。我想一直在窗口右上角显示分数,我该怎么办?
Other parts of the score code:
分数代码的其他部分:
int Score = 0;
public String ScoreCount = "Score: " + Score;
Edit:Thanks for the support, I'm still having trouble with getting it to display. I have:
int Score = 0;
public String ScoreCount = "Score: " + Score;
编辑:感谢您的支持,我仍然无法显示它。我有:
int 分数 = 0; public String ScoreCount = "Score:" + Score;
JLabel scoreLabel = new JLabel("Score: 0");
public void someoneScored()
{
scoreLabel.setBounds(10, 10, 100, 50);
scoreLabel.setText("Score: " + Score);
}
My game has a black background (just a rectangle) would I need to change the colour of the JLabel? (I tried to change the colour of the background to white, and I still couldn't see it).
我的游戏有黑色背景(只是一个矩形),我需要更改 JLabel 的颜色吗?(我试着把背景颜色改成白色,还是看不到)。
回答by christopher
Increment it, and update a JLabel
.
增加它,并更新一个JLabel
.
Example
例子
JLabel scoreLabel = new JLabel("Score: 0");
public void someoneScored()
{
score++;
scoreLabel.setText("Score: " + score);
}
Extra reading
额外阅读
A basic swing tutorial, like thismight be useful.
Given you don't seem to know this, you should read the Java Naming Conventions for how to format your variable names properly. You can read them here.
Edit
编辑
If you want to position your JLabel
, you can do this with the setLocation
method. However, I like to set the size and the location all at once, and I use the setBounds
method.
如果你想定位你的JLabel
,你可以用这个setLocation
方法来做到这一点。但是,我喜欢同时设置大小和位置,并且我使用了该setBounds
方法。
Example
例子
scoreLabel.setBounds(x, y, width, height);
To put this in the top left corner, your x
and y
will be Two very low numbers. Not 0
. Nothing looks worse than a label touching a corner of a window.
把它放在左上角,你的x
和y
将是两个非常低的数字。不是0
。没有什么比贴在窗户一角的标签更糟糕的了。
scoreLabel.setBounds(5, 5, width, height);