Движение танка в игре во время выстрела - C#
Формулировка задачи:
пишу курсач на с#..игра танки. должен быть голый шарп, никаких XNA или подобного. начал и сразу столкнулся с проблемой. нарисовал танк запихнул его в picturebox, передвижение клавиатурой,выстрел на 0.
полле нажатия на 0 пуля летит, а танк в это время двигаться не может..что делать подскажите?
public void Выстрел_p1(bool vistrel) { if (napravlenie == 1) { pictureBox2.Location = new Point(pictureBox1.Location.X+13,pictureBox1.Location.Y-4); pictureBox2.Visible = true; do { pictureBox2.Location = new Point(pictureBox2.Location.X, pictureBox2.Location.Y - 2); Thread.Sleep(30); } while (pictureBox2.Location.Y >= -3); }; pictureBox2.Visible = false; vistrel = false; } protected override void OnKeyDown(KeyEventArgs e) { base.OnKeyDown(e); X1 = pictureBox1.Location.X; Y1 = pictureBox1.Location.Y; switch (e.KeyCode) { case Keys.Up: { pictureBox1.Location = new Point(X1, Y1 - 3); if (pictureBox1.Location.Y <= 0) pictureBox1.Location = new Point(pictureBox1.Location.X,0); if (edet == true) { pictureBox1.Image = WindowsFormsApplication1.Properties.Resources.tank11; edet = false; } else { pictureBox1.Image = WindowsFormsApplication1.Properties.Resources.tank12; edet = true; } napravlenie = 1; break; } case Keys.Down: { pictureBox1.Location = new Point(X1, Y1 + 3); if (pictureBox1.Location.Y >= 480-32) pictureBox1.Location = new Point(pictureBox1.Location.X, 480-32); if (edet == true) { pictureBox1.Image = WindowsFormsApplication1.Properties.Resources.tankDown1; edet = false; } else { pictureBox1.Image = WindowsFormsApplication1.Properties.Resources.tankDown2; edet = true; } napravlenie = 3; break; } case Keys.Left: { pictureBox1.Location = new Point(X1 - 3, Y1); if (pictureBox1.Location.X <= 0) pictureBox1.Location = new Point(0, pictureBox1.Location.Y); if (edet == true) { pictureBox1.Image = WindowsFormsApplication1.Properties.Resources.tankLeft1; edet = false; } else { pictureBox1.Image = WindowsFormsApplication1.Properties.Resources.tankLeft2; edet = true; } napravlenie = 4; break; } case Keys.Right: { pictureBox1.Location = new Point(X1 + 3, Y1); if (pictureBox1.Location.X >= 640-32) pictureBox1.Location = new Point(640-32, pictureBox1.Location.Y); if (edet == true) { pictureBox1.Image = WindowsFormsApplication1.Properties.Resources.tankRight11; edet = false; } else { pictureBox1.Image = WindowsFormsApplication1.Properties.Resources.tankRight21; edet = true; } napravlenie = 2; break; } case Keys.NumPad0: { vistrel = true; Выстрел_p1(vistrel); break; } }
Решение задачи: «Движение танка в игре во время выстрела»
textual
Листинг программы
int napravlenie1 = 0, napravl_vistrela1, napravlenie2 = 0, napravl_vistrela2; bool vistrel_игрок1, polet_puli_игрок1, vistrel_игрок2, polet_puli_игрок2; int k,k2; //направления: 1-вверх; // 2-вправо; // 3-вниз; // 4-влево; protected override void OnKeyDown(KeyEventArgs e) { base.OnKeyDown(e); //switch (e.KeyCode) //{ // case Keys.Up: { napravlenie1 = 1; k = napravlenie1; break; } // case Keys.Down: { napravlenie1 = 3; k = napravlenie1; break; } // case Keys.Left: { napravlenie1 = 4; k = napravlenie1; break; } // case Keys.Right: { napravlenie1 = 2; k = napravlenie1; break; } // case Keys.NumPad0: { if (polet_puli_игрок1==false){ polet_puli_игрок1 = true; vistrel_игрок1 = true; Выстрел_p1();} // break; } //} if (e.KeyCode == Keys.Up) { napravlenie1 = 1; k = napravlenie1; } if (e.KeyCode == Keys.Down) { napravlenie1 = 3; k = napravlenie1; } if (e.KeyCode == Keys.Left) { napravlenie1 = 4; k = napravlenie1; } if (e.KeyCode == Keys.Right) { napravlenie1 = 2; k = napravlenie1; } if (e.KeyCode == Keys.NumPad0) { if (polet_puli_игрок1 == false) { polet_puli_игрок1 = true; vistrel_игрок1 = true; Выстрел_p1(); } } if (e.KeyCode == Keys.W) { napravlenie2 = 1; k2 = napravlenie2; } if (e.KeyCode == Keys.S) { napravlenie2 = 3; k2 = napravlenie2; } if (e.KeyCode == Keys.A) { napravlenie2 = 4; k2 = napravlenie2; } if (e.KeyCode == Keys.D) { napravlenie2 = 2; k2 = napravlenie2; } if (e.KeyCode == Keys.Space) { if (polet_puli_игрок2 == false) { polet_puli_игрок2 = true; vistrel_игрок2 = true; Выстрел_p2(); } } } public void Выстрел_p1() { pictureBox2.Hide(); if (napravl_vistrela1 == 1) { pictureBox2.Location = new Point(pictureBox1.Location.X + 13, pictureBox1.Location.Y - 4); } if (napravl_vistrela1 == 2) { pictureBox2.Location = new Point(pictureBox1.Location.X + 33, pictureBox1.Location.Y + 13); } if (napravl_vistrela1 == 3) { pictureBox2.Location = new Point(pictureBox1.Location.X + 13, pictureBox1.Location.Y + 36); } if (napravl_vistrela1 == 4) { pictureBox2.Location = new Point(pictureBox1.Location.X -4, pictureBox1.Location.Y +13); } } public void Выстрел_p2() { pictureBox4.Hide(); if (napravl_vistrela2 == 1) { pictureBox4.Location = new Point(pictureBox3.Location.X + 13, pictureBox3.Location.Y - 4); } if (napravl_vistrela2 == 2) { pictureBox4.Location = new Point(pictureBox3.Location.X + 33, pictureBox3.Location.Y + 13); } if (napravl_vistrela2 == 3) { pictureBox4.Location = new Point(pictureBox3.Location.X + 13, pictureBox3.Location.Y + 36); } if (napravl_vistrela2 == 4) { pictureBox4.Location = new Point(pictureBox3.Location.X - 4, pictureBox3.Location.Y + 13); } } public void perevorot(int i) { Bitmap img = new Bitmap(32, 32); Bitmap img2 = new Bitmap(WindowsFormsApplication1.Properties.Resources.tank11); Graphics g = Graphics.FromImage(img); Matrix m = new Matrix(); m.RotateAt(i, new Point(pictureBox1.Width / 2, pictureBox1.Height / 2), MatrixOrder.Prepend); m.Translate(pictureBox1.Width / 2, pictureBox1.Height / 2, MatrixOrder.Prepend); g.Transform = m; g.DrawImage(img2, img2.Width / 2 * (-1), img2.Height / 2 * (-1), img2.Width, img2.Height); pictureBox1.Image = img; } public void perevorot2(int i) { Bitmap img = new Bitmap(32, 32); Bitmap img2 = new Bitmap(WindowsFormsApplication1.Properties.Resources.tank22); Graphics g = Graphics.FromImage(img); Matrix m = new Matrix(); m.RotateAt(i, new Point(pictureBox3.Width / 2, pictureBox3.Height / 2), MatrixOrder.Prepend); m.Translate(pictureBox3.Width / 2, pictureBox3.Height / 2, MatrixOrder.Prepend); g.Transform = m; g.DrawImage(img2, img2.Width / 2 * (-1), img2.Height / 2 * (-1), img2.Width, img2.Height); pictureBox3.Image = img; } /// <summary> /// метод обработки конечных данных и вывода на экран /// </summary> /// <param name="vistrel"></param> /// <param name="napravlenie"></param> public void Draw(ref bool vistrel_игрок1,ref int napravlenie1) { //движение первого танка и его пули if (napravl_vistrela1 == 0) napravl_vistrela1 = napravlenie1; if ((napravlenie1 == 0)&&(polet_puli_игрок1==false)) napravl_vistrela1 = k; if (vistrel_игрок1 == true) { pictureBox2.Show(); if (napravl_vistrela1 == 1) pictureBox2.Location = new Point(pictureBox2.Location.X, pictureBox2.Location.Y - 3); if (napravl_vistrela1 == 2) pictureBox2.Location = new Point(pictureBox2.Location.X + 3, pictureBox2.Location.Y); if (napravl_vistrela1 == 3) pictureBox2.Location = new Point(pictureBox2.Location.X, pictureBox2.Location.Y + 3); if (napravl_vistrela1 == 4) pictureBox2.Location = new Point(pictureBox2.Location.X - 3, pictureBox2.Location.Y); if ((pictureBox2.Location.X <= 2) || (pictureBox2.Location.Y <= 2) || (pictureBox2.Location.X >= 627) || (pictureBox2.Location.Y >= 467)) { vistrel_игрок1 = false; pictureBox2.Hide(); napravl_vistrela1 = 0; polet_puli_игрок1 = false; } } if (napravlenie1 == 1) { pictureBox1.Location = new Point(pictureBox1.Location.X, pictureBox1.Location.Y - 2); perevorot(0); } if (napravlenie1 == 2) { pictureBox1.Location = new Point(pictureBox1.Location.X + 2, pictureBox1.Location.Y); perevorot(90); } if (napravlenie1 == 3) { pictureBox1.Location = new Point(pictureBox1.Location.X, pictureBox1.Location.Y + 2); perevorot(180); } if (napravlenie1 == 4) { pictureBox1.Location = new Point(pictureBox1.Location.X - 2, pictureBox1.Location.Y); perevorot(-90); } if (pictureBox1.Location.X <= 0) pictureBox1.Location = new Point(0, pictureBox1.Location.Y); if (pictureBox1.Location.Y <= 0) pictureBox1.Location = new Point(pictureBox1.Location.X, 0); if (pictureBox1.Location.X >= 606) pictureBox1.Location = new Point(606, pictureBox1.Location.Y); if (pictureBox1.Location.Y >= 446) pictureBox1.Location = new Point(pictureBox1.Location.X, 446); napravlenie1 = 0; //движение второго танка и его пули if (napravl_vistrela2 == 0) napravl_vistrela2 = napravlenie2; if ((napravlenie2 == 0) && (polet_puli_игрок2 == false)) napravl_vistrela2 = k2; if (vistrel_игрок2 == true) { pictureBox4.Show(); if (napravl_vistrela2 == 1) pictureBox4.Location = new Point(pictureBox4.Location.X, pictureBox4.Location.Y - 3); if (napravl_vistrela2 == 2) pictureBox4.Location = new Point(pictureBox4.Location.X + 3, pictureBox4.Location.Y); if (napravl_vistrela2 == 3) pictureBox4.Location = new Point(pictureBox4.Location.X, pictureBox4.Location.Y + 3); if (napravl_vistrela2 == 4) pictureBox4.Location = new Point(pictureBox4.Location.X - 3, pictureBox4.Location.Y); if ((pictureBox4.Location.X <= 2) || (pictureBox4.Location.Y <= 2) || (pictureBox4.Location.X >= 627) || (pictureBox4.Location.Y >= 467)) { vistrel_игрок2 = false; pictureBox4.Hide(); napravl_vistrela2 = 0; polet_puli_игрок2 = false; } } if (napravlenie2 == 1) { pictureBox3.Location = new Point(pictureBox3.Location.X, pictureBox3.Location.Y - 2); perevorot2(0); } if (napravlenie2 == 2) { pictureBox3.Location = new Point(pictureBox3.Location.X + 2, pictureBox3.Location.Y); perevorot2(90); } if (napravlenie2 == 3) { pictureBox3.Location = new Point(pictureBox3.Location.X, pictureBox3.Location.Y + 2); perevorot2(180); } if (napravlenie2 == 4) { pictureBox3.Location = new Point(pictureBox3.Location.X - 2, pictureBox3.Location.Y); perevorot2(-90); } if (pictureBox3.Location.X <= 0) pictureBox3.Location = new Point(0, pictureBox3.Location.Y); if (pictureBox3.Location.Y <= 0) pictureBox3.Location = new Point(pictureBox3.Location.X, 0); if (pictureBox3.Location.X >= 606) pictureBox3.Location = new Point(606, pictureBox3.Location.Y); if (pictureBox3.Location.Y >= 446) pictureBox3.Location = new Point(pictureBox3.Location.X, 446); napravlenie2 = 0; } private void timer1_Tick(object sender, EventArgs e) { Draw(ref vistrel_игрок1,ref napravlenie1); }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д