0% found this document useful (0 votes)
6 views5 pages

Mad 4

The document outlines the development of a gaming application using 2-D animations and gestures in Flutter. It includes an algorithm for creating the application, a sample program for a Snake game, and instructions for handling game logic and user interface. The result confirms the successful development of the application.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

Mad 4

The document outlines the development of a gaming application using 2-D animations and gestures in Flutter. It includes an algorithm for creating the application, a sample program for a Snake game, and instructions for handling game logic and user interface. The result confirms the successful development of the application.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

EX NO:4 Develop a gaming application that uses

DATE:20-02-25​ 2-D animations and gestures

AIM:
To develop a gaming application that uses 2-D animations and gestures.
ALGORITHM:
1.​ Create a new flutter project in android studio
2.​ File → new flutter project
3.​ Define the state variables that are needed.
4.​ Define the class..
5.​ Implement the buttonPressed method.
6.​ Build the user interface.
7.​ Handle the button link.
8.​ Update the text based on the state.
9.​ Run the application.
10.​Display the output and stop.
PROGRAM:
import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:math';
void main() {
runApp(SnakeGame());}
class SnakeGame extends StatefulWidget {
@override
_SnakeGameState createState() => _SnakeGameState();}
class _SnakeGameState extends State<SnakeGame> {
final int rows = 20;
final int columns = 10;
final int speed = 300;
List<Offset> snake = [Offset(5, 5)];
Offset direction = Offset(1, 0);
Offset food = Offset(3, 3);
Timer? timer;
int score = 0;
@override
void initState() {
super.initState();
startGame(); }
void startGame() {
timer = Timer.periodic(Duration(milliseconds: speed), (Timer t) {
setState(() {
moveSnake(); }); }); }
void moveSnake() {
Offset newHead = snake.first + direction;
if (newHead == food) {
snake.insert(0, newHead);
spawnFood();
score += 1;
} else {
snake.insert(0, newHead);
snake.removeLast(); }
if (isGameOver(newHead)) {
timer?.cancel();
showGameOverDialog(); }}
bool isGameOver(Offset position) {
return position.dx < 0 ||
position.dy < 0 ||
position.dx >= columns ||
position.dy >= rows ||
snake.skip(1).contains(position);}
void spawnFood() {
Random random = Random();
food = Offset(random.nextInt(columns).toDouble(), random.nextInt(rows).toDouble());}
void showGameOverDialog() {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text("Game Over"),
content: Text("Your score: $score"),
actions: [
TextButton(
onPressed: () {
Navigator.pop(context);
resetGame(); },
child: Text("Restart"), ),], ),); }
void resetGame() {
setState(() {
snake = [Offset(5, 5)];
direction = Offset(1, 0);
score = 0;
spawnFood();
startGame(); }); }
void changeDirection(Offset newDirection) {
if ((newDirection.dx + direction.dx).abs() != 2 &&
(newDirection.dy + direction.dy).abs() != 2) {
direction = newDirection; } }

@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text("Snake Game")),
body: Column(
children: [
Expanded(
flex: 3,
child: GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount:
columns),
itemCount: rows * columns,
itemBuilder: (context, index) {
int x = index % columns;
int y = index ~/ columns;
Offset pos = Offset(x.toDouble(), y.toDouble());
return Container(
margin: EdgeInsets.all(1),
decoration: BoxDecoration(
color: snake.contains(pos)
? Colors.black
: (pos == food ? Colors.red : Colors.grey[300]),
shape: BoxShape.rectangle, ),);},),),
Expanded(
child: Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
IconButton(
icon: Icon(Icons.arrow_upward),
onPressed: () => changeDirection(Offset(0, -1)), ), ],),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
IconButton(
icon: Icon(Icons.arrow_back),
onPressed: () => changeDirection(Offset(-1, 0)), ),
SizedBox(width: 50),
IconButton(
icon: Icon(Icons.arrow_forward),
onPressed: () => changeDirection(Offset(1, 0)),),], ),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
IconButton(
icon: Icon(Icons.arrow_downward),
onPressed: () => changeDirection(Offset(0, 1)),),],),],), ),
Text("Score: $score", style: TextStyle(fontSize: 20)), ],), ),);}}
OUTPUT:


RESULT:
Thus the gaming application that uses 2-D animations and gestures is developed
successfully

You might also like