衝突判定のやりかた
Spriteクラスの衝突判定はSprite.checkで使うことができるが、もっと手軽に使えないものかと。いちいち継承してクラス定義してhitメソッドを作ってとかするほどでもない場合に、Sprite.checkの戻り値で判断できるが、それすらめんどくさい。
どこまでもめんどくさがり。
#!ruby -Ks require 'dxruby' class Sprite def ===(v) Sprite.check(self, v) end end font = Font.new(32) s1 = Sprite.new(220,240) image_s1_1 = Image.new(200,40).draw_font_ex(100 - font.get_width("はじめから") / 2, 4, "はじめから", font, :color=>[255,255,255]) image_s1_2 = Image.new(200,40,[255,255,255]).draw_font_ex(100 - font.get_width("はじめから") / 2, 4, "はじめから", font, :color=>[0,0,0]) s1.image = image_s1_1 s2 = Sprite.new(220,280) image_s2_1 = Image.new(200,40).draw_font_ex(100 - font.get_width("つづきから") / 2, 4, "つづきから", font, :color=>[255,255,255]) image_s2_2 = Image.new(200,40,[255,255,255]).draw_font_ex(100 - font.get_width("つづきから") / 2, 4, "つづきから", font, :color=>[0,0,0]) s2.image = image_s2_1 s3 = Sprite.new(220,320) image_s3_1 = Image.new(200,40).draw_font_ex(100 - font.get_width("おわる") / 2, 4, "おわる", font, :color=>[255,255,255]) image_s3_2 = Image.new(200,40,[255,255,255]).draw_font_ex(100 - font.get_width("おわる") / 2, 4, "おわる", font, :color=>[0,0,0]) s3.image = image_s3_1 point = Sprite.new point.collision = [0,0] Window.loop do point.x, point.y = Input.mouse_pos_x, Input.mouse_pos_y s1.image = image_s1_1 s2.image = image_s2_1 s3.image = image_s3_1 case point when s1 s1.image = image_s1_2 when s2 s2.image = image_s2_2 when s3 s3.image = image_s3_2 end s1.draw s2.draw s3.draw end
===を定義してみた。
これでa===bで衝突判定ができるし、case〜when〜endで当たってるやつを選択できる。
せっかくだから追加しておこう。