我有一个球根据需要从桨和墙壁上弹起,然后我通过 draw.Rectangle 工具添加了一个单一的砖块,让球从上面弹起,然后改变它的颜色,但不能让它不可见以阻止任何进一步的碰撞. 我正在为我的积木使用一个数组,因为我可以有很多并且可以在被击中后将它们变为真或假
我的问题是我试图让球与所述阵列砖发生碰撞,但即使尽可能多地搜索,我也无法终生弄清楚。这是我认为“应该”为碰撞工作的代码片段
for (int i = 0; 1 < brickLive.Length; i++)
if ((y == brickLocation[i, 0]) && (x >= brickLocation[0, i]) && (x <= (brickLocation[0, i] + 60)))
yChange = -yChange;
据我了解,这段代码表示我检查球坐标是否在砖块位置的参数中的值。如果是则改变方向。使用当前代码它运行良好,直到我开始游戏(我单击插入按钮并启用弹跳按钮工作)
这是我的完整代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Breakout
{
public partial class Form1 : Form
{
private int x, y, x2, y2;
private int xChange, yChange;
int bat, batX, batXX, mouseX;
private Graphics paper;
private Pen pen, pen2, pen3;
private Pen brkpen;
private Random ranNum;
int brkLength = 60;
int brkHeight = 20;
int[,] brickLocation = { { 0, 100 }, { 61, 100 }, { 122, 100 } };
bool[] brickLive = { true, true, true };
public Form1()
{
InitializeComponent();
paper = picDisplayBat.CreateGraphics();
pen = new Pen(Color.Red);
pen.Width = 10;
ranNum = new Random();
paper = picDisplayBat.CreateGraphics();
pen = new Pen(Color.Blue);
pen.Width = 3;
paper = picDisplayBat.CreateGraphics();
pen2 = new Pen(Color.Red);
pen.Width = 3;
picDisplayBat.MouseMove += new
System.Windows.Forms.MouseEventHandler(picDraw_MouseMove);
paper = picDisplayBat.CreateGraphics();
brkpen = new Pen(Color.Black);
brkpen.Width = 3;
//paper = picDisplayBat.CreateGraphics();
//pen3 = new Pen(Color.Green);
//pen3.Width = 5;
}
private void picDraw_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) //DRAWING THE BAT TO MOVE WITH MOUSE
{
//paper.Clear(Color.White);
mouseX = e.X;
}
private void btnInsert_Click_1(object sender, EventArgs e)
{
{
btnBounce.Visible = true;
}
}
private void btnBounce_Click_1(object sender, EventArgs e)
{
{
timer1.Interval = 25;
timer1.Enabled = true;
x = ranNum.Next(1, picDisplayBat.Height);
y = ranNum.Next(1, picDisplayBat.Width);
xChange = ranNum.Next(1, 10); yChange = ranNum.Next(1, 10);
for (int i = 0; i < brickLive.Length; i++)
{
paper.DrawRectangle(brkpen, brickLocation[i, 0], brickLocation[i, 1], brkLength, brkHeight);
}
}
}
private void timer1_Tick(object sender, EventArgs e)
{
{
x = x + xChange;
y = y + yChange;
if (x >= picDisplayBat.Width)
xChange = -xChange;
if (y >= picDisplayBat.Height)
yChange = -yChange;
if (x <= 0)
xChange = -xChange;
if (y <= 0)
yChange = -yChange;
if ((y > picDisplayBat.Height - 20) && (x >= batX + 10) && (x <= batX + 50))
yChange = -yChange;
if ((y < picDisplayBat.Height - 295) && (x >= batX + 10) && (x <= batX + 50))
yChange = -yChange;
for (int i = 0; 1 < brickLive.Length; i++)
if ((y == brickLocation[i, 0]) && (x >= brickLocation[0, i]) && (x <= (brickLocation[0, i] + 60)))
yChange = -yChange;
paper.Clear(Color.White);
paper.DrawRectangle(pen, mouseX + 10, picDisplayBat.Height - 20, 50, 10); //bat 1
paper.DrawEllipse(pen, x, y, 10, 10); //ball
paper.DrawRectangle(pen2, mouseX + 10, picDisplayBat.Height - 295, 50, 10); //bat2
//paper.DrawRectangle(pen3, x2, y2, 60, 10);
bat = mouseX;
batX = mouseX;
batXX = mouseX;
for (int i = 0; i < brickLive.Length; i++)
{
paper.DrawRectangle(brkpen, brickLocation[i, 0], brickLocation[i, 1], brkLength, brkHeight);
}
}
}
private void btnExit_Click(object sender, EventArgs e)
{
Environment.Exit(0);
}
private void btnStop_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
paper.Clear(Color.White);
}
}
}