Dear Cerberus X User!
As we prepare to transition the forum ownership from Mike to Phil (TripleHead GmbH), we need your explicit consent to transfer your user data in accordance with our amended Terms and Rules in order to be compliant with data protection laws.
Please read the new Terms and Rules below, check the box to agree, and click "Accept" to continue enjoying your Cerberus X Forum experience. The deadline for consent is April 5, 2024.
Do not accept the amended Terms and Rules if you do not wish your personal data to be transferred to the future forum owner!
Accepting ensures:
- Continued access to your account with a short break for the actual transfer.
- Retention of your data under the same terms.
Without consent:
- You don't have further access to your forum user account.
- Your account and personal data will be deleted after April 5, 2024.
- Public posts remain, but usernames indicating real identity will be anonymized. If you disagree with a fictitious name you have the option to contact us so we can find a name that is acceptable to you.
We hope to keep you in our community and see you on the forum soon!
All the best
Your Cerberus X Team
xPlayer = Clamp( xPlayer, xMin, xMax )
yPlayer = Clamp( yPlayer, yMin, yMax )
Import mojo
Class Game Extends App
Field x:Float
Field y:Float
Field character:Image
Field p1:Game
Method OnCreate:Int()
character = LoadImage("character.png")
SetUpdateRate 60
Return 0
End method
Method OnUpdate:Int()
If KeyDown (KEY_LEFT) Then x = x - 4
If KeyDown (KEY_RIGHT) Then x = x + 4
If KeyDown (KEY_UP) Then y = y-4
If KeyDown (KEY_DOWN) Then y = y +4
xPlayer = Clamp (x:character, x:0, x:900)
yPlayer = Clamp (y:character, y:0, y:500)
SetUpdateRate = 60
Return 0
End Method
Method OnRender:Int()
DrawImage character, x, y
Return 0
End Method
End Class
Function Main:Int()
New Game()
Return 0
End Function
x=Clamp(x,0,900)
y=Clamp(y,0,500)
What you are doing here will cause a syntax error. You cannot pass colons as part of a call to function/method parameter. Colons are used when declaring variable types.xPlayer = Clamp (x:character, x:0, x:900)
' You should always use Strict mode as it help to trap programming errors
Strict
Import mojo
Global arena:CArena ' Variable representing the playing area
' The use of constants is recommended if you have lots of literal numbers that are related and you want to make a quick change.
' You only have to change the constant and not evey occurance of that literal number.
Const pixelsX:Int=4
Const pixelsY:Int=4
' A Class that represents the playing Arena
Class CArena
Field x:Int
Field y:Int
Field w:Int
Field h:Int
Field lineWidth:Int
' Set some defaults that can be over ridden
Method New(x:Int = 0, y:Int = 0, w:Int = 1, h:Int = 1, lineWidth:Int = 1)
' The use of Self helps to distinguish between the class variable and a local variable of the same name
' Self is a special variable that basically means this class.
Self.x = x
Self.y = y
Self.w = w
Self.h = h
Self.lineWidth = lineWidth
End Method
' Note that the variables x amd y for the sprite object are float data types
' and the arena field are ints. Therefore the arena variable must be cast to floats
' as we don't what to mess up the fraction parts of the values passed and returned.
' There is no automatic conversion and it would generate an unable to determine type error.
Method BoundsX:Float(x:Float, w:Int)
Return Clamp(x, Float(arena.x + Self.lineWidth), Float((arena.w - Self.lineWidth) - w))
End Method
Method BoundsY:Float(y:Float, h:Int)
Return Clamp(y, Float(arena.y + Self.lineWidth), Float((arena.h - Self.lineWidth) - h))
End Method
' Change the arena wall thickness
Method IncLineWidth:Void()
lineWidth += 1
End Method
Method DecLineWidth:Void()
lineWidth -= 1
End Method
' Draw the arena walls
Method DrawBounds:Void()
SetColor(255,0,0)
DrawRect(0,0,w,lineWidth) ' Top
DrawRect(0,h-lineWidth,w,lineWidth) ' Bottom
DrawRect(0,0,lineWidth,h) ' Left
DrawRect(w-lineWidth,0,lineWidth,h) ' Right
SetColor(255,255,255)
End Method
End Class
' Very rough sprite object encapsulation
Class CSpriteObject
Field x:Float
Field y:Float
Field w:Int
Field h:Int
Field img:Image
' Load image and create sprite object
Method New(imgPath:String, x:Int = 0, y:Int = 0)
Self.x = x
Self.y = y
img = LoadImage(imgPath)
Self.w = img.Width() ' Store the width and height of the image to avoid having to call the image method to retrieve them all the time.
Self.h = img.Height()
End Method
' Move the sprite in a direction and check the arena bounds
Method Left:Int(x:Float = 1)
Self.x = Self.x - x
Self.x = arena.BoundsX(Self.x, Self.w)
Return Self.x
End Method
Method Right:Int(x:Float = 1)
Self.x = Self.x + x
Self.x = arena.BoundsX(Self.x, Self.w)
Return Self.x
End Method
Method Down:Int(y:Float = 1)
Self.y = Self.y + y
Self.y = arena.BoundsY(Self.y, Self.h)
Return Self.y
End Method
Method Up:Int(y:Float = 1)
Self.y = Self.y - y
Self.y = arena.BoundsY(Self.y, Self.h)
Return Self.y
End Method
Method Draw:Void()
DrawImage(Self.img, Self.x, Self.y)
End Method
End Class
' Main game class
Class Game Extends App
Field character:CSpriteObject
Method OnCreate:Int()
SetUpdateRate 60
' Set the arena size to the device width and height
arena = New CArena(0, 0, DeviceWidth(), DeviceHeight())
' Load the image to use as a sprite and set it to the centre of the device. Use your own image
character = New CSpriteObject("blob.png", DeviceWidth()/2, DeviceHeight()/2)
Return 0
End Method
Method OnUpdate:Int()
' The sprite object should call any boundary checks after any change. The same goes for any change of the arena
If KeyDown(KEY_LEFT) Then character.Left(pixelsX)
If KeyDown(KEY_RIGHT) Then character.Right(pixelsX)
If KeyDown(KEY_UP) Then character.Up(pixelsY)
If KeyDown(KEY_DOWN) Then character.Down(pixelsY)
If KeyDown(KEY_Q) And arena.lineWidth < 30
arena.IncLineWidth()
character.x = arena.BoundsX(character.x, character.w)
character.y = arena.BoundsY(character.y, character.h)
Endif
If KeyDown(KEY_W) And arena.lineWidth > 1
arena.DecLineWidth()
character.x = arena.BoundsX(character.x, character.w)
character.y = arena.BoundsY(character.y, character.h)
Endif
Return 0
End Method
Method OnRender:Int()
Cls
DrawText("Use arrow keys to move sprite", 40, 40)
DrawText("Use Q & W to change the arena wall thickness.", 40, 52)
arena.DrawBounds()
character.Draw()
Return 0
End Method
End Class
' Program entry point
Function Main:Int()
New Game()
Return 0
End Function
Non taken.No offence, Dawlane
Strict
Import mojo
Function Main:Int()
New Game()
Return 0
End
Class Game Extends App
Field playerX:Float = 250
Field playerY:Float = 250
Field playerSize:Float = 10
Field BorderXMin:Float = 100
Field BorderXMax:Float = 400
Method OnCreate:Int()
SetUpdateRate(60)
Return 0
End
Method OnUpdate:Int()
If KeyDown(KEY_RIGHT)
playerX = playerX + 2
EndIf
If KeyDown(KEY_LEFT)
playerX = playerX - 2
EndIf
playerX = Clamp(playerX, BorderXMin, BorderXMax - playerSize)
Return 0
End
Method OnRender:Int()
Cls(0, 0, 0)
DrawText("Use left and right keys to move the square within the borders.", 10, 10)
'Draw lines to make the border visible
DrawLine(BorderXMin, 0, BorderXMin, 600)
DrawLine(BorderXMax, 0, BorderXMax, 600)
'Draw player as a square.
DrawRect(playerX, playerY, playerSize, playerSize)
Return 0
End
End Class