CPD/Pong/lib/main.dart

146 lines
3.2 KiB
Dart
Raw Normal View History

2023-11-21 15:53:43 +01:00
import 'package:flutter/material.dart';
2023-11-21 15:58:06 +01:00
void main() {
2023-11-21 17:28:39 +01:00
runApp(const PongGame());
2023-11-21 15:53:43 +01:00
}
2023-11-21 17:28:39 +01:00
class PongGame extends StatelessWidget {
const PongGame({super.key});
2023-11-21 15:53:43 +01:00
@override
Widget build(BuildContext context) {
2023-11-21 23:41:52 +01:00
return MaterialApp(
2023-11-21 17:28:39 +01:00
title: 'Pong Game',
2023-11-21 23:41:52 +01:00
theme:
ThemeData(scaffoldBackgroundColor: Color.fromARGB(255, 41, 38, 38)),
home: const StartScreen(),
2023-11-21 15:53:43 +01:00
);
}
}
2023-11-21 17:28:39 +01:00
class StartScreen extends StatelessWidget {
const StartScreen({super.key});
2023-11-21 15:53:43 +01:00
@override
2023-11-21 17:28:39 +01:00
Widget build(BuildContext context) {
return Scaffold(
body: GestureDetector(
onTap: () {
2023-11-21 23:41:52 +01:00
Navigator.of(context).push(MaterialPageRoute(
2023-11-21 17:28:39 +01:00
builder: (context) {
return const GameScreen();
},
2023-11-21 23:41:52 +01:00
));
2023-11-21 17:28:39 +01:00
},
2023-11-21 23:41:52 +01:00
child: const Center(
child: Text(
'Berühren um zu beginnen!',
key: Key("tapToStart"),
style: TextStyle(
fontSize: 30,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
),
2023-11-21 17:28:39 +01:00
),
);
}
2023-11-21 15:53:43 +01:00
}
2023-11-21 17:28:39 +01:00
class GameScreen extends StatefulWidget {
const GameScreen({super.key});
2023-11-21 15:53:43 +01:00
2023-11-21 17:28:39 +01:00
@override
State<GameScreen> createState() => _GameScreenState();
}
2023-11-21 15:53:43 +01:00
2023-11-21 17:28:39 +01:00
class _GameScreenState extends State<GameScreen> {
2023-11-21 23:41:52 +01:00
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;
});
}
2023-11-21 15:53:43 +01:00
@override
Widget build(BuildContext context) {
2023-11-21 23:41:52 +01:00
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;
2023-11-21 15:53:43 +01:00
}
}