Hi, I'm trying to get to grips with game programming but finding it a bit of a minefield, the idea I wish to try and code is a basic game that my grandson came up with
basically a picture of his head the bounces from side to side of the screen area, tryng to hit randomly placed pieces of fruit and avoiding bombs, only using the up and down keys.
I have had a play with an example I found somewhere in the forum, code below but I haven't got a clue on how to replace the bocks with images and the block with a png file
libraries used are jCore. jFX, jReflection and jXUI
Any help, advice would be gratefully received, many thanks
basically a picture of his head the bounces from side to side of the screen area, tryng to hit randomly placed pieces of fruit and avoiding bombs, only using the up and down keys.
I have had a play with an example I found somewhere in the forum, code below but I haven't got a clue on how to replace the bocks with images and the block with a png file
libraries used are jCore. jFX, jReflection and jXUI
Any help, advice would be gratefully received, many thanks
B4X:
#Region Project Attributes
#MainFormWidth: 1000
#MainFormHeight: 600
#End Region
Sub Process_Globals
Type Size(width As Float, height As Float)
Type Point(x As Float, y As Float)
Type brick(index As Int, size As Size, pos As Point, color As Int, isHit As Boolean)
Type ball(pos As Point, radius As Float, speedX As Float, speedY As Float)
Private fx As JFX
Public MainForm As Form
Public Question As String
Private xui As XUI
Private cnv As B4XCanvas
Private vpW, vpH As Float 'ignore
Private timer As Timer
Private ball As ball
Private bricklist As List
Private Button1 As B4XView
Public balloddeven As Int
Private lblCnt As Label
Private lblLives As Label
End Sub
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.RootPane.LoadLayout("Layout1")
MainForm.Show
AddKeyPressedListener(MainForm)
vpW = MainForm.Width - 40
vpH = MainForm.RootPane.Height - 40
Log("Form Height " & vpH & " Width " & vpW)
cnv.Initialize(MainForm.RootPane)
bricklist.Initialize
createball(CreatePoint(vpW/2,vpH/2))
Dim xPos As Float = vpW*0.2
balloddeven = 0
For i = 0 To 40
Dim testw As Float = Rnd(0 ,900)
Dim testh As Float = Rnd(0,500)
createBrick(CreatePoint(xPos+(testw),testh),i)
Next
timer.Initialize("timer",1000/60)
timer.Enabled = True
End Sub
Sub checkcollision
Dim index As Int = 0
For Each b As brick In bricklist
If isCollidingWithBall(b.pos,b.size) Then
b.isHit = True
If b.color = -5374161 Then lblCnt.Text = lblCnt.Text + 1
If b.color = -65536 Then lblLives.Text = lblLives.Text -1
If lblLives.Text = 0 Then
Question = "Game Over, You Lose"
GameLives(Question)
End If
If lblCnt.Text = 15 Then
Question = "Game Over, You Win"
GameLives(Question)
End If
bricklist.RemoveAt(index) 'remove the brick
Exit 'collision happend -> exit loop
End If
index = index + 1
Next
End Sub
Private Sub GameLives(QuestionSent As String)
Private Answ As Int
Answ = fx.Msgbox2(MainForm, QuestionSent, "Play Again?", "Yes", "", "No", fx.MSGBOX_CONFIRMATION)
If Answ = fx.DialogResponse.POSITIVE Then
clearcanvas
lblLives.Text = 3
lblCnt.Text = 0
bricklist.Initialize
createball(CreatePoint(vpW/2,vpH/2))
Dim xPos As Float = vpW*0.2
balloddeven = 0
For i = 0 To 40
Dim testw As Float = Rnd(0 ,980)
Dim testh As Float = Rnd(0,580)
createBrick(CreatePoint(xPos+(testw),testh),i)
Next
timer.Initialize("timer",1000/60)
timer.Enabled = True
Else
Button1_Click
End If
End Sub
Sub Button1_Click
ExitApplication
End Sub
Sub AddKeyPressedListener(myf As Form)
Dim r As Reflector
r.Target = myf.RootPane
r.AddEventHandler("Main_KeyPressed", "javafx.scene.input.KeyEvent.KEY_PRESSED")
r.AddEventHandler("Main_KeyPressed", "javafx.scene.input.KeyEvent.KEY_RELEASED")
End Sub
Private Sub Main_KeyPressed_Event(ev As Event)
Dim KE As Reflector
KE.Target = ev
Dim KeyCode As String = KE.RunMethod("getCode")
Select KeyCode
Case "UP"
ball.pos.y = ball.pos.y - 20
Case "DOWN"
ball.pos.y = ball.pos.y + 20
End Select
ev.Consume
End Sub
Sub createball(pos As Point)
ball.Initialize
ball.pos = pos
ball.radius = vpW*0.010
ball.speedX = 4
ball.speedY = 4
End Sub
Sub createBrick(pos As Point, index As Int)
Dim b As brick
b.Initialize
b.index = index
b.size = CreateSize(20, 20)
b.pos = pos
If balloddeven = 0 Then
b.color = xui.Color_RGB(173,255,47)
Else
b.color = xui.Color_RGB(255,0,0)
End If
If balloddeven = 0 Then
balloddeven = 1
Else
balloddeven = 0
End If
b.isHit = False
bricklist.Add(b)
End Sub
Sub timer_Tick
clearcanvas
moveBall
checkcollisionWithWalls
checkcollision
drawball
drawbrick
drawstatus
invalidate
invalidate
End Sub
Sub clearcanvas
cnv.ClearRect(cnv.TargetRect)
End Sub
Sub moveBall
ball.pos.x = ball.pos.x + ball.speedX
End Sub
Sub checkcollisionWithWalls
If ball.pos.x + ball.radius > vpW Then
ball.pos.x = vpW-ball.radius
ball.speedx = ball.speedx * -1
else if ball.pos.x - ball.radius < 0 Then
ball.pos.x = ball.radius
ball.speedx = ball.speedx * -1
End If
If ball.pos.y < 10 Then ball.pos.y = 10
If ball.pos.y > 590 Then ball.pos.y = 590
End Sub
Sub drawball
cnv.DrawCircle(ball.pos.x,ball.pos.y,ball.radius,xui.Color_White,True,0)
End Sub
Sub drawbrick
For Each b As brick In bricklist
cnv.DrawRect(returnRect(b.pos.x,b.pos.y,b.size.width,b.size.height),b.color,True,0)
Next
End Sub
Sub drawstatus
If bricklist.Size = 0 Then
cnv.DrawText("You win!",vpW/2,vpH/2,xui.CreateDefaultFont(22),xui.Color_White,"CENTER")
End If
End Sub
Sub invalidate
cnv.Invalidate
End Sub
Sub isCollidingWithBall(pos As Point, size As Size) As Boolean 'ignore
Dim touchedOnSides = False, touchedOnTopBottom = False As Boolean
Dim sideX As Float = ball.pos.x
Dim sideY As Float = ball.pos.y
If ball.pos.x < pos.x Then
sideX = pos.x
touchedOnSides = True
else if ball.pos.x > pos.x+size.width Then
sideX = pos.x+size.width
touchedOnSides = True
End If
If ball.pos.y < pos.y Then
sideY = pos.y
touchedOnTopBottom = True
else if ball.pos.y> pos.y+size.height Then
sideY = pos.y+size.height
touchedOnTopBottom = True
End If
Dim distX As Float = ball.pos.x-sideX
Dim distY As Float = ball.pos.y-sideY
Dim distance As Float = Sqrt((distX*distX) + (distY*distY))
If distance < ball.radius Then
If touchedOnSides Then ball.speedX = ball.speedX * -1
If touchedOnTopBottom Then ball.speedy = ball.speedy * -1
Return True
Else
Return False
End If
End Sub
Sub returnRect(x As Float, y As Float, w As Float, h As Float) As B4XRect 'ignore
Dim r As B4XRect
r.Initialize(x,y,x+w,y+h)
Return r
End Sub
Public Sub CreateSize (width As Float, height As Float) As Size
Dim t1 As Size
t1.Initialize
t1.width = width
t1.height = height
Return t1
End Sub
Public Sub CreatePoint (x As Float, y As Float) As Point
Dim t1 As Point
t1.Initialize
t1.x = x
t1.y = y
Return t1
End Sub