Strict
Import mojo
Global SCREEN_WIDTH%
Global SCREEN_HEIGHT%
Global dt:DeltaTimer
Global app:MyApp
Function Main:Void()
app = New MyApp()
End Function
Class MyApp Extends App
Field ball:Sprite
Field playerBat:Sprite
Field cpuBat:Sprite
Field backgroundImg:Image
Global FPS:Int = 60
Method OnCreate:Void()
' Store the device width and height
SCREEN_WIDTH = DeviceWidth()
SCREEN_HEIGHT = DeviceHeight()
Seed = Millisecs()
dt = New DeltaTimer (60)
' Load the image
backgroundImg = LoadImage("bg.jpg")
' Create the ball
ball = New Sprite(LoadImage("ball.png", 1, Image.MidHandle), SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2)
ball.dx = RndMin(-5,5,2)
ball.dy = RndMin(-5,5,2)
' Create the player
playerBat = New Sprite(LoadImage("bat.png", 1, Image.MidHandle), 30, SCREEN_HEIGHT / 2)
playerBat.setRGB(255,0,0)
playerBat.speedY = 1
playerBat.maxYSpeed = 7
' Create the AI
cpuBat = New Sprite(LoadImage("bat.png", 1, Image.MidHandle), SCREEN_WIDTH - 30, SCREEN_HEIGHT / 2)
cpuBat.setRGB(0,0,255)
cpuBat.speedY = 0.2
cpuBat.maxYSpeed = 5
cpuBat.scaleX = -1
' 60 FPS please
SetUpdateRate FPS
End Method
Method OnRender:Void()
DrawImage backgroundImg, 0, 0
playerBat.draw()
cpuBat.draw()
ball.draw()
drawGUI()
End Method
Method drawGUI:Void()
DrawText self.playerBat.score, SCREEN_WIDTH / 4, SCREEN_HEIGHT - 50
DrawText self.cpuBat.score, SCREEN_WIDTH - SCREEN_WIDTH / 4, SCREEN_HEIGHT - 50
End
Method OnUpdate:Void()
dt.UpdateDelta
controls()
ai()
ballUpdate()
collisions()
End Method
Method collisions:Void()
if rectsOverlap(ball.x - ball.w2, ball.y - ball.h2, ball.w, ball.h, playerBat.x - playerBat.w2, playerBat.y - playerBat.h2, playerBat.w, playerBat.h)
ball.dx = Abs(ball.dx)+0.5 * dt.delta
End
if rectsOverlap(ball.x - ball.w2, ball.y - ball.h2, ball.w, ball.h, cpuBat.x - cpuBat.w2, cpuBat.y - cpuBat.h2, cpuBat.w, cpuBat.h)
ball.dx = -(Abs(ball.dx)+0.5 * dt.delta)
End
End
Method ballUpdate:Void()
ball.move()
if ball.x < 0
self.cpuBat.score+=1
resetBall()
End
if ball.x > SCREEN_WIDTH
self.playerBat.score+=1
resetBall()
End
if ball.y < 0 or ball.y > SCREEN_HEIGHT
ball.dy = -ball.dy
End
ball.rotation-=ball.dx * dt.delta
End
Method resetBall:Void()
ball.x = SCREEN_WIDTH / 2
ball.y = SCREEN_HEIGHT / 2
ball.dx = RndMin(-5,5,2)
ball.dy = RndMin(-5,5,2)
End
Method controls:Void()
If KeyDown(KEY_UP)
playerBat.dy-=playerBat.speedY
if playerBat.dy<-playerBat.maxYSpeed then playerBat.dy = -playerBat.maxYSpeed
else If KeyDown(KEY_DOWN)
playerBat.dy+=playerBat.speedY
if playerBat.dy>playerBat.maxYSpeed then playerBat.dy = playerBat.maxYSpeed
else
playerBat.dy*=0.95 ' friction to slow it down
End
if playerBat.y < 0
playerBat.y = 0
playerBat.dy = 0
End
if playerBat.y > SCREEN_HEIGHT
playerBat.y = SCREEN_HEIGHT
playerBat.dy = 0
End
playerBat.move()
End
Method ai:Void()
if ball.dx > 0 then
if ball.y < cpuBat.y
cpuBat.dy-=cpuBat.speedY
if cpuBat.dy<-cpuBat.maxYSpeed then cpuBat.dy = -cpuBat.maxYSpeed
End
if ball.y > cpuBat.y
cpuBat.dy+=cpuBat.speedY
if cpuBat.dy>cpuBat.maxYSpeed then cpuBat.dy = cpuBat.maxYSpeed
End
else
cpuBat.dy*=0.95 ' friction to slow it down
end if
if cpuBat.y < 0
cpuBat.y = 0
cpuBat.dy = 0
End
if cpuBat.y > SCREEN_HEIGHT
cpuBat.y = SCREEN_HEIGHT
cpuBat.dy = 0
End
cpuBat.move()
End
End Class
Class Sprite
Field x#,y#
Field dx#, dy#
Field speedX#, speedY#
Field maxXSpeed#, maxYSpeed#
Field image:Image
field scaleX# = 1, scaleY# = 1
field rotation#
field red%, green%, blue%
field w%, h%
field w2%, h2%
field score%
Method New(img:Image,x#, y#)
self.image = img
self.x = x
self.y = y
self.w = img.Width()
self.h = img.Height()
self.w2 = self.w/2
self.h2 = self.h/2
End
Method setRGB:Void(r%,g%,b%)
self.red = r
self.green = g
self.blue = b
End
Method move:Void()
self.x+=self.dx * dt.delta
self.y+=self.dy * dt.delta
End
Method draw:Void()
SetColor red, green, blue ' doesnt work with images!?!??!
DrawImage(image, x, y, rotation, scaleX, scaleY)
SetColor 255, 255, 255
End
End
Function rectsOverlap:Int(x0:Float, y0:Float, w0:Float, h0:Float, x2:Float, y2:Float, w2:Float, h2:Float)
If x0 > (x2 + w2) Or (x0 + w0) < x2 Then Return False
If y0 > (y2 + h2) Or (y0 + h0) < y2 Then Return False
Return True
End Function
Function RndMin:Float(min#,max#,minAmount#)
local rv# = Rnd(min, max)
While Abs(rv) < minAmount
rv = Rnd(-min,max)
Wend
return rv
End
Class DeltaTimer
Field targetfps:Float = 60
Field currentticks:Float
Field lastticks:Float
Field frametime:Float
Field delta:Float
Method New (fps:Float)
targetfps = fps
lastticks = Millisecs()
End
Method UpdateDelta:Void()
currentticks = Millisecs()
frametime = currentticks - lastticks
delta = frametime / (1000.0 / targetfps)
lastticks = currentticks
End
End
[/code:176ym61k]
Still, I was very impressed by the claims. Will be an interesting project to watch!