Maze Game Problem (Enemies)

Creating Games? Developing something for fun?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Moorky
Posts: 3
Joined: Sat Nov 08, 2014 11:30 pm

Maze Game Problem (Enemies)

Post by Moorky » Sat Nov 08, 2014 11:43 pm

Hey Guys,

I made a Maze Game in Livecode but I have a problem because there are enemies in my Maze who can move and if you touch them you will respawn at the start.
Everything works fine but I don't know where I have to include the moving code of the enemie...
I know I have to include it in the Card Code but in which "command box" of it?
I tried to include the code it in "on openCard" but the program doesn't react after I start it the game (maybe overloaded).

Here is the code of the enemie moving:

Code: Select all

repeat forever
      put random(4) into Direction
      
      if Direction = 1 then
         set the top of graphic "enemie1" to the top of graphic "enemie1" - 3
         if top of graphic "enemie1" < 0 then
            set the top of graphic "enemie1" to 0
         end if
      end if
      
      if Direction = 2 then
         set the bottom of graphic "enemie1" to the bottom of graphic "enemie1" + 3
         if the bottom of graphic "enemie1" > the bottom of card "mycard" then
            set the bottom of graphic "enemie1" to the bottom of card "mycard"
         end if
      end if
      
      if Direction = 3 then
         set the left of graphic "enemie1" to the left of graphic "box" - 3
         if the left of graphic "enemie1" < 0 then
            set the left of graphic "enemie1" to 0
         end if
      end if
      
      if Direction = 4 then
         set the right of graphic "enemie1" to the right of graphic "enemie1" + 3
         if right of graphic "enemie1" > the right of card "mycard" then
            set the right of graphic "enemie1" to the right of card "mycard"
         end if
      end if
      wait 250 milliseconds
   end repeat
And if you want to see the whole Card Code:

Code: Select all

local Coins, Coin1, Coin2, Coin3, Coin4, Coin5, Coin6, Coin7, Coin8, Coin9, Coin10, Coin11, Coin12
local Direction

on openCard
   play "BGMusic.aif" looping
   put 0 into Coins
   put 0 into Coin1
   put 0 into Coin2
   put 0 into Coin3
   put 0 into Coin4
   put 0 into Coin5
   put 0 into Coin6
   put 0 into Coin7
   put 0 into Coin8
   put 0 into Coin9
   put 0 into Coin10
   put 0 into Coin11
   put 0 into Coin12
   put 0 into Direction
   
   repeat forever
      put random(4) into Direction
      
      if Direction = 1 then
         set the top of graphic "enemie1" to the top of graphic "enemie1" - 3
         if top of graphic "enemie1" < 0 then
            set the top of graphic "enemie1" to 0
         end if
      end if
      
      if Direction = 2 then
         set the bottom of graphic "enemie1" to the bottom of graphic "enemie1" + 3
         if the bottom of graphic "enemie1" > the bottom of card "mycard" then
            set the bottom of graphic "enemie1" to the bottom of card "mycard"
         end if
      end if
      
      if Direction = 3 then
         set the left of graphic "enemie1" to the left of graphic "box" - 3
         if the left of graphic "enemie1" < 0 then
            set the left of graphic "enemie1" to 0
         end if
      end if
      
      if Direction = 4 then
         set the right of graphic "enemie1" to the right of graphic "enemie1" + 3
         if right of graphic "enemie1" > the right of card "mycard" then
            set the right of graphic "enemie1" to the right of card "mycard"
         end if
      end if
      wait 250 milliseconds
   end repeat
   
end openCard

