くまちゃんのiOS/Androidゲームプログラミング

マイペースでゲームつくってます。

スコアを画像で描画する

1.数字の0〜9の画像を用意
2.スコア記憶用変数を定義
3.桁の数字を格納する配列を定義(例100万まで表示させるならn[7])
4.トップの桁の数字を取り出す n[0]=score/1000000
5.中間の桁の数字を取り出す n[1]=(score/100000)%10
6.桁を減らして10まで
7.一の位の数字を取り出す   n[6]=score%10
8.描画
*0をあらかじめ表示してもいい場合は、for文ですべて順番に描画
*桁が達していない場合に0は表示したくない場合は、ifでscoreがそれ以上の場合だけ描画

サンプル

n[0]=score/1000000;
n[1]=(score/100000)%10;
n[2]=(score/10000)%10;
n[3]=(score/1000)%10;
n[4]=(score/100)%10;
n[5]=(score/10)%10;
n[6]=score%10;
    
if (score>999999) [[p_bmp objectAtIndex:n[0]+104] drawAtPoint:(CGPointMake(96,0))];
if (score>99999)  [[p_bmp objectAtIndex:n[1]+104] drawAtPoint:(CGPointMake(112,0))];
if (score>9999)   [[p_bmp objectAtIndex:n[2]+104] drawAtPoint:(CGPointMake(128,0))];
if (score>999)    [[p_bmp objectAtIndex:n[3]+104] drawAtPoint:(CGPointMake(144,0))];
if (score>99)     [[p_bmp objectAtIndex:n[4]+104] drawAtPoint:(CGPointMake(160,0))];
if (score>9)      [[p_bmp objectAtIndex:n[5]+104] drawAtPoint:(CGPointMake(176,0))];
                  [[p_bmp objectAtIndex:n[6]+104] drawAtPoint:(CGPointMake(192,0))];