- Godot Engine Game Development Projects
- Chris Bradfield
- 276字
- 2021-06-18 18:51:29
Starting a new game
Next, the new_game() function will initialize everything for a new game:
func new_game():
playing = true
level = 1
score = 0
time_left = playtime
$Player.start($PlayerStart.position)
$Player.show()
$GameTimer.start()
spawn_coins()
In addition to setting the variables to their starting values, this function calls the Player's start() function to ensure it moves to the proper starting location. The game timer is started, which will count down the remaining time in the game.
You also need a function that will create a number of coins based on the current level:
func spawn_coins():
for i in range(4 + level):
var c = Coin.instance()
$CoinContainer.add_child(c)
c.screensize = screensize
c.position = Vector2(rand_range(0, screensize.x),
rand_range(0, screensize.y))
In this function, you create a number of instances of the Coin object (in code this time, rather than by clicking the Instance a Scene button), and add it as a child of the CoinContainer. Whenever you instance a new node, it must be added to the tree using add_child(). Finally, you pick a random location for the coin to appear in. You'll call this function at the start of every level, generating more coins each time.
Eventually, you'll want new_game() to be called when the player clicks the start button. For now, to test if everything is working, add new_game() to the end of your _ready() function and click Play the Project (F5). When you are prompted to choose a main scene, choose Main.tscn. Now, whenever you play the project, the Main scene will be started.
At this point, you should see your player and five coins appear on the screen. When the player touches a coin, it disappears.
- Hands-On Intelligent Agents with OpenAI Gym
- 走入IBM小型機世界
- 數據運營之路:掘金數據化時代
- Creo Parametric 1.0中文版從入門到精通
- Implementing AWS:Design,Build,and Manage your Infrastructure
- 網絡安全與防護
- 中國戰略性新興產業研究與發展·工業機器人
- 單片機C語言應用100例
- 手機游戲程序開發
- 大數據案例精析
- Mastering OpenStack(Second Edition)
- 自適應學習:人工智能時代的教育革命
- 從祖先到算法:加速進化的人類文化
- Hands-On Generative Adversarial Networks with Keras
- 特征工程入門與實踐