Translate

четверг, 24 мая 2012 г.

Android programming for beginners. "Hangman" the word game developing for android. Creating a custom component

  It's first acticle about android "hangman" game development.
  Target of this game is finding the word via guessing letters. If there is letter on the word then we can see all positions of this letter on the word. For example, the word is "knee". In gamestart all letters are unknown. If we pick "e", we see "??ee".





  We have 10 lives in gamestart. If picked letter is wrong (it there is not in the word), we lose one life.  It's game over, if there are not lives. We have won, if have picked all letters in the word.

  Best way for this game developing is creating custom component, which will render game objects on one's canvas in onDraw() method. Its name is LetterField, according class is LetterField.java

  So, game will include 2 classes : WordGameActivity and LetterField.



  In first, we must do this actions in LetterField class:

-determine LetterField as as class View extension

-create constructor

-create onMeasure() method

-make OnDraw() method - it's method for game objects rendering

  Determine width and height fields of LetterField (it's width and height of gamefield). Also, we must detemine field context as Context -for toast showing about win and fail in game.

  Class constructor will load width, height and context:
public LetterField(Context _context, double _width, double _height) {
        super(_context);
        width=_width;
        height=_height;
        context=_context;
}
OnMeasure() method is (it's determines sizes of component in activity):
@Override
protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension((int)width, (int)height);
}



  onDraw() method  first line must be superclass method call -  super.onDraw(canvas);

  Argument of onDraw() is canvas - for rendering. We will call function like this: canvas.drawLine(), canvas.drawText()

 @Override
 protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
 }



  So, full source of class at this moment is:
public class LetterField extends View { 
    double width;
    double height;
    Context context;

    public LetterField(Context _context, double _width, double _height) {
        super(_context);
        width=_width;
        height=_height;   
        context=_context;
    }

  
    @Override
    protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension((int)width, (int)height);
    }
  
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }
}

  Next step is putting component on the screen. Main activity code is:
public class WordGameActivity extends Activity {

    //Fields determining. width and height will be width and height of the device screen
    private LetterField field;
    private AbsoluteLayout Abs;
    double width;
    double height;
  
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //we will to unvisible top panel for usability
        requestWindowFeature(Window.FEATURE_NO_TITLE);
     
        //create new ablosuteLayout (we can out components on this in coordinate position)
        Abs=new AbsoluteLayout(this);
        setContentView(Abs);

        //Display parameters getting
        Display display = getWindowManager().getDefaultDisplay();
        width=display.getWidth();
        height=display.getHeight();
     
        field=new LetterField(this, (float)width, (float)(height));//call of constructor

        AbsoluteLayout.LayoutParams params=new AbsoluteLayout.LayoutParams(//we must determine position and sizes of the custom component. First parameters if width and height, second parameters if X and Y of top-left angle
                (int)width,(int)(height),0,0
        );
     
     
        field.setLayoutParams(params);//including parameters to field
        field.setBackgroundColor(0xBBFFFFFF);//field coloring. 
        //0x-hex system,  BB - 187 - of alpha, FF - 255 - of red, FF- 255 - of green, FF- 255 - of blue
     
        Abs.addView(field);
    }
}
 We are seeing the silver rectangle on the emulator.

Next part

Download source of game

Комментариев нет:

Отправить комментарий

Related Posts Plugin for WordPress, Blogger...