Autor Beitrag
Dude566
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 1592
Erhaltene Danke: 79

W8, W7 (Chrome, FF, IE)
Delphi XE2 Pro, Eclipse Juno, VS2012
BeitragVerfasst: Mo 11.10.10 15:02 
Hallo Leute,

ich weiss nicht wieso, aber ich verzweifel jedes mal wenn es um Spiele und die dazugehörigen Abfragen geht.

Ich möchte ganz simpel einen Ball auf meiner Paintbox bewegen, trifft er auf den Rand soll er abprallen.

Meine Klasse für den Ball
ausblenden volle Höhe Delphi-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:
63:
64:
65:
66:
unit uTBall;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

type
  TBall = class(TObject)
    private
      FPlace : TCanvas;
      FX : integer;
      FY : integer;
      FRadius : integer;
      FColor : TColor;
    public
      constructor Create(APlace : TCanvas);
      procedure Draw(AX, AY, ARadius : integer);
      procedure Move(XSpeed: integer; YSpeed : integer);
      function GetX : integer;
      function GetY : integer;
      function GetRadius : integer;
  end;

implementation

// Builds the new ball
constructor TBall.Create(APlace : TCanvas);
begin
  FPlace := APlace;
end;

// Sets postition of the ball and draws it
procedure TBall.Draw(AX: integer; AY: integer; ARadius: integer);
begin
  FX := AX;
  FY := AY;
  FRadius := ARadius;
  FPlace.Ellipse(FX - FRadius, FY - FRadius, FX + FRadius, FY + FRadius);
end;

// Sets ball to a new position and draws it with the Draw procedure
procedure TBall.Move(XSpeed: integer; YSpeed : integer);
begin
  Draw(FX + XSpeed, FY + YSpeed, FRadius);
end;

// Returns the X-Coordinate of the ball
function TBall.GetX;
begin
  result := FX;
end;

// Returns the Y-Coordinate of the ball
function TBall.GetY;
begin
  result := FY;
end;

// Returns the radius
function TBall.GetRadius;
begin
  result := FRadius;
end;
end.

Wirklich Anfängerkram, ich weiss nicht warum ich mich jedes mal schwer tue.

Auf dem Formular
ausblenden volle Höhe Delphi-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:
procedure TmainForm.btnStartClick(Sender: TObject);
var
  i : integer;
begin
  myBall := TBall.Create(PaintBox1.Canvas);
  myBall.Draw(random(100)+50, random(100)+50, random(20)+10);
  Timer1.Enabled := true;
end;

procedure TmainForm.Timer1Timer(Sender: TObject);
begin
  PaintBox1.Refresh;
  if (myBall.GetX - myBall.GetRadius < 1then
  begin
    myBall.Move(2,?);
  end
  else
  if (myBall.GetX + myBall.GetRadius >= PaintBox1.Width) then
  begin
    myBall.Move(-2,?);
  end
  else
  if (myBall.GetY - myBall.GetRadius < 1then
  begin
    myBall.Move(?,2);
  end
  else
  if (myBall.GetY + myBall.GetRadius >= PaintBox1.Height) then
  begin
    myBall.Move(?,-2);
  end
  else
    myBall.Move(2,2);
end;


Ich hab mir das ganze so gedacht wie oben, jedoch weiss ich beim Abprallen nicht aus welcher Richtung der Ball kam und somit fehlt mir dann immer ein Parameter für meine Move Prozedur.

Hoffe ihr könnt mir weiterhelfen, ist mir schon ein wenig peinlich bei so etwas zu fragen. :oops: :lol:

Schönen Gruß,
Dude566

_________________
Es gibt 10 Gruppen von Menschen: diejenigen, die das Binärsystem verstehen, und die anderen.
bummi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 1248
Erhaltene Danke: 187

XP - Server 2008R2
D2 - Delphi XE
BeitragVerfasst: Mo 11.10.10 15:14 
die Fragezeichen ersetzen durch XStep und YStep.

XStep, YStep anfangs initialisieren z.B. mit 2
bei Kollision rechts oder links XStep := - XStep

YStep entsprechend..
Dude566 Threadstarter
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 1592
Erhaltene Danke: 79

W8, W7 (Chrome, FF, IE)
Delphi XE2 Pro, Eclipse Juno, VS2012
BeitragVerfasst: Mo 11.10.10 15:39 
Danke :lol: , war ja klar das es nicht schwer sein konnte.

Funktioniert einwandfrei, jedoch am oberen Rand prallt er nicht ab.

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
// Kollision am oberen Rand
  if (myBall.GetY - myBall.GetRadius <= 1then
  begin
    YStep := + YStep;
    myBall.Move(XStep,YStep);
  end

_________________
Es gibt 10 Gruppen von Menschen: diejenigen, die das Binärsystem verstehen, und die anderen.
Tropby
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 71
Erhaltene Danke: 4

Vista, Win XP, Win 89
Turbo Delphi Ex.
BeitragVerfasst: Mo 11.10.10 15:45 
Probiere es mal mit

ausblenden Delphi-Quelltext
1:
YStep := -YStep;					

_________________
Tropby
Dude566 Threadstarter
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 1592
Erhaltene Danke: 79

W8, W7 (Chrome, FF, IE)
Delphi XE2 Pro, Eclipse Juno, VS2012
BeitragVerfasst: Mo 11.10.10 15:52 
Gut es funktioniert so, aber warum?
Wenn es oben auftrifft müsste es doch um wieder runterzukommen ins positive von Y, denn am oberen Rand ist Y ja 0.

Edit: Ach, das soll ja wegen des Umkehren der Vorzeichen immer Minus sein. :autsch:

_________________
Es gibt 10 Gruppen von Menschen: diejenigen, die das Binärsystem verstehen, und die anderen.