pong minimal

main
ben 2023-11-21 23:41:52 +01:00
parent b53ad6d476
commit bf51c6a614
5 changed files with 125 additions and 37 deletions

View File

@ -2,15 +2,18 @@
Pong Flutter project. Pong Flutter project.
## Getting Started - Ein einfaches Pong Spiel.
This project is a starting point for a Flutter application. - Nur Einzelspieler.
A few resources to get you started if this is your first Flutter project: - Score wird am Ende der Partie angezeigt.
- [Lab: Write your first Flutter app](https://docs.flutter.dev/get-started/codelab) - Spiel kann pausiert werden.
- [Cookbook: Useful Flutter samples](https://docs.flutter.dev/cookbook)
For help getting started with Flutter development, view the - Aktueller Score wird während der Pause angezeigt.
[online documentation](https://docs.flutter.dev/), which offers tutorials,
samples, guidance on mobile development, and a full API reference. Geplante Business Logik:
- Selbstständige Bewegung des Balls
- Einen Punktestand-Zähler
- Button um das Spiel zu pausieren

View File

@ -9,9 +9,11 @@ class PongGame extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return const MaterialApp( return MaterialApp(
title: 'Pong Game', title: 'Pong Game',
home: StartScreen(), theme:
ThemeData(scaffoldBackgroundColor: Color.fromARGB(255, 41, 38, 38)),
home: const StartScreen(),
); );
} }
} }
@ -24,13 +26,23 @@ class StartScreen extends StatelessWidget {
return Scaffold( return Scaffold(
body: GestureDetector( body: GestureDetector(
onTap: () { onTap: () {
/* Navigator.of(context).push(MaterialPageRoute( Navigator.of(context).push(MaterialPageRoute(
builder: (context) { builder: (context) {
return const GameScreen(); return const GameScreen();
}, },
)); */ ));
}, },
child: const Text('Berühren um zu beginnen!'), child: const Center(
child: Text(
'Berühren um zu beginnen!',
key: Key("tapToStart"),
style: TextStyle(
fontSize: 30,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
),
), ),
); );
} }
@ -44,8 +56,90 @@ class GameScreen extends StatefulWidget {
} }
class _GameScreenState extends State<GameScreen> { class _GameScreenState extends State<GameScreen> {
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 @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return const Placeholder(); 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;
} }
} }

View File

@ -2,7 +2,7 @@ name: pong
description: Pong Flutter project. description: Pong Flutter project.
# The following line prevents the package from being accidentally published to # The following line prevents the package from being accidentally published to
# pub.dev using `flutter pub publish`. This is preferred for private packages. # pub.dev using `flutter pub publish`. This is preferred for private packages.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev publish_to: "none" # Remove this line if you wish to publish to pub.dev
# The following defines the version and build number for your application. # The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43 # A version number is three numbers separated by dots, like 1.2.43
@ -19,7 +19,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
version: 1.0.0+1 version: 1.0.0+1
environment: environment:
sdk: '>=3.1.3 <4.0.0' sdk: ">=3.1.3 <4.0.0"
# Dependencies specify other packages that your package needs in order to work. # Dependencies specify other packages that your package needs in order to work.
# To automatically upgrade your package dependencies to the latest versions # To automatically upgrade your package dependencies to the latest versions
@ -31,7 +31,6 @@ dependencies:
flutter: flutter:
sdk: flutter sdk: flutter
# The following adds the Cupertino Icons font to your application. # The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons. # Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2 cupertino_icons: ^1.0.2
@ -52,7 +51,6 @@ dev_dependencies:
# The following section is specific to Flutter packages. # The following section is specific to Flutter packages.
flutter: flutter:
# The following line ensures that the Material Icons font is # The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in # included with your application, so that you can use the icons in
# the material Icons class. # the material Icons class.

View File

@ -0,0 +1,7 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:pong/main.dart';
void main() {
test('moveRacket', () {});
}

View File

@ -1,30 +1,16 @@
// This is a basic Flutter widget test.
//
// To perform an interaction with a widget in your test, use the WidgetTester
// utility in the flutter_test package. For example, you can send tap and scroll
// gestures. You can also use WidgetTester to find child widgets in the widget
// tree, read text, and verify that the values of widget properties are correct.
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import 'package:pong/main.dart'; import 'package:pong/main.dart';
void main() { void main() {
testWidgets('Counter increments smoke test', (WidgetTester tester) async { testWidgets('Tests "Berühren um zu beginnen!"', (WidgetTester tester) async {
// Build our app and trigger a frame. final tapToStart = find.byKey(const ValueKey("tapToStart"));
await tester.pumpWidget(const PongGame());
// Verify that our counter starts at 0. await tester.pumpWidget(const MaterialApp(home: StartScreen()));
expect(find.text('0'), findsOneWidget); await tester.tap(tapToStart);
expect(find.text('1'), findsNothing);
// Tap the '+' icon and trigger a frame.
await tester.tap(find.byIcon(Icons.add));
await tester.pump(); await tester.pump();
// Verify that our counter has incremented. expect(find.text("Berühren um zu beginnen!"), findsOneWidget);
expect(find.text('0'), findsNothing);
expect(find.text('1'), findsOneWidget);
}); });
} }