当前位置:   article > 正文

Flutter游戏制作flame官方范例(一)打蚊子_flutter flame 中文文档

flutter flame 中文文档

1、准备工作:vsCode https://code.visualstudio.com   &&  install the Flutter and Dart plugins for Visual Studio Code

AndroidSDK  https://developer.android.com/studio/#downloads     If you don’t want to install the whole Android Studio and only interested in the SDK, scroll down to the Command line tools only section of the download page

Flutter SDK/Framework       https://flutter.dev/docs/get-started/install

确保能跑个de'mo。 

flutter create boxgame

delete the whole test folder.

Set up the game loop

 

对异步不了解可以看下这个:https://dart.dev/codelabs/async-await

在main的下面建立 box-game.dart

  1. import 'dart:ui';
  2. import 'package:flame/game.dart';
  3. class BoxGame extends Game {
  4. void render(Canvas canvas) {
  5. // TODO: implement render
  6. }
  7. void update(double t) {
  8. // TODO: implement update
  9. }
  10. }

请注意当前插件版本,尽量用高的。

下面往页面里写一个背景和白色的Box

  1. Rect bgRect = Rect.fromLTWH(0, 0, screenSize.width, screenSize.height);
  2. Paint bgPaint = Paint();
  3. bgPaint.color = Color(0xff000000);
  4. canvas.drawRect(bgRect, bgPaint);
  5. double screenCenterX = screenSize.width / 2;
  6. double screenCenterY = screenSize.height / 2;
  7. Rect boxRect = Rect.fromLTWH(
  8. screenCenterX - 75,
  9. screenCenterY - 75,
  10. 150,
  11. 150
  12. );
  13. Paint boxPaint = Paint();
  14. boxPaint.color = Color(0xffffffff);
  15. canvas.drawRect(boxRect, boxPaint);

接下来要加入按钮动作了。

修改main.dart

  1. BoxGame game = BoxGame();
  2. TapGestureRecognizer tapper = TapGestureRecognizer();
  3. tapper.onTapDown = game.onTapDown;
  4. runApp(game.widget);
  5. flameUtil.addGestureRecognizer(tapper);

修改 box.dart

  1. import 'dart:ui';
  2. import 'package:flame/game.dart';
  3. import 'package:flutter/gestures.dart';
  4. class BoxGame extends Game {
  5. Size screenSize;
  6. bool hasWon = false;
  7. void render(Canvas canvas) {
  8. Rect bgRect = Rect.fromLTWH(0, 0, screenSize.width, screenSize.height);
  9. Paint bgPaint = Paint();
  10. bgPaint.color = Color(0xff000000);
  11. canvas.drawRect(bgRect, bgPaint);
  12. double screenCenterX = screenSize.width / 2;
  13. double screenCenterY = screenSize.height / 2;
  14. Rect boxRect =
  15. Rect.fromLTWH(screenCenterX - 75, screenCenterY - 75, 150, 150);
  16. Paint boxPaint = Paint();
  17. // boxPaint.color = Color(0xffffffff);
  18. if (hasWon) {
  19. boxPaint.color = Color(0xff00ff00);
  20. } else {
  21. boxPaint.color = Color(0xffffffff);
  22. }
  23. canvas.drawRect(boxRect, boxPaint);
  24. }
  25. void update(double t) {
  26. // TODO: implement update
  27. }
  28. void resize(Size size) {
  29. screenSize = size;
  30. super.resize(size);
  31. }
  32. void onTapDown(TapDownDetails d) {
  33. double screenCenterX = screenSize.width / 2;
  34. double screenCenterY = screenSize.height / 2;
  35. if (d.globalPosition.dx >= screenCenterX - 75 &&
  36. d.globalPosition.dx <= screenCenterX + 75 &&
  37. d.globalPosition.dy >= screenCenterY - 75 &&
  38. d.globalPosition.dy <= screenCenterY + 75) {
  39. hasWon = true;
  40. }
  41. }
  42. }

