281 lines
7.4 KiB
Dart
281 lines
7.4 KiB
Dart
import 'dart:async';
|
|
import 'dart:math';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:testmoduled/gameOver.dart';
|
|
import 'package:testmoduled/main.dart';
|
|
import 'dart:ui';
|
|
|
|
import 'package:testmoduled/ranking.dart';
|
|
import 'package:testmoduled/username.dart';
|
|
|
|
late Offset playerOffset;
|
|
|
|
int mkg = 0;
|
|
int fg = 0;
|
|
int pf = 0;
|
|
int fish = 0;
|
|
int gt = 0;
|
|
|
|
|
|
|
|
class Piece{
|
|
late int score;
|
|
late String assets;
|
|
late Offset offset;
|
|
bool alive =true;
|
|
Piece(this.score,this.assets,this.offset);
|
|
}
|
|
|
|
|
|
class Game extends StatefulWidget {
|
|
const Game({super.key});
|
|
|
|
@override
|
|
State<Game> createState() => _GameState();
|
|
}
|
|
|
|
class _GameState extends State<Game> with TickerProviderStateMixin{
|
|
late int live;
|
|
late int score;
|
|
late int duration;
|
|
late Timer timer;
|
|
bool isPause =false;
|
|
late AnimationController controller;
|
|
late List<Piece> pieces;
|
|
late double speed;
|
|
@override
|
|
void initState() {
|
|
// TODO: implement initState
|
|
super.initState();
|
|
resetGame();
|
|
}
|
|
|
|
void resetGame(){
|
|
live = 3;
|
|
score = 0;
|
|
duration = 0;
|
|
speed = 0.5;
|
|
pieces = [];
|
|
playerOffset = Offset(phoneWidth-100, phoneHeight-110);
|
|
timer = Timer.periodic(Duration(seconds: 1), (_) {
|
|
duration++;
|
|
String randomString = dishList[Random().nextInt(dishList.length)];
|
|
pieces.add(Piece(
|
|
mapData[randomString]["score"],
|
|
mapData[randomString]["assets"],
|
|
Offset(Random().nextInt((phoneWidth-160).toInt())+80, 300)
|
|
));
|
|
setState(() {
|
|
|
|
});
|
|
});
|
|
controller = AnimationController(vsync: this,duration: Duration(seconds: 1))
|
|
..addListener(a)
|
|
..repeat()
|
|
;
|
|
}
|
|
|
|
void a(){
|
|
myDown();
|
|
upDate();
|
|
gameOver();
|
|
setState(() {
|
|
});
|
|
}
|
|
|
|
|
|
void myDown(){
|
|
for (var piece in pieces) {
|
|
piece.offset+=Offset(0,speed);
|
|
if(piece.offset.dy>phoneHeight){
|
|
piece.alive = false;
|
|
if(piece.score>0){
|
|
live--;
|
|
}
|
|
}
|
|
}
|
|
setState(() {
|
|
|
|
});
|
|
}
|
|
|
|
void gameOver(){
|
|
if(live==0){
|
|
controller.dispose();
|
|
timer.cancel();
|
|
rankList.add(ScoreModal(score, duration, textEditingController.value.text));
|
|
Navigator.of(context).push(MaterialPageRoute(builder: (c)=>GameOver()));
|
|
}
|
|
|
|
}
|
|
|
|
void upDate(){
|
|
pieces.removeWhere((element) => !element.alive);
|
|
speed = (score~/100).toDouble()+0.5;
|
|
// if(score%50==0){
|
|
// live++;
|
|
// }
|
|
for (var piece in pieces) {
|
|
if((piece.offset.dy-playerOffset.dy).abs()<65 && (piece.offset.dx-playerOffset.dx).abs()<65){
|
|
piece.alive =false;
|
|
score+=piece.score;
|
|
if(piece.score==30){
|
|
duration+=30;
|
|
}
|
|
if(piece.score==-20){
|
|
live--;
|
|
}
|
|
|
|
switch(piece.score){
|
|
case 10:
|
|
fg++;
|
|
case 20:
|
|
mkg++;
|
|
case 30:
|
|
pf++;
|
|
case -20:
|
|
fish++;
|
|
case -10:
|
|
gt++;
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
setState(() {
|
|
|
|
});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
backgroundColor: Colors.white,
|
|
body: GestureDetector(
|
|
onPanUpdate: (d){
|
|
if(d.globalPosition.dx>80 && d.globalPosition.dx<phoneWidth-80){
|
|
playerOffset = Offset(d.globalPosition.dx, playerOffset.dy);
|
|
}
|
|
setState(() {
|
|
|
|
});
|
|
},
|
|
child: Stack(
|
|
children: [
|
|
//暂停
|
|
Positioned(
|
|
right: 16,top: 16,
|
|
child: GestureDetector(onTap: (){
|
|
setState(() {
|
|
isPause = true;
|
|
controller.stop();
|
|
});
|
|
},child: Image.asset("lib/assets/stop.png",height: 45,width: 45,))
|
|
),
|
|
//随即物
|
|
for (var piece in pieces )
|
|
Positioned.fromRect(
|
|
rect: Rect.fromCenter(center: piece.offset, width: 50, height: 50),
|
|
child: Image.asset("lib/assets/${piece.assets}"),
|
|
),
|
|
//暂停页面
|
|
isPause?buildGamePause():Positioned(left: 0,top: 0,child: SizedBox()),
|
|
Positioned(
|
|
left: 0,right: 0,
|
|
bottom: 0,
|
|
child: Image.asset(
|
|
"lib/assets/game_bg.jpg",
|
|
fit: BoxFit.fill,
|
|
height: 50,
|
|
)
|
|
),
|
|
Positioned(
|
|
left: 16,
|
|
top: 16,
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Container(
|
|
height: 30,
|
|
width: 140,
|
|
alignment: Alignment.center,
|
|
decoration: BoxDecoration(
|
|
image: DecorationImage(image: AssetImage("lib/assets/title_bg.png"),fit: BoxFit.fill)
|
|
),
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceAround,
|
|
children: [
|
|
Image.asset("lib/assets/board.png",fit: BoxFit.cover),
|
|
Text("$score")
|
|
],
|
|
)
|
|
),
|
|
Container(
|
|
height: 30,
|
|
width: 100,
|
|
alignment: Alignment.center,
|
|
decoration: BoxDecoration(
|
|
image: DecorationImage(image: AssetImage("lib/assets/title_bg.png"),fit: BoxFit.fill)
|
|
),
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceAround,
|
|
children: [
|
|
Image.asset("lib/assets/life.png",fit: BoxFit.cover),
|
|
Text("X $live")
|
|
],
|
|
)
|
|
),
|
|
Text("Time: ${(duration~/60).toString().padLeft(2,"0")}:${(duration%60).toString().padLeft(2,"0")}",style: TextStyle(fontSize: 16,fontWeight: FontWeight.w500,color: Colors.black87),)
|
|
],
|
|
)
|
|
),
|
|
|
|
Positioned.fromRect(
|
|
rect: Rect.fromCenter(center: playerOffset, width: 120, height: 120),
|
|
child: Image.asset("lib/assets/player.png",fit: BoxFit.fill,))
|
|
|
|
|
|
|
|
],
|
|
),
|
|
)
|
|
);
|
|
}
|
|
|
|
Widget buildGamePause(){
|
|
return Container(
|
|
color: Colors.black54,
|
|
child: Center(
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
Text("Game Pause",style: TextStyle(fontSize: 22,fontWeight: FontWeight.bold,color: Colors.white),),
|
|
GestureDetector(onTap: (){
|
|
setState(() {
|
|
isPause = false;
|
|
controller.repeat();
|
|
});
|
|
},child: Image.asset("lib/assets/play.png",height: 50,width: 50,fit: BoxFit.fill,)),
|
|
SizedBox(height: 16,),
|
|
GestureDetector(onTap: (){
|
|
resetGame();
|
|
isPause = false;
|
|
setState(() {
|
|
|
|
});
|
|
},child: Image.asset("lib/assets/restart.png",height: 50,width: 50,fit: BoxFit.fill,)),
|
|
SizedBox(height: 16,),
|
|
GestureDetector(onTap: ()=>Navigator.of(context).push(MaterialPageRoute(builder: (c)=>StartGame())),child: Image.asset("lib/assets/out.png",height: 50,width: 50,fit: BoxFit.fill,)),
|
|
|
|
|
|
],
|
|
)
|
|
)
|
|
);
|
|
}
|
|
|
|
|
|
|
|
}
|