Rainy Knifes

游戏展示

Rainy Knifes

游戏开发

1
git clone https://github.com/GameDevLog/Rainy-Knifes
  • Project Settings > General > Display > Window
    • Size
      • Width: 480
      • Height: 800
    • Stretch
      • Mode: 2d
      • Aspect: keep

导入资源

Scene

  • Node: Main
    • Sprites: Background
    • Node2D: GroundParent
      • Sprites: ground x 4

Inspector

  • Background > Node2D > Transform
    • Position
      • x: 241.067
      • y: 403.912
    • Scale
      • x: 0.67
      • y: 0.63

创建动画

Scene

  • Node2D: Player
    • Sprites: Idle
    • AnimationPlayer: IdleAnimation
    • Sprites: Walk
    • AnimationPlayer: WalkAnimation

Inspector

  • Player > Node2D > Transform

    • Position
      • x: 240
      • y: 480
  • Idle

    • Sprite > Animation
      • Hframes: 2
    • Node2D > Transform > Scale
      • x: 0.5
      • y: 0.5
  • WalkAnimation > AnimationPlayer > Playback Options

    • Speed: 3

Animation

  • New: Idle

    • Animation Length: 0.2
    • Current Pos.: 0
  • Idle > Sprite > Animation

    • Frame: 0 +
  • New: Walk

    • Animation Length: 1

移动 Player

1
using Godot;
2
using System;
3
4
public class Player : Node2D
5
{
6
	private Sprite idleSprite;
7
	private Sprite walkSprite;
8
9
    private AnimationPlayer idleAnimation;
10
    private AnimationPlayer walkAnimation;
11
12
    [Export]
13
    float moveSpeed = 500f;
14
	
15
    public override void _Ready()
16
    {
17
        idleSprite = GetNode<Sprite>("Idle");
18
        walkSprite = GetNode<Sprite>("Walk");
19
20
        idleAnimation = GetNode<AnimationPlayer>("IdleAnimation");
21
        walkAnimation = GetNode<AnimationPlayer>("WalkAnimation");
22
    }
23
24
    public override void _Process(float delta)
25
    {
26
        base._Process(delta);
27
28
        if (Input.IsActionPressed("left"))
29
        {
30
            Vector2 temp = Position;
31
            temp.x -= moveSpeed * delta;
32
            Position = temp;
33
        }
34
        else if (Input.IsActionPressed("right"))
35
        {
36
            Vector2 temp = Position;
37
            temp.x += moveSpeed * delta;
38
            Position = temp;
39
        }
40
    }
41
}

Scenes & Prefabs

生成刀

Scene

  • Node2D: Knife
    • Sprite: Knife

Inspector

  • Knife > Node2D > Transform
    • Rotation Degree: -90

Scene

  • Main
    • Position2D: MinX
    • Position2D: MaxX
    • Timer

Inspector

  • MinX > Node2D > Transform > Position
    • x: 10
    • y: -50
  • MaxX > Node2D > Transform > Position
    • x: 470
    • y: -50
  • Timer > Timer
    • Wait Time: 0.7
    • Autostart: True

碰撞检测

Inspector > Node2D > Z Index

  • Z Index

    • Background: 0
    • backg: 1
    • Player: 2
    • Knife: 3
  • Knife

    • Area2D

Node

  • Groups > Manage Groups > Add
    • Knife

Player.cs

1
public class Player : Node2D {
2
    void _OnCollisionEntered(Area2D area) { 
3
        if(area.IsInGroup("Knife")) {
4
            gameplayScript.PlayerDied();
5
            QueueFree();
6
        }
7
    }
8
}

重新开始

Scene

  • Main
    • Timer: RestartTimer

Main.cs

1
using Godot;
2
using System;
3
4
public class Main : Node
5
{
6
    [Export]
7
    private PackedScene knifePrefab;
8
9
    private Position2D minX;
10
    private Position2D maxX;
11
12
    private Timer restartTimer;
13
14
    public override void _Ready()
15
    {
16
        minX = GetNode<Position2D>("MinX");
17
        maxX = GetNode<Position2D>("MaxX");
18
19
        restartTimer = GetNode<Timer>("RestartTimer");
20
        restartTimer.Connect("timeout", this, "_OnRestartGame");
21
        restartTimer.WaitTime = 2f;
22
        restartTimer.OneShot = true;
23
    }
24
25
    private void _OnTimerTimeout()
26
    {
27
        Knife knife = knifePrefab.Instance() as Knife;
28
        float x = (float)GD.RandRange(minX.GetPosition().x, maxX.GetPosition().x);
29
30
        knife.Position = new Vector2(x, minX.GetPosition().y);
31
        AddChild(knife);
32
    }
33
34
    private void _OnRestartGame()
35
    {
36
        GetTree().ReloadCurrentScene();
37
    }
38
39
    public void PlayerDied()
40
    {
41
        restartTimer.Start();
42
    }
43
}

小结

参考:

下载

游戏

Release: https://github.com/GameDevLog/GameDevLogTemplete/releases

源码

GitHub: https://github.com/GameDevLog/GameDevLogTemplete

找到游戏列表对应的 GameDevLog

本文标题:Rainy Knifes

文章作者:iOSDevLog

发布时间:2020年01月09日 - 13:46:54

最后更新:2020年01月09日 - 21:38:34

原始链接:https://game.iosdevlog.com/2020/01/09/Rainy-Knifes/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

iOSDevLog wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!