import 'package:flutter/material.dart'; void main() { runApp(const PongGame()); } class PongGame extends StatelessWidget { const PongGame({super.key}); @override Widget build(BuildContext context) { return MaterialApp( title: 'Pong Game', theme: ThemeData(scaffoldBackgroundColor: Color.fromARGB(255, 41, 38, 38)), home: const StartScreen(), ); } } class StartScreen extends StatelessWidget { const StartScreen({super.key}); @override Widget build(BuildContext context) { return Scaffold( body: GestureDetector( onTap: () { Navigator.of(context).push(MaterialPageRoute( builder: (context) { return const GameScreen(); }, )); }, child: const Center( child: Text( 'Berühren um zu beginnen!', key: Key("tapToStart"), style: TextStyle( fontSize: 30, fontWeight: FontWeight.bold, color: Colors.white, ), ), ), ), ); } } class GameScreen extends StatefulWidget { const GameScreen({super.key}); @override State createState() => _GameScreenState(); } class _GameScreenState extends State { final racketWidth = 120.0; final racketHeight = 25.0; final racketBottomOffset = 100.0; double ballX = 0; double ballY = 0; double racketX = 20; double racketY = 0; moveRacket(double x) { setState(() { racketX = x - racketWidth / 2; }); } @override Widget build(BuildContext context) { return Scaffold( body: GestureDetector( onHorizontalDragUpdate: (details) { moveRacket(details.globalPosition.dx); }, child: CustomPaint( key: const Key("customPaint"), painter: PongGamePainter( racketHeight: 30, racketWidth: 100, racketX: racketX, racketBottomOffset: racketBottomOffset, ballSize: 20, ballX: 10, ballY: 10), size: Size.infinite, ), ), ); } } class PongGamePainter extends CustomPainter { final double ballSize; final double ballX; final double ballY; final double racketX; final double racketWidth; final double racketHeight; final double racketBottomOffset; PongGamePainter({ required this.ballSize, required this.ballX, required this.ballY, required this.racketX, required this.racketWidth, required this.racketHeight, required this.racketBottomOffset, }); @override void paint(Canvas canvas, Size size) { final racketPaint = Paint()..color = Colors.white; final ballPaint = Paint()..color = Colors.white; canvas.drawOval( Rect.fromLTWH(ballX, ballY, ballSize, ballSize), ballPaint, ); canvas.drawRect( Rect.fromLTWH( racketX, size.height - racketHeight - racketBottomOffset, racketWidth, racketHeight, ), racketPaint, ); } @override bool shouldRepaint(covariant PongGamePainter oldDelegate) { return ballX != oldDelegate.ballX || ballY != oldDelegate.ballY || racketX != oldDelegate.racketX; } }