Created
November 28, 2023 15:47
-
-
Save davidmigloz/65c639c90ca100569d005278b975d065 to your computer and use it in GitHub Desktop.
Generated code from Pixels2Flutter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Certainly! Below is the `main.dart` file for a simple Flutter app that simulates a Pong-like game based on the provided design and instructions. | |
```dart | |
import 'dart:math'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter/services.dart'; | |
void main() { | |
runApp(PongGame()); | |
} | |
class PongGame extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
debugShowCheckedModeBanner: false, | |
home: Scaffold( | |
backgroundColor: Colors.black, | |
body: GameArea(), | |
), | |
); | |
} | |
} | |
class GameArea extends StatefulWidget { | |
@override | |
_GameAreaState createState() => _GameAreaState(); | |
} | |
class _GameAreaState extends State<GameArea> { | |
double player1Y = 0; | |
double player2Y = 0; | |
double ballX = 0; | |
double ballY = 0; | |
double ballAngle = 0; | |
double ballSpeed = 5; | |
int player1Score = 0; | |
int player2Score = 0; | |
bool gameStarted = false; | |
void startGame() { | |
gameStarted = true; | |
ballX = 0; | |
ballY = 0; | |
ballAngle = Random().nextDouble() * pi * 2; | |
ballSpeed = 5; | |
moveBall(); | |
} | |
void moveBall() { | |
Future.delayed(Duration(milliseconds: 16), () { | |
setState(() { | |
ballX += cos(ballAngle) * ballSpeed; | |
ballY += sin(ballAngle) * ballSpeed; | |
// Collision with top and bottom | |
if (ballY <= -1 || ballY >= 1) { | |
ballAngle = 2 * pi - ballAngle; | |
} | |
// Collision with paddles | |
if (ballX <= -1 && ballY >= player1Y - 0.1 && ballY <= player1Y + 0.1 || | |
ballX >= 1 && ballY >= player2Y - 0.1 && ballY <= player2Y + 0.1) { | |
ballAngle = pi - ballAngle; | |
ballSpeed += 0.5; | |
} | |
// Scoring | |
if (ballX <= -1.2) { | |
player2Score++; | |
gameStarted = false; | |
} else if (ballX >= 1.2) { | |
player1Score++; | |
gameStarted = false; | |
} | |
}); | |
if (gameStarted) { | |
moveBall(); | |
} | |
}); | |
} | |
void movePlayer1(double offset) { | |
setState(() { | |
player1Y = (player1Y + offset).clamp(-1.0, 1.0); | |
}); | |
} | |
void movePlayer2(double offset) { | |
setState(() { | |
player2Y = (player2Y + offset).clamp(-1.0, 1.0); | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Shortcuts( | |
shortcuts: <LogicalKeySet, Intent>{ | |
LogicalKeySet(LogicalKeyboardKey.arrowUp): MoveIntent(-0.1, 1), | |
LogicalKeySet(LogicalKeyboardKey.arrowDown): MoveIntent(0.1, 1), | |
LogicalKeySet(LogicalKeyboardKey.arrowLeft): MoveIntent(-0.1, 2), | |
LogicalKeySet(LogicalKeyboardKey.arrowRight): MoveIntent(0.1, 2), | |
LogicalKeySet(LogicalKeyboardKey.space): ServeIntent(), | |
}, | |
child: Actions( | |
actions: <Type, Action<Intent>>{ | |
MoveIntent: CallbackAction<MoveIntent>( | |
onInvoke: (MoveIntent intent) => intent.player == 1 | |
? movePlayer1(intent.offset) | |
: movePlayer2(intent.offset), | |
), | |
ServeIntent: CallbackAction<ServeIntent>( | |
onInvoke: (ServeIntent intent) => startGame(), | |
), | |
}, | |
child: Focus( | |
autofocus: true, | |
child: Stack( | |
children: [ | |
// Player 1's paddle | |
Positioned( | |
top: (MediaQuery.of(context).size.height / 2) * (player1Y + 1) - 50, | |
left: 20, | |
child: Paddle(color: Colors.red), | |
), | |
// Player 2's paddle | |
Positioned( | |
top: (MediaQuery.of(context).size.height / 2) * (player2Y + 1) - 50, | |
right: 20, | |
child: Paddle(color: Colors.blue), | |
), | |
// Ball | |
Positioned( | |
top: (MediaQuery.of(context).size.height / 2) * (ballY + 1) - 10, | |
left: (MediaQuery.of(context).size.width / 2) * (ballX + 1) - 10, | |
child: Ball(), | |
), | |
// Scores | |
Positioned( | |
top: 20, | |
left: MediaQuery.of(context).size.width / 4, | |
child: Text( | |
player1Score.toString(), | |
style: TextStyle(color: Colors.white, fontSize: 30), | |
), | |
), | |
Positioned( | |
top: 20, | |
right: MediaQuery.of(context).size.width / 4, | |
child: Text( | |
player2Score.toString(), | |
style: TextStyle(color: Colors.white, fontSize: 30), | |
), | |
), | |
// Net | |
Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: List.generate(15, (index) => Padding( | |
padding: const EdgeInsets.symmetric(vertical: 4.0), | |
child: Container( | |
width: 2, | |
height: 10, | |
color: Colors.white, | |
), | |
)), | |
), | |
), | |
], | |
), | |
), | |
), | |
); | |
} | |
} | |
class Paddle extends StatelessWidget { | |
final Color color; | |
Paddle({required this.color}); | |
@override | |
Widget build(BuildContext context) { | |
return Container( | |
width: 10, | |
height: 100, | |
color: color, | |
); | |
} | |
} | |
class Ball extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Container( | |
width: 20, | |
height: 20, | |
decoration: BoxDecoration( | |
color: Colors.white, | |
shape: BoxShape.circle, | |
), | |
); | |
} | |
} | |
class MoveIntent extends Intent { | |
final double offset; | |
final int player; | |
MoveIntent(this.offset, this.player); | |
} | |
class ServeIntent extends Intent {} | |
``` | |
This code sets up a basic Pong game with two paddles and a ball. The paddles are controlled by the arrow keys, and the ball is served with the spacebar. The ball bounces off the paddles and the top and bottom edges of the screen. The speed of the ball increases after each point scored, and the score is kept at the top of the screen. The game resets the ball position after each point. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment