Advertisement
Junaid_Hossain

Dodge the Creeps

Jul 7th, 2023
1,577
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #main.gd
  2.  
  3. extends Node
  4.  
  5. @export var alien_scene: PackedScene
  6. var score = 0
  7.  
  8. func _ready():
  9.     randomize()
  10.    
  11. func new_game():
  12.     score = 0
  13.     $StartTimer.start()
  14.     await $StartTimer.timeout
  15.     $ScoreTimer.start()
  16.     $AlienTimer.start()
  17.    
  18. func game_over():
  19.     $ScoreTimer.stop()
  20.  
  21. func _on_alien_timer_timeout():
  22.     var alien_spawn_location = $AlienPath/AlienSpawnLocation
  23.     alien_spawn_location.progress_ratio = randf()
  24.  
  25.     var alien = alien_scene.instance()
  26.     add_child(alien)
  27.  
  28.     alien.position = alien_spawn_location.position
  29.  
  30.     var direction = alien_spawn_location.rotation + PI / 2
  31.     direction += randf_range(-PI / 4, PI / 4)
  32.     alien.rotation = direction
  33.  
  34.     var velocity = Vector2(randf_range(alien.min_speed, alien.max_speed), 0)
  35.     alien.linear_velocity = velocity.rotated(direction)
  36.     print(alien_spawn_location)
  37.     print(velocity)
  38.  
  39.  
  40.  
  41. func _on_score_timer_timeout():
  42.     score += 1
  43.     $HUD.update_score(score)
  44.  
  45.  
  46. #alien.gd
  47. extends RigidBody2D
  48.  
  49. @export var min_speed = 150.0
  50. @export var max_speed = 250.0
  51.  
  52. func _ready():
  53.     $AnimatedSprite2D.play()
  54.     var alien_types = $AnimatedSprite2D.sprite_frames.get_animation_names()
  55.     $AnimatedSprite2D.animation = alien_types[randi() % alien_types.size()]
  56.  
  57.  
  58. func _on_visible_on_screen_notifier_2d_screen_exited():
  59.     queue_free()
  60.  
  61.  
  62. #HUD.gd
  63. extends CanvasLayer
  64.  
  65. signal start_game
  66.  
  67. func update_score(score):
  68.     $ScoreLabel.text = str(score)
  69.    
  70. func show_message(text):
  71.     $MessageLabel.text = text
  72.     $MessageLabel.show()
  73.     $MessageTimer.start()
  74.    
  75. func show_game_over():
  76.     show_message("Game Over")
  77.  
  78. func _on_button_pressed():
  79.     $Button.hide()
  80.     emit_signal("start_game")
  81.  
  82.  
  83. func _on_message_timer_timeout():
  84.     $MessageLabel.hide()
  85.  
  86.  
  87.  
  88.  
  89. #player.gd
  90. extends Area2D
  91.  
  92. signal hit
  93.  
  94. @export var speed = 400.0
  95. var screen_size = Vector2.ZERO
  96.  
  97. func _ready():
  98.     screen_size = get_viewport_rect().size
  99.     #print(screen_size)
  100.  
  101. func _process(delta):
  102.     var direction = Vector2.ZERO
  103.     if Input.is_action_pressed("move_right"):
  104.         direction.x += 1
  105.     if Input.is_action_pressed("move_left"):
  106.         direction.x -= 1
  107.     if Input.is_action_pressed("move_up"):
  108.         direction.y -= 1
  109.     if Input.is_action_pressed("move_down"):
  110.         direction.y += 1
  111.    
  112.     if direction.length() > 0:
  113.         direction = direction.normalized()
  114.         $AnimatedSprite2D.play()
  115.     else:
  116.         $AnimatedSprite2D.stop()
  117.        
  118.     position += direction * speed * delta
  119.     position.x = clamp(position.x, 0, screen_size.x)
  120.     position.y = clamp(position.y, 0, screen_size.y)
  121.    
  122.     if direction.x != 0:
  123.         $AnimatedSprite2D.animation = "right"
  124.         $AnimatedSprite2D.flip_h = direction.x < 0
  125.         $AnimatedSprite2D.flip_v = false
  126.     elif direction.y != 0:
  127.         $AnimatedSprite2D.animation = "up"
  128.         $AnimatedSprite2D.flip_v = direction.y > 0
  129.  
  130. func start(new_position):
  131.     position = new_position
  132.     show()
  133.     $CollisionShape2D.disabled = false
  134.  
  135. func _on_body_entered(body):
  136.     hide()
  137.     $CollisionShape2D.set_deferred("disabled", true)
  138.     emit_signal("hit")
  139.  
  140.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement