dont stop
пишу прогу соударение шаров.
аррр.. возникла еще проблема..
точнее 3..
1)не могу сделать так чтобы количество шаров вводилось при запуске программы.
в коде выдает ошибку.
2)прописала столкновение шаров.ошибку не выдает, но почему-то не работает_)
3)1 шар летает в форме, а потом куда-то улетает
код:
читать дальшеunit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls;
const
MaxBalls=10;
Colors: array[0..4] of TColor = (clRed, clBlue, clLime, clYellow, clFuchsia);
type
TDirection=(dLeft,dUpleft,dupright,dright,
ddownright,ddownleft,ddown,dup);
TBall=record
Button1: TButton;
id:byte;
x,y,x1,y1:integer;
size:byte;
Direction:TDirection;
Speed:byte;
Color:TColor;
BorderColor:TColor;
end;
TForm1 = class(TForm)
Paint: TButton;
Timer1: TTimer;
PaintBox1: TPaintBox;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
procedure Timer1Timer(Sender: TObject);
procedure PaintClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
procedure DrawBalls;
{ Private declarations }
public
{ Public declarations }
balls:array[1..MaxBalls] of TBall;
end;
var
Form1: TForm1;
MyBMP:TBitmap;
ball:TBall;
map_width, map_height: word;
j,i:integer;
implementation
{$R *.dfm}
procedure TForm1.DrawBalls;
var
i: integer;
begin
with MyBMP.Canvas do
begin
Brush.Color:=clWhite;
FillRect(ClipRect);
end;
for I := 1 to MaxBalls do
with balls[i] do
begin
MyBMP.Canvas.Pen.Color:=BorderColor;
MyBMP.Canvas.Brush.Color:=Color;
MyBMP.Canvas.ellipse(x-size,y-size,x+size,y+size);
end;
PaintBox1.Canvas.Draw(0,0,MyBMP);
end;
procedure TForm1.PaintClick(Sender: TObject);
var
i: integer;
//создаем шары
begin
randomize;
for I := 1 to MaxBalls do
with balls[i] do begin
id:=i;
size:=StrToint(Edit2.Text);
direction:=TDirection (random(8) );
speed:=StrToint(Edit3.Text);
color:=colors[Random(5)];
BorderColor:=clBlack;
x:=random(471)+10;
y:=random(471)+10;
x1:=x+40;
y1:=y+40;
end;
Timer1.Enabled:=true;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var
i: integer;
begin
for i:=1 to MaxBalls do
begin
ball := balls[i];
case Ball.Direction of
dLeft: with ball do x:=x-Speed;
dUpleft: with ball do begin
x:=x-Speed;
y:=y-Speed;
end;
dupright: with ball do begin
x:=x+Speed;
y:=y-Speed;
end;
dright: with ball do x:=x+Speed;
ddownright: with ball do begin
x:=x+Speed;
y:=y+Speed;
end;
ddownleft: with ball do begin
x:=x-Speed;
y:=y+Speed;
end;
ddown: with ball do y:=y+Speed;
dup: with ball do y:=y-Speed;
end;
if ball.x<=10 then
begin
if( Ball.Direction = dLeft ) Then Ball.Direction:= dright;
if( Ball.Direction = dUpleft ) Then Ball.Direction:= dupright;
if( Ball.Direction = ddownleft ) Then Ball.Direction:= ddownright;
end;
if ball.y<=10 then
begin
if( Ball.Direction = dUpleft ) Then Ball.Direction:= ddownleft;
if( Ball.Direction = dup ) Then Ball.Direction:= ddown;
if( Ball.Direction = dupright ) Then Ball.Direction:= ddownright;
end;
if ball.x >= (PaintBox1.Width-13) then
begin
if( Ball.Direction = dright ) Then Ball.Direction:= dLeft;
if( Ball.Direction = ddownright ) Then Ball.Direction:= ddownleft;
if( Ball.Direction = dupright ) Then Ball.Direction:=dUpleft;
end;
if ball.y >= (PaintBox1.Height-12) then
begin
if( Ball.Direction = ddown ) Then Ball.Direction:= dup;
if( Ball.Direction = ddownleft ) Then Ball.Direction:= dUpleft;
if( Ball.Direction = ddownright ) Then Ball.Direction:= dupright;
end;
balls[i]:=ball;
end;
//столкновение шаров
for j :=1 to Maxballs do begin
if i=j then continue;
if sqrt(sqr(ball.x-balls[j].x)+sqr(ball.y-balls[j].y))<=2*balls[j].size+ball.speed
then
begin
if Ball.Direction = dLeft then Ball.Direction:= dright;
if Ball.Direction = dright then Ball.Direction:= dLeft;
if Ball.Direction = dUpleft then Ball.Direction:= ddownright;
if Ball.Direction = ddownright then Ball.Direction:= dUpleft;
if Ball.Direction = dupright then Ball.Direction:= ddownleft;
if Ball.Direction= ddownleft then Ball.Direction:= dupright;
if Ball.Direction = ddown then Ball.Direction:= dup;
if Ball.Direction = dup then Ball.Direction:= ddown;
if Balls[j].Direction = dLeft then Balls[j].Direction:= dright;
if Balls[j].Direction = dright then Balls[j].Direction:= dLeft;
if Balls[j].Direction = dUpleft then Balls[j].Direction:= ddownright;
if Balls[j].Direction = ddownright then Balls[j].Direction:= dUpleft;
if Balls[j].Direction = dupright then Balls[j].Direction:= ddownleft;
if Balls[j].Direction = ddownleft then Balls[j].Direction:= dupright;
if Balls[j].Direction = ddown then Balls[j].Direction:= dup;
if Balls[j].Direction = dup then Balls[j].Direction:= ddown;
DrawBalls;
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
MyBMP:=TBitmap.Create;
MyBMP.Width:=500;
MyBMP.Height:=500;
MyBMP.Canvas.Pen.Color:=clRed;
MyBMP.Canvas.Brush.Color:=clRed;
map_width:=500;
map_height:=500;
PaintBox1.Width:=map_width;
PaintBox1.Height:=map_height;
end;
end.
аррр.. возникла еще проблема..
точнее 3..
1)не могу сделать так чтобы количество шаров вводилось при запуске программы.
в коде выдает ошибку.
2)прописала столкновение шаров.ошибку не выдает, но почему-то не работает_)
3)1 шар летает в форме, а потом куда-то улетает
код:
читать дальшеunit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls;
const
MaxBalls=10;
Colors: array[0..4] of TColor = (clRed, clBlue, clLime, clYellow, clFuchsia);
type
TDirection=(dLeft,dUpleft,dupright,dright,
ddownright,ddownleft,ddown,dup);
TBall=record
Button1: TButton;
id:byte;
x,y,x1,y1:integer;
size:byte;
Direction:TDirection;
Speed:byte;
Color:TColor;
BorderColor:TColor;
end;
TForm1 = class(TForm)
Paint: TButton;
Timer1: TTimer;
PaintBox1: TPaintBox;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
procedure Timer1Timer(Sender: TObject);
procedure PaintClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
procedure DrawBalls;
{ Private declarations }
public
{ Public declarations }
balls:array[1..MaxBalls] of TBall;
end;
var
Form1: TForm1;
MyBMP:TBitmap;
ball:TBall;
map_width, map_height: word;
j,i:integer;
implementation
{$R *.dfm}
procedure TForm1.DrawBalls;
var
i: integer;
begin
with MyBMP.Canvas do
begin
Brush.Color:=clWhite;
FillRect(ClipRect);
end;
for I := 1 to MaxBalls do
with balls[i] do
begin
MyBMP.Canvas.Pen.Color:=BorderColor;
MyBMP.Canvas.Brush.Color:=Color;
MyBMP.Canvas.ellipse(x-size,y-size,x+size,y+size);
end;
PaintBox1.Canvas.Draw(0,0,MyBMP);
end;
procedure TForm1.PaintClick(Sender: TObject);
var
i: integer;
//создаем шары
begin
randomize;
for I := 1 to MaxBalls do
with balls[i] do begin
id:=i;
size:=StrToint(Edit2.Text);
direction:=TDirection (random(8) );
speed:=StrToint(Edit3.Text);
color:=colors[Random(5)];
BorderColor:=clBlack;
x:=random(471)+10;
y:=random(471)+10;
x1:=x+40;
y1:=y+40;
end;
Timer1.Enabled:=true;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var
i: integer;
begin
for i:=1 to MaxBalls do
begin
ball := balls[i];
case Ball.Direction of
dLeft: with ball do x:=x-Speed;
dUpleft: with ball do begin
x:=x-Speed;
y:=y-Speed;
end;
dupright: with ball do begin
x:=x+Speed;
y:=y-Speed;
end;
dright: with ball do x:=x+Speed;
ddownright: with ball do begin
x:=x+Speed;
y:=y+Speed;
end;
ddownleft: with ball do begin
x:=x-Speed;
y:=y+Speed;
end;
ddown: with ball do y:=y+Speed;
dup: with ball do y:=y-Speed;
end;
if ball.x<=10 then
begin
if( Ball.Direction = dLeft ) Then Ball.Direction:= dright;
if( Ball.Direction = dUpleft ) Then Ball.Direction:= dupright;
if( Ball.Direction = ddownleft ) Then Ball.Direction:= ddownright;
end;
if ball.y<=10 then
begin
if( Ball.Direction = dUpleft ) Then Ball.Direction:= ddownleft;
if( Ball.Direction = dup ) Then Ball.Direction:= ddown;
if( Ball.Direction = dupright ) Then Ball.Direction:= ddownright;
end;
if ball.x >= (PaintBox1.Width-13) then
begin
if( Ball.Direction = dright ) Then Ball.Direction:= dLeft;
if( Ball.Direction = ddownright ) Then Ball.Direction:= ddownleft;
if( Ball.Direction = dupright ) Then Ball.Direction:=dUpleft;
end;
if ball.y >= (PaintBox1.Height-12) then
begin
if( Ball.Direction = ddown ) Then Ball.Direction:= dup;
if( Ball.Direction = ddownleft ) Then Ball.Direction:= dUpleft;
if( Ball.Direction = ddownright ) Then Ball.Direction:= dupright;
end;
balls[i]:=ball;
end;
//столкновение шаров
for j :=1 to Maxballs do begin
if i=j then continue;
if sqrt(sqr(ball.x-balls[j].x)+sqr(ball.y-balls[j].y))<=2*balls[j].size+ball.speed
then
begin
if Ball.Direction = dLeft then Ball.Direction:= dright;
if Ball.Direction = dright then Ball.Direction:= dLeft;
if Ball.Direction = dUpleft then Ball.Direction:= ddownright;
if Ball.Direction = ddownright then Ball.Direction:= dUpleft;
if Ball.Direction = dupright then Ball.Direction:= ddownleft;
if Ball.Direction= ddownleft then Ball.Direction:= dupright;
if Ball.Direction = ddown then Ball.Direction:= dup;
if Ball.Direction = dup then Ball.Direction:= ddown;
if Balls[j].Direction = dLeft then Balls[j].Direction:= dright;
if Balls[j].Direction = dright then Balls[j].Direction:= dLeft;
if Balls[j].Direction = dUpleft then Balls[j].Direction:= ddownright;
if Balls[j].Direction = ddownright then Balls[j].Direction:= dUpleft;
if Balls[j].Direction = dupright then Balls[j].Direction:= ddownleft;
if Balls[j].Direction = ddownleft then Balls[j].Direction:= dupright;
if Balls[j].Direction = ddown then Balls[j].Direction:= dup;
if Balls[j].Direction = dup then Balls[j].Direction:= ddown;
DrawBalls;
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
MyBMP:=TBitmap.Create;
MyBMP.Width:=500;
MyBMP.Height:=500;
MyBMP.Canvas.Pen.Color:=clRed;
MyBMP.Canvas.Brush.Color:=clRed;
map_width:=500;
map_height:=500;
PaintBox1.Width:=map_width;
PaintBox1.Height:=map_height;
end;
end.
столкновение шаров.. там сравнивается последний шар со всеми остальными.. то есть только он и будет сталкиваться, можно ещё цикл добавить ^^
а то, что из формы вылетает.. не знаю даже, ну например вот "if ball.x<=10 then" откуда эта 10? попробуйте поставить сумму size и speed
ну вот как-то так )