Autor Beitrag
wobbsy
Hält's aus hier
Beiträge: 2



BeitragVerfasst: Mo 07.07.14 13:20 
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.

ausblenden volle Höhe C#-Quelltext
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;
            }
        }
        //Punkte werden später noch eingebaut und gezählt.
        private void Laden(object sender, EventArgs e)
        {
            Punkte.Text = "0";

        }

    }


    }//Ende


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
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4708
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Mo 07.07.14 13:44 
Du kannst sie zum Beispiel schnell in einer Liste packen um dann den Any Operator von LINQ zu verwenden.
Es mach auch den Code etwas kompakter wenn du nicht sofort die Location änderst sondern nur gegen die potentielle neue Position prüfst und dann änderst wenn es passt. Anstatt die Änderung bei Kollisionen wieder zurückzunehmen.

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    Point newLocation = ball.Location;
    Rectangle newRect = ball.Bounds;

    switch (e.KeyCode)
    {
        case Keys.A:
            newLocation = new Point(ball.Location.X - 10, ball.Location.Y);
            newRect.Offset(-100);
            break;
        case Keys.D:
            newLocation = new Point(ball.Location.X + 10, ball.Location.Y);
            newRect.Offset(+100);
            break;
        .... u.s.w.
    }
    List<PictureBox> walls = new List<PictureBox>() { pictureBox1, pictureBox2, pictureBox3 };

    if (!walls.Any(pb => pb.Bounds.IntersectsWith(newRect)))
        ball.Location = newLocation;
}
wobbsy Threadstarter
Hält's aus hier
Beiträge: 2



BeitragVerfasst: Mo 07.07.14 14:09 
Vielen Dank :)
Das hat mir sehr geholfen!
mfg