![](/img/trans.png)
[英]Is there a way and how can i make the program increment the obstacleSpeed by one every time the score increments by one?
[英]how can i add a mouse click to this program that will increment a score by one each time the ball is clicked in C#?
如何在此程序中添加鼠标单击,每次在C#中单击该球,分数就会增加一?
namespace Ball_timer_2005
{
public class Form1 : System.Windows.Forms.Form
{
const int radius = 20;
const int velocity = 5;
int xC, yC, xDelta=10, yDelta=10, xSize, ySize; // class level variables
private System.Windows.Forms.Timer timer1;
private System.ComponentModel.IContainer components;
public Form1()
{
InitializeComponent();
// TODO: Add any constructor code after InitializeComponent call
this.ResizeRedraw = true; // Tell form to redraw itself when resized
timer1.Start();
Form1_Resize(this, EventArgs.Empty); // Force a Resize Event as pgm starts
//
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.timer1 = new System.Windows.Forms.Timer(this.components);
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Interval = 25;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Name = "Form1";
this.Text = "Bouncing Ball";
this.Resize += new System.EventHandler(this.Form1_Resize);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Resize(object sender, System.EventArgs e)
{
xSize = this.ClientSize.Width; // Set current window size
ySize = this.ClientSize.Height;
xC = xSize/2; // Place ball in center of window
yC = ySize/2;
DrawBall(); // Draw the ball in the window
}
private void timer1_Tick(object sender, System.EventArgs e)
{
DrawBall(); // Draw ball in next frame of animation
}
private void DrawBall()
{
Graphics g = this.CreateGraphics();
Brush b = new SolidBrush(this.BackColor);
g.FillEllipse(b, xC-radius, yC-radius, 2*radius, 2*radius); //erase old ball
xC += xDelta; //move ball
yC += yDelta;
if ((xC+radius >= ClientSize.Width) || (xC - radius <= 0)) //check for wall hits
xDelta = -xDelta;
if ((yC+radius >= ClientSize.Height) || (yC - radius <= 0))
yDelta = -yDelta;
b = new SolidBrush(Color.GreenYellow); // draw new ball
g.FillEllipse(b, xC-radius, yC-radius, 2*radius, 2*radius);
b.Dispose();
g.Dispose();
}
}
}
1.这是我到目前为止的代码可以帮助一个人吗? 1.这是我到目前为止的代码可以帮助一个人吗? 1.这是我到目前为止的代码可以帮助一个人吗?
这完全有帮助吗
http://www.daniweb.com/software-development/csharp/threads/317766/mouse-coordinates-within-a-form
您也许可以使用它来检测鼠标位置/球宽度半径的重叠
您可以通过处理Form的MouseClick
事件来完成此操作。
步骤1:将Ellipse
坐标存储到“ Region
以标识Mouse Click
的位置。 Mouse Click
稍后”。
//Declare it as class members
List<Region> regionList = new List<Region>();
int EclipseX = 10;
int EclipseY = 50;
int BallWidth = 100;
int BallHeight = 100;
//inside paint function
SolidBrush brush = new SolidBrush(Color.Blue);
e.Graphics.FillEllipse(brush, EclipseX,EclipseY, BallWidth, BallHeight);
regionList.Add(new Region(new Rectangle(EclipseX,EclipseY, BallWidth, BallHeight)));
步骤2:现在声明Form
的MouseClick
事件
this.MouseClick += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseClick);
第三步:现在处理Form
的MouseClick
事件。
MouseClick
事件在此处e
提供MouseEventArgs
参数。 MouseEventArgs
具有两个属性。 property
是X
可为您提供Mouse Click
的X-axis
位置。 property
是Y
它为您提供Mouse Click
的Y-axis
位置。 IsVisible
属性将这些X
和Y
值与现有的Region
值进行比较。 您需要从List
(在绘制Ellipse
时添加)中获取每个Region
,并使用Region
类的IsVisible
属性检测鼠标是否单击了Ellipse
。
//declare it as class member int MouseClicksCount = 0; bool isfound=false; //event handler private void Form1_MouseClick(object sender, MouseEventArgs e) { isfound=false; foreach(Region r in regionList) { if (r.IsVisible(eX, eY)) { isfound=true; break; } } if (isfound) MouseClicksCount++; }
完整的解决方案(示例代码):
public partial class Form1 : Form
{
int MouseClicksCount = 0;
List<Region> regionList = new List<Region>();
int EclipseX = 10;
int EclipseY = 50;
int BallWidth = 100;
int BallHeight = 100;
bool isfound=false;
public Form1()
{
InitializeComponent();
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
this.MouseClick += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseClick);
}
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
isfound=false;
foreach(Region r in regionList)
{
if (r.IsVisible(e.X, e.Y))
{
isfound=true;
break;
}
}
if (isfound)
MouseClicksCount++;
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
SolidBrush brush = new SolidBrush(Color.Blue);
e.Graphics.FillEllipse(brush, EclipseX,EclipseY, BallWidth, BallHeight);
regionList.Add(new Region(new Rectangle(EclipseX,EclipseY, BallWidth, BallHeight)));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.