Get ai generated code Jumpscafe
package com.example.mazegame
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.view.MotionEvent
import android.view.View
class GameView(context: Context) : View(context) {
private val playerPaint = Paint().apply { color = Color.BLUE }
private val wallPaint = Paint().apply { color = Color.BLACK }
private val goalPaint = Paint().apply { color = Color.GREEN }
private val maze = arrayOf(
intArrayOf(1,1,1,1,1,1),
intArrayOf(1,0,0,0,0,1),
intArrayOf(1,0,1,1,0,1),
intArrayOf(1,0,1,0,0,1),
intArrayOf(1,0,0,0,1,1),
intArrayOf(1,1,1,0,0,1),
intArrayOf(1,1,1,1,1,1)
)
private var cellSize = 0
private var playerX = 1
private var playerY = 1
private val goalX = 4
private val goalY = 5
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
cellSize = width / maze[0].size
for (y in maze.indices) {
for (x in maze[0].indices) {
val paint = when {
x == goalX && y == goalY -> goalPaint
maze[y][x] == 1 -> wallPaint
else -> null
}
paint?.let {
canvas.drawRect(
x * cellSize.toFloat(),
y * cellSize.toFloat(),
(x + 1) * cellSize.toFloat(),
(y + 1) * cellSize.toFloat(),
paint
)
}
}
}
// Draw player
canvas.drawCircle(
playerX * cellSize + cellSize / 2f,
playerY * cellSize + cellSize / 2f,
cellSize / 3f,
playerPaint
)
}
override fun onTouchEvent(event: MotionEvent): Boolean {
if (event.action == MotionEvent.ACTION_DOWN) {
val x = event.x
val y = event.y
val playerCenterX = playerX * cellSize + cellSize / 2f
val playerCenterY = playerY * cellSize + cellSize / 2f
val dx = x - playerCenterX
val dy = y - playerCenterY
if (Math.abs(dx) > Math.abs(dy)) {
if (dx > 0) movePlayer(1, 0) else movePlayer(-1, 0)
} else {
if (dy > 0) movePlayer(0, 1) else movePlayer(0, -1)
}
}
return true