试图让球从数组组件中反弹回来?(想不出更好的词)C#.
本文关键字:更好 回来 数组 组件 | 更新日期: 2023-09-27 18:33:18
我有一个球根据需要从桨和墙壁上弹起,然后我通过抽奖添加了一块奇异的砖块。矩形工具,让球从中反弹,然后改变其颜色,但无法使其不可见以阻止任何进一步的碰撞。我正在为我的砖块使用数组,因为我可以有很多,并且可以在被击中后将它们变成真或假
我的问题是我试图让球与所说的阵列砖碰撞,但即使通过尽可能多的谷歌搜索,我一生也无法弄清楚。 这是我的代码片段,我认为"应该"适用于碰撞
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);
}
}
}
这
对我来说看起来不对。
if ((y == brickLocation[i, 0])
&& (x >= brickLocation[0, i])
&& (x <= (brickLocation[0, i] + 60)))
在我看来,您应该始终[i, ...
砖阵列位置,因此要么[i, 0]
用于针对水平组件的测试,要么[i, 1]
用于垂直组件。