官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 阳曲县| 栖霞市| 年辖:市辖区| 中宁县| 平武县| 台前县| 集贤县| 乌鲁木齐县| 宣武区| 绵竹市| 牡丹江市| 绵阳市| 襄汾县| 澄城县| 合作市| 鹿邑县| 静乐县| 眉山市| 和林格尔县| 永仁县| 朝阳区| 新疆| 江陵县| 德保县| 永宁县| 农安县| 寿光市| 保康县| 上栗县| 沙田区| 南木林县| 鞍山市| 广灵县| 莎车县| 子洲县| 鹤壁市| 泗水县| 盐城市| 望都县| 贡觉县| 郧西县|