特定の座標に線を描画するために onPaint メソッドを使用しています。これはコード全体です。
namespace Lunar_Lander{
public partial class Form1 : Form
{
System.Media.SoundPlayer player= new System.Media.SoundPlayer();
System.Media.SoundPlayer player1 = new System.Media.SoundPlayer();
private double x,y;
public static double dx,dy;
private int gorivo = 1000;
private int zivoti = 3;
//private int score = 0;
public System.Drawing.Graphics g;
public System.Drawing.Pen pen1 = new System.Drawing.Pen(Color.Blue, 2F);
static int th = 16;
static int t = 0;
static Color[] c = new Color[10];
public Form1()
{
InitializeComponent();
Thread t1 = new Thread(new ThreadStart(SplashForm));
t1.Start();
Thread.Sleep(3500);
t1.Abort();
Thread.Sleep(1000);
//zvuk();
initGame();
cSet();
}
private void SplashForm()
{
Form2 newSplashForm = new Form2();
newSplashForm.ShowDialog();
newSplashForm.Dispose();
}
void cSet()
{
c[0] = Color.Red;
c[1] = Color.Blue;
c[2] = Color.Yellow;
c[3] = Color.Green;
c[4] = Color.Pink;
c[5] = Color.Brown;
c[6] = Color.Violet;
c[7] = Color.White;
c[8] = Color.Silver;
c[9] = Color.Purple;
}
private void Form1_Load(object sender, EventArgs e)
{
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
g.PageUnit = GraphicsUnit.Inch;
Pen MyPen = new Pen(c[t], th / g.DpiX);
if(v>2)
g.DrawLine(MyPen, Convert.ToInt32(picLander.Location.X), Convert.ToInt32(picLander.Location.Y), 1, 1);
g.DrawEllipse(MyPen, Convert.ToInt32(picLander.Location.X), Convert.ToInt32(picLander.Location.Y), 10, 10);
}
static int v=0;
private void zvuk()
{
player.Stream = Properties.Resources.System_Shock_2___Engsong;
player.PlayLooping();
}
private void timer1_Tick(object sender, EventArgs e)
{
v++;
dy += .1;
picLander.Image = myPics.Images[0];
kretanje();
Statistika();
this.SetStyle(ControlStyles.ResizeRedraw, true);
}
private void kretanje()
{
x += dx;
if (x > this.Width - picLander.Width)
{
x = 0;
}
if (x < 0)
{
x = Convert.ToDouble(this.Width - picLander.Width);
}
y += dy;
if (y > this.Height - picLander.Height)
{
y = 0;
}
if (y < 0)
{
y = Convert.ToDouble(this.Height - picLander.Height);
}
picLander.Location = new Point(Convert.ToInt32(x),Convert.ToInt32(y));
}
public void Statistika()
{
lbldx.Text = "Horizontalna brzina: " + Math.Round(dx,2);
lbldy.Text = "Vertikalna brzina: " + Math.Round(dy,2);
lblGorivo.Text = "Gorivo: " + gorivo;
lblZivoti.Text = "Brodova: " + zivoti;
//lblScore.Text = "score: " + score;
debljina.Text = "Debljina: " + th;
switch (t)
{
case 0:
boja.Text = "Boja:Crvena";
break;
case 1:
boja.Text = "Boja:Plava";
break;
case 2:
boja.Text = "Boja:Zuta";
break;
case 3:
boja.Text = "Boja:Zelena";
break;
case 4:
boja.Text = "Boja:Pink";
break;
case 5:
boja.Text = "Boja:Braon";
break;
case 6:
boja.Text = "Boja:Ljubicasta";
break;
case 7:
boja.Text = "Boja:Bela";
break;
case 8:
boja.Text = "Boja:Srebrna";
break;
case 9:
boja.Text = "Boja:Purpurna";
break;
}
}
public void Kraj(){
DialogResult odgovor;
zivoti--;
if (zivoti <= 0){
odgovor = MessageBox.Show("Igraj ponovo?","Kraj Igre",MessageBoxButtons.YesNo);
if (odgovor == DialogResult.Yes){
zivoti = 3;
gorivo = 1000;
initGame();
} else {
Application.Exit();
}
}}
public void initGame()
{
Random roller = new Random();
dx = Convert.ToDouble(roller.Next(5) - 2);
dy = Convert.ToDouble(roller.Next(5) - 2);
x = Convert.ToDouble(roller.Next(this.Width));
y = Convert.ToDouble(roller.Next(this.Height));
gorivo = 1000;
timer1.Enabled = true;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
gorivo--;
if (gorivo < 0)
{
timer1.Enabled = false;
MessageBox.Show("Nestalo Vam je goriva!!");
gorivo += 40;
Kraj();
initGame();
}
switch (e.KeyData)
{
case Keys.Up:
picLander.Image = myPics.Images[1];
dy -= 0.8;
break;
case Keys.Left:
picLander.Image = myPics.Images[2];
dx=dx-0.8;
break;
case Keys.Right:
picLander.Image = myPics.Images[3];
dx=dx+0.8;
break;
case Keys.Down:
picLander.Image = myPics.Images[4];
dy += 0.8;
break;
case Keys.W:
th++;
break;
case Keys.S:
{
if (th == 0) th = 0;
else th--;
}
break;
case Keys.A:
{ if (t == 9)t = 0; else t++; }
break;
case Keys.D:
{ if (t == 0)t = 9; else t--; }
break;
default:
break;
}
}
}
}
線は正常に描画されますが、楕円が描画されません。何か不足していますか?いくつかの静的座標で楕円を描画しようとすると、うまくいきます。