Advertisement
DrCoeloCephalo

Kewt Game sample code

Jan 15th, 2024
962
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
GDScript 9.14 KB | Gaming | 0 0
  1. Player Code:
  2.  
  3. ```
  4. extends KinematicBody2D
  5.  
  6. export (int) var speed = 200
  7.  
  8. onready var InvincibilityTimer = $InvincibilityTimer
  9.  
  10. var velocity = Vector2()
  11.  
  12. var hurtAnimTime = false
  13. onready var HurtAnimTimer = $HurtAnimTimer
  14.  
  15. func get_input():
  16.     velocity = Vector2()
  17.     if Input.is_action_pressed("right") and Global.paused == false:
  18.         velocity.x += 1
  19.     if Input.is_action_pressed("left") and Global.paused == false:
  20.         velocity.x -= 1
  21.     if Input.is_action_pressed("down") and Global.paused == false:
  22.         velocity.y += 1
  23.     if Input.is_action_pressed("up") and Global.paused == false:
  24.         velocity.y -= 1
  25.     elif hurtAnimTime == true:
  26.         velocity.x = 0
  27.         velocity.y = 0
  28.     velocity = velocity.normalized() * speed
  29.    
  30.     if Input.is_action_just_pressed("pause"):
  31.         get_tree().call_group("Gamestate", "PauseGame")
  32.         Global.paused = true
  33.  
  34. func _physics_process(_delta):
  35.     get_input()
  36.     if velocity == Vector2.ZERO and hurtAnimTime == false:
  37.         $AnimationTree.get("parameters/playback").travel("idle")
  38.     elif hurtAnimTime == true:
  39.         $AnimationTree.get("parameters/playback").travel("hurt")
  40.         $AnimationTree.set("parameters/idle/blend_position", velocity)
  41.         $AnimationTree.set("parameters/hurt/blend_position", velocity)
  42.     else:
  43.         $AnimationTree.get("parameters/playback").travel("walk")
  44.         $AnimationTree.set("parameters/idle/blend_position", velocity)
  45.         $AnimationTree.set("parameters/walk/blend_position", velocity)
  46.     velocity = move_and_slide(velocity)
  47.  
  48. func hurt():
  49.     print("I'm hurt!")
  50.     $AnimationPlayer.play("red_flash")
  51.     $AnimationPlayer.queue("visible_flash")
  52.     if HurtAnimTimer.is_stopped():
  53.         hurtAnimTime = true
  54.         HurtAnimTimer.start()
  55.         #This should make our hurt animation play for when we are hit.
  56.     if InvincibilityTimer.is_stopped():
  57.         InvincibilityTimer.start()
  58.     Global.hurt = true
  59.  
  60. func heal():
  61.     print("I'm healed!")
  62.  
  63. func gem_up():
  64.     print("I got a gem!")
  65.  
  66.  
  67. func _on_InvincibilityTimer_timeout():
  68.     Global.hurt = false
  69.     $AnimationPlayer.stop()
  70.  
  71. func _on_HurtAnimTimer_timeout():
  72.     hurtAnimTime = false
  73.  
  74.  
  75. ```
  76.  
  77. Knight Enemy Code:
  78.  
  79. ```
  80. extends Node2D
  81.  
  82. var motion = Vector2()
  83. var state = 0
  84. #0 for nothing, 1 = right, 2 = left 3 = up 4 = down
  85.  
  86. var AttackAnimTime = false
  87. onready var AttackAnimTimer = $AttackAnimTimer
  88.  
  89.  
  90. func _ready():
  91.     pass
  92.     #print(state)
  93.    
  94.     add_to_group("Gamestate")
  95.  
  96. func _physics_process(delta):
  97.     if state == 0:
  98.         pass
  99.     elif state == 1:
  100.         motion.x = 100
  101.         #straigh right
  102.     elif state == 2:
  103.         motion.x = -100
  104.         #straight left
  105.     elif state == 3:
  106.         motion.y = 100
  107.         #straight down
  108.     elif state == 4:
  109.         motion.y = -100
  110.         #straight up
  111.     elif state == 5:
  112.         motion.y = -100
  113.         motion.x = -100
  114.         #diagonal up left
  115.     elif state == 6:
  116.         motion.x = 100
  117.         motion.y = 100
  118.         #diagonal down right
  119.     elif state == 7:
  120.         motion.x = 100
  121.         motion.y = -100
  122.         #diagonal up right
  123.     elif state == 8:
  124.         motion.x = -100
  125.         motion.y = 100
  126.         #diagonal down left
  127.    
  128.     if motion == Vector2.ZERO:
  129.         $AnimationTree.get("parameters/playback").travel("idle")
  130.     elif AttackAnimTime == true:
  131.         $AnimationTree.get("parameters/playback").travel("attack")
  132.         $AnimationTree.set("parameters/idle/blend_position", motion)
  133.         $AnimationTree.set("parameters/attack/blend_position", motion)
  134.     else:
  135.         $AnimationTree.get("parameters/playback").travel("walk")
  136.         $AnimationTree.set("parameters/idle/blend_position", motion)
  137.         $AnimationTree.set("parameters/walk/blend_position", motion)
  138.    
  139.     position += motion * delta
  140.  
  141. func _on_Hitbox_body_entered(body):
  142.     if body.is_in_group("Trees"):
  143.         #This makes it so if the knights collide with the tree, they despawn.
  144.         queue_free()
  145.         #print("Hit trees")
  146.     elif Global.hurt == false:
  147.         emit_signal("tree_exited")
  148.         get_tree().call_group("Gamestate", "hurt")
  149.  
  150. func _on_SightBox_body_entered(_body):
  151.     if AttackAnimTimer.is_stopped():
  152.         AttackAnimTime = true
  153.         AttackAnimTimer.start()
  154.  
  155.  
  156. func _on_AttackAnimTimer_timeout():
  157.     AttackAnimTime = false
  158.  
  159.  
  160. ```
  161.  
  162. Main Game Code:
  163.  
  164. ```
  165. extends Node2D
  166.  
  167. var rand_x
  168. var rand_y
  169.  
  170. var lives = 3
  171. var gems = 0
  172.  
  173. onready var pause_menu = $PauseMenu
  174.  
  175. onready var ResumeButton = $PauseMenu/MarginContainer/VBoxContainer/ResumeButton
  176. onready var MainMenuButton = $PauseMenu/MarginContainer/VBoxContainer/MainMenuButton
  177. onready var QuitButton = $PauseMenu/MarginContainer/VBoxContainer/QuitButton
  178.  
  179. var EasyTotal = 10
  180. var MediumTotal = 20
  181. var HardTotal = 50
  182.  
  183. onready var Spawn1 = $Spawn1
  184. onready var Spawn2 = $Spawn2
  185. onready var Spawn3 = $Spawn3
  186. onready var Spawn4 = $Spawn4
  187. onready var Spawn5 = $Spawn5
  188. onready var Spawn6 = $Spawn6
  189. onready var Spawn7 = $Spawn7
  190. onready var Spawn8 = $Spawn8
  191.  
  192. var SpawnArray = [1, 2, 3, 4, 5, 6, 7, 8]
  193. var SpawnArrayBasket = []
  194.  
  195. var SpawnChoice = 0
  196.  
  197. onready var GemTimer = $GemTimer
  198. onready var HeartTimer = $HeartTimer
  199. onready var KnightTimer = $KnightTimer
  200.  
  201. onready var Music = $Music
  202. onready var GemSound = $GemSound
  203. onready var HealSound = $HealSound
  204. onready var HitSound = $HitSound
  205.  
  206. func _ready():
  207.     add_to_group("Gamestate")
  208.    
  209.     Global.game_playing = true
  210.    
  211.     if Global.NewGemTotal == 10:
  212.         GemTimer.wait_time = 3
  213.         HeartTimer.wait_time = 15
  214.         KnightTimer.wait_time = 2
  215.     elif Global.NewGemTotal == 20:
  216.         GemTimer.wait_time = 4
  217.         HeartTimer.wait_time = 20
  218.         KnightTimer.wait_time = 1.5
  219.     else:
  220.         GemTimer.wait_time = 5
  221.         HeartTimer.wait_time = 25
  222.         KnightTimer.wait_time = 0.5
  223.    
  224.     GemTimer.start()
  225.     HeartTimer.start()
  226.     print(SpawnArray)
  227.     randomize()
  228.     SpawnArrayBasket = SpawnArray.duplicate()
  229.     SpawnArray.shuffle()
  230.     Music.play()
  231.  
  232.  
  233. func PauseGame():
  234.     if Global.paused and Global.game_playing:
  235.         pause_menu.show()
  236.         Engine.time_scale = 0
  237.         ResumeButton.grab_focus()
  238.     else:
  239.         pause_menu.hide()
  240.         Engine.time_scale = 1
  241.         Global.paused = false
  242.  
  243. func StartGame():
  244.     pass
  245. # Called every frame. 'delta' is the elapsed time since the previous frame.
  246. #func _process(delta):
  247. #   pass
  248.  
  249. func _on_HeartTimer_timeout():
  250.     var NewHeart = preload("res://Player and Enemies/Heart.tscn").instance()
  251.     rand_x = rand_range(70, 500)
  252.     rand_y = rand_range(70, 550)
  253.     NewHeart.global_position = Vector2(rand_x, rand_y)
  254.     add_child(NewHeart)
  255.  
  256. func _on_GemTimer_timeout():
  257.     var NewGem = preload("res://Player and Enemies/Gem.tscn").instance()
  258.     rand_x = rand_range(70, 500)
  259.     rand_y = rand_range(70, 550)
  260.     NewGem.global_position = Vector2(rand_x, rand_y)
  261.     add_child(NewGem)
  262.  
  263. func RollForNumber():
  264.     if SpawnArray.empty():
  265.         #Fill the SpawnArray again and shuffle it.
  266.         #Makes it so it is way less likely to get the same random
  267.         #variable twice in a row.
  268.         SpawnArray = SpawnArrayBasket.duplicate()
  269.         SpawnArray.shuffle()
  270.         var FirstSpawn = SpawnArray.pop_front()
  271.         return FirstSpawn
  272.     else:
  273.         SpawnChoice = int(SpawnArray[randi() % SpawnArray.size()])
  274.         #print(SpawnChoice)
  275.  
  276. func SpawnPlace():
  277.     var NewKnight = preload("res://Player and Enemies/KnightEnemyAnimated.tscn").instance()
  278.     if SpawnChoice == 1:
  279.         NewKnight.global_position = Spawn1.position
  280.         add_child(NewKnight)
  281.         NewKnight.state = 6
  282.     if SpawnChoice == 2:
  283.         NewKnight.global_position = Spawn2.position
  284.         add_child(NewKnight)
  285.         var DirectionArray = [1, 6, 7]
  286.         var DirectionChoice = int(DirectionArray[randi() % DirectionArray.size()])
  287.         NewKnight.state = DirectionChoice
  288.     if SpawnChoice == 3:
  289.         NewKnight.global_position = Spawn3.position
  290.         add_child(NewKnight)
  291.         NewKnight.state = 7
  292.     if SpawnChoice == 4:
  293.         NewKnight.global_position = Spawn4.position
  294.         add_child(NewKnight)
  295.         var DirectionArray = [3, 6, 8]
  296.         var DirectionChoice = int(DirectionArray[randi() % DirectionArray.size()])
  297.         NewKnight.state = DirectionChoice
  298.     if SpawnChoice == 5:
  299.         NewKnight.global_position = Spawn5.position
  300.         add_child(NewKnight)
  301.         var DirectionArray = [4, 5, 7]
  302.         var DirectionChoice = int(DirectionArray[randi() % DirectionArray.size()])
  303.         NewKnight.state = DirectionChoice
  304.     if SpawnChoice == 6:
  305.         NewKnight.global_position = Spawn6.position
  306.         add_child(NewKnight)
  307.         NewKnight.state = 8
  308.     if SpawnChoice == 7:
  309.         NewKnight.global_position = Spawn7.position
  310.         add_child(NewKnight)
  311.         var DirectionArray = [2, 5, 8]
  312.         var DirectionChoice = int(DirectionArray[randi() % DirectionArray.size()])
  313.         NewKnight.state = DirectionChoice
  314.     if SpawnChoice == 8:
  315.         NewKnight.global_position = Spawn8.position
  316.         add_child(NewKnight)
  317.         NewKnight.state = 5
  318.     else:
  319.         pass
  320.     var DirectionVariable = NewKnight.get("state")
  321.     #print(DirectionVariable)
  322.     if DirectionVariable == 1:
  323.         pass
  324.  
  325. func _on_KnightTimer_timeout():
  326.     RollForNumber()
  327.     SpawnPlace()
  328.  
  329. func heal():
  330.     lives += 1
  331.     $Player.heal()
  332.     HealSound.play()
  333.     get_tree().call_group("Lives+Gems", "heal", lives)
  334.     if lives == 0:
  335.         end_game()
  336.  
  337. func hurt():
  338.     lives -= 1
  339.     $Player.hurt()
  340.     HitSound.play()
  341.     get_tree().call_group("Lives+Gems", "hurt", lives)
  342.     if lives == 0:
  343.         end_game()
  344.  
  345.  
  346. func gem_up():
  347.     #Make sure plus sign is first to properly change scenes
  348.     gems += 1
  349.     GemSound.play()
  350.     if gems == Global.NewGemTotal:
  351.         won_game()
  352.     else:
  353.         $Player.gem_up()
  354.         get_tree().call_group("Lives+Gems", "gem_up", gems)
  355.  
  356. func won_game():
  357. # warning-ignore:return_value_discarded
  358.     get_tree().change_scene("res://Levels/WinScreen.tscn")
  359. func end_game():
  360. # warning-ignore:return_value_discarded
  361.     get_tree().change_scene("res://Levels/LoseScreen.tscn")
  362.    
  363.  
  364.  
  365. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement