LoginSignup
psuke92
@psuke92

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

四足歩行の猫を書きたいです。

processingのpythonモードで四足歩行の猫を書きたいです。
そしてその猫(1匹)をマウスについてくる(動く)ように書きたいです。
プログラミング初心者なもので、どなたか教えていただけると幸いです。

今このような感じです。変更すべき点をご教示ください。

def setup():
    size(800, 600)

def draw():
    background(255)
    chase()
    draw_fish()

def draw_cat(x, y):
    diff_x = [[40, 0, -20, 0, -10],
              [-40, 0, 0, 20, 10]]
    diff_y = [[22, 0, -10, -10, 0],
              [22, 0, 0, 0, 10]]
    size_w = [80, 100, 5, 5, 10]
    size_h = [40, 80, 5, 5, 5]
    ear_lx = [[35, 40, 5],
              [30, 40, -20]]
    ear_rx = [[-30, -40, 20],
              [-35, -40, -5]]
    ear_y = [-50, -20, -20]
    tail_x = [[75, 90], [-75, -90]]
    tail_y = [20, -5]
    leg1_x = [[20, 20], [-20, -20]]
    leg1_y = [50, 15]
    leg2_x = [[33, 33], [-33, -33]]
    leg2_y = [50, 15]
    leg3_x = [[50, 50], [-50, -50]]
    leg3_y = [50, 15]
    leg4_x = [[63, 63], [-63, -63]]
    leg4_y = [50, 15]
    mouth_x = [[-5, -5], [10, 10]]
    mouth_y = [25, 25]
    
    if x > mouseX and y > mouseY:
        mode = 0
    elif x <= mouseX and y > mouseY:
        mode = 1
    elif x > mouseX and y <= mouseY:
        mode = 2
    else:
        mode = 3

    noStroke()
    for i in range(5):
        if i < 2:
            fill(239, 228, 176)
        else:
            fill(0)
        ellipse(x+diff_x[mode % 2][i], y+diff_y[mode // 2][i],
                size_w[i], size_h[i])

    fill(239, 228, 176)
    triangle(x+ear_lx[mode % 2][0], y+ear_y[0],
             x+ear_lx[mode % 2][1], y+ear_y[1],
             x+ear_lx[mode % 2][2], y+ear_y[2])
    triangle(x+ear_rx[mode % 2][0], y+ear_y[0],
             x+ear_rx[mode % 2][1], y+ear_y[1],
             x+ear_rx[mode % 2][2], y+ear_y[2])

    stroke(239, 228, 176)
    strokeWeight(10)
    line(x+tail_x[mode % 2][0], y+tail_y[0],
         x+tail_x[mode % 2][1], y+tail_y[1])

    stroke(239, 228, 176)
    strokeWeight(12)
    line(x+leg1_x[mode % 2][0], y+leg1_y[0],
         x+leg1_x[mode % 2][1], y+leg1_y[1])
    
    stroke(239, 228, 176)
    strokeWeight(12)
    line(x+leg2_x[mode % 2][0], y+leg2_y[0],
         x+leg2_x[mode % 2][1], y+leg2_y[1])
    
    stroke(239, 228, 176)
    strokeWeight(12)
    line(x+leg3_x[mode % 2][0], y+leg3_y[0],
         x+leg3_x[mode % 2][1], y+leg3_y[1])
    
    stroke(239, 228, 176)
    strokeWeight(12)
    line(x+leg4_x[mode % 2][0], y+leg4_y[0],
         x+leg4_x[mode % 2][1], y+leg4_y[1])
    
    fill(255, 0, 0)
    ellipse(x+mouth_x[mode % 2][0], y+mouth_y[0], 20, 20)
    
class cat:
    x = 400
    y = 300
    speed = 1

def chase():
    cat.x += max(min(mouseX - cat.x, cat.speed), -cat.speed)
    cat.y += max(min(mouseY - cat.y, cat.speed), -cat.speed)
    draw_cat(cat.x-25, cat.y-25)

def draw_fish():
    noStroke()
    fill(150, 200, 250)
    ellipse(mouseX, mouseY, 50, 20)
    triangle(mouseX+10, mouseY, mouseX+30, mouseY-10, mouseX+30, mouseY+10)
0

2Answer

ソースコードをコピー&ペーストして動作確認できるように、マークダウンのコードブロックを使って ソースコードを貼り付けてください。

追いかける猫を追加してみました。
あとは、猫の描画を変えるとかしてみてください。

class cat:
    x = 400
    y = 300
    speed = 3


def chase():
    cat.x += max(min(mouseX - cat.x, cat.speed), -cat.speed)
    cat.y += max(min(mouseY - cat.y, cat.speed), -cat.speed)
    draw_cat(cat.x, cat.y)


def setup():
    size(800, 600)


def draw():
    background(255)
    for i in range(5):
        for j in range(3):
            x = 100 + i * 150
            y = 150 + j * 150
            draw_cat(x, y)
    chase()
    draw_fish()


def draw_cat(x, y):
    diff_x = [[0, 0, -20, 0, -10],
              [0, 0, 0, 20, 10]]
    diff_y = [[50, 0, -10, -10, 0],
              [50, 0, 0, 0, 10]]
    size_w = [40, 100, 5, 5, 10]
    size_h = [60, 80, 5, 5, 5]
    ear_lx = [[35, 40, 5],
              [30, 40, -20]]
    ear_rx = [[-30, -40, 20],
              [-35, -40, -5]]
    ear_y = [-50, -20, -20]
    tail_x = [[0, 40], [0, -40]]
    tail_y = [75, 40]

    if x > mouseX and y > mouseY:
        mode = 0
    elif x <= mouseX and y > mouseY:
        mode = 1
    elif x > mouseX and y <= mouseY:
        mode = 2
    else:
        mode = 3

    noStroke()
    for i in range(5):
        if i < 2:
            fill(239, 228, 176)
        else:
            fill(0)
        ellipse(x+diff_x[mode % 2][i], y+diff_y[mode // 2][i],
                size_w[i], size_h[i])

    fill(239, 228, 176)
    triangle(x+ear_lx[mode % 2][0], y+ear_y[0],
             x+ear_lx[mode % 2][1], y+ear_y[1],
             x+ear_lx[mode % 2][2], y+ear_y[2])
    triangle(x+ear_rx[mode % 2][0], y+ear_y[0],
             x+ear_rx[mode % 2][1], y+ear_y[1],
             x+ear_rx[mode % 2][2], y+ear_y[2])

    stroke(239, 228, 176)
    strokeWeight(10)
    line(x+tail_x[mode % 2][0], y+tail_y[0],
         x+tail_x[mode % 2][1], y+tail_y[1])


def draw_fish():
    noStroke()
    fill(150, 200, 250)
    ellipse(mouseX, mouseY, 50, 20)
    triangle(mouseX+10, mouseY, mouseX+30, mouseY-30, mouseX+30, mouseY+10)
1

Comments

  1. @psuke92

    Questioner
    確認してみました。とてもすごくて感動しました!本当にありがとうございます。
    ついてきてくれる猫以外(後ろに並ぶ猫)を消したいのですができますか?
    お願いばかりしてしまって申し訳ないです。
    よろしくお願いいたします。
  2. インデントが崩れていて実行できません。
    質問を修正してコードを張り付け、コードの最初の行の前に
    ```py
    と書き、コードの最後の行の次の行に
    ```
    と書いてください。
  3. @psuke92

    Questioner
    このような感じで大丈夫ですか?
    丁寧に教えていただいてありがとうございます。
    猫の形はどのようなものでも大丈夫です。
    よろしくお願いいたします。
  4. 追いかける猫を追加してみました。
  5. forループでdraw_catしてる処理を削除すれば追いかける猫だけになりますよ。
  6. @psuke92

    Questioner
    ありがとうございます。やってみます。
    もう一つだけ教えていただきたいのですが、この状態でマウスを押した時に、猫のいる所に文字を1文字ずつ落としていくにはどう書くのが良いですか?
    回答いただけると嬉しいです。
    よろしくお願い致します。
  7. マウスを押した処理をするサンプルプログラムを動かして、わからないことを新たに質問してみてください。
  8. @psuke92

    Questioner
    しらかみゅさんのコードを使わせていただいて、少し書いてみました。
    さかなの下に猫の口を書きたくやってみたのですが、口の周りに体と同じ色が出てしまいます。改善できますか?
    できるようであればお願いいたします。
  9. @psuke92

    Questioner
    分かりにくい文章ですみません。
    ねこに赤色の口を描きたく、class cat の上にmouthで描きました。しかし上手く動作してくれず。
    どこに入れることによって口を書くことが出来るのか教えていただきたいです。
    よろしくお願い致します。
  10. まずは簡単なことから始めてみてはいかがですか?
    さかなの色を変えるだけ。
    猫の口の形を変えるだけ。
    とか。
  11. @psuke92

    Questioner
    分かりました。やってみます!
    ありがとうございます。
This answer has been deleted for violation of our Terms of Service.

Your answer might help someone💌