Hallo, ich bin noch neu in der Programmierwelt und möchte nachdem ich nun die Tutorials von Microsoft Visual C# gemacht habe ein simples Spiel programmieren.
Es soll eine PictureBox (bei mir "ball" genannt) mit den "W,A,S,D" Tasten durch ein Labyrinth bewegt werden.
Es gibt (zur Zeit) 2 "Mauern", die aus PictureBoxen bestehen.
Wenn der ball auf eine Wand trifft, soll der ball nicht weiter gehen.
Soweit habe ich es schon geschafft.
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: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62:
| using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;
namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); }
private void Form1_KeyDown(object sender, KeyEventArgs e) { switch (e.KeyCode) { case Keys.A: ball.Location = new Point(ball.Location.X - 10, ball.Location.Y); if (pictureBox1.Bounds.IntersectsWith(ball.Bounds) || pictureBox2.Bounds.IntersectsWith(ball.Bounds)) { ball.Location = new Point(ball.Location.X + 10, ball.Location.Y); } break;
case Keys.D: ball.Location = new Point(ball.Location.X + 10, ball.Location.Y); if (pictureBox1.Bounds.IntersectsWith(ball.Bounds) || pictureBox2.Bounds.IntersectsWith(ball.Bounds)) { ball.Location = new Point(ball.Location.X - 10, ball.Location.Y); } break;
case Keys.W: ball.Location = new Point(ball.Location.X, ball.Location.Y - 10); if (pictureBox1.Bounds.IntersectsWith(ball.Bounds) || pictureBox2.Bounds.IntersectsWith(ball.Bounds)) { ball.Location = new Point(ball.Location.X, ball.Location.Y + 10); } break;
case Keys.S: ball.Location = new Point(ball.Location.X, ball.Location.Y + 10); if (pictureBox1.Bounds.IntersectsWith(ball.Bounds) || pictureBox2.Bounds.IntersectsWith(ball.Bounds)) { ball.Location = new Point(ball.Location.X, ball.Location.Y - 10); } break; } } private void Laden(object sender, EventArgs e) { Punkte.Text = "0";
}
}
} |
Wenn ich nun jedoch mehr als 2 Wände (pictureBox1 & pictureBox2) einfügen möchte (z.B. 10), ist meine Lösung bei der if-Abfrage nicht gerade passend.
Ich habe in vielen Foren und Bolgs gelesen, dass man die PictureBoxen in eine liste<> packen kann, jedoch habe ich keine Ahnung wie ich das mache bzw. wo es im Quellcode hingehört.
Könntet ihr mir dabei helfen?
mfg wobbsy