main.dart

  1. import 'box-game.dart';
  2. import 'package:flame/util.dart';
  3. import 'package:flutter/services.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:flutter/gestures.dart';
  6. void main() async {
  7. Util flameUtil = Util();
  8. await flameUtil.fullScreen();
  9. await flameUtil.setOrientation(DeviceOrientation.portraitUp);
  10. BoxGame game = BoxGame();
  11. TapGestureRecognizer tapper = TapGestureRecognizer();
  12. tapper.onTapDown = game.onTapDown;
  13. runApp(game.widget);
  14. flameUtil.addGestureRecognizer(tapper);
  15. }

至此这个游戏编写完成,他判断你点击的位置如果是中间白色区域,判定胜利,并变成绿色。游戏结束,无论怎么点都不能变白了。在按R后,可以重置游戏。

接下来的实例是一个更加弱智的游戏,。。。。。

如果你还有兴趣的话,就接着看。。。

1、cd ..  回到你的项目目录。flutter create langaw 创建一个新的项目。

2、删除test文件夹,修改main.dart如下图

3、添加依赖:https://github.com/flame-engine/flame,官网当前版本是这个。

添加完确保进行了packages get.

4 \照旧我们会用到设备方向 DeviceOrientation ,需要引用 util 和 Service

新建一个文件langaw-game.dart

  1. import 'dart:ui';
  2. import 'package:flame/game.dart';
  3. class LangawGame extends Game{
  4. Size screenSize;
  5. void render(Canvas canvas){}
  6. void update(double t){}
  7. void resize(Size size){}
  8. }

再新建一个文件 components/fly.dart 

我的颜色挑选自:https://flatuicolors.com

  1. import 'dart:ui'
  2. class Fly {    
  3.   void render(Canvas c) {}
  4.   void update(double t) {}
  5. }
  1. import 'dart:ui';
  2. import 'package:langaw/langaw-game.dart';
  3. class Fly {
  4. Rect flyRect;
  5. final LangawGame game;
  6. Paint flyPaint;
  7. Fly(this.game, double x, double y) {
  8. flyRect = Rect.fromLTWH(x, y, game.tileSize, game.tileSize);
  9. flyPaint = Paint();
  10. flyPaint.color = Color(0xff6ab04c);
  11. }
  12. void render(Canvas c) {}
  13. void update(double t) {}
  14. }

最终我们的程序为:修改main.dart

  1. import 'package:flame/util.dart';
  2. import 'package:flutter/services.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:langaw/langaw-game.dart';
  5. import 'package:flutter/gestures.dart';
  6. void main() async {
  7. Util flameUtil = Util();
  8. await flameUtil.fullScreen();
  9. await flameUtil.setOrientation(DeviceOrientation.portraitUp);
  10. LangawGame game = LangawGame();
  11. runApp(game.widget);
  12. TapGestureRecognizer tapper = TapGestureRecognizer();
  13. tapper.onTapDown = game.onTapDown;
  14. flameUtil.addGestureRecognizer(tapper);
  15. }

