每场比赛之后的戈特乒乓球重播
我目前正在使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜球是
arigidbody2d
。它们打算由物理引擎移动,并直接移动它们(例如设置
位置
)可能会导致问题。这是我经常建议初学者使用Kinematicbody
(2D
)的原因之一,因此他们不是在“战斗”物理引擎。无论如何,这是一个警察的答案。您想传送
arigidbody2d
,让我们看看如何做。以下内容是 - 我认为 - 传递
arigidbody2d
的难以捉摸的适当方法:您基本上是在说:“嘿,物理引擎!是的,您。我希望您将身体放在这个位置。 “物理引擎进行:“好”。
physics2dserver
API旨在用于高级使用,而使用节点则无法使用。因此,不是初学者友好的。取而代之的是,传送a
arigidbody2d
的最常见解决方案涉及向其添加脚本。看起来像这样:然后您像这样使用它:
在这里,您已经告诉物理引擎每次都想更改某些东西(如果定义它,它将调用
_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 useKinematicBody
(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
: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:And then you use it like this:
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.