delphi - Bouncing Ball Game -
working on school project , need please... have built game purpose avoid primary ball crashing random balls generate every 30 seconds. problem random balls not staying within frame , not sure doing wrong. appreciated. thank you
procedure tfrmgamepage.enemyball(shpenemy: tshape); var boutside, babove, bbelow, bfarleft, bfarright : boolean; ixmove, iymove, iydirec{negative = increase, positive = decrease}, ixdirec{positive = increase, negative = decrease} : integer; begin babove := pnlarena.height-shpenemy.top > pnlarena.height; bbelow := pnlarena.height < shpenemy.top; bfarleft := pnlarena.width-shpenemy.left > pnlarena.width; bfarright := pnlarena.width < shpenemy.left; ixmove:=random(3)+1; iymove:=random(3)+1; ixdirec:=1; iydirec:=1; //check if shape outside. if babove=true or bbelow=true or bfarleft=true or bfarright=true begin boutside:=true; end else begin boutside:=false; end; // if shape outside swop relavent direction if boutside=true begin begin if babove=true begin iydirec:=1; end; if bbelow=true begin iydirec:=-1; end; if bfarright begin ixdirec:=-1; end; if bfarleft begin ixdirec:=1; end; end; end; shpenemy.top := shpenemy.top + iymove * iydirec; shpenemy.left := shpenemy.left + ixmove * ixdirec; // change pos of enemy shapes end;
you've made 2 main mistakes:
- you not calculating correctly conditions (
babove
,bbelow
etc.) responsible change of ball direction.
the important thing know here ball position relative parent (pnlarena
in case). explain in different words: ball doesn't know outside world, pnlarena
whole world ball. if window coordinate system origin top left far left of pnlarena
equals 0 (pnlarena.left = 0
) , far top 0 (pnlarena.top = 0
).
knowing guess ball cross left border of world when shpenemy.left < 0
. not go onto details other directions, try understand code i've provided.
- the second mistake more subtle.
shpenemy
not bounce correctly walls of arena. thingshpenemy
not remember last direction. need change direction when necessery, not each time.
here working example:
unit unit144; interface uses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, stdctrls, extctrls; // our shape needs remeber direction, otherwise won't move correctly. type tballshape = class(tshape) public xdirec, ydirec: integer; constructor create(aowner: tcomponent; aparent: twincontrol); reintroduce; end; type tform144 = class(tform) pnlarena: tpanel; button1: tbutton; procedure button1click(sender: tobject); procedure formcreate(sender: tobject); private ballshape: tballshape; public procedure enemyball(shpenemy: tballshape); end; var form144: tform144; implementation {$r *.dfm} { tform144 } procedure tform144.button1click(sender: tobject); begin enemyball(ballshape); end; procedure tform144.enemyball(shpenemy: tballshape); var boutside, babove, bbelow, bfarleft, bfarright : boolean; ixmove, iymove : integer; begin ixmove:=random(3)+1; iymove:=random(3)+1; babove := shpenemy.top < 0; bbelow := shpenemy.top + shpenemy.height > pnlarena.height; bfarleft := shpenemy.left < 0; bfarright := shpenemy.left + shpenemy.width > pnlarena.width; //check if shape outside. if babove or bbelow or bfarleft or bfarright begin boutside:=true; end else begin boutside:=false; end; // if shape outside swop relavent direction if boutside=true begin begin if babove=true begin shpenemy.ydirec:=1; end; if bbelow=true begin shpenemy.ydirec:=-1; end; if bfarright begin shpenemy.xdirec:=-1; end; if bfarleft begin shpenemy.xdirec:=1; end; end; end; shpenemy.top := shpenemy.top + iymove * shpenemy.ydirec; shpenemy.left := shpenemy.left + ixmove * shpenemy.xdirec; // change pos of enemy shapes end; procedure tform144.formcreate(sender: tobject); begin randomize; ballshape := tballshape.create(self, pnlarena); ballshape.shape := stcircle; end; { tballshape } constructor tballshape.create(aowner: tcomponent; aparent: twincontrol); var ldirection: integer; begin inherited create(aowner); width := 20; height := 20; // chose random direction of our ball. self.parent := aparent; ldirection := random(1); if ldirection = 0 xdirec := - 1 else xdirec := 1; ldirection := random(1); if ldirection = 0 ydirec := - 1 else ydirec := 1; // must place our ball somewhere on parent. left := random(aparent.width) - self.width; if left < 0 left := 0; top := random(aparent.height) - self.height; if top < 0 top := 0; end; end.
hope helps.
Comments
Post a Comment