![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | khal2furious |
So i am new to Godot but i am trying to learng how to make a my first game. I am having issues with the dodge the creeps game. when running the game I dont see any enemies but the timer is counting and i am walking around. a few seconds in, the player disappears but the timer continues (leading me to believe the enemies are invisible but there is no game over message).
One thing to mention before looking at the code, all mobs have been changed to zombies. so whenerv you see zomies that means mob n the case of the dodge the creeps game. if you could explain it to me in zombie, it would be better so I dont get confused.
I code for everything so far is below:
HUD.gd
extends CanvasLayer
signal start_game
func show_message(text):
$MessageLabel.text = text
$MessageLabel.show()
$MessageTimer.start()
func game_over():
show_message(“Game Over”)
yield($MessageTimer, “timeout”)
$StartButton.show()
$MessageLabel.text = “Don’t Touch \nMe”
$MessageLabel.show()
func update_score(score):
$ScoreLabel.text = str(score)
func _on_MessageTimer_timeout():
$MessageLabel.hide()
func _on_StartButton_pressed():
$StartButton.hide()
emit_signal(“start_game”)
-player.gd
extends Area2D
signal hit
export(int) var SPEED
var velocity = Vector2()
var screensize
func _ready():
hide()
screensize = get_viewport_rect().size
func _process(delta):
velocity = Vector2()
velocity = velocity.normalized() * SPEED
if Input.is_action_pressed(“ui_right”):
velocity.x += 1
if Input.is_action_pressed(“ui_left”):
velocity.x -= 1
if Input.is_action_pressed(“ui_down”):
velocity.y += 1
if Input.is_action_pressed(“ui_up”):
velocity.y -= 1
if velocity.length() > 0:
$AnimatedSprite.play()
velocity = velocity.normalized() * SPEED
else:
$AnimatedSprite.animation = “Left”
$AnimatedSprite.stop()
position += velocity * delta
position.x = clamp(position.x, 0, screensize.x)
position.y = clamp(position.y, 0, screensize.y)
if velocity.x != 0 || velocity.y != 0:
$AnimatedSprite.animation = "Right"
$AnimatedSprite.flip_v = false
$AnimatedSprite.flip_h = velocity.x < 0
#for up of down flipping
#elif velocity.y != 0:
#$AnimatedSprite.animation = "Left"
#$AnimatedSprite.flip_v = velocity.y < 0
#$AnimatedSprite.flip_h = false
func _on_player_body_entered(body):
hide()
emit_signal(“hit”)
$CollisionShape2D.disabled = true
func start(pos):
position = pos
show()
$CollisionShape2D.disabled = false
-Main.gd
extends Node
export (PackedScene) var Zombie
var score
func _ready():
randomize()
func new_game():
score = 0
$player.start($StartPosition.position)
$StartTimer.start()
$HUD.update_score(score)
$HUD.show_message(“Get Ready”)
func game_over():
$ScoreTimer.stop()
$ZombieTimer.stop()
$HUD.game_over()
func _on_StartTimer_timeout():
$ZombieTimer.start()
$ScoreTimer.start()
func _on_ScoreTimer_timeout():
score += 1
$HUD.update_score(score)
func _on_ZombieTimer_timeout():
$ZombiePath/ZombieSpawnLocation.set_offset(randi())
var zombie = Zombie.instance() #create a zombie and add to the scene
add_child(zombie)
var direction = $ZombiePath/ZombieSpawnLocation.rotation
zombie.position = $ZombiePath/ZombieSpawnLocation.position
direction += rand_range(-PI/4, PI/4) #to not only move up, down, left, right (add randomness)
zombie.rotation = direction
zombie.set_linear_velocity(Vector2(rand_range(zombie.MIN_SPEED, zombie.MAX_SPEED), 0).rotated(direction))
Zombie.gd
extends RigidBody2D
export(int) var MIN_SPEED #min speed of enemy
export(int) var MAX_SPEED #max speed of enemy
var mob_types = [“Zombie”, “ZombieStrong”, “ZombieFat”]
func _ready():
$AnimatedSprite.animation = mob_types[randi() % mob_types.size()]
func _on_VisibilityNotifier2D_screen_exited():
queue_free()