Best Codes Notes'
Best Codes Notes'
dart';
import 'dart:async';
import 'dart:math';
import 'package:flutter/services.dart';
void main() {
runApp(N_GAMES());
}
const GameOption({
required this.gameN,
required this.onPressed,
});
@override
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: onPressed,
child: Padding(
padding: const EdgeInsets.all(20),
child: Text(gameN, style: TextStyle(color: Colors.blue, fontSize: 15)),
),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.black, // Change button color
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(28.0),
),
),
);
}
}
Offset? swipeStartPosition;
static const double swipeThreshold = 50.0;
@override
void initState() {
super.initState();
startGame();
RawKeyboard.instance.addListener(handleKeyboard);
}
@override
void dispose() {
RawKeyboard.instance.removeListener(handleKeyboard);
timer?.cancel();
super.dispose();
}
void startGame() {
snake.clear();
final int mid = (rows * columns / 2).toInt();
for (int i = 0; i < initialSnakeLength; i++) {
snake.add(mid - i);
}
generateFood();
isPlaying = true;
timer?.cancel();
timer = Timer.periodic(Duration(milliseconds: speed), (timer) {
if (!isPaused) {
move();
}
});
}
void generateFood() {
final random = Random();
food = random.nextInt(rows * columns);
}
void move() {
setState(() {
int head = snake.first;
int newHead;
switch (direction) {
case Direction.up:
newHead = head - columns;
break;
case Direction.down:
newHead = head + columns;
break;
case Direction.left:
newHead = head - 1;
break;
case Direction.right:
newHead = head + 1;
break;
}
if (newHead == food) {
score++;
generateFood();
snake.insert(0, newHead);
} else {
snake.removeLast();
snake.insert(0, newHead);
}
if (isGameOver()) {
gameOver();
}
});
}
bool isGameOver() {
for (int i = 1; i < snake.length; i++) {
if (snake[i] == snake.first) {
return true;
}
}
if (snake.first < 0 ||
snake.first >= rows * columns ||
(direction == Direction.left && snake.first % columns == 0) ||
(direction == Direction.right && (snake.first + 1) % columns == 0)) {
return true;
}
return false;
}
void gameOver() {
setState(() {
isPlaying = false;
timer?.cancel();
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
child: Text('𝗢𝗞'),
},
style: ButtonStyle(foregroundColor:
MaterialStateProperty.all<Color>(Colors.white),
),
)
],
);
},
);
});
}
void pauseGame() {
setState(() {
isPaused = !isPaused;
timer?.cancel();
if (!isPaused) {
timer = Timer.periodic(Duration(milliseconds: speed), (timer) {
if (!isPaused) {
move();
}
});
}
});
}
@override
Widget build(BuildContext context) {
return GestureDetector(
onHorizontalDragStart: (details) {
swipeStartPosition = details.globalPosition;
},
onHorizontalDragEnd: (details) {
if (swipeStartPosition != null) {
double dx = details.velocity.pixelsPerSecond.dx;
if (dx.abs() > swipeThreshold) {
if (dx > 0) {
changeDirection(Direction.right);
} else {
changeDirection(Direction.left);
}
}
}
swipeStartPosition = null;
},
onVerticalDragStart: (details) {
swipeStartPosition = details.globalPosition;
},
onVerticalDragEnd: (details) {
if (swipeStartPosition != null) {
double dy = details.velocity.pixelsPerSecond.dy;
if (dy.abs() > swipeThreshold) {
if (dy > 0) {
changeDirection(Direction.down);
} else {
changeDirection(Direction.up);
}
}
}
swipeStartPosition = null;
},
child: Scaffold(
appBar: AppBar(
backgroundColor: Colors.black,
Widget buildFoodCell() {
return Container(
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(5),
),
);
}
Widget buildEmptyCell() {
return Container(
decoration: BoxDecoration(
color: Colors.grey,
borderRadius: BorderRadius.circular(5),
),
);
}
}
const TicTacToeGameOption({
required this.gameN,
required this.onPressed,
});
@override
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: onPressed,
child: Padding(
padding: const EdgeInsets.all(20),
child: Text(gameN, style: TextStyle(color: Colors.blue,fontSize: 15)),
),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.black, // Change button color
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(28.0),
),
),
);
}
}
void resetGame() {
setState(() {
board = List.generate(3, (_) => List.filled(3, ''));
xTurn = true;
filledCells = 0;
});
}
'𝗣𝗟𝗔𝗬 𝗔𝗚𝗔𝗜𝗡',
child: Text(
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.black,
leading: IconButton(
icon: Icon(Icons.arrow_back, color: Colors.white),
onPressed: () {
Navigator.pop(context);
},
),
const MemoryOption({
required this.gameN,
required this.onPressed,
});
@override
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: onPressed,
child: Padding(
padding: const EdgeInsets.all(20),
child: Text(gameN, style: TextStyle(color: Colors.blue, fontSize: 15)),
),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.black, // Change button color
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(28.0),
),
),
);
}
}
@override
void initState() {
super.initState();
generateItems();
items.shuffle();
int length = items.length;
if (length > 0 && length % 2 == 0) {
tiles = List.filled(length, ''); // Initialize with empty strings
flipped = List.filled(length, false); // Initialize with false values
} else {
// Handle invalid length of items list
throw Exception("Invalid length of items list");
}
}
void generateItems() {
List<String> originalItems = ['🍎', '🍊', '🍋', '🍉', '🍇', '🍌', '🍒', '🍍'];
items = [];
for (String item in originalItems) {
items.addAll([item, item]); // Duplicate each item
}
}
void resetGame() {
setState(() {
items.shuffle();
tiles = List.generate(items.length, (index) => items[index]);
flipped = List.generate(items.length, (index) => false);
matchedIndices.clear();
gameActive = true;
moves = 0;
});
}
void showWinnerDialog() {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Congratulations!'),
content: Text('You won in $moves moves!'),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop();
resetGame();
},
child: Text('Play Again'),
),
],
);
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.black,
leading: IconButton(
icon: Icon(Icons.arrow_back, color: Colors.white),
onPressed: () {
Navigator.of(context).pop();
},
Colors.white,fontSize: 15)),
),
backgroundColor: Colors.black,
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Expanded(
child: GridView.builder(
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 4,
childAspectRatio: 1,
),
itemCount: tiles.length,
itemBuilder: (context, index) {
return GestureDetector(
onTap: () {
if (!flipped[index] && gameActive) {
setState(() {
moves++;
});
checkMatch(index);
}
},
child: Container(
margin: EdgeInsets.all(4),
color: flipped[index] || matchedIndices.contains(index)
? Colors.grey
: Colors.blue,
child: Center(
child: Text(
flipped[index] || matchedIndices.contains(index)
? tiles[index]
: '',
style: TextStyle(fontSize: 32),
),
),
),
);
},
),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: resetGame,
child: Text('Reset', style: TextStyle(color: Colors.white)),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.black,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
padding: EdgeInsets.symmetric(vertical: 15, horizontal: 50),
),
),
],
),
);
}
}