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

#1 2010-12-13 21:57:07

Farrow88
Scratcher
Registered: 2009-04-14
Posts: 8

Tower Defense Script WANTED!

Are there any good Tower Defense game that show FIRING?!
And I mean real Tower Defense, as in AUTO TOWERS!!!
Oh, I'm getting off topic. CAN ANYONE get me a good Script for making Towers shoot at the enemies? I'm trying to make one, but I'm not certain how the heck to get the towers to shoot at the closest enemy (and with 100 Tower Limit, thats a lot of scripting)

Offline

 

#2 2010-12-14 09:24:12

fishmonk
Scratcher
Registered: 2009-09-29
Posts: 100+

Re: Tower Defense Script WANTED!

I'm a bit rusty, but I think I can help. You are gonna have to modify this code some, but I think it should do the trick.
you are gonna need a ring sprite around the tower.
for the first if, make it if the enemy touches the ring.
point at towards the enemy and repeat until the enemy isnt touching.
This should make the tower point towards the enemy while the enemy is in range. You can only have one enemy in range at a time, otherwise it runs into trouble. For the bullet, you make a sprite and have it go to the tower. You will need one bullet for each tower. when the enemy touches the ring, broadcast something. When the bullet receives whatever the broadcast is, make it visible, point it towards teh enemy, and have it move towards teh enemy until it hits. keep pointing it at teh enemy. When it hits, reset it. You might have to set the enemy to broadcast the message every two seconds while it is hitting the ring to make it fire more than once. Does taht help?


All hail the Holy Mackrel! God of bad puns!

Offline

 

#3 2010-12-22 15:20:12

Farrow88
Scratcher
Registered: 2009-04-14
Posts: 8

Re: Tower Defense Script WANTED!

@fishmonk
That will be difficult. I think I can modify that, just slightly, so that it can have more then one enemy in range. However, it might not work. And making it so that only one would be in range would be difficult, if not impossible.
As for the ring idea, that's perfect, but difficult. To have it be just the right size to match the range... hard.
Thanks for the help, all the same

Offline

 

#4 2010-12-22 15:33:53

Farrow88
Scratcher
Registered: 2009-04-14
Posts: 8

Re: Tower Defense Script WANTED!

Hmm... Might have figured out the second problem.
IF... Variable Tower 1 Range=50
Set size to 50%

Offline

 

#5 2010-12-22 16:47:44

archmage
Scratcher
Registered: 2007-05-18
Posts: 1000+

Re: Tower Defense Script WANTED!

A tower defense game will not run very well on Scratch. But I will tell you how to properly write the scripts for the towers.

First of all you use know how to properly check series of sprites.

Say we have baddies 1 to 25

We do this, to check which baddie is closest and within range.

//on tower
set i to 0
set nearest_baddie to (baddie1)
repeat 25{
change i by 1
if (distance to (join (baddie)(i)) < distance to (nearest_baddie)){
      set nearest_baddie to (join (baddie) to (i))
     }
}
}

When you are done with that, to fire at the enemy you do this. Each tower needs its own personal bullet for this.

//on tower
if (distance to nearest_baddie < shoot_radius){
broadcast shoot bullet1    //change the number depending on what tower is used
}


Now from this you can see that a tower defense game would require a lot of loops to check things. Scratch performs very poorly and slowly with loops which is why this game won't really work out in scratch.


Hi, I am Archmage coder extraordinaire. I do Scratch,pascal,java,php,html, AS2 and AS3. Leave me a message if you want coding advice. Also check out my personal website, lots of good stuff about web development, Flash, and Scratch (v1 and v2) !

Offline

 

#6 2010-12-29 10:05:17

ArrowHead
Scratcher
Registered: 2010-10-17
Posts: 92

Re: Tower Defense Script WANTED!

My script EDITED:

baddie1:

<when green flag clicked>
<forever>
<if><( <distance to[ tower <<> {range of tower} )>
<add [baddie1] to list [threats]>


tower:


<when green flag clicked>
<forever>
<if><<  <not><(length of list threats <=> 0  )>  >>
<point towards( item (length of list threats) of list threats)
<broadcast[ shoot  ]and wait c>

Last edited by ArrowHead (2011-01-03 19:21:07)


See previews of Earth, my current project written in C# and XNA, and provide feedback here!

Offline

 

#7 2010-12-29 14:17:20

AfraidOfClouds
Scratcher
Registered: 2010-06-03
Posts: 100+

Re: Tower Defense Script WANTED!

yo move it to requests


"I like to aggravate salesmen. I walk into the store and they say, 'Can I help you, sir?' I say, 'Yeah, you got anything I want?'
He says, 'Well, how would I know?' I say, 'You started this.'"

Offline

 

#8 2010-12-30 20:18:00

floppy_gunk
Scratcher
Registered: 2008-11-14
Posts: 500+

Re: Tower Defense Script WANTED!

In fact, I'm making a tower defense game right now! Maybe once I'm done you can look at the script.


http://img163.imageshack.us/img163/1917/2856lg.jpg Get it now!  smile

Offline

 

#9 2012-06-10 19:37:56

Thescratch3
Scratcher
Registered: 2011-06-14
Posts: 1000+

Re: Tower Defense Script WANTED!

ArrowHead wrote:

My script EDITED:

baddie1:

<when green flag clicked>
<forever>
<if><( <distance to[ tower <<> {range of tower} )>
<add [baddie1] to list [threats]>


tower:


<when green flag clicked>
<forever>
<if><<  <not><(length of list threats <=> 0  )>  >>
<point towards( item (length of list threats) of list threats)
<broadcast[ shoot  ]and wait c>

The problem with your script is that if the baddie is very far away, but visible, he won't be shot at, (won't be on the list of threats).


View my projects. Or face The scratch curse! (Get it?)
http://i56.tinypic.com/2cdk8hy.png

Offline

 

#10 2012-06-10 22:15:51

BoltBait
Scratcher
Registered: 2009-03-09
Posts: 1000+

Re: Tower Defense Script WANTED!

This thread is pretty old ...

But, I wrote a tower defense game.

http://scratch.mit.edu/projects/BoltBait/1897019

If you want to know how it works, I posted it in pieces to my test account here:

http://scratch.mit.edu/users/BoltTest


Animated sigs must be banned!
http://boltbait.com/j.pnghttp://boltbait.com/s.pnghttp://boltbait.com/d.pnghttp://boltbait.com/a.pnghttp://boltbait.com/p.png

Offline

 

#11 2012-06-18 21:02:39

ArrowHead
Scratcher
Registered: 2010-10-17
Posts: 92

Re: Tower Defense Script WANTED!

Thescratch3 wrote:

ArrowHead wrote:

My script EDITED:

baddie1:

when gf clicked
forever
  if<(distance to[tower v]) < (range of tower) >
  add [baddie1] to [threats v]
tower:

when gf clicked
forever
  if<not<(length of [list threats v]) = (0)>>
  point towards( item (length of list [threats v]) of list [threats v])
broadcast[ shoot v]and wait

The problem with your script is that if the baddie is very far away, but visible, he won't be shot at, (won't be on the list of threats).

well, isn't that the point of having range?  there is always the possibility of very high range (65,536 should be enough)


See previews of Earth, my current project written in C# and XNA, and provide feedback here!

Offline

 

Board footer