Menu
CoddyTech

أطلق النار على الصخور

جزء من قسم المشاريع في رحلة Godot على Coddy. الدرس 17 من 36.

الصخرة هي Area2D، والرصاصة جسم، لذا تخبر body_entered الصخرةَ عندما تصطدم بها رصاصة. كل رصاصة ضمن المجموعة bullets.

func _on_body_entered(body):
	if body.is_in_group("bullets"):
		body.queue_free()
		burst()
		queue_free()

أزِل الرصاصة أيضًا. وإلا فستواصل الطيران وتحطّم الصخرة التي خلفها. الدالة burst() موجودة بالفعل في rock.gd. وهي تُطلق سحابة من الغبار في المكان الذي كانت فيه الصخرة. استدعها قبل queue_free().

challenge icon

التحدي

متوسط

اجعل الرصاصات تحطّم الصخور.

  1. في rock.gd، اربط body_entered بـ _on_body_entered في _ready.
  2. عندما يكون الجسم ضمن المجموعة bullets، أزل الرصاصة، واستدعِ burst() وأزل الصخرة.

اضغط على «العب» وأطلق النار على صخرة: ستتناثر إلى غبار. ثم اضغط على «تحقق».

جرّب بنفسك

extends Area2D

var velocity = Vector2.ZERO

func _physics_process(delta):
	position += velocity * delta
	rotation += 0.5 * delta
	if position.x < -30:
		position.x += 700
	if position.x > 670:
		position.x -= 700
	if position.y < -30:
		position.y += 420
	if position.y > 390:
		position.y -= 420

# Bursts into dust where the rock is. Call it when the rock breaks.
func burst():
	var dust = CPUParticles2D.new()
	dust.position = position
	dust.one_shot = true
	dust.explosiveness = 1.0
	dust.amount = 20
	dust.lifetime = 0.7
	dust.spread = 180.0
	dust.gravity = Vector2.ZERO
	dust.initial_velocity_min = 30.0
	dust.initial_velocity_max = 120.0 * scale.x
	dust.scale_amount_min = 2.0
	dust.scale_amount_max = 5.0
	dust.color = Color(0.75, 0.82, 0.95)
	dust.finished.connect(dust.queue_free)
	get_parent().add_child(dust)
	dust.emitting = true

جميع دروس المشاريع

تدرّب بنفسك: Playground لـ Godot