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: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200:
| unit UBall2;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls ;
type TFSpielfeld = class(TForm) IBild: TImage; TTakt: TTimer; procedure FormCreate(Sender: TObject); procedure TTaktTimer(Sender: TObject);
private
public end;
tBall = class x,y,vx,vy,r,winkel: single; farbe :tColor;
Procedure Init(farbeNeu:tColor; xNeu,yNeu, vxNeu, vyNeu, rNeu,winkelNeu :single); Procedure ZeigeDich(canvas:TCanvas);
Procedure BewegeDich(canvas:TCanvas; IBild:tImage);virtual; end; var FSpielfeld: TFSpielfeld; winkel : single; BallNum,i : integer; Ball : array [1..20] of TBall ; implementation {$R *.dfm}
Procedure tBall.Init(farbeNeu:tColor; xNeu,yNeu, vxNeu, vyNeu, rNeu, winkelNeu:single); begin farbe:=farbeNeu; r := rNeu; x := XNeu; y := yNeu; vx := cos(winkelNeu * Pi / 180) * vxNeu; vy := sin(winkelNeu * Pi / 180) * vyNeu; winkel := winkelNeu;
end;
Procedure tBall.ZeigeDich(canvas:TCanvas); begin
begin canvas.brush.color:= Farbe; canvas.Ellipse(Round(x-r), Round(y-r), Round(x+r), Round(y+r)); end; end;
Procedure tBall.BewegeDich(canvas:TCanvas;IBild:tImage); var stossx, stossy, impulsex, impulsey, stoss_geschw: Double ; var i,j : integer; begin
for i := 1 to BallNum do begin
With canvas do begin Ball[i].x := Ball[i].x + Ball[i].vx; Ball[i].y := Ball[i].y + Ball[i].vy; if Ball[i].x < Ball[i].r + 25 then Ball[i].vx := -Ball[i].vx; if Ball[i].x > IBild.Width -25 - Ball[i].r then Ball[i].vx := -Ball[i].vx; if Ball[i].y < Ball[i].r + 25 then Ball[i].vy := -Ball[i].vy; if Ball[i].y > IBild.Height -25 - Ball[i].r then Ball[i].vy := -Ball[i].vy; Ball[i].vx := Ball[i].vx - Ball[i].vx * 0.001; Ball[i].vy := Ball[i].vy - Ball[i].vy * 0.001; for j := 1 to BallNum do begin if i <> j then begin if sqr(Ball[j].x - Ball[i].x) + sqr(Ball[j].y - Ball[i].y) < sqr(Ball[i].r + Ball[j].r) then begin stossx := Ball[j].vx - Ball[i].vx; stossy := Ball[j].vy - Ball[i].vy; impulsex := (Ball[j].x - Ball[i].x) / sqrt(sqr(Ball[j].x - Ball[i].x) + sqr(Ball[j].y - Ball[i].y)); impulsey := (Ball[j].y - Ball[i].y) / sqrt(sqr(Ball[j].x - Ball[i].x) + sqr(Ball[j].y - Ball[i].y)); Stoss_geschw := (stossx * impulsex) + (stossy * impulsey); impulsex := impulsex * Stoss_geschw * (Ball[i].r + Ball[j].r) / 2; impulsey := impulsey * Stoss_geschw * (Ball[i].r + Ball[j].r) / 2; Ball[i].vx := Ball[i].vx + impulsex / Ball[i].r; Ball[i].vy := Ball[i].vy + impulsey / Ball[i].r; Ball[j].vx := Ball[j].vx - impulsex / Ball[j].r; Ball[j].vy := Ball[j].vy - impulsey / Ball[j].r;
end; end; end; end; end; end;
procedure TFSpielfeld.TTaktTimer(Sender: TObject); var i : integer; begin for i:= 1 to Ballnum do begin
Ball[i].BewegeDich(IBild.canvas,FSpielfeld.IBild); Ball[i].ZeigeDich(IBild.Canvas);
end; end;
procedure TFSpielfeld.FormCreate(Sender: TObject); var r: integer; posx_8, posy_8 : single; begin posx_8 := 500 ; posy_8 := 200 ; r:= 15 ; BallNum := 16; DoubleBuffered:= true;
With IBild.Canvas do begin Pen.width := 50; Brush.Color:= clgreen; Rectangle(0,0, IBild.Width, IBild.Height); Pen.width := 1;
end; begin
Ball[1] := TBall.Create; Ball[1].init(clwhite,(100),(200), (2), (0), r,Random(360));
Ball[2] := TBall.Create; Ball[2].init(clblack,(posx_8),(posy_8), (0), (0), r,Random(360));
Ball[3] := TBall.Create; Ball[3].init(clyellow,(posx_8),(posy_8+(2*r)+1), (0), (0), r,Random(360));
Ball[4] := TBall.Create; Ball[4].init(clblue,(posx_8),(posy_8-(2*r)-1), (0), (0), r,Random(360));
Ball[5] := TBall.Create; Ball[5].init(clred,(posx_8-(2*r)),(posy_8-r-1), (0), (0), r,Random(360));
Ball[6] := TBall.Create; Ball[6].init(clpurple,(posx_8-(2*r)),(posy_8+r+1), (0), (0), r,Random(360));
Ball[7] := TBall.Create; Ball[7].init(clcream,(posx_8-(4*r)),(posy_8), (0), (0), r,Random(360));
Ball[8] := TBall.Create; Ball[8].init(clnavy,(posx_8+(2*r)),(posy_8+r+1), (0), (0), r,Random(360));
Ball[9] := TBall.Create; Ball[9].init(clmaroon,(posx_8+(2*r)),(posy_8-r-1), (0), (0), r,Random(360));
Ball[10] := TBall.Create; Ball[10].init(clolive,(posx_8+(2*r)),(posy_8-(3*r)-1.5), (0), (0), r,Random(360));
Ball[11] := TBall.Create; Ball[11].init(cllime,(posx_8+(2*r)),(posy_8+(3*r)+1.5), (0), (0), r,Random(360));
Ball[12] := TBall.Create; Ball[12].init(clteal,(posx_8+(4*r)),(posy_8), (0), (0), r,Random(360));
Ball[13] := TBall.Create; Ball[13].init(claqua,(posx_8+(4*r)),(posy_8+(2*r)+1), (0), (0), r,Random(360));
Ball[14] := TBall.Create; Ball[14].init(clgreen,(posx_8+(4*r)),(posy_8-(2*r)-1), (0), (0), r,Random(360));
Ball[15] := TBall.Create; Ball[15].init(clgray,(posx_8+(4*r)),(posy_8-(4*r)-2), (0), (0), r,Random(360));
Ball[16] := TBall.Create; Ball[16].init(clskyblue,(posx_8+(4*r)),(posy_8+(4*r)+2), (0), (0), r,Random(360));
end; end;
Initialization Randomize; end. |