iphonelazext: cleanup the code of iarkanoid

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@1143 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
skalogryz
2010-01-26 06:51:09 +00:00
parent 7d79873fce
commit e0f1a16a8b
3 changed files with 176 additions and 51 deletions

View File

@ -29,7 +29,6 @@ type
TBrick = record
bounds : TRect;
hitRect : TRect;
health : Integer;
flash : Integer;
end;
@ -39,20 +38,19 @@ type
TArkanoid = class(TObject)
private
fPaddleX : single;
procedure SetPaddleX(const AValue: single);
protected
procedure BoundsBack(const HitRect: TRect; var dx, dy: single);
procedure BallMove(var move: TGameItem; atime: single);
procedure ResetPaddle;
procedure SetPaddleX(const AValue: single);
public
bricks : array [0..99] of TBrick;
brickCount : Integer;
balls : array [0..9] of TGameItem;
ballcount : Integer;
width : Integer;
height : Integer;
PaddleY : single;
PaddleW : single;
balls : array [0..9] of TGameItem;
ballcount : Integer;
width : Integer;
height : Integer;
PaddleY : single;
PaddleW : single;
constructor Create(awidth, aheight: Integer);
procedure InitBricks;
@ -95,15 +93,6 @@ begin
fPaddleX := AValue;
end;
procedure TArkanoid.BoundsBack(const HitRect: TRect; var dx, dy: single);
var
my : Integer;
begin
my := HitRect.Top + (HitRect.Bottom - HitRect.Top) div 2;
if abs(my - dy)>5 then dy := -dy
else dx := -dx;
end;
procedure InitMoveRecord(sx, sy, dx, dy: single; var r: TMoveRecord);
var
diff : single;
@ -114,7 +103,6 @@ begin
r.dy:=dy;
diff:=dx-sx;
//todo:
if diff<>0 then begin
r.k:=(dy-sy) / diff;
if r.k<>0 then r.divk:=1/r.k
@ -200,11 +188,12 @@ var
hitidx : Integer;
hsides : THitSides;
hitsides : THitSides;
hlen : single ;
hlen : single;
hitlen : single;
hpnt : TPoint;
hitpnt : TPoint;
mr : TMoveRecord;
br : TRect;
begin
with move do begin
if sticked then Exit;
@ -252,23 +241,23 @@ begin
dec(bricks[hitidx].health);
end;
if (cy >= PaddleY-5) and (cx >= PaddleX) and (cx <= PaddleX + PaddleW) and (dy>0) then begin
t := (cx - PaddleX) / PaddleW;
br:=Bounds( Round(PaddleX), Round(PaddleY), Round(PaddleW), 2);
if HitRect(br, mr, hpnt, hsides) then begin
t := (hpnt.x - PaddleX) / PaddleW;
t:=pi*(1-(t*0.8+0.1));
dx := cos(t)*ballVelocity;
dy := -sin(t)*ballVelocity;
cy := PaddleY-5;
cy := hpnt.y;
cx := hpnt.x;
end else if cy > height then begin
ResetPaddle;
Exit;
end;
x:=cx;
y:=cy;
end;
end;
procedure TArkanoid.ResetPaddle;
var
i : integer;
@ -297,14 +286,6 @@ begin
height := aheight;
end;
procedure MakeHitRect(const boundsRect: TRect; var hitRect: TRect);
begin
hitRect.left := boundsRect.left - 3;
hitRect.Top := boundsRect.top - 3;
hitRect.right := boundsRect.right + 3;
hitRect.bottom:= boundsRect.bottom+ 3;
end;
procedure TArkanoid.InitBricks;
var
i : Integer;
@ -319,7 +300,6 @@ begin
for i:=0 to brickCount - 1 do begin
bricks[i].health := random(4)+1;
MakeHitRect(bricks[i].bounds, bricks[i].hitRect);
bricks[i].flash:=0;
end;
end;

View File

