full code
I imagined some people could look at it and learn
hopefully a knight will come to a rescue
// Project: pool2d
// Created: 2016-04-08
//#include "types.agc"
//#include "screen.agc"
// set window properties
type vec2
x as float
y as float
endtype
type vec3
x as float
y as float
z as float
endtype
type vec4
x as float
y as float
z as float
a as float
endtype
type ent
id as integer
img as integer
phys as integer
pos as vec3
color as vec4
size as vec3
endtype
type ActionTimer
time as integer
delay as integer
endtype
SetupScreen("Pool 2d")
SetPrintSize(26)
SetPhysicsGravity(0,0)
SetPhysicsScale( 0.02)
LoadContent()
global Timer1 as integer
global HitTimer as ActionTimer
HitTimer.delay = 25
global dim Balls[100] as ent
global dim BallHoles[6] as ent
global dim Ents[100] as ent
global Stick as ent
global CurrentBall as integer
global CurrentEnt as integer
CreateTable(VW/2,VH/2,VW/4)
CreateStick()
HitSeq = 0
SetViewZoomMode(1)
SetPhysicsWallBottom(0)
SetPhysicsWallLeft(0)
SetPhysicsWallRight(0)
SetPhysicsWallTop(0)
SetPhysicsDebugOn()
firstHit = 1
do
Timer1 = GetMilliseconds()
if GetpointerPressed() = 1
SetSpritePhysicsImpulse(stick.id,GetSpriteXByOffset(stick.id),GetSpriteYByOffset(stick.id),0,-1000)
hitSeq = 1
endif
if hitSeq > 0
if GetSpriteFirstContact( stick.id )
HitTimer.time = Timer1
SetSpritePhysicsImpulse(stick.id,GetSpriteXByOffset(stick.id),GetSpriteYByOffset(stick.id),0,500)
hitSeq = -1
endif
endif
//for i = 1 to CurrentBall
//if GetSpriteFirstContact(balls[i].id)
//SetSpritePhysicsImpulse(balls[i].id,GetSpriteXByOFfset(balls[i].id),GetspriteYByOffset(balls[i].id),-10 + random(0,20),-10 + random(0,20))
//endif
//next i
Print( ScreenFPS() )
StepPhysics(1)
Sync()
loop
function CreateBall(x as float,y as float,size as float,colR as integer,colG as integer,colB as integer)
inc CurrentBall
Balls[CurrentBall].id = CreateSprite(BallHollowImg)
SetSpriteSize(Balls[CurrentBall].id,size,size)
SetSpritePositionByOffset(balls[currentball].id,x,y)
SetSpriteColor(balls[CurrentBall].id,ColR,ColG,ColB,255)
SetSpriteShape(balls[currentball].id,1)
SetSpritePhysicsOn(Balls[CurrentBall].id,2)
SetSpriteAngle(balls[CurrentBall].id,random(0,360))
SetSpritePhysicsMass(balls[CurrentBall].id,size/5)
//SetSpritePhysicsFriction(balls[currentBall].id,1)
SetSpritePhysicsRestitution(balls[currentBall].id,0.25)
endfunction
function CreateTable(x as float, y as float,Size as float)
inc CurrentEnt
// top
Ents[currentEnt].id = CreateSprite(0)
SetSpriteSize(Ents[CurrentEnt].id,Size,Size/200)
SetSpritePositionByOffset(Ents[CurrentEnt].id,x,y-Size*1.5/2)
SetSpritePhysicsOn(Ents[CurrentEnt].id,1)
SetSpriteGroup(ents[CurrentEnt].id,-1)
SetSpritePhysicsRestitution(ents[CurrentEnt].id,1)
// right
inc CurrentEnt
Ents[CurrentEnt].id = CreateSprite(0)
SetSpriteSize(ents[CurrentEnt].id,Size/200,Size*1.5)
SetSpritePositionByOffset(ents[CurrentEnt].id,x+size/2,y)
SetSpritePhysicsOn(Ents[CurrentEnt].id,1)
SetSpriteGroup(ents[CurrentEnt].id,-1)
SetSpritePhysicsRestitution(ents[CurrentEnt].id,1)
// bottom
inc CurrentEnt
Ents[CurrentEnt].id = CreateSprite(0)
SetSpriteSize(Ents[CurrentEnt].id,Size,Size/200)
SetSpritePositionByOffset(Ents[CurrentEnt].id,x,y+size*1.5/2)
SetSpritePhysicsOn(Ents[CurrentEnt].id,1)
SetSpriteGroup(ents[CurrentEnt].id,-1)
SetSpritePhysicsRestitution(ents[CurrentEnt].id,1)
// left
inc CurrentEnt
Ents[CurrentEnt].id = CreateSprite(0)
SetSpriteSize(ents[CurrentEnt].id,Size/200,Size*1.5)
SetSpritePositionByOffset(ents[CurrentEnt].id,x-size/2,y)
SetSpritePhysicsOn(Ents[CurrentEnt].id,1)
SetSpriteGroup(ents[CurrentEnt].id,-1)
SetSpritePhysicsRestitution(ents[CurrentEnt].id,1)
// Top Left Hole
inc CurrentEnt
Ents[CurrentEnt].id = CreateSprite(ArcImg)
SetSpriteSize(ents[CurrentEnt].id,size/7.5,size/7.5)
SetSpritePositionByOffset(ents[CurrentEnt].id,x-Size/2+GetSpriteWidth(Ents[CurrentEnt].id)/2,y-Size/2-GetSpriteWidth(ents[CurrentEnt].id)*1.33)
SetSpriteflip(ents[CurrentEnt].id,1,1)
// Top Right Hole
inc CurrentEnt
Ents[CurrentEnt].id = CreateSprite(ArcImg)
SetSpriteSize(ents[CurrentEnt].id,size/7.5,size/7.5)
SetSpritePositionByOffset(ents[CurrentEnt].id,x+Size/2-GetSpriteWidth(Ents[CurrentEnt].id)/2,y-Size/2-GetSpriteWidth(ents[CurrentEnt].id)*1.33)
SetSpriteflip(ents[CurrentEnt].id,0,1)
// Bottom Left
inc CurrentEnt
Ents[CurrentEnt].id = CreateSprite(ArcImg)
SetSpriteSize(ents[CurrentEnt].id,size/7.5,size/7.5)
SetSpritePositionByOffset(ents[CurrentEnt].id,x-Size/2+GetSpriteWidth(Ents[CurrentEnt].id)/2,y+Size/2+GetSpriteWidth(ents[CurrentEnt].id)*1.33)
SetSpriteflip(ents[CurrentEnt].id,1,0)
// bottom Right Hole
inc CurrentEnt
Ents[CurrentEnt].id = CreateSprite(ArcImg)
SetSpriteSize(ents[CurrentEnt].id,size/7.5,size/7.5)
SetSpritePositionByOffset(ents[CurrentEnt].id,x+Size/2-GetSpriteWidth(Ents[CurrentEnt].id)/2,y+Size/2+GetSpriteWidth(ents[CurrentEnt].id)*1.33)
SetSpriteflip(ents[CurrentEnt].id,0,0)
// Center Right Hole
inc CurrentEnt
Ents[CurrentEnt].id = CreateSprite(HalfImg)
SetSpriteSize(ents[CurrentEnt].id,size/5,size/5)
SetSpritePositionByOffset(ents[CurrentEnt].id,x-Size/2+GetSpriteWidth(Ents[CurrentEnt].id)/2,y)
SetSpriteflip(ents[CurrentEnt].id,0,0)
// Center Left Hole
inc CurrentEnt
Ents[CurrentEnt].id = CreateSprite(HalfImg)
SetSpriteSize(ents[CurrentEnt].id,size/5,size/5)
SetSpritePositionByOffset(ents[CurrentEnt].id,x+Size/2-GetSpriteWidth(Ents[CurrentEnt].id)/2,y)
SetSpriteflip(ents[CurrentEnt].id,1,0)
CreateBall(x,y+ size/3,VW/55,255,255,255) // WhiteBall
CreateBall(x,y-Size/4,VW/50,255,0,0)
CreateBall(x,y-Size/4.5-GetSpriteHeight(Balls[CurrentBall-1].id),VW/50,255,0,0)
CreateBall(x-GetSpriteWidth(Balls[CurrentBall-1].id)/2,y-Size/4.5-GetSpriteHeight(Balls[CurrentBall-1].id),VW/50,255,0,0)
CreateBall(x+GetSpriteWidth(Balls[CurrentBall-1].id)/2,y-Size/4.5-GetSpriteHeight(Balls[CurrentBall-1].id),VW/50,255,0,0)
CreateBall(x,y-Size/4.5-GetSpriteHeight(Balls[CurrentBall-1].id)*2,VW/50,255,0,0)
CreateBall(x+GetSpriteWidth(Balls[CurrentBall-1].id)/2,y-Size/4.5-GetSpriteHeight(Balls[CurrentBall-1].id)*2,VW/50,255,0,0)
CreateBall(x-GetSpriteWidth(Balls[CurrentBall-1].id)/2,y-Size/4.5-GetSpriteHeight(Balls[CurrentBall-1].id)*2,VW/50,255,0,0)
CreateBall(x+GetSpriteWidth(Balls[CurrentBall-1].id)*2/2,y-Size/4.5-GetSpriteHeight(Balls[CurrentBall-1].id)*2,VW/50,255,0,0)
CreateBall(x-GetSpriteWidth(Balls[CurrentBall-1].id)*2/2,y-Size/4.5-GetSpriteHeight(Balls[CurrentBall-1].id)*2,VW/50,255,0,0)
CreateBall(x,y-Size/4.5-GetSpriteHeight(Balls[CurrentBall-1].id)*3,VW/50,255,0,0)
CreateBall(x+GetSpriteWidth(Balls[CurrentBall-1].id)/2,y-Size/4.5-GetSpriteHeight(Balls[CurrentBall-1].id)*3,VW/50,255,0,0)
CreateBall(x-GetSpriteWidth(Balls[CurrentBall-1].id)/2,y-Size/4.5-GetSpriteHeight(Balls[CurrentBall-1].id)*3,VW/50,255,0,0)
CreateBall(x+GetSpriteWidth(Balls[CurrentBall-1].id)*2/2,y-Size/4.5-GetSpriteHeight(Balls[CurrentBall-1].id)*3,VW/50,255,0,0)
CreateBall(x-GetSpriteWidth(Balls[CurrentBall-1].id)*2/2,y-Size/4.5-GetSpriteHeight(Balls[CurrentBall-1].id)*3,VW/50,255,0,0)
CreateBall(x+GetSpriteWidth(Balls[CurrentBall-1].id)*3/2,y-Size/4.5-GetSpriteHeight(Balls[CurrentBall-1].id)*3,VW/50,255,0,0)
CreateBall(x-GetSpriteWidth(Balls[CurrentBall-1].id)*3/2,y-Size/4.5-GetSpriteHeight(Balls[CurrentBall-1].id)*3,VW/50,255,0,0)
endfunction
function CreateStick()
Stick.id = CreateSprite(0)
SetSpriteSize(Stick.id,Vw/100,VW/3)
SetSpritePositionByOffset(Stick.id,GetSpriteXByOffset(balls[1].id),GetSpriteYByOffset(balls[1].id)+ GetSpriteHeight(Stick.id)/1.5)
SetSpritePhysicsOn(stick.id,2)
SetSpriteGroup(stick.id,-1)
endfunction
function LoadContent()
global BallHollowImg as integer : BallHollowImg = LoadImage("h_Circle.png")
global ArcImg as integer : ArcImg = LoadImage("arc.png")
global HalfImg as integer : HalfImg = LoadImage("half.png")
global eBoxImg as integer : eBoxImg = LoadImage("ebox.png")
endfunction
function SetupScreen(windowName as string)
global DW as integer
global DH as integer
global VW as float
global VH as float
DW = GetMaxDeviceWidth()
DH = GetMaxDeviceHeight()
VW = DW
VH = DH
SetWindowTitle( WindowName )
SetWindowSize( DW,DH, 0 )
// set display properties
SetVirtualResolution( VW,VH )
SetOrientationAllowed( 1, 1, 1, 1 )
endfunction