Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #main.gd
- extends Node
- @export var alien_scene: PackedScene
- var score = 0
- func _ready():
- randomize()
- func new_game():
- score = 0
- $StartTimer.start()
- await $StartTimer.timeout
- $ScoreTimer.start()
- $AlienTimer.start()
- func game_over():
- $ScoreTimer.stop()
- func _on_alien_timer_timeout():
- var alien_spawn_location = $AlienPath/AlienSpawnLocation
- alien_spawn_location.progress_ratio = randf()
- var alien = alien_scene.instance()
- add_child(alien)
- alien.position = alien_spawn_location.position
- var direction = alien_spawn_location.rotation + PI / 2
- direction += randf_range(-PI / 4, PI / 4)
- alien.rotation = direction
- var velocity = Vector2(randf_range(alien.min_speed, alien.max_speed), 0)
- alien.linear_velocity = velocity.rotated(direction)
- print(alien_spawn_location)
- print(velocity)
- func _on_score_timer_timeout():
- score += 1
- $HUD.update_score(score)
- #alien.gd
- extends RigidBody2D
- @export var min_speed = 150.0
- @export var max_speed = 250.0
- func _ready():
- $AnimatedSprite2D.play()
- var alien_types = $AnimatedSprite2D.sprite_frames.get_animation_names()
- $AnimatedSprite2D.animation = alien_types[randi() % alien_types.size()]
- func _on_visible_on_screen_notifier_2d_screen_exited():
- queue_free()
- #HUD.gd
- extends CanvasLayer
- signal start_game
- func update_score(score):
- $ScoreLabel.text = str(score)
- func show_message(text):
- $MessageLabel.text = text
- $MessageLabel.show()
- $MessageTimer.start()
- func show_game_over():
- show_message("Game Over")
- func _on_button_pressed():
- $Button.hide()
- emit_signal("start_game")
- func _on_message_timer_timeout():
- $MessageLabel.hide()
- #player.gd
- extends Area2D
- signal hit
- @export var speed = 400.0
- var screen_size = Vector2.ZERO
- func _ready():
- screen_size = get_viewport_rect().size
- #print(screen_size)
- func _process(delta):
- var direction = Vector2.ZERO
- if Input.is_action_pressed("move_right"):
- direction.x += 1
- if Input.is_action_pressed("move_left"):
- direction.x -= 1
- if Input.is_action_pressed("move_up"):
- direction.y -= 1
- if Input.is_action_pressed("move_down"):
- direction.y += 1
- if direction.length() > 0:
- direction = direction.normalized()
- $AnimatedSprite2D.play()
- else:
- $AnimatedSprite2D.stop()
- position += direction * speed * delta
- position.x = clamp(position.x, 0, screen_size.x)
- position.y = clamp(position.y, 0, screen_size.y)
- if direction.x != 0:
- $AnimatedSprite2D.animation = "right"
- $AnimatedSprite2D.flip_h = direction.x < 0
- $AnimatedSprite2D.flip_v = false
- elif direction.y != 0:
- $AnimatedSprite2D.animation = "up"
- $AnimatedSprite2D.flip_v = direction.y > 0
- func start(new_position):
- position = new_position
- show()
- $CollisionShape2D.disabled = false
- func _on_body_entered(body):
- hide()
- $CollisionShape2D.set_deferred("disabled", true)
- emit_signal("hit")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement