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!

文字を表示させたい

解決したいこと

猫がマウス(魚)に3回ついたときに一文字ずつ背景に表示させ、4回目で文字をすべて消す。
この繰り返しをするプログラムを書きたいです。
それに伴い、一番下に記載しました名前を追加したいです。
プログラム初心者なものでやり方が分かりません。
どなたかご教示ください。よろしくお願い致します。

該当するソースコード

#name(hiragana)
ka_x1 = [60, 180, 70]
ka_x2 = [220, 220, 130]
ka_y1 = [240, 450, 450]
ka_y2 = [240, 240, 150] #か
       
zu_x1 = [290, 280, 380, 470, 510]
zu_x2 = [430, 430, 440, 470, 510]
zu_y1 = [210, 450, 290, 200, 200]
zu_y2 = [210, 210, 450, 150, 150] #ず

ki_x1 = [570, 550, 620]
ki_x2 = [720, 740, 690]
ki_y1 = [230, 320, 150]
ki_y2 = [230, 320, 450] #き

def setup():
    size(800, 600)
    fill

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

def draw_cat(x, y):
    for j in range(3):
        stroke(255, 0, 0)
        line(ka_x1[j], ka_y1[j], ka_x2[j], ka_y2[j])
        
    for k in range(5):
        stroke(0, 255, 0)
        line(zu_x1[k], zu_y1[k], zu_x2[k], zu_y2[k])
        
    for l in range(3):
        stroke(0, 0, 255)
        line(ki_x1[l], ki_y1[l], ki_x2[l], ki_y2[l])
    
    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])
    
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

その文字座標データから単純に文字を表示するだけのコードを自分で書いてみませんか?

1

Comments

  1. @psuke92

    Questioner
    lineやforを使ったものを一回考えたのですが、やり方が悪いのかうまくいきませんでした。
    今一度自分で考えてみるのでヒントをいただけませんか?
    お願いします。
  2. 猫を表示するdraw_cat処理を入れ替えて、「か」だけ描いてみてはいかがですか?
  3. @psuke92

    Questioner
    すみません。関数がまず定まっていないので関数のヒントをいただきたいです。
    コードについては自分でまず考えます。
    お願いします。
  4. 関数はdraw_catをそのまま使って、中身を入れ替えればいいですよ。
    猫を描画する処理を「か」を表示する処理に変更するのが簡単な第一歩かと思います。
    「か」を描けないなら、線を1本描く処理を書いてみるといいですよ。
  5. @psuke92

    Questioner
    プログラムを書かれている方から見ると下手な書き方だと思いますが、名前を出すことはできました。(draw_catの下にforでいれました)
    マウスカーソルの位置に猫が来た時に、一文字ずつ出すやり方はどのようにしたらよいでしょうか。
    4回目で文字をすべて消して、5回目でまた一文字目って感じで考えています。
    教えていただきたいです。よろしくお願いいたします。
  • 文字を描画する処理を関数に分ける
  • さかなを捕まえた回数を数える
  • さかなを捕まえている間(同じ座標の間)は回数を数えない
  • 猫を描画する前に背景に文字を描画する
  • 表示する文字数は剰余演算(%)を使って求める

といたことをするといいですよ。

#name(katakana)
ka_x1 = [60, 180, 70]
ka_x2 = [220, 220, 130]
ka_y1 = [240, 450, 450]
ka_y2 = [240, 240, 150]
       
zu_x1 = [290, 280, 380, 470, 510]
zu_x2 = [430, 430, 440, 470, 510]
zu_y1 = [210, 450, 290, 200, 200]
zu_y2 = [210, 210, 450, 150, 150]

ki_x1 = [570, 550, 620]
ki_x2 = [720, 740, 690]
ki_y1 = [230, 320, 150]
ki_y2 = [230, 320, 450]

def setup():
    size(800, 600)

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

def draw_ka():    
    stroke(255, 0, 0)
    for x1, y1, x2, y2 in zip(ka_x1, ka_y1, ka_x2, ka_y2):
        line(x1, y1, x2, y2)

def draw_zu():
    stroke(0, 255, 0)
    for x1, y1, x2, y2 in zip(zu_x1, zu_y1, zu_x2, zu_y2):
        line(x1, y1, x2, y2)
        
def draw_ki():
    stroke(0, 0, 255)
    for x1, y1, x2, y2 in zip(ki_x1, ki_y1, ki_x2, ki_y2):
        line(x1, y1, x2, y2)
        
def draw_name(count):
    name = [draw_ka, draw_zu, draw_ki]
    for draw_func in name[:count % (len(name) + 1)]:
        draw_func()

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])
    
class cat:
    x = 400
    y = 300
    speed = 1
    
class eat:
    x = -1
    y = -1
    count = 0

def chase():
    cat.x += max(min(mouseX - cat.x, cat.speed), -cat.speed)
    cat.y += max(min(mouseY - cat.y, cat.speed), -cat.speed)
    if cat.x == mouseX and cat.y == mouseY:
        if eat.x != mouseX or eat.y != mouseY:
            eat.count += 1
            eat.x = mouseX
            eat.y = mouseY
    draw_name(eat.count)
    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

Comments

  1. @psuke92

    Questioner
    書いていただいてありがとうございます。
    確認しました。
    想像していたものを完成させていただき、私には書くことができないのでとても感謝しています。
    これからもっとコードを書く練習をして自分で考えて書けるようになりたいと思います。
    この度は私の数多くの質問に答えていただき、またコードを書いていただいて本当にありがとうございました。
    また何か疑問が生まれた時、助けていただけると嬉しいです。
    ありがとうございました。

Your answer might help someone💌