Pyxelで作る06 スコアとスコア表示
参考サイト
今回も↓のサイトにお世話になっています
dev.classmethod.jp
今回追加したコード
self.scoreという得点用の変数を作った初期化
playerとfruitの衝突判定内で、得点を加算
draw(self)内でUIとして表示
from random import randint import pyxel class App: def __init__(self): 【省略】 #得点初期化 self.score = 0 def update(self): 【省略】 def update_player(self): 【省略】 def update_fruit(self, x, y, is_active): #衝突判定(playerとfruitの座標の絶対値から衝突しているか判定している) if is_active and abs(x - self.player_x) < 12 and abs(y - self.player_y) < 12: is_active = False #表示を消す self.score += 10 #scoe加算 【省略】 def draw(self): 【省略】 #score表示用に整形(format関数の文字列操作を利用) s = "Score:{:>4}".format(self.score) #text表示(x座標、y座標、文字列、color) pyxel.text(5, 4, s, 7) App()
ポイント
UIの表示部分ここ
s = "Score:{:>4}".format(self.score)
pythonのformat関数の文字列操作をしていました
詳しく説明していたサイトがあって参考にしました
gammasoft.jp
つまり今回はコードでは
- Score:文字列は常時表示
- インデックス無し(self.scoreだけだからかな)
- >はテキストの右寄せ表示
- 4は表示の最小幅
次は、効果音かな