我需要以某种方式使用我的DrawBall方法与DrawBat相交,这样当球击中球棒时,它会使用相交方法进行偏转。我怎样才能将我的方法声明为允许这样做的类型?
public partial class Form1 : Form
{
private Graphics paper;
private SolidBrush brush, brushBlue, brushGreen, brushRed,
brushBlk, brushYellow;
private int x, y, xDirection, yDirection, xBat;
private int playCount, brushSize, ball;
private Random randomNumber;
private bool btnDisplayBricksClicked;
public Form1()
{
InitializeComponent();
paper = picBox.CreateGraphics();
brush = new SolidBrush(Color.Red);
brushBlue = new SolidBrush(Color.Blue);
brushGreen = new SolidBrush(Color.Green);
brushRed = new SolidBrush(Color.Red);
brushBlk = new SolidBrush(Color.Black);
brushYellow = new SolidBrush(Color.Yellow);
randomNumber = new Random();
btnDisplayBricksClicked = false;
ball = DrawBall(); //need help here error: cannot implicitly convert void to int
}
private void MoveBall()
{
timer1.Interval = randomNumber.Next(50, 100);
timer1.Enabled = true;
x = x + xDirection;
y = y + yDirection;
if (x >= picBox.Width)
xDirection = -xDirection;
if (y >= picBox.Height)
yDirection = -yDirection;
if (x <= 0)
xDirection = -xDirection;
if (y <= 0)
yDirection = -yDirection;
}
private void DrawBall()
{
brushSize = 15;
paper.FillEllipse(brush, x, y, brushSize, brushSize);
}
private void DrawBricks(Graphics paper)
{
int xPos = 25;
int yPos = 25;
int width = 100;
int height = 20;
paper.FillRectangle(brushBlue, xPos, yPos, width, height);
paper.FillRectangle(brushGreen, xPos + 110, yPos, width, height);
paper.FillRectangle(brushRed, xPos + 220, yPos, width, height);
paper.FillRectangle(brushBlk, xPos + 330, yPos, width, height);
paper.FillRectangle(brushYellow, xPos + 440, yPos, width, height);
}
private void DrawBat(Graphics paper)
{
paper.FillRectangle(brushBlue, xBat - 25, picBox.Height - 20, 50, 10);
}
private void CheckCollision()
{
}
private void picBox_MouseMove(object sender, MouseEventArgs e)
{
xBat = e.X;
}
private void btnLaunchRandom_Click(object sender, EventArgs e)
{
if (playCount == 0)
{
paper.Clear(Color.LightSteelBlue);
timer1.Enabled = false;
MessageBox.Show("Please insert £1 to play, You have no credit");
return;
}
playCount--;
x = randomNumber.Next(20, 300);
y = randomNumber.Next(0, picBox.Height / 2);
xDirection = randomNumber.Next(5, 20);
yDirection = randomNumber.Next(5, 20);
MoveBall();
}
private void btnInsertCoin_Click(object sender, EventArgs e)
{
paper.Clear(Color.LightSteelBlue);
playCount = 5;
}
private void timer1_Tick(object sender, EventArgs e)
{
paper.Clear(Color.LightSteelBlue);
DrawBall();
MoveBall();
DrawBat(paper);
if (btnDisplayBricksClicked)
{
DrawBricks(paper);
}
}
private void btnLeft_Click(object sender, EventArgs e)
{
if (playCount == 0)
{
paper.Clear(Color.LightSteelBlue);
timer1.Enabled = false;
MessageBox.Show("Please insert £1 to play, You have no credit");
return;
}
playCount--;
x = picBox.Width / 2;
y = picBox.Height / 2;
xDirection = randomNumber.Next(5, 20) - 21;
yDirection = 1;
MoveBall();
}
private void btnRight_Click(object sender, EventArgs e)
{
if (playCount == 0)
{
paper.Clear(Color.LightSteelBlue);
timer1.Enabled = false;
MessageBox.Show("Please insert £1 to play, You have no credit");
return;
}
playCount--;
x = picBox.Width / 2;
y = picBox.Height / 2;
xDirection = randomNumber.Next(5, 10) + 15;
yDirection = 1;
MoveBall();
}
private void btnUp_Click(object sender, EventArgs e)
{
if (playCount == 0)
{
paper.Clear(Color.LightSteelBlue);
timer1.Enabled = false;
MessageBox.Show("Please insert £1 to play, You have no credit");
return;
}
playCount--;
x = picBox.Width / 2;
y = picBox.Height / 2;
xDirection = 1;
yDirection = randomNumber.Next(5, 20) - 21;
MoveBall();
}
private void btnDown_Click(object sender, EventArgs e)
{
if (playCount == 0)
{
paper.Clear(Color.LightSteelBlue);
timer1.Enabled = false;
MessageBox.Show("Please insert £1 to play, You have no credit");
return;
}
playCount--;
x = picBox.Width / 2;
y = picBox.Height / 2;
xDirection = 1;
yDirection = randomNumber.Next(1, 5) + 6;
MoveBall();
}
private void btnDisplayBricks_Click(object sender, EventArgs e)
{
btnDisplayBricksClicked = true;
}
}
}
答案 0 :(得分:0)
好的,所以我将不得不继续努力,直到明天或今晚才会发布完整的答案,但要让你开始......想想“对象”而不是方法
例如,你的Ball是一个对象(类),你的Bat是一个对象(类)。
private Ball ball = new Ball();
private Bat bat = new Bat();
public class Ball : Form1
{
public int Xpos { get; set; }
public int Ypos { get; set; }
public void Draw()
{
brushSize = 15;
paper.FillEllipse(brush, Xpos, Ypos, brushSize, brushSize);
}
public void Move(int xDir, int yDir)
{
timer1.Interval = randomNumber.Next(50, 100);
timer1.Enabled = true;
Xpos = Xpos + xDir;
Ypos = Ypos + yDir;
if (x >= picBox.Width)
xDir = -xDir;
if (y >= picBox.Height)
yDir = -yDir;
if (x <= 0)
xDir = -xDir;
if (y <= 0)
yDir = -yDir;
}
}
public class Bat : Form1
{
public int Xpos { get; set; }
public int Ypos { get; set; }
public void Draw()
{
paper.FillRectangle(brushBlue, xBat - 25, picBox.Height - 20, 50, 10);
}
}
创建对象(类)后,即可开始创建这些对象可以执行的操作(方法)。对于这个快速示例,我主要使用您已有的方法并更改它们的调用方式。例如,现在如果你想画球并让你的球移动,你会这样做:
ball.Draw();
ball.Move(x, y);
这样你就可以调用对象本身的动作了。在任何时候,你都可以像这样检查球对象的位置:
int ballXpos = ball.Xpos;
int ballYpos = ball.Ypos;
然后,您可以将此信息与您对蝙蝠对象的类似信息进行比较,然后根据此信息对此进行操作。
希望这足以让你开始走上正轨。我可能会在今晚晚些时候为你完成这个,否则,明天我可以解决这个问题。如果您有任何问题,请告诉我。