1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
|
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
const int geschwindigkeit = 30;
const int refresh = 50;
const int radius = 50;
int x = 0, y = 0;
Random r = new Random();
private void timer1_Tick(object sender, EventArgs e)
{
int richtung = r.Next(360); //Winkel
x += (int)(geschwindigkeit * Math.Sin(richtung));
y += (int)(geschwindigkeit * Math.Cos(richtung));
if (x < 0) x = 0;
if (y < 0) y = 0;
if (x > pictureBox1.Width - radius) x = pictureBox1.Width - radius;
if (y > pictureBox1.Height - radius) y = pictureBox1.Height - radius;
Graphics g = pictureBox1.CreateGraphics();
//altes Bild löschen
g.FillRectangle(new SolidBrush(Color.White), -1, -1, pictureBox1.Width + 1, pictureBox1.Height + 1);
//neues Bild einfügen
g.FillEllipse(new SolidBrush(Color.Red), x - radius, y - radius, 2 * radius, 2 * radius);
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Enabled = true;
timer1.Interval = refresh;
x = r.Next(radius, pictureBox1.Width - radius);
y = r.Next(radius, pictureBox1.Height - radius);
}
}
} |