Saturday, January 29, 2011

Tutorial: Android & Mind Games

Developing a game on android is a whole new ball game i.e. very different from making database apps. You have to know the logic behind every move. The flow of the program is complex and you also have to keep in mind about the memory issues if necessary.
Mind games are great mind-teasers as well as thrilling to the general smartphone users. Ever wondered what are the essentials that a typical mind game program is comprised of?
We usually have the following: Random Numbers, Timer, and MotionEvent.
Here is a source code for a little game on Android that i named "Reflexo", as it measures one's reflexes :)


main.xml

reflexo.java:


package com.games.reflexo;

import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.MotionEvent;
import android.view.View;

import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class Reflexo extends Activity {
/** Called when the activity is first created. */

int score = 0;


int left = 0;
int top = 0;
int right = 0;
int bottom = 0;

Button startGame;
ImageView imgView;
TextView scoreDisplay;

MyCount counter;
int length = 10000;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

startGame = (Button)findViewById(R.id.start);
imgView = (ImageView)findViewById(R.id.img);
scoreDisplay = (TextView)findViewById(R.id.score);

startGame.setOnClickListener(new View.OnClickListener(){

public void onClick(View v)
{
startGame.setVisibility(0);
counter.start();
}

});

imgView.setOnTouchListener(new OnTouchListener(){

public boolean onTouch(View v,MotionEvent e) {


if (e.getAction() == MotionEvent.ACTION_DOWN)
{
score++;
}
return true;
}});


counter = new MyCount(length, 300);
}


public class MyCount extends CountDownTimer
{

public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);



}

public void onFinish() {
scoreDisplay.setVisibility(1);
scoreDisplay.setText("Score:" + score);
}

public void onTick(long millisUntilFinished) {

Random randomNum = new Random();
left = randomNum.nextInt(200);
top = randomNum.nextInt(400);
right = randomNum.nextInt(200);
bottom = randomNum.nextInt(400);



if (millisUntilFinished / 100 % 2 == 0)
{
imgView.setPadding(left, top, right, bottom);
imgView.setVisibility(View.VISIBLE);
}
else
{
imgView.setVisibility(View.INVISIBLE);
}


}
}


}



