package com.example.pinball import android.app.Activity import android.content.Context import android.graphics.* import android.os.Bundle import android.view.Choreographer import android.view.MotionEvent import android.view.View import android.view.Window import android.view.WindowManager import android.widget.Button import android.widget.FrameLayout import android.widget.TextView import android.widget.Toast import java.util.* import kotlin.math.abs class MainActivity : Activity() { // Screen dimensions private var tableWidth = 0 private var tableHeight = 0 // Paddle properties private var racketY = 0f private var racketHeight = 0 private var racketWidth = 0 private var racketX = 0f // Ball properties private var ballSize = 0 private var ballX = 0f private var ballY = 0f // Physics (Pixels per second) private var velocityX = 0f private var velocityY = 0f private val baseSpeed = 1000f // Base speed in pixels/sec private var lastFrameTimeNanos = 0L // Game State private var isLose = false private var score = 0 private var gameTimeSeconds = 0f private var currentLevel = 1 // Bricks data class Brick( val rect: RectF, var isBroken: Boolean = false, val color: Int ) private var bricks = mutableListOf() private lateinit var gameView: GameView private lateinit var scoreText: TextView private lateinit var btnRestart: Button private val rand = Random() // Game Loop private val frameCallback = object : Choreographer.FrameCallback { override fun doFrame(frameTimeNanos: Long) { if (isLose) return if (lastFrameTimeNanos != 0L) { val dt = (frameTimeNanos - lastFrameTimeNanos) / 1_000_000_000f updateGame(dt) } lastFrameTimeNanos = frameTimeNanos gameView.invalidate() scoreText.text = "Score: $score Level: $currentLevel" Choreographer.getInstance().postFrameCallback(this) } } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) requestWindowFeature(Window.FEATURE_NO_TITLE) window.setFlags( WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN ) setContentView(R.layout.activity_main) // Initialize dimensions racketHeight = resources.getDimensionPixelSize(R.dimen.racket_height) racketWidth = resources.getDimensionPixelSize(R.dimen.racket_width) ballSize = resources.getDimensionPixelSize(R.dimen.ball_size) val displayMetrics = resources.displayMetrics tableWidth = displayMetrics.widthPixels tableHeight = displayMetrics.heightPixels // Move paddle up (approx 20% from bottom) racketY = tableHeight * 0.80f gameView = GameView(this) findViewById(R.id.game_container).addView(gameView) scoreText = findViewById(R.id.score_text) btnRestart = findViewById(R.id.btn_restart) val btnSave = findViewById