Getting started with the j2_2d games (6) timer, rewards and punishments, and comments

Source: Internet
Author: User

Vi. Timer, rewards and punishments and evaluation
We should add our timer. I want to start timing from the beginning of the game and constantly update it to the screen. After the game is over, the timer will stop working.
First, record the current time during initialization:
Gametime = 0;
Gametimeoffset = System. currentTimeMillis ();
In the future, as long as the game does not have gameover, the calculation will be performed once in each update cycle:
Gametime = (System. currentTimeMillis ()-gametimeoffset)/1000;
// Convert to seconds
The following is to display it. Do you still remember the font class we used to implement? This is useful. First, instantiate a font class:
Img = ImageTools. getImage ("/pic/B _number.png ");
Fontbig = new Font (g, img, 10, 15,
New char [] {'0', '1', '2', '3', '4', '5', '6', '7 ', '8', '9 '});
Then it is displayed:
Fontbig. drawString (String. valueOf (gametime), screenwidth/2-15, 10 );
It's really convenient, yeah!
Next is the reward system, which requires that a bomb be added to the player every 20 s.
Int awardindex = (int) gametime/20; // calculate the reward time
If (awardindex> bombawardtop)
Awardindex = bombawardtop;
If (bombaward [awardindex]! = 0) {// if there is no reward for this 20 s
Bombnum + = bombaward [awardindex];
Bombaward [awardindex] = 0; // rewarded
}
As simple as possible and give some tips is the principle I wrote this article, so the evaluation system is very ez.
We create a helper class that provides a method to input the game time and return a String comment.
Public class StringTools {
Protected StringTools (){
}
Public static String timeOpinion (long gametime ){
If (gametime <10 ){
Return "Do you play with your foot? ";
// Return "I can't belive, your are a game master ";
} Else if (gametime <16 ){
Return "come boy, you can do it! ";
} Else if (gametime <20 ){
Return "what a pity! Try again .";
} Else if (gametime <25 ){
Return "very well, you are a real man .";
} Else if (gametime <30 ){
Return "I know you have talent of this game .";

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.