C#如何改变弹跳球的颜色? [英] C# how to change color of bouncing ball?

查看:102
本文介绍了C#如何改变弹跳球的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要改变球的颜色当球碰撞在球的边缘时,从每次碰撞中它的颜色应该是随机的,并且应该保持这种颜色直到第二次碰撞发生。

有人可以帮我吗?



我尝试过的事情:



公共部分类Form2:表格
{
球;
Graphics g;
public Form2()
{
InitializeComponent();
g = this.CreateGraphics();
ball = new Ball(80,80,30,30,g,this);

}

private void Form2_Paint(object sender,PaintEventArgs e)
{
ball.draw();
}
}



类Ball
{
public int x;
public int y;
public int r;
public int s;
public Graphics g;
public Thread td;
public Form2 fr;
public SolidBrush myvariable;
public Random rnd;
public Ball(int x,int y,int r,int s,Graphics g,Form2 fr)
{
this.fr = fr;
this.x = x;
this.y = y;
this.r = r;
this.s = s;
this.g = g;
td = new Thread(run);
td.Start();
}
public void draw()
{
myvariable = new SolidBrush(Color.FromArgb(rnd.Next(150),rnd.Next(205),rnd.Next( 160)));
g.FillEllipse(myvariable,x,y,r,s);

}

public void run()
{
for(;;)
{
this.move() ;
Thread.Sleep(50);
}
}
public void move()
{
int newball_x = x + r;
int newball_y = y + s;
if(newball_x< -5 || newball_x> this.fr.ClientSize.Width)
{
r = -r;

}
if(newball_y< 0 || newball_y> this.fr.ClientSize.Height)
{
s = -s;
}
x + = r;
y + = s;
this.fr.Invalidate();
}
}

解决方案

将SolidBrush定义移到任何方法之外,因此它成为类的一部分(无论如何你应该这样做,因为所有的图形对象都是稀缺的资源,当你完成它们时需要正确处理 - 如果你不这样做,你会在遇到问题时遇到问题。

然后,类会为你保留该值。

当你检测到它撞到墙上时,处理现有实例,并使用随机颜色创建一个新的SolidBrush:如何从System.Drawing.Color中选择一种随机颜色? [ ^

I need to change color of ball When the ball collides on the edges of the form,From each collision its color should become random,and this color should be maintained until the second clash has occurred.
Anyone can help me please?

What I have tried:

public partial class Form2 : Form
    {
        Ball ball;
        Graphics g;
        public Form2()
        {
            InitializeComponent();
            g = this.CreateGraphics();
            ball = new Ball(80, 80, 30,30, g, this);

        }

        private void Form2_Paint(object sender, PaintEventArgs e)
        {
            ball.draw();
        }
    }



class Ball
    {
        public int x;
        public int y;
        public int r;
        public int s;
        public Graphics g;
        public Thread td;
        public Form2 fr;
        public SolidBrush myvariable;
        public Random rnd;
        public Ball(int x, int y, int r,int s, Graphics g, Form2 fr)
        {
            this.fr = fr;
            this.x = x;
            this.y = y;
            this.r = r;
            this.s = s;
            this.g = g;
            td = new Thread(run);
            td.Start();
        }
        public void draw()
        {
             myvariable = new SolidBrush(Color.FromArgb(rnd.Next(150), rnd.Next(205), rnd.Next(160)));
            g.FillEllipse(myvariable, x, y, r, s);
            
        }
        
        public void run()
        {
            for (;;)
            {
                this.move();
                Thread.Sleep(50);
            }
        }
        public void move()
        {
            int newball_x = x + r;
            int newball_y = y + s;
            if (newball_x < -5 || newball_x > this.fr.ClientSize.Width)
            {
                r = -r;
                
            }
            if (newball_y < 0 || newball_y > this.fr.ClientSize.Height)
            {
                s = -s;
            }
            x += r;
            y += s;
            this.fr.Invalidate();
        }
    }

解决方案

Move the SolidBrush definition outside any method, so it becomes part of the class (you should do this anyway, as all Graphics objects are scarce resources and need to be Disposed correctly when you are finished with them - if you don't you will get problems as they run out).
The class then persists the value for you.
When you detect it hit the wall, Dispose the existing instance, and create a new SolidBrush using a random colour: How to choose a random color from System.Drawing.Color?[^]


这篇关于C#如何改变弹跳球的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