mirror of
https://github.com/stan220/godot-docs.git
synced 2026-09-08 18:29:11 +00:00
Merge pull request #305 from popenke/patch-1
Fixed some parts where code was rendered as normal text
This commit is contained in:
@@ -148,11 +148,11 @@ our ball move.
|
|||||||
var screen_size
|
var screen_size
|
||||||
var pad_size
|
var pad_size
|
||||||
|
|
||||||
# constant for pad speed (in pixels/second)
|
# Constant for pad speed (in pixels/second)
|
||||||
const INITIAL_BALL_SPEED = 80
|
const INITIAL_BALL_SPEED = 80
|
||||||
# speed of the ball (also in pixels/second)
|
# Speed of the ball (also in pixels/second)
|
||||||
var ball_speed = INITIAL_BALL_SPEED
|
var ball_speed = INITIAL_BALL_SPEED
|
||||||
# constant for pads speed
|
# Constant for pads speed
|
||||||
const PAD_SPEED = 150
|
const PAD_SPEED = 150
|
||||||
|
|
||||||
func _ready():
|
func _ready():
|
||||||
@@ -175,10 +175,8 @@ default, so a small adjustment of ``pad_size / 2`` must be added.
|
|||||||
|
|
||||||
func _process(delta):
|
func _process(delta):
|
||||||
var ball_pos = get_node("ball").get_pos()
|
var ball_pos = get_node("ball").get_pos()
|
||||||
var left_rect = Rect2( get_node("left").get_pos() - pad_size*0.5,
|
var left_rect = Rect2( get_node("left").get_pos() - pad_size*0.5, pad_size )
|
||||||
pad_size )
|
var right_rect = Rect2( get_node("right").get_pos() - pad_size*0.5, pad_size )
|
||||||
var right_rect = Rect2( get_node("right").get_pos() - pad_size*0.5,
|
|
||||||
pad_size )
|
|
||||||
|
|
||||||
Now, let's add some movement to the ball in the ``_process()`` function.
|
Now, let's add some movement to the ball in the ``_process()`` function.
|
||||||
Since the ball position is stored in the ``ball_pos`` variable,
|
Since the ball position is stored in the ``ball_pos`` variable,
|
||||||
@@ -200,8 +198,7 @@ the floor and the roof:
|
|||||||
::
|
::
|
||||||
|
|
||||||
# Flip when touching roof or floor
|
# Flip when touching roof or floor
|
||||||
if ((ball_pos.y < 0 and direction.y < 0) or (ball_pos.y >
|
if ((ball_pos.y < 0 and direction.y < 0) or (ball_pos.y > screen_size.y and direction.y > 0)):
|
||||||
screen_size.y and direction.y > 0)):
|
|
||||||
direction.y = -direction.y
|
direction.y = -direction.y
|
||||||
|
|
||||||
Second, the pads: if one of the pads was touched, we need to invert the
|
Second, the pads: if one of the pads was touched, we need to invert the
|
||||||
@@ -212,8 +209,7 @@ speed a little.
|
|||||||
::
|
::
|
||||||
|
|
||||||
# Flip, change direction and increase speed when touching pads
|
# Flip, change direction and increase speed when touching pads
|
||||||
if ((left_rect.has_point(ball_pos) and direction.x < 0) or
|
if ((left_rect.has_point(ball_pos) and direction.x < 0) or (right_rect.has_point(ball_pos) and direction.x > 0)):
|
||||||
(right_rect.has_point(ball_pos) and direction.x > 0)):
|
|
||||||
direction.x = -direction.x
|
direction.x = -direction.x
|
||||||
direction.y = randf()*2.0 - 1
|
direction.y = randf()*2.0 - 1
|
||||||
direction = direction.normalized()
|
direction = direction.normalized()
|
||||||
@@ -243,24 +239,22 @@ is done using the Input class:
|
|||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
#move left pad
|
# Move left pad
|
||||||
var left_pos = get_node("left").get_pos()
|
var left_pos = get_node("left").get_pos()
|
||||||
|
|
||||||
if (left_pos.y > 0 and Input.is_action_pressed("left_move_up")):
|
if (left_pos.y > 0 and Input.is_action_pressed("left_move_up")):
|
||||||
left_pos.y += -PAD_SPEED * delta
|
left_pos.y += -PAD_SPEED * delta
|
||||||
if (left_pos.y < screen_size.y and
|
if (left_pos.y < screen_size.y and Input.is_action_pressed("left_move_down")):
|
||||||
Input.is_action_pressed("left_move_down")):
|
|
||||||
left_pos.y += PAD_SPEED * delta
|
left_pos.y += PAD_SPEED * delta
|
||||||
|
|
||||||
get_node("left").set_pos(left_pos)
|
get_node("left").set_pos(left_pos)
|
||||||
|
|
||||||
#move right pad
|
# Move right pad
|
||||||
var right_pos = get_node("right").get_pos()
|
var right_pos = get_node("right").get_pos()
|
||||||
|
|
||||||
if (right_pos.y > 0 and Input.is_action_pressed("right_move_up")):
|
if (right_pos.y > 0 and Input.is_action_pressed("right_move_up")):
|
||||||
right_pos.y += -PAD_SPEED * delta
|
right_pos.y += -PAD_SPEED * delta
|
||||||
if (right_pos.y < screen_size.y and
|
if (right_pos.y < screen_size.y and Input.is_action_pressed("right_move_down")):
|
||||||
Input.is_action_pressed("right_move_down")):
|
|
||||||
right_pos.y += PAD_SPEED * delta
|
right_pos.y += PAD_SPEED * delta
|
||||||
|
|
||||||
get_node("right").set_pos(right_pos)
|
get_node("right").set_pos(right_pos)
|
||||||
|
|||||||
Reference in New Issue
Block a user