我正在进行一场乒乓球比赛,我的“球”和“paddle2”物体之间发生了撞击。看来我的桨位不会随着我的桨状物移动,所以不是球从我的桨上弹起而是从桨叶开始的一个看不见的圆圈反弹。而不是从桨开始位置反弹,它从0到0,从左上角落下。 我的碰撞密码在//////////////////////////////////中如果有帮助lol
非常感谢任何帮助!
PS。我知道我的代码对所有人来说都是垃圾,但这是我的第一个项目,我仍在学习基础知识。
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D backgroundTexture;
Rectangle background = new Rectangle(0, 0, 850, 510);
Texture2D paddle1Texture;
Rectangle paddle1 = new Rectangle(115, 230, 60, 60);
Texture2D paddle2Texture;
Rectangle paddle2 = new Rectangle(675, 230, 60, 60);
Vector2 paddle2Pos;
Texture2D ballTexture;
Rectangle ball = new Rectangle(410, 245, 30, 30);
Vector2 ballPos;
Vector2 ballDir;
float ballSpeed = 6;
//p1score
public Game1()
: base()
{
graphics = new GraphicsDeviceManager(this);
graphics.IsFullScreen = false;
graphics.PreferredBackBufferHeight = 510;
graphics.PreferredBackBufferWidth = 850;
Content.RootDirectory = "Content";
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
// TODO: use this.Content to load your game content here
ballTexture = Content.Load<Texture2D>("ball");
ballPos = new Vector2(410, 245);
ballDir = new Vector2(1, 1);
ballDir.Normalize();
paddle1Texture = Content.Load<Texture2D>("paddle1");
paddle2Texture = Content.Load<Texture2D>("paddle2");
paddle2Pos = new Vector2(675, 230);
backgroundTexture = Content.Load<Texture2D>("background");
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
// TODO: Add your update logic here
//right paddle
if (Keyboard.GetState().IsKeyDown(Keys.Up))
paddle2.Y -= 7;
if (Keyboard.GetState().IsKeyDown(Keys.Down))
paddle2.Y += 7;
if (Keyboard.GetState().IsKeyDown(Keys.Left))
paddle2.X -= 7;
if (Keyboard.GetState().IsKeyDown(Keys.Right))
paddle2.X += 7;
if (paddle2.Y + 60 > 505)
paddle2.Y = 505 - 60;
if (paddle2.Y < 5)
paddle2.Y = 5;
if (paddle2.X < 427)
paddle2.X = 427;
if (paddle2.X + 60 > 845)
paddle2.X = 845 - 60;
//left paddle
if (Keyboard.GetState().IsKeyDown(Keys.W))
paddle1.Y -= 7;
if (Keyboard.GetState().IsKeyDown(Keys.S))
paddle1.Y += 7;
if (Keyboard.GetState().IsKeyDown(Keys.A))
paddle1.X -= 7;
if (Keyboard.GetState().IsKeyDown(Keys.D))
paddle1.X += 7;
if (paddle1.Y + 60 > 505)
paddle1.Y = 505 - 60;
if (paddle1.Y < 5)
paddle1.Y = 5;
if (paddle1.X + 60 > 423)
paddle1.X = 423 - 60;
if (paddle1.X < 5)
paddle1.X = 5;
//ball movement and boundary
//if (paddle2.Intersects(ball))
// ballSpeed = 6;
//if (paddle1.Intersects(ball))
// ballSpeed = 6;
{
}
{
ballPos += ballDir * ballSpeed;
ball = new Rectangle((int)ballPos.X, (int)ballPos.Y, 30, 30);
}
ballPos += ballDir * ballSpeed;
if (ballPos.Y + 35 > graphics.PreferredBackBufferHeight)
{
ballPos.Y = graphics.PreferredBackBufferHeight - 35;
ballDir.Y = -ballDir.Y;
}
if (ballPos.Y < 5)
{
ballPos.Y = 5;
ballDir.Y = -ballDir.Y;
}
if (ballPos.X + 35 > graphics.PreferredBackBufferWidth)
{
ballPos.X = graphics.PreferredBackBufferWidth - 35;
ballDir.X = -ballDir.X;
}
if (ballPos.X < 5)
{
ballPos.X = 5;
ballDir.X = -ballDir.X;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
float distance = (ballPos.X - paddle2Pos.X) * (ballPos.X - paddle2Pos.X) + (ballPos.Y - paddle2Pos.Y) * (ballPos.Y - paddle2Pos.Y);
float radius = (15 + 30) * (15 + 30);
if (distance <= radius)
{
Vector2 dir = (ballPos = -paddle2Pos);
dir.Normalize();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
spriteBatch.Begin();
spriteBatch.Draw(backgroundTexture, background, Color.White);
spriteBatch.Draw(paddle1Texture, paddle1, Color.White);
spriteBatch.Draw(paddle2Texture, paddle2, Color.White);
spriteBatch.Draw(ballTexture, ball, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
答案 0 :(得分:1)
paddle2Pos
在LoadContent()
中初始化为
paddle2Pos = new Vector2(675, 230);
但从未更改过您上传的代码。但是,它用于以下计算:
float distance = (ballPos.X - paddle2Pos.X) * (ballPos.X - paddle2Pos.X) + (ballPos.Y - paddle2Pos.Y) * (ballPos.Y - paddle2Pos.Y);
float radius = (15 + 30) * (15 + 30);
if (distance <= radius)
{
Vector2 dir = (ballPos = -paddle2Pos);
dir.Normalize();
}
这是问题吗?
我看到变量paddle1
,paddle2
和paddle2Pos
(但没有paddle1Pos
)。 paddle2pos
只是您要删除并替换为paddle2
的字段,但错过了几个地方吗?用paddle2pos
替换paddle2
会改善情况吗?
此外,还不清楚这项检查是否有意义。您正在检查ballPos
是否比给定半径更接近paddle2Pos
&#34;因为乌鸦飞过&#34; - 但我相信Pong中的桨和球实际上是长方形的。既然你有矩形,你为什么不使用Rectangle.Intersect
?