This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2012-04-26 15:37:39

leonreynolds
New Scratcher
Registered: 2012-04-26
Posts: 1

Pacman evil guy

Hey how can i make one AI sprite go round a map in random directions without getting stuck?

Offline

 

#2 2012-04-29 05:31:06

jontmy00
Scratcher
Registered: 2011-11-28
Posts: 1000+

Re: Pacman evil guy

Maybe you should check out other people's Pacman projects.  wink


FOR ALL THE NEWS ON UPDATES FOR SIMPLISTICRAFT, CLICK HERE.

Offline

 

#3 2012-04-29 08:19:17

xJira
Scratcher
Registered: 2012-03-24
Posts: 91

Re: Pacman evil guy

What about this:

Here is an example maze:
http://oi48.tinypic.com/1zv9a8y.jpg

The Blue box is the object that should move around, the red dot is a sensor. It will be made invisable with the "Set Ghost-Effect to 100"-Block. Both objects move on a raster, the sensor is always one step ahead. Btw, one square is 20 x 20. So the sensor-position depends on the position of the Blue box AND on its direction. Here's how the sensor must move:

http://oi50.tinypic.com/2q16kr4.jpg

Yeah, actually the blue box should move to  wink  After every step, it broadcasts "Check" and waits. (Waiting is important!!)

 when gf clicked
 forever
 move [20] steps
 broadcast [check v] and wait
 THE OTHER PART OF THIS SCRIPT IS AT THE BOTTOM
 IT MUST BE RIGHT HERE IN THIS LOOP
 
When the sensor receivs "Check", it checks if it's touching the color of the wall in front of the blue box. Therefor, it goes to the square in front of the blue box:

when I receive [check v]
if <([direction v] of [Blue Box]) = [90]>
set x to (([x Position v] of [Blue Box]) + [20])
set y to ([y Position v] of [Blue Box]) 
end

if <([direction v] of [Blue Box]) = [-90]>
set x to (([x Position v] of [Blue Box]) - [20])
set y to ([y Position v] of [Blue Box]) 
end

if <([direction v] of [Blue Box]) = [0]>
set x to ([x Position v] of [Blue Box])
set y to (([y Position v] of [Blue Box])  + [20])
end

if <([direction v] of [Blue Box]) = [180]>
set x to ([x Position v] of [Blue Box])
set y to (([y Position v] of [Blue Box])  - [20])
end
If it does not touch the wall-color (black in this example), it does nothing and this script ends. Then the Blue Box is allowed to make it's next step.