langaw-game.dart:

  1. import 'dart:math';
  2. import 'package:langaw/components/fly.dart';
  3. import 'dart:ui';
  4. import 'package:flame/game.dart';
  5. import 'package:flame/flame.dart';
  6. import 'package:flutter/gestures.dart';
  7. class LangawGame extends Game {
  8. Size screenSize;
  9. double tileSize;
  10. List<Fly> flies;
  11. Random rnd;
  12. LangawGame() {
  13. initialize();
  14. }
  15. void initialize() async {
  16. flies = List<Fly>();
  17. rnd = Random();
  18. resize(await Flame.util.initialDimensions());
  19. spawnFly();
  20. }
  21. void render(Canvas canvas) {
  22. Rect bgRect = Rect.fromLTWH(0, 0, screenSize.width, screenSize.height);
  23. Paint bgPaint = Paint();
  24. bgPaint.color = Color(0xff576574);
  25. canvas.drawRect(bgRect, bgPaint);
  26. flies.forEach((Fly fly) => fly.render(canvas));
  27. }
  28. void update(double t) {
  29. flies.forEach((Fly fly) => fly.update(t));
  30. flies.removeWhere((Fly fly) => fly.isOffScreen);
  31. }
  32. void resize(Size size) {
  33. screenSize = size;
  34. tileSize = screenSize.width / 9;
  35. }
  36. void onTapDown(TapDownDetails d) {
  37. flies.forEach((Fly fly) {
  38. if (fly.flyRect.contains(d.globalPosition)) {
  39. fly.onTapDown();
  40. }
  41. });
  42. }
  43. void spawnFly() {
  44. double x = rnd.nextDouble() * (screenSize.width - tileSize);
  45. double y = rnd.nextDouble() * (screenSize.height - tileSize);
  46. flies.add(Fly(this, x, y));
  47. }
  48. }

components/fly.dart

  1. import 'dart:ui';
  2. import 'package:langaw/langaw-game.dart';
  3. class Fly {
  4. Rect flyRect;
  5. final LangawGame game;
  6. Paint flyPaint;
  7. bool isDead = false;
  8. bool isOffScreen = false;
  9. Fly(this.game, double x, double y) {
  10. flyRect = Rect.fromLTWH(x, y, game.tileSize, game.tileSize);
  11. flyPaint = Paint();
  12. flyPaint.color = Color(0xff6ab04c);
  13. }
  14. void render(Canvas c) {
  15. c.drawRect(flyRect, flyPaint);
  16. }
  17. void update(double t) {
  18. if (isDead) {
  19. flyRect = flyRect.translate(0, game.tileSize * 12 * t);
  20. }
  21. if (flyRect.top > game.screenSize.height) {
  22. isOffScreen = true;
  23. }
  24. }
  25. void onTapDown() {
  26. flyPaint.color = Color(0xffff4757);
  27. isDead = true;
  28. game.spawnFly();
  29. }
  30. }

运行结果可以点小方块,并朝下方移动,最终自己销毁。

4、新建一个view.dart

  1. enum View {
  2. home,
  3. playing,
  4. lost,
  5. }

如果向修改包名,你需要修改一下几处:

  • ./android/app/src/main/AndroidManifest.xml  
  •  ./android/app/src/debug/AndroidManifest.xmla
  •  ./android/app/build.gradle  
  • ./android/app/src/main/java/com/example/langaw/MainActivity.java).
  • 还有相印的文件夹名 

签名打包命令:keytool -genkey -v -keystore d:\key.jks -keyalg RSA -keysize 2048 -validity 36500 -alias key

key.properties 放到android下,

  1. storePassword=dongyu
  2. keyPassword=dongyu
  3. keyAlias=key
  4. storeFile=D://key.jks

修改android/app/build.gradle:

  1. def keystoreProperties = new Properties()
  2. def keystorePropertiesFile = rootProject.file('key.properties')
  3. if (keystorePropertiesFile.exists()) {
  4. keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
  5. }
  6. android {
  7. ....
  8. signingConfigs {
  9. release {
  10. keyAlias keystoreProperties['keyAlias']
  11. keyPassword keystoreProperties['keyPassword']
  12. storeFile file(keystoreProperties['storeFile'])
  13. storePassword keystoreProperties['storePassword']
  14. }
  15. }
  16. buildTypes {
  17. release {
  18. // TODO: Add your own signing config for the release build.
  19. // Signing with the debug keys for now, so `flutter run --release` works.
  20. signingConfig signingConfigs.release
  21. }
  22. }

 最后打包命令:

flutter build apk

最终源码和打包后的文件在这里下载

https://download.csdn.net/download/ldy889/12014595

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/97745
推荐阅读
相关标签
  

闽ICP备14008679号