0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

windows11で、sketchup6 その90

Posted at

概要

windows11に、sketchup6を入れてみた。
rubyで、3Dを書く。
練習問題やってみた。

練習問題

ブロック崩しを書け。

写真

image.png

サンプルコード

起動 Sketchup.active_model.select_tool Block.new



require 'sketchup.rb'


class Block
	def activate
	end

	def deactivate(view)
		view.invalidate
	end

	def initialize
		model = Sketchup.active_model
		entities = model.active_entities
		@tid = 0
		@state = 0
		@r = [1, 1, 1, 1, 1, 1, 1, 1, 1]
		@ball = entities.add_group
		circle = @ball.entities.add_circle [0, 1, 2], Z_AXIS, 1.0, 20
		circle_face = @ball.entities.add_face circle
		circle_face.pushpull -0.3
		trans = Geom::Transformation.translation [10, 10, 0]
		@ball.transform! trans

		@blocks = {}
		@blocks[0] = entities.add_group
		face0 = @blocks[0].entities.add_face([0, 0, 0], [3, 0, 0], [3, 2, 0], [0, 2, 0])
		face0.pushpull -0.3
		trans0 = Geom::Transformation.translation [2, 30, 0]
		@blocks[0].transform! trans0

		@blocks[1] = entities.add_group
		face1 = @blocks[1].entities.add_face([0, 0, 0], [3, 0, 0], [3, 2, 0], [0, 2, 0])
		face1.pushpull -0.3
		trans1 = Geom::Transformation.translation [2, 26, 0]
		@blocks[1].transform! trans1

		@blocks[2] = entities.add_group
		face2 = @blocks[2].entities.add_face([0, 0, 0], [3, 0, 0], [3, 2, 0], [0, 2, 0])
		face2.pushpull -0.3
		trans2 = Geom::Transformation.translation [2, 22, 0]
		@blocks[2].transform! trans2

		@blocks[3] = entities.add_group
		face3 = @blocks[3].entities.add_face([0, 0, 0], [3, 0, 0], [3, 2, 0], [0, 2, 0])
		face3.pushpull -0.3
		trans3 = Geom::Transformation.translation [6, 30, 0]
		@blocks[3].transform! trans3

		@blocks[4] = entities.add_group
		face4 = @blocks[4].entities.add_face([0, 0, 0], [3, 0, 0], [3, 2, 0], [0, 2, 0])
		face4.pushpull -0.3
		trans4 = Geom::Transformation.translation [6, 26, 0]
		@blocks[4].transform! trans4

		@blocks[5] = entities.add_group
		face5 = @blocks[5].entities.add_face([0, 0, 0], [3, 0, 0], [3, 2, 0], [0, 2, 0])
		face5.pushpull -0.3
		trans5 = Geom::Transformation.translation [6, 22, 0]
		@blocks[5].transform! trans5

		@blocks[6] = entities.add_group
		face6 = @blocks[6].entities.add_face([0, 0, 0], [3, 0, 0], [3, 2, 0], [0, 2, 0])
		face6.pushpull -0.3
		trans6 = Geom::Transformation.translation [10, 30, 0]
		@blocks[6].transform! trans6

		@blocks[7] = entities.add_group
		face7 = @blocks[7].entities.add_face([0, 0, 0], [3, 0, 0], [3, 2, 0], [0, 2, 0])
		face7.pushpull -0.3
		trans7 = Geom::Transformation.translation [10, 26, 0]
		@blocks[7].transform! trans7

		@blocks[8] = entities.add_group
		face8 = @blocks[8].entities.add_face([0, 0, 0], [3, 0, 0], [3, 2, 0], [0, 2, 0])
		face8.pushpull -0.3
		trans8 = Geom::Transformation.translation [10, 22, 0]
		@blocks[8].transform! trans8

		wall0 = entities.add_group
		face9 = wall0.entities.add_face([0, 0, 0], [0, 30, 0], [1, 30, 0], [1, 0, 0])
		face9.pushpull -0.3

		wall1 = entities.add_group
		face10 = wall1.entities.add_face([14, 0, 0], [14, 30, 0], [15, 30, 0], [15, 0, 0])
		face10.pushpull -0.3

		@padol = entities.add_group
		face11 = @padol.entities.add_face([0, 0, 0], [3, 0, 0], [3, 2, 0], [0, 2, 0])
		face11.pushpull -0.3
		trans11 = Geom::Transformation.translation [9, 3, 0]
		@padol.transform! trans11

		@tid = UI.start_timer(1.0, true) {
			tick
		}
	end
	def onLButtonDown(flags, x, y, view)
		ip1 = view.inputpoint x, y
		point = ip1.position
		if point.x < 7.0
			trans = Geom::Transformation.translation [-2, 0, 0]
		else
			trans = Geom::Transformation.translation [2, 0, 0]
		end
		@padol.transform! trans
	end

	def tick
#30
#21
		btrans = @ball.transformation
		bpoint = btrans.origin
		x = bpoint.x
		y = bpoint.y
		if (x < 4)
			if (@state == 3)
				@state = 0
			else
				@state = 1
			end
		end
		if (x > 13)
			if (@state == 0)
				@state = 3
			else
				@state = 2
			end
		end
		if check(x, y, @padol) == 1
			if (@state == 2)
				@state = 3
			else
				@state = 0
			end
		end
		for n in  0..8 do
			if @r[n] == 1
				if check(x, y, @blocks[n]) == 1
					@r[n] = 0
					@blocks[n].erase!
					if (@state == 3)
						@state = 2
					else
						@state = 1
					end
				end
			end
		end
		case @state
		when 0
			trans = Geom::Transformation.translation [ 0.5,  0.5, 0]
		when 1
			trans = Geom::Transformation.translation [ 0.5, -0.5, 0]
		when 2
			trans = Geom::Transformation.translation [-0.5, -0.5, 0]
		when 3
			trans = Geom::Transformation.translation [-0.5,  0.5, 0]
		end
		@ball.transform! trans
	end

	def check(x2, y2, b)
		trans = b.transformation
		point = trans.origin
		x = point.x
		y = point.y
		distance = Math::sqrt((x2 - x) * (x2 - x) + (y2 - y) * (y2 - y))
		#puts distance
		if distance < 2.5
			1
		else
			0
		end
	end
end





以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?