Hi!
Not really something for the showcase, but i want to share anyway. Maybe it helps some new users to AGK2. The source code is open source, so you are free to use, edit, improve, extent.
I use AGK2 only since 2 days, so the code may be not the cleanest, fastest or most advanced, but the game works.
Controls:
Mouse - move paddle
R - Reset (sometimes the ball gets stuck)
Esc - quit game
// Project: Pong
// Created: 2015-04-14
// x3meblue
///////////////////////////////////
// define screen size
global scrW as Integer = 640
global scrH as Integer = 480
// set window properties
SetWindowTitle("AGK2 Pong")
SetWindowSize(scrW, scrH, 0)
// set display properties
SetVirtualResolution(scrW, scrH)
// set background color
SetClearColor(0, 125, 175)
///////////////////////////////////
// define game state [0 = setup; 1 = running; 2 = end game]
gameState = 0
///////////////////////////////////
// define colors
TYPE GameColors
white
red
ENDTYPE
global c as GameColors
c.white = MakeColor(255, 255, 255)
c.red = MakeColor(255, 0, 0)
///////////////////////////////////
// define playing field
TYPE Playfield
border
px1
px2
py1
py2
lx1
ly1
lx2
ly2
ENDTYPE
global pf as Playfield
pf.border = 3
pf.px1 = pf.border
pf.py1 = pf.border
pf.px2 = scrW - pf.border
pf.py2 = scrH - pf.border
pf.lx1 = scrW / 2
pf.ly1 = pf.border
pf.lx2 = pf.lx1
pf.ly2 = pf.py2
///////////////////////////////////
// define paddle size
global paddleW = 16
global paddleH = 64
TYPE Paddle
id
score
x
y
sx
sy
speed
ENDTYPE
// draw paddle image
Render()
DrawBox(0, 0, paddleW, paddleH, c.white, c.white, c.white, c.white, 1)
Render()
paddleImg = GetImage(0, 0, paddleW, paddleH)
ClearScreen()
// create player paddle
global paddle1 as Paddle
paddle1.id = CreateSprite(paddleImg)
paddle1.score = 0
// define paddle1 start position
paddle1.sx = 2 * paddleW
paddle1.sy = (scrH - paddleH) / 2
// create opponent paddle
global paddle2 as Paddle
paddle2.id = CreateSprite(paddleImg)
paddle2.score = 0
// define paddle2 start position
paddle2.sx = scrW - (2 * paddleW) - paddleW
paddle2.sy = paddle1.sy
paddle2.x = paddle2.sx
paddle2.y = paddle2.sy
paddle2.speed = 5
///////////////////////////////////
// define ball size
global ballW = 12
global ballH = 12
TYPE Ball
id
x
y
sx
sy
speedX
speedY
ENDTYPE
global aBall as Ball
// define ball start position
aBall.sx = (scrW - ballW) / 2
aBall.sy = (scrH - ballH) / 2
aBall.x = aBall.sx
aBall.y = aBall.sy
aBall.speedX = 6
aBall.speedY = -6
// draw ball & save as a sprite
Render()
DrawBox(0, 0, ballW, ballH, c.white, c.white, c.white, c.white, 1)
Render()
ballImg = GetImage(0, 0, ballW, ballH)
ClearScreen()
aBall.id = CreateSprite(ballImg)
///////////////////////////////////
GameStartText()
///////////////////////////////////
do
if gameState = 0 AND GetPointerPressed() = 1
gameState = 1
DeleteText(1)
DeleteText(2)
ScoreText()
endif
if GetRawKeyPressed(27)
gameState = 2
endif
if GetRawKeyPressed(82)
paddle1.score = 0
paddle2.score = 0
UpdateScoreText()
ResetGame()
endif
select gameState
case 0:
SetupGame()
endcase
case 1:
RunGame()
endcase
case default:
EndGame()
endcase
endselect
Sync()
loop
function SetupGame()
// draw player paddle
SetSpritePosition(paddle1.id, paddle1.sx, paddle1.sy)
// draw opponent paddle
SetSpritePosition(paddle2.id, paddle2.sx, paddle2.sy)
// draw ball
SetSpritePosition(aBall.id, aBall.sx, aBall.sy)
endfunction
function RunGame()
// draw playing field
DrawBox(pf.px1, pf.py1, pf.px2, pf.py2, c.white, c.white, c.white, c.white, 0)
DrawLine(pf.lx1, pf.ly1, pf.lx2, pf.ly2, c.white, c.white)
UpdatePlayer()
UpdateOpponent()
UpdateBall()
endfunction
function UpdatePlayer()
// get player vertical position
paddle1.y = GetPointerY()
// check playfield borders
if paddle1.y < pf.py1 then paddle1.y = pf.py1
if paddle1.y > pf.py2 - paddleH then paddle1.y = pf.py2 - paddleH
// draw player paddle
SetSpritePosition(paddle1.id, paddle1.sx, paddle1.y)
endfunction
function UpdateOpponent()
if paddle2.y < aBall.y
paddle2.y = paddle2.y + paddle2.speed
elseif paddle2.y > aBall.y
paddle2.y = paddle2.y - paddle2.speed
endif
// check playfield borders
if paddle2.y < pf.py1 then paddle2.y = pf.py1
if paddle2.y > pf.py2 - paddleH then paddle2.y = pf.py2 - paddleH
SetSpritePosition(paddle2.id, paddle2.x, paddle2.y)
endfunction
function UpdateBall()
if GetSpriteCollision(paddle1.id, aBall.id) = 1
aBall.speedX = aBall.speedX * -1
aBall.speedY = aBall.speedY * RandomSign(1)
aBall.x = paddle1.x + paddleW + ballW
elseif GetSpriteCollision(paddle2.id, aBall.id) = 1
aBall.speedX = aBall.speedX * -1
aBall.speedY = aBall.speedY * RandomSign(1)
aBall.x = paddle2.x - ballW
else
if aBall.x < 0
inc paddle2.score
UpdateScoreText()
ResetGame()
elseif aBall.x > scrW
inc paddle1.score
UpdateScoreText()
ResetGame()
endif
if aBall.y < 0
aBall.speedY = aBall.speedY * -1
elseif aBall.y > scrH
aBall.speedY = aBall.speedY * -1
endif
endif
aBall.x = aBall.x + aBall.speedX
aBall.y = aBall.y + aBall.speedY
SetSpritePosition(aBall.id, aBall.x, aBall.y)
endfunction
function ResetGame()
Sleep(500)
paddle1.x = paddle1.sx
paddle1.y = paddle1.sy
paddle2.x = paddle2.sx
paddle2.y = paddle2.sy
aBall.x = aBall.sx
aBall.y = aBall.sy
endfunction
function EndGame()
// close the application
end
endfunction
function GameStartText()
CreateText(1, "AGK2 Pong")
CreateText(2, "Press Left Mouse Button")
for i = 1 to 2
SetTextSize(i, 28)
SetTextSpacing(i, 2)
SetTextColor(i, 255, 0, 0, 255)
SetTextPosition(i, (scrW - GetTextTotalWidth(i)) / 2, (GetTextTotalHeight(i) * 2) * i)
next i
endfunction
function ScoreText()
CreateText(3, Str(paddle1.score) + " " + Str(paddle2.score))
SetTextSize(3, 72)
SetTextSpacing(3, 2)
SetTextColor(3, 150, 150, 0, 255)
SetTextPosition(3, (scrW - GetTextTotalWidth(3)) / 2, (GetTextTotalHeight(3)))
endfunction
function UpdateScoreText()
SetTextString(3, Str(paddle1.score) + " " + Str(paddle2.score))
endfunction
AGK2 Tier 1 / GameGuru