on arrowkey x
   
   --Controls
   if x is "up" then
      set the top of graphic "box" to the top of graphic "box" - 3
      if top of graphic "box" < 0 then
         set the top of graphic "box" to 0
      end if
      if intersect( graphic "box" , group "maze" , "opaque pixels") then
         set the bottom of graphic "box" to the bottom of graphic "box" + 3.1
      end if 
   end if
   
   if x is "down" then
      set the bottom of graphic "box" to the bottom of graphic "box" + 3
      if the bottom of graphic "box" > the bottom of card "mycard" then
         set the bottom of graphic "box" to the bottom of card "mycard"
      end if
      if intersect( graphic "box" , group "maze" , "opaque pixels") then
         set the bottom of graphic "box" to the bottom of graphic "box" - 3.1
      end if 
   end if
   
   if x is "left" then
      set the left of graphic "box" to the left of graphic "box" - 3
      if the left of graphic "box" < 0 then
         set the left of graphic "box" to 0
      end if
      if intersect( graphic "box" , group "maze" , "opaque pixels") then
         set the left of graphic "box" to the left of graphic "box" + 3.1
      end if 
   end if
   
   if x is "right" then
      set the right of graphic "box" to the right of graphic "box" + 3
      if right of graphic "box" > the right of card "mycard" then
         set the right of graphic "box" to the right of card "mycard"
      end if
      if intersect( graphic "box" , group "maze" , "opaque pixels") then
         set the right of graphic "box" to the right of graphic "box" - 3.1
      end if 
   end if
   --
   
   --Enemies
   if intersect( graphic "box" , graphic "enemie1", "opaque pixels") then
      set the loc of graphic "box" to 120,70
   end if 
   if intersect( graphic "box" , graphic "enemie2", "opaque pixels") then
      set the loc of graphic "box" to 120,70
   end if 
   if intersect( graphic "box" , graphic "enemie3", "opaque pixels") then
      set the loc of graphic "box" to 120,70
   end if 
   if intersect( graphic "box" , graphic "enemie4", "opaque pixels") then
      set the loc of graphic "box" to 120,70
   end if 
   if intersect( graphic "box" , graphic "enemie5", "opaque pixels") then
      set the loc of graphic "box" to 120,70
   end if 
   if intersect( graphic "box" , graphic "enemie6", "opaque pixels") then
      set the loc of graphic "box" to 120,70
   end if 
   --
   
   --Coins
   if intersect( graphic "box" , graphic "Coin1", "opaque pixels") then
      if Coin1 = 0 then
         add 1 to Coins
      end if
      put 1 into Coin1
      hide graphic "Coin1"
      put Coins into field "Coinsv"
   end if 
   if intersect( graphic "box" , graphic "Coin2", "opaque pixels") then
      if Coin2 = 0 then
         add 1 to Coins
      end if
      put 1 into Coin2
      hide graphic "Coin2"
      put Coins into field "Coinsv"
   end if 
   if intersect( graphic "box" , graphic "Coin3", "opaque pixels") then
      if Coin3 = 0 then
         add 1 to Coins
      end if
      put 1 into Coin3
      hide graphic "Coin3"
      put Coins into field "Coinsv"
   end if 
   if intersect( graphic "box" , graphic "Coin4", "opaque pixels") then
      if Coin4 = 0 then
         add 1 to Coins
      end if
      put 1 into Coin4
      hide graphic "Coin4"
      put Coins into field "Coinsv"
   end if 
   if intersect( graphic "box" , graphic "Coin5", "opaque pixels") then
      if Coin5 = 0 then
         add 1 to Coins
      end if
      put 1 into Coin5
      hide graphic "Coin5"
      put Coins into field "Coinsv"
   end if 
   if intersect( graphic "box" , graphic "Coin6", "opaque pixels") then
      if Coin6 = 0 then
         add 1 to Coins
      end if
      put 1 into Coin6
      hide graphic "Coin6"
      put Coins into field "Coinsv"
   end if 
   if intersect( graphic "box" , graphic "Coin7", "opaque pixels") then
      if Coin7 = 0 then
         add 1 to Coins
      end if
      put 1 into Coin7
      hide graphic "Coin7"
      put Coins into field "Coinsv"
   end if 
   if intersect( graphic "box" , graphic "Coin8", "opaque pixels") then
      if Coin8 = 0 then
         add 1 to Coins
      end if
      put 1 into Coin8
      hide graphic "Coin8"
      put Coins into field "Coinsv"
   end if 
   if intersect( graphic "box" , graphic "Coin9", "opaque pixels") then
      if Coin9 = 0 then
         add 1 to Coins
      end if
      put 1 into Coin9
      hide graphic "Coin9"
      put Coins into field "Coinsv"
   end if 
   if intersect( graphic "box" , graphic "Coin10", "opaque pixels") then
      if Coin10 = 0 then
         add 1 to Coins
      end if
      put 1 into Coin10
      hide graphic "Coin10"
      put Coins into field "Coinsv"
   end if 
   if intersect( graphic "box" , graphic "Coin11", "opaque pixels") then
      if Coin11 = 0 then
         add 1 to Coins
      end if
      put 1 into Coin11
      hide graphic "Coin11"
      put Coins into field "Coinsv"
   end if 
   if intersect( graphic "box" , graphic "Coin12", "opaque pixels") then
      if Coin12 = 0 then
         add 1 to Coins
      end if
      put 1 into Coin12
      hide graphic "Coin12"
      put Coins into field "Coinsv"
   end if 
   --
   
   --Goal
   if intersect( graphic "box" , graphic "Goal" , "opaque pixels") then
      set the loc of graphic "box" to 120,70
      show graphic "Coin1"
      show graphic "Coin2"
      show graphic "Coin3"
      show graphic "Coin4"
      show graphic "Coin5"
      show graphic "Coin6"
      show graphic "Coin7"
      show graphic "Coin8"
      show graphic "Coin9"
      show graphic "Coin10"
      show graphic "Coin11"
      show graphic "Coin12"
      put 0 into Coins
      put 0 into Coin1
      put 0 into Coin2
      put 0 into Coin3
      put 0 into Coin4
      put 0 into Coin5
      put 0 into Coin6
      put 0 into Coin7
      put 0 into Coin8
      put 0 into Coin9
      put 0 into Coin10
      put 0 into Coin11
      put 0 into Coin12
      put Coins into field "Coinsv"
      go card 3
   end if 
   --
   
