游戏展示
游戏开发
1 | git clone https://github.com/GameDevLog/Rainy-Knifes |
- Project Settings > General > Display > Window
- Size
- Width:
480
- Height:
800
- Width:
- Stretch
- Mode:
2d
- Aspect:
keep
- Mode:
- Size
导入资源
Scene
- Node:
Main
- Sprites:
Background
- Node2D:
GroundParent
- Sprites:
ground
x 4
- Sprites:
- Sprites:
Inspector
- Background > Node2D > Transform
- Position
- x:
241.067
- y:
403.912
- x:
- Scale
- x:
0.67
- y:
0.63
- x:
- Position
创建动画
Scene
- Node2D:
Player
- Sprites:
Idle
- AnimationPlayer:
IdleAnimation
- Sprites:
Walk
- AnimationPlayer:
WalkAnimation
- Sprites:
Inspector
Player > Node2D > Transform
- Position
- x:
240
- y:
480
- x:
- Position
Idle
- Sprite > Animation
- Hframes:
2
- Hframes:
- Node2D > Transform > Scale
- x:
0.5
- y:
0.5
- x:
- Sprite > Animation
WalkAnimation > AnimationPlayer > Playback Options
- Speed:
3
- Speed:
Animation
New:
Idle
- Animation Length:
0.2
- Current Pos.:
0
- Animation Length:
Idle > Sprite > Animation
- Frame:
0
+
- Frame:
New:
Walk
- Animation Length:
1
- Animation Length:
移动 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 | [ ] |
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
- Sprite:
Inspector
- Knife > Node2D > Transform
- Rotation Degree:
-90
- Rotation Degree:
Scene
- Main
- Position2D:
MinX
- Position2D:
MaxX
- Timer
- Position2D:
Inspector
- MinX > Node2D > Transform > Position
- x:
10
- y:
-50
- x:
- MaxX > Node2D > Transform > Position
- x:
470
- y:
-50
- x:
- Timer > Timer
- Wait Time:
0.7
- Autostart:
True
- Wait Time:
碰撞检测
Inspector > Node2D > Z Index
Z Index
- Background:
0
- backg:
1
- Player:
2
- Knife:
3
- Background:
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
- Timer:
Main.cs
1 | using Godot; |
2 | using System; |
3 | |
4 | public class Main : Node |
5 | { |
6 | [ ] |
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