C# XNA-矩形错误
我正在做一个乒乓球游戏,我试着这样做,当球碰到球拍时,它会反弹,就这么简单。但是当我使用矩形时,我认为使用C# XNA-矩形错误,c#,xna,collision,pong,C#,Xna,Collision,Pong,我正在做一个乒乓球游戏,我试着这样做,当球碰到球拍时,它会反弹,就这么简单。但是当我使用矩形时,我认为使用if(ballRect.Intersects(gPaddle.gRect))是制造碰撞的最佳方法。 但是当我开始比赛时,一切都出了问题,球消失了,球拍纹理变细了,有一部分没有跟上球拍,是的。。代码如下: Greenbail.cs: public class GreenPaddle { public Texture2D gPtexture; public Vector2
if(ballRect.Intersects(gPaddle.gRect))
是制造碰撞的最佳方法。
但是当我开始比赛时,一切都出了问题,球消失了,球拍纹理变细了,有一部分没有跟上球拍,是的。。代码如下:
Greenbail.cs:
public class GreenPaddle
{
public Texture2D gPtexture;
public Vector2 position;
public Rectangle gpRect;
public Vector2 origin;
public int speed = 2;
public GreenPaddle()
{
}
public void LoadContent(ContentManager Content)
{
gPtexture = Content.Load<Texture2D>("greenpaddle");
position = new Vector2(20, 200);
gpRect = new Rectangle((int)position.X, (int)position.Y,
gPtexture.Width, gPtexture.Height);
}
public void Update(GameTime gameTime)
{
KeyboardState keyState = Keyboard.GetState();
//Movement
PaddleMovement();
//Border Collision
isCollidingWithBorders();
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(gPtexture, position, gpRect, Color.White);
}
public Boolean isCollidingWithBorders()
{
if (position.Y < 83 && gpRect.Y < 83)
{
position.Y = 83;
gpRect.Y = 83;
return true;
}
if (position.Y > 400 && gpRect.Y > 400)
{
position.Y = 400;
gpRect.Y = 400;
return true;
}
else { return false; }
}
public void PaddleMovement()
{
KeyboardState keyState = Keyboard.GetState();
if (keyState.IsKeyDown(Keys.W))
{
position.Y -= speed;
gpRect.Y -= speed;
}
if (keyState.IsKeyDown(Keys.S))
{
position.Y += speed;
gpRect.Y += speed;
}
}
}
公共级绿桨
{
公共纹理2d-gp纹理;
公共向量2位置;
公共矩形gpRect;
公共向量2起源;
公共int速度=2;
公共绿桨()
{
}
公共void加载内容(ContentManager内容)
{
gPtexture=Content.Load(“绿桨”);
位置=新矢量2(20,200);
gpRect=新矩形((int)位置.X,(int)位置.Y,
gPtexture.宽度,gPtexture.高度);
}
公开作废更新(游戏时间游戏时间)
{
KeyboardState keyState=Keyboard.GetState();
//运动
划桨运动();
//边境冲突
isCollidingWithBorders();
}
公共作废抽签(SpriteBatch SpriteBatch)
{
spriteBatch.Draw(gPtexture、position、gpRect、Color.White);
}
公共布尔值isCollidingWithBorders()
{
if(位置Y<83&&gpRect.Y<83)
{
位置Y=83;
gpRect.Y=83;
返回true;
}
如果(位置Y>400&&gpRect.Y>400)
{
位置Y=400;
gpRect.Y=400;
返回true;
}
else{return false;}
}
公共安全运动
{
KeyboardState keyState=Keyboard.GetState();
if(keyState.IsKeyDown(Keys.W))
{
位置Y-=速度;
gpRect.Y-=速度;
}
if(keyState.IsKeyDown(Keys.S))
{
位置Y+=速度;
gpRect.Y+=速度;
}
}
}
Ball.cs:
public class Ball
{
GreenPaddle gPaddle = new GreenPaddle();
Texture2D ballTexture;
Vector2 ballPosition;
Rectangle ballRect;
int speed = 2;
bool movingUp, movingLeft;
public Ball()
{
movingLeft = true;
movingUp = true;
}
public void LoadContent(ContentManager Content)
{
ballTexture = Content.Load<Texture2D>("ball");
ballPosition = new Vector2(380, 225);
ballRect = new Rectangle((int)ballPosition.X, (int)ballPosition.Y,
ballTexture.Width, ballTexture.Height);
}
public void Update(GameTime gameTime)
{
BallMovement();
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(ballTexture, ballPosition, ballRect, Color.White);
}
public void BallMovement()
{
if (movingUp) { ballPosition.Y -= speed; ballRect.Y -= speed; }
if (!movingUp) { ballPosition.Y += speed; ballRect.Y += speed; }
if (movingLeft) { ballPosition.X -= speed; ballRect.X -= speed; }
if (!movingLeft) { ballPosition.X += speed; ballRect.X += speed; }
if (ballRect.Intersects(gPaddle.gpRect))
movingLeft = false;
if (ballPosition.Y < 85)
{
movingUp = false;
}
}
}
公开课舞会
{
greenblade gPaddle=新的greenblade();
纹理2d球状纹理;
矢量2球位;
长方形;
内速度=2;
布尔移动,向左移动;
公共舞会
{
左移=真;
movingUp=true;
}
公共void加载内容(ContentManager内容)
{
ballTexture=Content.Load(“球”);
球位置=新矢量2(380225);
ballRect=新矩形((int)ballPosition.X,(int)ballPosition.Y,
球纹理。宽度,球纹理。高度);
}
公开作废更新(游戏时间游戏时间)
{
球运动();
}
公共作废抽签(SpriteBatch SpriteBatch)
{
spriteBatch.Draw(球纹理、球位置、球直、颜色、白色);
}
公众运动
{
if(movingUp){ballPosition.Y-=速度;ballcrect.Y-=速度;}
如果(!movingUp){ballPosition.Y+=速度;ballcrect.Y+=速度;}
如果(向左移动){ballPosition.X-=速度;ballcrect.X-=速度;}
如果(!movingLeft){ballPosition.X+=速度;ballRect.X+=速度;}
if(ballRect.Intersects(gPaddle.gpRect))
左移=假;
if(球位Y<85)
{
movingUp=false;
}
}
}
游戏1.cs:
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D BackGround;
GreenPaddle gPaddle = new GreenPaddle();
Ball ball = new Ball();
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.PreferredBackBufferHeight = 500;
}
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);
BackGround = Content.Load<Texture2D>("pongBG");
gPaddle.LoadContent(Content);
ball.LoadContent(Content);
}
/// <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)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
gPaddle.Update(gameTime);
ball.Update(gameTime);
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);
spriteBatch.Begin();
spriteBatch.Draw(BackGround, new Vector2(0f, 0f), Color.White);
gPaddle.Draw(spriteBatch);
ball.Draw(spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}
}
公共类游戏1:Microsoft.Xna.Framework.Game
{
图形管理器图形;
SpriteBatch SpriteBatch;
纹理2D背景;
greenblade gPaddle=新的greenblade();
球=新球();
公共游戏1()
{
graphics=新的GraphicsDeviceManager(此);
Content.RootDirectory=“Content”;
graphics.PreferredBackBufferHeight=500;
}
受保护的覆盖无效初始化()
{
//TODO:在此处添加初始化逻辑
base.Initialize();
}
///
///LoadContent将在每个游戏中调用一次,并且是加载的地方
///你所有的内容。
///
受保护的覆盖void LoadContent()
{
//创建一个新的SpriteBatch,可用于绘制纹理。
spriteBatch=新spriteBatch(图形设备);
背景=Content.Load(“pongBG”);
gPaddle.LoadContent(Content);
球。装载内容(内容);
}
///
///UnloadContent将在每个游戏中调用一次,并且是卸载的地方
///所有内容。
///
受保护的覆盖无效UnloadContent()
{
//TODO:在此卸载任何非ContentManager内容
}
///
///允许游戏运行逻辑,例如更新世界,
///检查碰撞、收集输入和播放音频。
///
///提供计时值的快照。
受保护覆盖无效更新(游戏时间游戏时间)
{
//允许游戏退出
if(GamePad.GetState(PlayerIndex.One).Buttons.Back==ButtonState.Pressed)
这是Exit();
gPaddle.Update(游戏时间);
更新(游戏时间);
更新(游戏时间);
}
///
///这就是所谓的比赛应该平局的时候。
///
///提供计时值的快照。
受保护覆盖无效绘制(游戏时间游戏时间)
{
图形设备。清晰(颜色:矢车菊蓝);
spriteBatch.Begin();
spriteBatch.Draw(背景,新矢量2(0f,0f),彩色,白色);
gPaddle.Draw(spriteBatch);
球。抽签(spriteBatch);
spriteBatch.End();
基础。抽签(游戏时间);
}
}
我该怎么办?我错过了什么?
提前感谢:)碰撞测试在
球
类中。在其中,您正在测试球是否与名为gPaddle
的类的私有成员碰撞,而不是您的游戏使用的以及您更新和绘制的类
替换
GreenPaddle gPaddle = new GreenPaddle();
与
另外,不要使用2个布尔值和一个标量来表示速度。使用矢量2
。你的ballmotation
方法在我们看来是错误的
GreenPaddle gPaddle;
...
public Ball(GreenPaddle paddle)
{
this.gPaddle = paddle;
}
if (ballRect.Intersects(gPaddle.gpRect))
{
movingLeft = !movingLeft
movingTop = !movingTop;
}
if (ballRect.Intersects(gPaddle.gpRect))
{
velocity = -velocity;
}