Autor Beitrag
Palmm130
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 26



BeitragVerfasst: Di 27.09.11 10:25 
Nochmals Hallo,
im Zuge meines aktuellen OOP-Projektes hab ich eine Klasse "Ball" von einer PictureBox erstellt, welche auf einem Panel, dem "Spielfeld" sich frei bewegen können soll.
Funktioniert soweit prächtig, jedoch soll er bei Kollision mit dem Spielfeldrand die Richtung wechseln. Auf der Unterseite tut er es, auf der Oberseite nicht.
Hier nun aller relevanter Code:
Ball.cs:
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:
namespace Arkanoid
{
    class Ball : System.Windows.Forms.PictureBox
    {
        private bool up = false;
        private bool down = true;

        public void MoveYourself()
        {
            if (down)
            {
                this.Top = this.Top + 1;
            }
            else if (up)
            {
                this.Top = this.Top - 1;
            }
        }
        public void ChangeDirectionY()
        {
            if (up)
            {
                up = false;
                down = true;
            }
            if (down)
            {
                up = true;
                down = false;
            }
        }
    }
}

Form1.cs:
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:
namespace Arkanoid
{
    public partial class Form1 : Form
    {
        Timer tim_thegame = new Timer();
        Ball ball = new Ball();

        private void Form1_Load(object sender, EventArgs e)
        {
            ball.Width = 16;
            ball.Height = 16;
            ball.Image = Arkanoit.Properties.Resources.ball;
            ball.Top = 200;
            ball.Left = 200;
            pan_gamefield.Controls.Add(ball);
            tim_thegame.Tick += new EventHandler(tim_thegame_Tick);
            tim_thegame.Interval = 10;
            tim_thegame.Enabled = true;
        }


        void tim_thegame_Tick(object sender, EventArgs e)
        {
            ///
            /// Ball-Movement;
            /// 
            if (ball.Top <= pan_gamefield.Top)
            {
                ball.ChangeDirectionY();
            }
            else if (ball.Bottom >= pan_gamefield.Bottom)
            {
                ball.ChangeDirectionY();
            }
            ball.MoveYourself();
        }
}


Der Ball startet jetzt auf dem Spielfeld und fährt nach unten, prallt ab und verschwindet nach oben aus dem Bild. Habe ich etwas bei der Kollisionsabfrage falsch gemacht?
Warum geht es nach unten, aber nach oben nicht?
Horschdware
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 744
Erhaltene Danke: 54

Win XP Pro, Win 7 Pro x64
Delphi 7, Delphi XE, C++ Builder 5, SAP R/3
BeitragVerfasst: Di 27.09.11 10:58 
user profile iconPalmm130 hat folgendes geschrieben Zum zitierten Posting springen:

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
   
        public void ChangeDirectionY()
        {
            if (up)
            {
                up = false;
                down = true;
            }
            if (down)

            {
                up = true;
                down = false;
            }



Du setzt "down" auf true und fragst direkt danach, ob down true ist und setzt dann wiederum up auf true.
Hier wäre ein "else" praktisch.

PS: Kleiner tip:

Wenn du up/down als integer zusammenfasst und link/rechts ebenso kannst du ja folgendes machen:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
class Ball {
   private int dy = 1;
   private int dx = 1;
... }

Move {
  this.Top  += dy;
  this.Left += dx;
}

ChangeDirY {
  dy *= -1;
}
...

Bei der Kollision musst du dann jeweils nur mit -1 multiplizieren. Du sparst dir so das Hantieren mit den bool'schen Variablen.

_________________
Delphi: XE - OS: Windows 7 Professional x64
Palmm130 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 26



BeitragVerfasst: Di 27.09.11 11:31 
Dummdidumm, aua!

Vielen Dank, hab ich ausser Acht gelasse -.-
thepaine91
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 763
Erhaltene Danke: 27

Win XP, Windows 7, (Linux)
D6, D2010, C#, PHP, Java(Android), HTML/Js
BeitragVerfasst: Di 27.09.11 13:47 
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
// Falls du bei deiner bisherigen Angehensweise bleibst.
public void ChangeDirectionY()
{
            if (up)
            {
                up = false;
                down = true;
            }
            if (down)
            {
                up = true;
                down = false;
            }
// Würde auch so funktionieren vorausgesetzt die Werte werden korrekt initialisiert.
  up = ! up;
  down = ! down;
}