Projectile Attack

Projectile Attack preview image

1 collaborator

Uri_dolphin3 Uri Wilensky (Author)

Tags

game 

Tagged by Reuven M. Lerner almost 6 years ago

Model group CCL | Visible to everyone | Changeable by group members (CCL)
Model was written in NetLogo 5.0.4 • Viewed 423 times • Downloaded 29 times • Run 2 times
Download the 'Projectile Attack' modelDownload this modelEmbed this model

Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)


WHAT IS IT?

This model is based on the classic game Tank Wars. Players must adjust angle and power of fire to hit a target with a projectile. Several factors, including gravity and wind must be taken into account.

HOW IT WORKS

The warhead is fired and moves ballistically towards its target.

HOW TO USE IT

The SETUP button creates a new randomly-positioned target and obstacle wall.

The GO button causes the game logic to be active and must be pressed anytime the game is in play.

The FIRE button launches the projectile with the currently specified parameters.

The WIND slider adjusts the direction and magnitude of the wind.

Properties of the obstacle wall can be configured including its height (WALL-HEIGHT slider), it's location (WALL-POSITION slider), and what effect it has on wind (WALL-BLOCKS-WIND? switch)

THINGS TO TRY

Try to hit the target!

NETLOGO FEATURES

This model uses the tie command for links, to conveniently make it so that changing the heading of the tank causes its gun to swivel and the "aiming arrow" to also swivel.

This model also fakes having no ceiling to the world, by having the shell (projectile) wrap around to the bottom of the view, but it is hidden until it comes back down again.

RELATED MODELS

Lunar Lander

CREDITS AND REFERENCES

Thanks to James Newell for his work on this model.

HOW TO CITE

If you mention this model in a publication, we ask that you include these citations for the model itself and for the NetLogo software:

COPYRIGHT AND LICENSE

Copyright 2008 Uri Wilensky.

CC BY-NC-SA 3.0

This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.

Commercial licenses are also available. To inquire about commercial licenses, please contact Uri Wilensky at uri@northwestern.edu.

Comments and Questions

Please start the discussion about this model! (You'll first need to log in.)

Click to Run Model

breed [ clouds cloud ]
breed [ targets target ]
breed [ tanks tank ]
breed [ guns gun ]
breed [ shells shell ]


globals [ x-vel y-vel velocity previous-wall-height previous-wall-pos]

to setup
  clear-all
  set-default-shape tanks "tank"
  set-default-shape guns "gun"
  set-default-shape clouds "cloud"
  set-default-shape shells "ball"

  ask patches [ set pcolor 97 ]
  ask patches with [ pycor < -15 ]
    [ set pcolor black ]
  ; next we create the tank, it's gun, and the aiming arrow
  create-tanks 1
  [
    set heading angle
    setxy -16 -14.2
    set color green - 2
    set size 3
    hatch-guns 1 ; the tank's gun/nozzle
    [
      set size 1
      set color green - 3
      ; this confusing line of code creates a directed "tie" link
      ; between the tank and its gun
      ask myself [ create-link-to myself  [ tie hide-link ]]

      hatch 1 ; the "aiming arrow"
      [
        set breed turtles
        fd 3
        set color red - 1
        ; and now we create a directed "tie" link between
        ; the gun and its aiming arrow
        ask myself [ create-link-to myself [ tie hide-link ]]
      ]
    ]
  ]
  create-clouds 1
  [
    set shape "cloud"
    setxy 0 15
    set color 8
    set size 3.5
    set heading 90
  ]
  create-targets 1
  [
    set color black
    set shape "heli"
    setxy 15 (random 32) - 15
    set size 2
  ]

  ; create the wall
  ask patches with [ pycor > -16 ]
  [
    ifelse (pycor < wall-height - 15 and pxcor = wall-position )
      [ set pcolor gray ]
      [ set pcolor 97 ]
  ]
  set previous-wall-height wall-height
  set previous-wall-pos wall-position
end 

to go
  if not any? targets [ stop ]

  ask tanks [ set heading angle ]

  if ( previous-wall-height != wall-height or previous-wall-pos != wall-position )
  [
    ask patches with [ pycor > -16 ]
    [ ifelse (pycor < wall-height - 15 and pxcor = wall-position )
        [ set pcolor gray ]
        [ set pcolor 97 ]
    ]
    set previous-wall-height wall-height
    set previous-wall-pos wall-position
  ]
  every .05
  [
    ask shells
    [
      setxy (xcor + x-vel) (ycor + y-vel)
      set y-vel (y-vel - .01)
      ; NOTE: this is *NOT* modeling a realistic wind effect on a projectile.
      ; Instead, we just treat wind like gravitational acceleration along x-axis
      if (wall-block-wind? = false or
         (wall-block-wind? = true and (ycor > wall-height - 15) or ((xcor - wall-position) * wind < 0)))
        [ set x-vel (x-vel + (wind / 10000)) ]
      set velocity sqrt (( x-vel ^ 2 ) + (y-vel ^ 2))
      if (velocity > 1)
      [
        set x-vel x-vel / velocity
        set y-vel y-vel / velocity
        set velocity 1
      ]
      check-shell
    ]
    ask clouds [ setxy (xcor + wind / 100) (ycor) ]
  ]
  display
end 

to fire
  if not any? shells
  [
    ask tanks
    [
      hatch-shells 1
      [
        set size 1
        set color black
        set x-vel ( sin angle * ( Power / 100 ))
        set y-vel ( cos angle * ( Power / 100 ))
        set velocity Power / 100
      ]
    ]
  ]
end 

; This procedure uses a clever hack to simulate having no ceiling for the projectile
; Basically, if the projectile goes up above the ceiling, it actually wraps around,
; but it becomes hidden, until it comes back down again.  Obviously, this method
; isn't bulletproof, but it works pretty well for the purposes of this game.

to check-shell
  if ( pycor = -17 and hidden? = false )
      [ hide-turtle ]
  if ( pycor = 17 and hidden? = true )
      [ show-turtle ]
  if ( hidden? = false )
  [
     if ( xcor > 17 or ycor < -15 or pcolor = gray )
       [ die ]
     if ( any? targets-here )
       [
         ask patches in-radius 2 [ set pcolor yellow ]
         ask patches in-radius 1 [ set pcolor red ]
         ask targets-here [ die ]
         die
       ]
     set pcolor scale-color sky velocity 2 0
  ]
end 


; Copyright 2008 Uri Wilensky.
; See Info tab for full copyright and license.

There are 10 versions of this model.

Uploaded by When Description Download
Uri Wilensky about 6 years ago Updated to NetLogo 5.0.4 Download this version
Uri Wilensky over 6 years ago Updated version tag Download this version
Uri Wilensky over 6 years ago Updated to version from NetLogo 5.0.3 distribution Download this version
Uri Wilensky over 7 years ago Updated to NetLogo 5.0 Download this version
Uri Wilensky almost 9 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky almost 9 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky almost 9 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky almost 9 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky almost 9 years ago Model from NetLogo distribution Download this version
Uri Wilensky almost 9 years ago Projectile Attack Download this version

Attached files

File Type Description Last updated
Projectile Attack.png preview Preview for 'Projectile Attack' about 6 years ago, by Uri Wilensky Download

This model does not have any ancestors.

This model does not have any descendants.