38 comments:

  1. I really like your blog. You make it interesting to read and entertaining at the same time. I cant wait to read more from you.
    Oracle training in pune

    ReplyDelete
  2. Thanks for the informative article. This is one of the best resources I have found in quite some time. Nicely written and great info. I really cannot thank you enough for sharing.

    rpa Training in Chennai

    rpa Training in bangalore

    rpa Training in pune

    blueprism Training in Chennai

    blueprism Training in bangalore

    blueprism Training in pune

    rpa online training

    ReplyDelete
  3. Thank you so much for a well written, easy to understand article on this. It can get really confusing when trying to explain it – but you did a great job. Thank you!
    Click here:
    angularjs training in marathahalli
    Click here:
    angularjs training in bangalore

    ReplyDelete
  4. This is such a great post, and was thinking much the same myself. Another great update.
    Click here:
    Microsoft azure training in velarchery
    Click here:
    Microsoft azure training in sollinganallur

    ReplyDelete
  5. I'm here representing the visitors and readers of your own website say many thanks for many remarkable
    python training institute in chennai
    python training in velachery
    python training institute in chennai

    ReplyDelete
  6. Excellent blog, I wish to share your post with my folks circle. It’s really helped me a lot, so keep sharing post like this
    Blueprism training in marathahalli


    AWS Training in chennai

    AWS Training in bangalore



    ReplyDelete
  7. I love the blog. Great post. It is very true, people must learn how to learn before they can learn. lol i know it sounds funny but its very true. . .
    python training in tambaram
    python training in annanagar
    python training in OMR

    ReplyDelete
  8. Very nice post here and thanks for it .I always like and such a super contents of these post.Excellent and very cool idea and great content of different kinds of the valuable information's.
    Data Science Training in Chennai
    Data science training in bangalore
    Data science online training
    Data science training in pune
    Data science training in kalyan nagar
    selenium training in chennai

    ReplyDelete
  9. Hello I am so delighted I found your blog, I really found you by mistake, while I was looking on Yahoo for something else, anyways I am here now and would just like to say thanks for a tremendous post. Please do keep up the great work.
    java training in chennai | java training in bangalore

    java online training | java training in pune

    java training in chennai | java training in bangalore

    ReplyDelete
  10. A universal message I suppose, not giving up is the formula for success I think. Some things take longer than others to accomplish, so people must understand that they should have their eyes on the goal, and that should keep them motivated to see it out til the end.
    python training in Bangalore
    python training in pune
    python online training
    python training in chennai

    ReplyDelete
  11. A good blog always comes-up with new and exciting information and while reading I have feel that this blog is really have all those quality that qualify a blog to be a one.I wanted to leave a little comment to support you and wish you a good continuation. Wishing you the best of luck for all your blogging efforts read this.
    python training in pune
    python online training
    python training in OMR

    ReplyDelete
  12. This is most informative and also this post most user friendly and super navigation to all posts... Thank you so much for giving this information to me.. 
    Devops Training in Chennai

    Devops Training in Bangalore

    Devops Training in pune

    ReplyDelete
  13. Really you have done great job,There are may person searching about that now they will find enough resources by your post
    Blueprism training in Bangalore

    Blueprism training in Pune

    Blueprism training in Chennai

    ReplyDelete
  14. Appreciating the persistence you put into your blog and detailed information you provide
    Blueprism training in tambaram

    Blueprism training in annanagar

    Blueprism training in velachery

    ReplyDelete
  15. The knowledge of technology you have been sharing thorough this post is very much helpful to develop new idea. here by i also want to share this.

    angularjs Training

    in chennai

    angularjs Training in chennai

    angularjs-Training in tambaram

    angularjs-Training in sholinganallur

    angularjs-Training in velachery

    ReplyDelete
  16. This is very good content you share on this blog. it's very informative and provide me future related information.

    java training in jayanagar | java training in electronic city

    java training in chennai | java training in USA

    ReplyDelete
  17. Thank you for taking the time to provide us with your valuable information. We strive to provide our candidates with excellent care and we take your comments to heart.As always, we appreciate your confidence and trust in us
    java training in tambaram | java training in velachery

    java training in omr | oracle training in chennai

    ReplyDelete
  18. https://christopheralunlewis.blogspot.com/2010/07/adding-alphabetical-lists-in-salesforce.html?showComment=1540194049057#c1720707150627834170

    ReplyDelete
  19. This comment has been removed by the author.

    ReplyDelete
  20. I am really happy with your blog because your article is very unique and powerful for new reader.
    Click here:
    selenium training in chennai
    selenium training in bangalore
    selenium training in Pune
    selenium training in pune
    Selenium Online Training

    https://mcqquestion.blogspot.com/2013/06/web-technologies-internet-technologies_4.html

    ReplyDelete
  21. This comment has been removed by the author.

    ReplyDelete
  22. That was a great message in my carrier, and It's wonderful commands like mind relaxes with understand words of knowledge by information's.
    devops online training

    aws online training

    data science with python online training

    data science online training

    rpa online training

    ReplyDelete

  23. Your info is really amazing with impressive content..Excellent blog with informative concept. Really I feel happy to see this useful blog, Thanks for sharing such a nice blog..
    If you are looking for any Big data Hadoop Related information please visit our website Big Data Hadoop Training In Bangalore page!

    ReplyDelete
  24. This is most informative and also this post most user friendly and super navigation to all posts... Thank you so much for giving this information to me.. Automation Anywhere Training in Bangalore

    ReplyDelete
  25. nice work keep it up thanks for sharing the content.Thanks for sharing this type of information, it is so useful.Its a very amazing blog !!! This valuable and very informative blog is going to help the people to a great extent. Thanks for sharing it here.
    Data Science Training In Chennai

    Data Science Online Training In Chennai

    Data Science Training In Bangalore

    Data Science Training In Hyderabad

    Data Science Training In Coimbatore

    Data Science Training

    Data Science Online Training

    ReplyDelete