我有一个应用程序,其中是球(椭圆)和 4 个矩形(每个用于窗口边缘)。我尝试编写一个应用程序,让球在边缘移动和弹跳。我不知道如何制作一种在碰撞为真时球改变方向的方法。
XAML:
<Window x:Class="naszybkodlatestu.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="320" Width="640">
<Canvas>
<Rectangle Name="boardTop"
Height="2"
Fill="Black"
Width="609" Canvas.Left="7" Canvas.Top="10"/>
<Rectangle Name="boardLeft"
Height="270"
Fill="Black"
Width="2" Canvas.Left="614" Canvas.Top="10"/>
<Rectangle Name="boardRight"
Height="269"
Fill="Black"
Width="2" Canvas.Left="7" Canvas.Top="12"/>
<Rectangle Name="boardBot"
Height="2"
Fill="Black"
Width="609" Canvas.Left="7" Canvas.Top="278" />
<Ellipse Name="ball"
Width="15"
Height="15"
Fill="Red"
Canvas.Left="303"
Canvas.Top="107"/>
</Canvas>
</Window>
C#:
DispatcherTimer dTimer;
private void InitTimer()
{
dTimer = new DispatcherTimer();
dTimer.Tick += new EventHandler(TickdTimer);
dTimer.Interval = new TimeSpan(0, 0, 0, 0, 1);
dTimer.Start();
}
public MainWindow()
{
InitializeComponent();
InitTimer();
InitMove();
}
private bool isMovingUp = false;
private bool isMovingDown = false;
private bool isMovingLeft = true;
private bool isMovingRight = false;
private double ballDirectionX = 0;
private double ballDirectionY = 0;
private double ballSpeed = 1;
private bool CollisionDetection()
{
if (CheckCollision(ball, boardBot) || CheckCollision(ball, boardRight) ||
CheckCollision(ball, boardTop) || CheckCollision(ball, boardLeft))
{
return true;
}
else return false;
}
public static bool CheckCollision(FrameworkElement a, FrameworkElement b)
{
Rect rect1 = new Rect((double)a.GetValue(Canvas.LeftProperty), (double)a.GetValue(Canvas.TopProperty), a.Width, a.Height);
Rect rect2 = new Rect((double)b.GetValue(Canvas.LeftProperty), (double)b.GetValue(Canvas.TopProperty), b.Width, b.Height);
if (rect1.IntersectsWith(rect2))
{
return true;
}
else return false;
}
private void InitMove()
{
if (isMovingDown)
{
ballDirectionY += ballSpeed;
}
if (isMovingUp)
{
ballDirectionY -= ballSpeed;
}
if (isMovingLeft)
{
ballDirectionX -= ballSpeed;
}
if (isMovingRight)
{
ballDirectionX += ballSpeed;
}
ball.SetValue(Canvas.TopProperty, ballDirectionY);
ball.SetValue(Canvas.LeftProperty, ballDirectionX);
}
所以我在 InitMove() 中尝试了类似的方法。这只是一个测试代码,向您展示什么是问题。
private void InitMove()
{
.......
if (CollisionDetection())
{
if (isMovingLeft)
{
isMovingLeft = false;
isMovingRight = true;
}
}
所以球从左到右改变了方向。现在好了。
当我想输入下一个“if”时,问题就开始了。
private void InitMove()
{
.......
if (CollisionDetection())
{
if (isMovingLeft)
{
isMovingLeft = false;
isMovingRight = true;
}
if (isMovingRight)
{
isMovingRight = false;
isMovingLeft = true;
}
}
现在球通过矩形的边缘并且没有反弹。所以这是我的问题。
如何使它正确?
//编辑
当我添加其他
if (isMovingLeft)
{
isMovingLeft = false;
isMovingRight = true;
}
else if (isMovingRight)
{
isMovingRight = false;
isMovingLeft = true;
}
球刚刚停在窗边。
//编辑2
好的,所以我想我知道该怎么做,谢谢
if (isMovingLeft)
{
isMovingLeft = false;
isMovingRight = true;
ballDirectionX += 2;
}
else if (isMovingRight)
{
isMovingRight = false;
isMovingLeft = true;
ballDirectionX -= 2;
}
所以我添加了一些动作来将这个球推出墙壁碰撞。我暂时没有看到其他方法。