extends Node2D # attributs du script courant # variables de travail var screen_size var pad_size var direction = Vector2(1.0, 0.0) # Vitesse initiale de la balle(en pixels/seconde) const INITIAL_BALL_SPEED = 80 # Vitesse de la balle (en pixels/seconde) var ball_speed = INITIAL_BALL_SPEED # Vitesse des raquettes const PAD_SPEED = 150 func _ready(): # initialisation des attributs self.pad_size = get_node("left").get_texture().get_size() self.screen_size = get_viewport_rect().size # Activer les traitements pour chaque frame sur temps libre set_process(true) func _process(delta): # position courante de la balle var ball_position = get_node("ball").get_pos() # Construit une aire occupée par la raquette gauche # pour la detection de collision var left_rect = Rect2(get_node("left").get_pos() - self.pad_size * 0.5, self.pad_size) var right_rect = Rect2(get_node("right").get_pos() - self.pad_size * 0.5, self.pad_size) # Calcul de la nouvelle position ball_position += self.direction * self.ball_speed * delta # La balle change de direction lorsqu'elle touche les bords haut et bas if ((ball_position.y < 0 and self.direction.y < 0) or (ball_position.y > self.screen_size.y and self.direction.y > 0)): self.direction.y = -self.direction.y # Change la direction et augmente la vitesse lorsque la balle touche les raquettes if ((left_rect.has_point(ball_position) and self.direction.x < 0) or (right_rect.has_point(ball_position) and self.direction.x > 0)): self.direction.x = -self.direction.x self.direction.y = randf()*2.0 - 1 self.direction = self.direction.normalized() self.ball_speed *= 1.1 # Si la balle sort, elle est replacée au centre avec vitesse et position initiale if (ball_position.x < 0 or ball_position.x > self.screen_size.x): ball_position = self.screen_size*0.5 self.ball_speed = self.INITIAL_BALL_SPEED self.direction = Vector2(1.0, 0.0) # Mise a jour de la position de la balle get_node("ball").set_pos(ball_position) # Bouger la raquette gauche var left_pos = get_node("left").get_pos() if (left_pos.y > 0 and Input.is_action_pressed("left_move_up")): left_pos.y += -self.PAD_SPEED * delta if (left_pos.y < screen_size.y and Input.is_action_pressed("left_move_down")): left_pos.y += self.PAD_SPEED * delta get_node("left").set_pos(left_pos) # Bouger la raquette droite var right_pos = get_node("right").get_pos() if (right_pos.y > 0 and Input.is_action_pressed("right_move_up")): right_pos.y += -self.PAD_SPEED * delta if (right_pos.y < screen_size.y and Input.is_action_pressed("right_move_down")): right_pos.y += self.PAD_SPEED * delta get_node("right").set_pos(right_pos)