Skip to content

Instantly share code, notes, and snippets.

@davidmigloz
Created December 1, 2023 22:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidmigloz/a5590da8f116b32913e5ee7129ff8d90 to your computer and use it in GitHub Desktop.
Save davidmigloz/a5590da8f116b32913e5ee7129ff8d90 to your computer and use it in GitHub Desktop.
Generated code from pixels2flutter.dev
import 'dart:math';
import 'dart:ui';
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: GameScreen(),
),
);
}
}
class GameScreen extends StatefulWidget {
@override
_GameScreenState createState() => _GameScreenState();
}
class _GameScreenState extends State<GameScreen>
with SingleTickerProviderStateMixin {
double paddleWidth = 20.0;
double paddleHeight = 100.0;
double ballSize = 20.0;
double ballX = 0;
double ballY = 0;
double ballXVelocity = 5;
double ballYVelocity = 5;
double player1Y = 0;
double player2Y = 0;
int player1Score = 0;
int player2Score = 0;
bool gameStarted = false;
late Animation<double> animation;
late AnimationController controller;
@override
void initState() {
super.initState();
controller = AnimationController(
duration: const Duration(minutes: 10000),
vsync: this,
);
animation = Tween<double>(begin: 0, end: 10000).animate(controller)
..addListener(() {
setState(() {
moveBall();
checkScore();
});
});
RawKeyboard.instance.addListener(_handleKeyEvent);
}
void _handleKeyEvent(RawKeyEvent event) {
if (event.runtimeType == RawKeyDownEvent) {
if (event.logicalKey == LogicalKeyboardKey.arrowUp) {
setState(() {
player1Y -= 20;
});
} else if (event.logicalKey == LogicalKeyboardKey.arrowDown) {
setState(() {
player1Y += 20;
});
} else if (event.logicalKey == LogicalKeyboardKey.arrowLeft) {
setState(() {
player2Y -= 20;
});
} else if (event.logicalKey == LogicalKeyboardKey.arrowRight) {
setState(() {
player2Y += 20;
});
} else if (event.logicalKey == LogicalKeyboardKey.space) {
startGame();
}
}
}
void startGame() {
setState(() {
if (!gameStarted) {
ballX = 0;
ballY = 0;
ballXVelocity = 5;
ballYVelocity = 5;
gameStarted = true;
controller.forward();
}
});
}
void moveBall() {
setState(() {
ballX += ballXVelocity;
ballY += ballYVelocity;
// Check for paddle collision
if (ballX <= -MediaQuery.of(context).size.width / 2 + paddleWidth &&
ballY >= player1Y - paddleHeight / 2 &&
ballY <= player1Y + paddleHeight / 2) {
ballXVelocity = -ballXVelocity;
}
if (ballX >= MediaQuery.of(context).size.width / 2 - paddleWidth &&
ballY >= player2Y - paddleHeight / 2 &&
ballY <= player2Y + paddleHeight / 2) {
ballXVelocity = -ballXVelocity;
}
// Check for wall collision
if (ballY <= -MediaQuery.of(context).size.height / 2 ||
ballY >= MediaQuery.of(context).size.height / 2) {
ballYVelocity = -ballYVelocity;
}
});
}
void checkScore() {
if (ballX <= -MediaQuery.of(context).size.width / 2 ||
ballX >= MediaQuery.of(context).size.width / 2) {
if (ballX < 0) {
player2Score++;
} else {
player1Score++;
}
ballX = 0;
ballY = 0;
gameStarted = false;
controller.reset();
}
}
@override
void dispose() {
controller.dispose();
RawKeyboard.instance.removeListener(_handleKeyEvent);
super.dispose();
}
@override
Widget build(BuildContext context) {
return Stack(
children: <Widget>[
Positioned(
top: MediaQuery.of(context).size.height / 2 +
player1Y -
paddleHeight / 2,
left: 20,
child: Paddle(paddleWidth, paddleHeight, Colors.red),
),
Positioned(
top: MediaQuery.of(context).size.height / 2 +
player2Y -
paddleHeight / 2,
right: 20,
child: Paddle(paddleWidth, paddleHeight, Colors.blue),
),
Positioned(
top: MediaQuery.of(context).size.height / 2 + ballY - ballSize / 2,
left: MediaQuery.of(context).size.width / 2 + ballX - ballSize / 2,
child: Ball(ballSize),
),
Positioned(
top: 0,
right: 0,
left: 0,
child: Center(
child: Text(
'$player1Score : $player2Score',
style: TextStyle(color: Colors.white, fontSize: 30),
),
),
),
Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: List.generate(
13,
(index) => Padding(
padding: const EdgeInsets.symmetric(vertical: 4.0),
child: Container(
width: 2,
height: 20,
color: Colors.white,
),
)),
),
),
],
);
}
}
class Paddle extends StatelessWidget {
final double width;
final double height;
final Color color;
Paddle(this.width, this.height, this.color);
@override
Widget build(BuildContext context) {
return Container(
width: width,
height: height,
color: color,
);
}
}
class Ball extends StatelessWidget {
final double size;
Ball(this.size);
@override
Widget build(BuildContext context) {
return Container(
width: size,
height: size,
decoration: BoxDecoration(
color: Colors.white,
shape: BoxShape.circle,
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment