Einzelnen Beitrag anzeigen

Pfusch

Registriert seit: 12. Aug 2013
7 Beiträge
 
#15

AW: Problem mit Multiplikation

  Alt 13. Aug 2013, 00:39
So habe jetzt einige Umschichtungen bei meinem Code vorgenommen, das hat das Problem aber leider nicht gelöst, aber dafür ist er jetzt etwas übersichtlicher. Habe jetzt mal meine ganzen Units bis auf ein paar unwichtige Kommentare etc. reinkopiert. Wenn ich ein Integer statt Single verwende funktioniert es übrigens auch nicht.
Nochmal vielen Dank für die bisherigen Vorschläge.

Delphi-Quellcode:
unit ufrmBreakout;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls, uCoords, uBrick, UPaddle, UBall;

type
  TfrmBreakout = class(TForm)
    lblTime: TLabel;
    lblScore: TLabel;
    ptbGame: TPaintBox;
    TmrGame: TTimer;
    procedure FormCreate(Sender: TObject);
    procedure TimerTick(Sender: TObject);

  private
    bricks : TList;
    paddle : TPaddle;
    balls : TList;

  end;

const
  BrickColumn = 8;
  BrickRows = 5;
  PaddleStartX = 192;
  PaddleStartY = 380;
  BallStartAngle = 90;

var
  frmBreakout: TfrmBreakout;

implementation

{$R *.dfm}

  procedure TfrmBreakout.FormCreate(Sender: TObject);

  var
    Coords: TCoords;
    Brick: TBrick;
    x,y: Integer;
    xBall, yBall : Integer;

  begin
       //create the bricks
       bricks := TList.Create;
       for x := 0 to (BrickColumn-1) do
       begin
            for y := 0 to (BrickRows-1) do
            begin
                 Coords := TCoords.Create(x*uBrick.BrickWidth,y*uBrick.BrickHeight);
                 Brick := TBrick.Create(Coords);
                 bricks.Add(Brick);
            end;
       end;

       //create the paddle
       Coords := TCoords.Create(PaddleStartX,PaddleStartY);
       paddle := TPaddle.Create(Coords);

       //create the ball
       xBall := PaddleStartX + (uPaddle.PaddleWidth div 2);
       yBall := PaddleStartY - UBall.BallRadius;
       Coords := TCoords.Create(xBall,yBall);
       balls := TList.Create;
       balls.Add(TBall.Create(Coords,BallStartAngle,0,-1));

  end;

  procedure TfrmBreakout.TimerTick(Sender: TObject);
  var
    i, j : Integer;
    Ball: TBall;
    Brick: TBrick;

  begin
       for i := 0 to balls.Count-1 do
       begin
            Ball := balls[i];
            for j := 0 to bricks.Count-1 do
            begin
                 Brick := bricks[j];
                 Brick.Collide(Ball);
            end;
            Ball.Coords.X := Ball.Coords.X+round(Ball.XSpeed);
            Ball.Coords.Y := Ball.Coords.Y+round(Ball.YSpeed);
            lblScore.Caption := 'y: ' + FloatToStr(Ball.YSpeed);
       end;

  end;

  end.
Delphi-Quellcode:
unit uBrick;

interface

uses uCoords, uBall;

const
  BrickHeight = 32;
  BrickWidth = 64;

type
  TBrick = Class

  private
    fCoords : TCoords;

  public
    Constructor Create(coords: TCoords);
    Destructor Destroy(); override;
    property Coords: TCoords read fCoords;
    procedure Collide(Ball: TBall);
    procedure onCollision(Edge: Integer; Ball: TBall);
    function RightX : Integer;
    function BottomY : Integer;

end;

implementation

  constructor TBrick.Create(coords: TCoords);
  begin
       self.fCoords := coords;
  end;

  destructor TBrick.Destroy;
  begin
       fCoords.Destroy();
       inherited;
  end;

  function TBrick.RightX() : Integer;
  begin
       Result := (self.fCoords.X + BrickWidth);
  end;

  function TBrick.BottomY() : Integer;
  begin
       Result := (self.fCoords.Y + BrickHeight);
  end;

  procedure TBrick.Collide(Ball: TBall);
  begin
       if (Ball.Coords.X <= self.RightX()) and (Ball.Coords.X >= self.fCoords.X) and (Ball.Coords.Y = self.BottomY()) then
       begin
            OnCollision(1,Ball);
       end
       else if (Ball.Coords.X <= self.RightX()) and (Ball.Coords.X >= self.fCoords.X) and (Ball.Coords.Y = self.fCoords.Y) then
       begin
            OnCollision(2,Ball);
       end
       else if (Ball.Coords.Y <= self.BottomY()) and (Ball.Coords.Y >= self.fCoords.Y) and (Ball.Coords.X = self.fCoords.X) then
       begin
            OnCollision(3,Ball);
       end
       else if (Ball.Coords.Y <= self.BottomY()) and (Ball.Coords.Y >= self.fCoords.Y) and (Ball.Coords.X = self.RightX()) then
       begin
            OnCollision(4,Ball);
       end;
  end;

  procedure TBrick.onCollision(Edge : Integer; Ball : TBall);
  begin
       case Edge of
           1: Ball.YSpeed := Ball.YSpeed * (-1);
           2: Ball.YSpeed := Ball.YSpeed * (-1);
           3: Ball.XSpeed := Ball.XSpeed * (-1);
           4: Ball.XSpeed := Ball.XSpeed * (-1);
       end;
  end;

end.
Delphi-Quellcode:
unit uBall;

interface

uses uCoords, uCalculate;

const
  BallRadius = 8;

type
  TBall = Class

  private
    fCoords : TCoords;
    fDestCoords : TCoords;
    fAngle : Integer;
    fXSpeed : Single;
    fYSpeed : Single;

  public
    Constructor Create(coords: TCoords; angle: Integer; xspeed, yspeed : Single);
    Destructor Destroy(); override;
    property Coords: TCoords read fCoords;
    property DestCoords: TCoords read fDestCoords;
    property XSpeed: Single read fXSpeed write fXSpeed;
    property YSpeed: Single read fYSpeed write fYSpeed;

end;

implementation

  constructor TBall.Create(coords: TCoords; angle : Integer; xspeed, yspeed : Single);
  begin
       self.fCoords := coords;
       self.fAngle := angle;
       self.fXSpeed := xspeed;
       self.fYSpeed := yspeed;
  end;

  destructor TBall.Destroy;
  begin
       fCoords.Destroy();
       inherited;
  end;

end.
Delphi-Quellcode:
unit uCoords;

interface

type
  TCoords = Class

  private
    fx : Integer;
    fy : Integer;

  public
    Constructor Create(x,y: Integer);
    Destructor Destroy(); override;
    property X: Integer read fx write fx;
    property Y: Integer read fy write fy;

end;

implementation

  constructor TCoords.Create(x,y: Integer);
  begin
       self.fx := x;
       self.fy := y;
  end;

  destructor TCoords.Destroy;
  begin
       inherited;
  end;

end.
  Mit Zitat antworten Zitat