Menu
CoddyTech

البطل: الوصول إلى الحافة

جزء من قسم الإدخال والحركة في رحلة Godot على Coddy. الدرس 28 من 28.

الهدف هو الحافة العالية التي عليها العلم. يحمل البطل Banner، وهو Label مخفي يقول لقد نجحت!

على الأرض، تكون قيمة position.y الخاصة بالبطل 278. وعلى الدرجة تكون 218، وعلى الحافة 158. لذا يكون البطل على الحافة عندما يكون على الأرض وتكون قيمة position.y أقل من 170:

if is_on_floor() and position.y < 170:
	$Banner.visible = true

الجزء is_on_floor() مهم. فالقفز من الأرض يمر أيضًا بالقيمة 170 للحظة، لكن في الهواء.

للفوز، اقفز فوق الحفرة، ثم إلى الدرجة، ثم إلى الحافة.

challenge icon

التحدي

متوسط

أظهر لافتة عندما يصل البطل إلى الحافة.

  1. في نهاية _physics_process()، أضف شرط if: is_on_floor() وposition.y < 170.
  2. أظهر $Banner بداخله.

اضغط على «العب» واصعد إلى الحافة: ستظهر You made it! على اللافتة. ثم اضغط على «تحقق».

جرّب بنفسك

extends CharacterBody2D

var speed = 220
var acceleration = 1200
var friction = 2400
var gravity = 1000
var jump_velocity = -500
var start_position = Vector2()

func _ready():
	start_position = position

func _physics_process(delta):
	velocity.y += gravity * delta
	if Input.is_action_just_pressed("jump") and is_on_floor():
		velocity.y = jump_velocity
	var direction = Input.get_axis("move_left", "move_right")
	if direction == 0:
		velocity.x = move_toward(velocity.x, 0, friction * delta)
	else:
		velocity.x = move_toward(velocity.x, direction * speed, acceleration * delta)
	if direction > 0:
		$Look.scale.x = 1
	if direction < 0:
		$Look.scale.x = -1
	move_and_slide()
	if position.y > 400:
		position = start_position
		velocity = Vector2(0, 0)
quiz iconاختبر نفسك

يتضمن هذا الدرس اختبارًا قصيرًا. ابدأ الدرس للإجابة عليه وتتبّع تقدمك.

جميع دروس الإدخال والحركة

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