This it what the sensor should do, if it touches a wall (because the bluebox has to change it's direction then): First, you need the variable "Turn?". If the sensor touches the wall-colour it sets "Turn?" to yes. Then, the sensor checks in which direction the blue box might walk next. Therefore, it checks the the four squares next to the blue box.

http://oi45.tinypic.com/2gsizow.jpg

If they aren't black, the Blue Box could walk there next. So we need the List "Possible Directions". First, the sensor delets all of the list. If it does not touch the wall, it adds the possible Direction to the List. In this example, this would be 90 (right) and 180 (down).

So we continue the script like this:

if <touching color [#000000]?>
set [Turn? v] to [Yes]
delete [All] from [Possible directions v]

set x to (([x Position v] of [Blue Box]) + [20])
set y to ([y Position v] of [Blue Box]) 
if <not <touching color [#000000]?>>
add [90] to  [Possible directions v]
end

set x to (([x Position v] of [Blue Box]) - [20])
set y to ([y Position v] of [Blue Box]) 
if <not <touching color [#000000]?>>
add [-90] to  [Possible directions v]
end

set x to ([x Position v] of [Blue Box])
set y to (([y Position v] of [Blue Box])  + [20])
if <not <touching color [#000000]?>>
add [0] to  [Possible directions v]
end

set x to ([x Position v] of [Blue Box])
set y to (([y Position v] of [Blue Box])  - [20])

if <not <touching color [#000000]?>>
add [180] to  [Possible directions v]
end

else
 set [Turn? v] to [No] //Of course, if it doesnt' touch black at the beginning, it sets turn to no.
end
 
You remember that the Blue Box who received has been waiting? Well, now that the script is done, it stops waiting and checks, if Turn? = Yes. If so, it goes to one of the POSSIBLE DIRECTIONS. So it chooses a random item form the list "Possible directions" and then the whole loop runs again  smile

if <(Turn?) = [Yes]>
 point in direction (item [any] of [Possible Directions v])
wait [ ... ] secs // The longer it waits, the slower it moves and the easier is the game.
 
Wow, this is a lot^^ I really hope you use these scripts  big_smile  I haven't checked this, but I'm  pretty sure it will work.
I hope I explained this well and my english is OK! If there are questions, just ask.

xJira

Offline

 

#4 2012-04-30 04:55:12

jontmy00
Scratcher
Registered: 2011-11-28
Posts: 1000+

Re: Pacman evil guy

xJira wrote:

What about this:

Here is an example maze:
http://oi48.tinypic.com/1zv9a8y.jpg

The Blue box is the object that should move around, the red dot is a sensor. It will be made invisable with the "Set Ghost-Effect to 100"-Block. Both objects move on a raster, the sensor is always one step ahead. Btw, one square is 20 x 20. So the sensor-position depends on the position of the Blue box AND on its direction. Here's how the sensor must move:

http://oi50.tinypic.com/2q16kr4.jpg

Yeah, actually the blue box should move to  wink  After every step, it broadcasts "Check" and waits. (Waiting is important!!)

 when gf clicked
 forever
 move (20) steps
 broadcast [check v] and wait
 THE OTHER PART OF THIS SCRIPT IS AT THE BOTTOM
 IT MUST BE RIGHT HERE IN THIS LOOP
 
When the sensor receivs "Check", it checks if it's touching the color of the wall in front of the blue box. Therefor, it goes to the square in front of the blue box:

when I receive [check v]
if <([direction v] of [Blue Box v]) = [90]>
set x to (([x Position v] of [Blue Box v]) + (20))
set y to ([y Position v] of [Blue Box v]) 
end

if <([direction v] of [Blue Box v]) = [-90]>
set x to (([x Position v] of [Blue Box v]) - (20))
set y to ([y Position v] of [Blue Box v]) 
end

if <([direction v] of [Blue Box v]) = [0]>
set x to ([x Position v] of [Blue Box v])
set y to (([y Position v] of [Blue Box v])  + (20))
end

if <([direction v] of [Blue Box v]) = [180]>
set x to ([x Position v] of [Blue Box v])
set y to (([y Position v] of [Blue Box v])  - (20))
end
If it does not touch the wall-color (black in this example), it does nothing and this script ends. Then the Blue Box is allowed to make it's next step.

This it what the sensor should do, if it touches a wall (because the bluebox has to change it's direction then): First, you need the variable "Turn?". If the sensor touches the wall-colour it sets "Turn?" to yes. Then, the sensor checks in which direction the blue box might walk next. Therefore, it checks the the four squares next to the blue box.

http://oi45.tinypic.com/2gsizow.jpg

If they aren't black, the Blue Box could walk there next. So we need the List "Possible Directions". First, the sensor delets all of the list. If it does not touch the wall, it adds the possible Direction to the List. In this example, this would be 90 (right) and 180 (down).

So we continue the script like this:

if <touching color [#000000]?>
set [Turn? v] to [Yes]
delete (All v) from [Possible directions v]

set x to (([x Position v] of [Blue Box]) + [20])
set y to ([y Position v] of [Blue Box]) 
if <not <touching color [#000000]?>>
add [90] to  [Possible directions v]
end

set x to (([x Position v] of [Blue Box]) - [20])
set y to ([y Position v] of [Blue Box]) 
if <not <touching color [#000000]?>>
add [-90] to  [Possible directions v]
end

set x to ([x Position v] of [Blue Box])
set y to (([y Position v] of [Blue Box])  + [20])
if <not <touching color [#000000]?>>
add [0] to  [Possible directions v]
end

set x to ([x Position v] of [Blue Box])
set y to (([y Position v] of [Blue Box])  - [20])

if <not <touching color [#000000]?>>
add [180] to  [Possible directions v]
end

else
 set [Turn? v] to [No] //Of course, if it doesnt' touch black at the beginning, it sets turn to no.
end
 
You remember that the Blue Box who received has been waiting? Well, now that the script is done, it stops waiting and checks, if Turn? = Yes. If so, it goes to one of the POSSIBLE DIRECTIONS. So it chooses a random item form the list "Possible directions" and then the whole loop runs again  smile

if <(Turn?) = [Yes]>
 point in direction (item (any v) of [Possible Directions v])
wait ( ... ) secs // The longer it waits, the slower it moves and the easier is the game.
 
Wow, this is a lot^^ I really hope you use these scripts  big_smile  I haven't checked this, but I'm  pretty sure it will work.
I hope I explained this well and my english is OK! If there are questions, just ask.

xJira

Fixed your scripts  big_smile


FOR ALL THE NEWS ON UPDATES FOR SIMPLISTICRAFT, CLICK HERE.

Offline

 

#5 2012-04-30 05:25:56

Jem12
Scratcher
Registered: 2012-04-20
Posts: 100+

Re: Pacman evil guy

ENOUGH with complicated scripts what about "if touching colour XXX move 10 spaces XXX"
this would mean you need different coloured wall but it MIGHT just MIGHT work....

sorry if I'm no help...  sad


"In the begging the universe was created, this made a lot of people angry and was widly regarded as a bad move" ~  Douglas Adams ~ The resturant at the end of the universe

Offline

 

#6 2012-04-30 06:24:23

jontmy00
Scratcher
Registered: 2011-11-28
Posts: 1000+

Re: Pacman evil guy

Jem12 wrote:

ENOUGH with complicated scripts what about "if touching colour XXX move 10 spaces XXX"
this would mean you need different coloured wall but it MIGHT just MIGHT work....

sorry if I'm no help...  sad

It's always best to have more complicated but better scripts.  big_smile


FOR ALL THE NEWS ON UPDATES FOR SIMPLISTICRAFT, CLICK HERE.

Offline

 

Board footer