每场比赛之后的戈特乒乓球重播

发布于 2025-01-21 23:00:45 字数 1304 浏览 0 评论 0原文

我目前正在使用Godot从事我的乒乓球项目。

一切都很好,但是只有一个问题。

一侧得分后,球被卡在中间而不起作用。

我应该添加什么?这是我的代码:

# Declare member variables here.
var ball
var player
var computer
var player_score = 0
var computer_score = 0
var winning_score = 5
var initial_velocity_x = 250
var initial_velocity_y = 10

# Called when the node enters the scene tree for the first time.
func _ready():
    player = get_node('player')
    computer = get_node('computer')
    ball = get_node('ball')


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
    player.position.y = get_viewport().get_mouse_position().y
    computer.position.y = ball.position.y
    
    if ball.position.x > 1024:
        reset_position()
        ball.linear_velocity.x = initial_velocity_x
        ball.linear_velocity.y = initial_velocity_y
        computer_score += 1
        
    if ball.position.x < 0:
        reset_position()
        ball.linear_velocity.x = -initial_velocity_x
        ball.linear_velocity.y = initial_velocity_y
        player_score += 1
    
    if player_score >= winning_score or computer_score >= winning_score:
        player_score = 0
        computer_score = 0
    
func reset_position():
    ball.position.x = 512
    ball.position.y = 300

I'm currently working on my Pong game project using Godot.

Everything is working well but there's only one problem.

After one side gets scored, the ball is stuck in the middle and not working.

What should I add? Here is my code:

# Declare member variables here.
var ball
var player
var computer
var player_score = 0
var computer_score = 0
var winning_score = 5
var initial_velocity_x = 250
var initial_velocity_y = 10

# Called when the node enters the scene tree for the first time.
func _ready():
    player = get_node('player')
    computer = get_node('computer')
    ball = get_node('ball')


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
    player.position.y = get_viewport().get_mouse_position().y
    computer.position.y = ball.position.y
    
    if ball.position.x > 1024:
        reset_position()
        ball.linear_velocity.x = initial_velocity_x
        ball.linear_velocity.y = initial_velocity_y
        computer_score += 1
        
    if ball.position.x < 0:
        reset_position()
        ball.linear_velocity.x = -initial_velocity_x
        ball.linear_velocity.y = initial_velocity_y
        player_score += 1
    
    if player_score >= winning_score or computer_score >= winning_score:
        player_score = 0
        computer_score = 0
    
func reset_position():
    ball.position.x = 512
    ball.position.y = 300

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

机场等船 2025-01-28 23:00:45

我猜球是arigidbody2d

它们打算由物理引擎移动,并直接移动它们(例如设置位置)可能会导致问题。这是我经常建议初学者使用Kinematicbody2D)的原因之一,因此他们不是在“战斗”物理引擎。

无论如何,这是一个警察的答案。您想传送arigidbody2d,让我们看看如何做。


以下内容是 - 我认为 - 传递arigidbody2d的难以捉摸的适当方法:

func reset_position():
    Physics2DServer.body_set_state(
        ball.get_rid(),
        Physics2DServer.BODY_STATE_TRANSFORM,
        Transform2D.IDENTITY.translated(Vector2(512, 300))
    )

您基本上是在说:“嘿,物理引擎!是的,您。我希望您将身体放在这个位置。 “物理引擎进行:“好”。

physics2dserver API旨在用于高级使用,而使用节点则无法使用。因此,不是初学者友好的。


取而代之的是,传送a arigidbody2d的最常见解决方案涉及向其添加脚本。看起来像这样:

extends RigidBody2D


var _teleport_target:Vector2
var _teleporting:bool


func _integrate_forces(state: Physics2DDirectBodyState) -> void:
    if _teleporting:
        state.transform.origin = _teleport_target
        _teleporting = false


func teleport(position:Vector2) -> void:
    _teleporting = true
    _teleport_target = position

然后您像这样使用它:

func reset_position():
    ball.teleport(Vector2(512, 300))

在这里,您已经告诉物理引擎每次都想更改某些东西(如果定义它,它将调用_integrate_forces)。有时您会通过告诉它移动身体来回答。

I'm guessing the ball is a RigidBody2D.

They are intended to be moved by the physics engine, and moving them directly (e.g. setting position) can cause problems. This one of the reason I often recommend beginners to use KinematicBody(2D), so they are not "fighting" the physics engine.

Anyway, that is a cop-out answer. You want to teleport a RigidBody2D, let us see how to do it.


The following is - in my opinion - the elusive proper way to teleport a RigidBody2D:

func reset_position():
    Physics2DServer.body_set_state(
        ball.get_rid(),
        Physics2DServer.BODY_STATE_TRANSFORM,
        Transform2D.IDENTITY.translated(Vector2(512, 300))
    )

You are basically saying: "Hey, Physics Engine! Yes, you. I want you to place the body at this position." And the Physics Engines goes: "OK".

The Physics2DServer API is intended for advanced use, when using nodes won't do. And as such, not beginner friendly.


Instead, the most common solution to teleport a RigidBody2D involves adding an script to it. It looks like this:

extends RigidBody2D


var _teleport_target:Vector2
var _teleporting:bool


func _integrate_forces(state: Physics2DDirectBodyState) -> void:
    if _teleporting:
        state.transform.origin = _teleport_target
        _teleporting = false


func teleport(position:Vector2) -> void:
    _teleporting = true
    _teleport_target = position

And then you use it like this:

func reset_position():
    ball.teleport(Vector2(512, 300))

Here you have told the Physics Engine to ask you every time if you want to change something (it will call _integrate_forces if you define it). And occasionally you answer by telling it to move the body.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文