プログラミング超初心者のゲーム製作 by Pyxel(6日目)

今日は「分岐処理」を学びます。

今日書いたコード

結果:ニコちゃんが画面左から右に移動し、真ん中まできたら画面上に移動する

import pyxel

def draw_stuff(x, y, color_a, color_b):
    pyxel.circ(x, y, 5, color_a)
    pyxel.line(x - 2, y - 2, x - 2, y - 1, color_b)
    pyxel.line(x + 2, y - 2, x + 2, y - 1, color_b)
    pyxel.line(x - 2, y + 2, x + 2, y + 2, color_b)

pyxel.init(120, 120, title = "Day05")
stuff_x = 0
stuff_y = 60

while True:
    if stuff_x < 60:
        pyxel.cls(1)
        draw_stuff(stuff_x, stuff_y, 7, 8 )
        pyxel.flip()
        stuff_x += 1
    elif not stuff_x < 60:
        pyxel.cls(1)
        draw_stuff(stuff_x, stuff_y, 7, 8)
        pyxel.flip()
        stuff_y -= 1

コードの要点

if 条件式A:
  条件Aを満たした時に実行したい処理
elif 条件式B:
  条件Bを満たした時に実行したい処理
else:
  いずれの条件も満たさない時に実行したい処理

ある条件を満たしたら別の処理を実行したい時はif文を使う
実行したい処理は字下げして書くこと。
条件を満たした時はTrue、満たしてない時はFalseという値で表す
pyxel.width:画面の幅が入っている変数
pyxel.height:画面の高さが入っている変数