end arrowKey

on closeCard
   play stop
   put 0 into Coin1
   put 0 into Coin2
   put 0 into Coin3
   put 0 into Coin4
   put 0 into Coin5
   put 0 into Coin6
   put 0 into Coin7
   put 0 into Coin8
   put 0 into Coin9
   put 0 into Coin10
   put 0 into Coin11
   put 0 into Coin12
end closeCard
So please tell me where I have to include the enemie moving in my card code or tell me if there is a mistake in the code of moving. (I started with LiveCode 2 Days ago.. I'm not experienced)

With best regards, Moorky.

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: Maze Game Problem (Enemies)

Post by Simon » Sun Nov 09, 2014 1:07 am

Hi Moorky,
Welcome to the forum!

You are doing great for just 2 days!
Your idea of a "command box" is what we call a "handler" and you get to make your own handlers e.g. moveEnemy, dogBite, smellySocks. You can name them almost anything you like, don't worry liveCode will tell you when you try to use a "reserved word".

In your case it would be something like

Code: Select all

on moveEnemy
--your complete repeat loop---
end moveEnemy
For the openCard you would use

Code: Select all

on openCard
blah
blah
blah
moveEnemy
end openCard
Now your repeat forever is not really good form, get rid of the repeat and use something like this

Code: Select all

on moveEnemy
--remove repeat loop but keep the stuff inside of it---
send moveEnemy to me in 250 milliseconds
end moveEnemy
You see how that is working? It's calling itself :) wicked cool
To stop it in your close card you would put

Code: Select all

repeat for each line tLine in the pendingMessages
cancel item 1 of tLIne
end repeat
That "send" becomes a pending message that you must cancel out.

There is a bunch more that should be done as checks but I'll let you start with that.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

Moorky
Posts: 3
Joined: Sat Nov 08, 2014 11:30 pm

Re: Maze Game Problem (Enemies)

Post by Moorky » Mon Nov 10, 2014 4:52 pm

Aww thank you so much! :)

Post Reply

Return to “Games”