@ -11,9 +11,10 @@
<MainUnit Value="0"/>
<TargetFileExt Value=""/>
<Title Value="iArkanoid"/>
<UseAppBundle Value="False"/>
<UseXPManifest Value="True"/>
<Icon Value="0"/>
<ActiveEditorIndexAtStart Value="0"/>
<ActiveEditorIndexAtStart Value="1"/>
</General>
<VersionInfo>
<ProjectVersion Value=""/>
@ -35,27 +36,35 @@
<RunParams>
<local>
<FormatVersion Value="1"/>
<LaunchingApplication PathPlusParams="/usr/X11R6/bin/xterm -T 'Lazarus Run Output' -e $(LazarusDir)/tools/runwait.sh $(TargetCmdLine)"/>
<LaunchingApplication Use="True" PathPlusParams="'/Developer/Platforms/iPhoneSimulator.platform/Developer/Applications/iPhone Simulator.app/Contents/MacOS/iPhone Simulator'"/>
</local>
</RunParams>
<Units Count="3">
<Units Count="5">
<Unit0>
<Filename Value="iArk.pas"/>
<IsPartOfProject Value="True"/>
<UnitName Value="iArk"/>
<CursorPos X="17" Y="96"/>
<TopLine Value="94"/>
<CursorPos X="29" Y="279"/>
<TopLine Value="262"/>
<EditorIndex Value="0"/>
<UsageCount Value="20"/>
<UsageCount Value="30"/>
<Bookmarks Count="1">
<Item0 X="1" Y="79" ID="2"/>
</Bookmarks>
<Loaded Value="True"/>
<SyntaxHighlighter Value="Delphi"/>
</Unit0>
<Unit1>
<Filename Value="gameengine.pas"/>
<UnitName Value="gameengine"/>
<CursorPos X="1" Y="1"/>
<TopLine Value="1"/>
<UsageCount Value="10"/>
<CursorPos X="1" Y="50"/>
<TopLine Value="35"/>
<EditorIndex Value="1"/>
<UsageCount Value="16"/>
<Bookmarks Count="1">
<Item0 X="1" Y="197" ID="3"/>
</Bookmarks>
<Loaded Value="True"/>
<SyntaxHighlighter Value="Delphi"/>
</Unit1>
<Unit2>
@ -66,10 +75,143 @@
<UnitName Value="project_iphone_options"/>
<CursorPos X="17" Y="82"/>
<TopLine Value="67"/>
<UsageCount Value="10"/>
<UsageCount Value="9"/>
</Unit2>
<Unit3>
<Filename Value="../../../../fpc/rtl/objpas/sysutils/sysstrh.inc"/>
<CursorPos X="40" Y="33"/>
<TopLine Value="18"/>
<UsageCount Value="14"/>
</Unit3>
<Unit4>
<Filename Value="../../../../fpc/packages/cocoaint/src/uikit/UIScreen.inc"/>
<CursorPos X="14" Y="51"/>
<TopLine Value="37"/>
<UsageCount Value="10"/>
</Unit4>
</Units>
<JumpHistory Count="0" HistoryIndex="-1"/>
<JumpHistory Count="30" HistoryIndex="29">
<Position1>
<Filename Value="gameengine.pas"/>
<Caret Line="256" Column="19" TopLine="248"/>
</Position1>
<Position2>
<Filename Value="gameengine.pas"/>
<Caret Line="63" Column="14" TopLine="48"/>
</Position2>
<Position3>
<Filename Value="gameengine.pas"/>
<Caret Line="294" Column="1" TopLine="264"/>
</Position3>
<Position4>
<Filename Value="gameengine.pas"/>
<Caret Line="1" Column="1" TopLine="1"/>
</Position4>
<Position5>
<Filename Value="gameengine.pas"/>
<Caret Line="41" Column="13" TopLine="26"/>
</Position5>
<Position6>
<Filename Value="gameengine.pas"/>
<Caret Line="42" Column="25" TopLine="26"/>
</Position6>
<Position7>
<Filename Value="gameengine.pas"/>
<Caret Line="63" Column="60" TopLine="48"/>
</Position7>
<Position8>
<Filename Value="gameengine.pas"/>
<Caret Line="85" Column="31" TopLine="70"/>
</Position8>
<Position9>
<Filename Value="gameengine.pas"/>
<Caret Line="90" Column="17" TopLine="70"/>
</Position9>
<Position10>
<Filename Value="gameengine.pas"/>
<Caret Line="95" Column="11" TopLine="70"/>
</Position10>
<Position11>
<Filename Value="gameengine.pas"/>
<Caret Line="263" Column="19" TopLine="241"/>
</Position11>
<Position12>
<Filename Value="iArk.pas"/>
<Caret Line="177" Column="24" TopLine="172"/>
</Position12>
<Position13>
<Filename Value="iArk.pas"/>
<Caret Line="170" Column="27" TopLine="146"/>
</Position13>
<Position14>
<Filename Value="iArk.pas"/>
<Caret Line="173" Column="67" TopLine="158"/>
</Position14>
<Position15>
<Filename Value="gameengine.pas"/>
<Caret Line="256" Column="55" TopLine="242"/>
</Position15>
<Position16>
<Filename Value="gameengine.pas"/>
<Caret Line="1" Column="1" TopLine="1"/>
</Position16>
<Position17>
<Filename Value="gameengine.pas"/>
<Caret Line="44" Column="1" TopLine="29"/>
</Position17>
<Position18>
<Filename Value="gameengine.pas"/>
<Caret Line="293" Column="22" TopLine="273"/>
</Position18>
<Position19>
<Filename Value="gameengine.pas"/>
<Caret Line="1" Column="1" TopLine="1"/>
</Position19>
<Position20>
<Filename Value="gameengine.pas"/>
<Caret Line="293" Column="22" TopLine="278"/>
</Position20>
<Position21>
<Filename Value="gameengine.pas"/>
<Caret Line="315" Column="1" TopLine="300"/>
</Position21>
<Position22>
<Filename Value="gameengine.pas"/>
<Caret Line="1" Column="1" TopLine="1"/>
</Position22>
<Position23>
<Filename Value="gameengine.pas"/>
<Caret Line="293" Column="1" TopLine="278"/>
</Position23>
<Position24>
<Filename Value="iArk.pas"/>
<Caret Line="171" Column="34" TopLine="265"/>
</Position24>
<Position25>
<Filename Value="iArk.pas"/>
<Caret Line="1" Column="1" TopLine="1"/>
</Position25>
<Position26>
<Filename Value="iArk.pas"/>
<Caret Line="129" Column="1" TopLine="110"/>
</Position26>
<Position27>
<Filename Value="iArk.pas"/>
<Caret Line="1" Column="1" TopLine="1"/>
</Position27>
<Position28>
<Filename Value="gameengine.pas"/>
<Caret Line="1" Column="1" TopLine="1"/>
</Position28>
<Position29>
<Filename Value="gameengine.pas"/>
<Caret Line="46" Column="1" TopLine="34"/>
</Position29>
<Position30>
<Filename Value="gameengine.pas"/>
<Caret Line="51" Column="13" TopLine="31"/>
</Position30>
</JumpHistory>
</ProjectOptions>
<CompilerOptions>
<Version Value="8"/>

View File

@ -160,13 +160,18 @@ end;
function isCoordNearPabble(game: TArkanoid; x, y: single; Border: Integer): Boolean;
var
p : TPoint;
r : TRect;
p : TPoint;
r : TRect;
px,py : Integer;
pw : Integer;
begin
p.x := Round(x);
p.y := Round(y);
r := Bounds(Round(game.PaddleX) - Border, Round(game.PaddleY) - Border, Round(game.PaddleW)+Border*2, PaddleHeight + Border*2);
Result := PtInRect(r, p);
px:=Round(game.PaddleX)-Border;
pw:=Round(game.PaddleW)+Border*2;
py:=Round(game.PaddleY)-Border;
r:=Rect(px, py, px+pw, Round(UIScreen.mainScreen.bounds.size.height));
Result:=PtInRect(r, p);
end;
procedure MyMainWindow.touchesBegan_withEvent(touches: NSSetPointer; event: UIEvent);
@ -271,8 +276,6 @@ var
pool : NSAutoreleasePool;
retVal : Integer;
{$R iArk.res}
begin
randomize;
pool := NSAutoreleasePool(NSAutoreleasePool.alloc).init;