onready var _labelScore = $MarginContainer/HBoxContainer/LabelScore onready var _labelTime = $MarginContainer/HBoxContainer/LabelTime onready var _labelMessage = $VBoxContainer/LabelMessage onready var _labelReady = $VBoxContainer/LabelReady onready var _buttonStart = $MarginContainer2/ButtonStart
# 当前游戏是否被暂停,初始为“是” var _isPaused = true
# 监听用户的输入 func _input(event): if event.is_action_pressed('start'): # 这个if条件语句只会在游戏开始时运行一次! if self.get_tree().paused != _isPaused: self.emit_signal('start_game')
export(PackedScene) var coinScene = null export(PackedScene) var powerScene = null export(float) var minPlayerDist = 80 export(float) var minObstacleDist = 120
onready var _player = $Player onready var _startPosition = _player.position onready var _ui = $HUD/UI onready var _pointsCurve = $CactusPoints.curve onready var _cactus = $CactusPoints/Cactus onready var _coinContainer = $CoinContainer onready var _countTimer = $CountTimer onready var _powerTimer = $PowerTimer onready var _gameOverAudioPlayer = $GameOverAudio onready var _levelAudioPlayer = $LevelUpAuido
var _level = 0# 当前关卡 var _timeLeft = 0# 剩余时间 var _totalCoins = 0# 金币总数 var _collectedCoins = 0# 收集金币数
# 创建当前关卡的所有金币 func _spawnCoins(): if coinScene == null: return var playerPos = _player.position var obstaclePos = _cactus.position for i in range(_totalCoins): var coin = coinScene.instance() var pos = _makeRandomPosition() # 如果金币产生位置在玩家或者障碍物内,则重新生成一个位置 while pos.distance_to(playerPos) < minPlayerDist || pos.distance_to(obstaclePos) < minObstacleDist: pos = _makeRandomPosition() coin.position = pos _coinContainer.add_child(coin)
# 设置当前关卡的障碍物置 func _spawnObstacles(): var index = randi() % _pointsCurve.get_point_count() var position = _pointsCurve.get_point_position(index) _cactus.position = position
# 根据窗口尺寸设计随机金币位置 func _makeRandomPosition(): var x = rand_range(0, ProjectSettings.get('display/window/size/width')) var y = rand_range(0, ProjectSettings.get('display/window/size/height')) return Vector2(x, y)