Pong HubNet

No preview image

1 collaborator

Uri_dolphin3 Uri Wilensky (Author)

Tags

hubnet 

Tagged by Reuven M. Lerner about 11 years ago

Model group CCL | Visible to everyone | Changeable by group members (CCL)
Model was written in NetLogo 3.2pre6 • Viewed 518 times • Downloaded 47 times • Run 0 times
Download the 'Pong HubNet' modelDownload this modelEmbed this model

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


VERSION

$Id: Pong HubNet.nlogo 37529 2008-01-03 20:38:02Z craig $

WHAT IS IT?

This section could give a general understanding of what the model is trying to show or explain.

HOW IT WORKS

This section could explain what rules the agents use to create the overall behavior of the model.

HOW TO USE IT

This section could explain how to use the model, including a description of each of the items in the interface tab.

THINGS TO NOTICE

This section could give some ideas of things for the user to notice while running the model.

THINGS TO TRY

This section could give some ideas of things for the user to try to do (move sliders, switches, etc.) with the model.

EXTENDING THE MODEL

This section could give some ideas of things to add or change in the procedures tab to make the model more complicated, detailed, accurate, etc.

NETLOGO FEATURES

This section could point out any especially interesting or unusual features of NetLogo that the model makes use of, particularly in the Procedures tab. It might also point out places where workarounds were needed because of missing features.

RELATED MODELS

This section could give the names of models in the NetLogo Models Library or elsewhere which are of related interest.

CREDITS AND REFERENCES

This section could contain a reference to the model's URL on the web if it has one, as well as any other necessary credits or references.

Comments and Questions

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

Click to Run Model

breed [balls ball]
breed [paddles paddle]
breed [top-clients top-client]
breed [bottom-clients bottom-client]

globals [ game-over? playing? computer-points user-points ]

paddles-own [ user? ]
top-clients-own [ user-id ]
bottom-clients-own [ user-id ]

to reset-score
  ca
  set-default-shape balls "circle"
  set-default-shape paddles "paddle"
  set computer-points 0
  set user-points 0
  create-paddles 1 [
    setxy 0 min-pycor
    set user? true
    set size 5
    set color red
  ]
  create-paddles 1 [
    setxy 0 max-pycor
    set user? false
    set size 5
    set color red
  ]
end 

to setup
  hubnet-set-client-interface "COMPUTER" []
  hubnet-reset
  ask balls [ die ]
  set game-over? false
  set playing? false
  create-balls 1 [
    setxy 0 0
    set heading (-45 + random 90) + (random 2 * 180)
    set color green
  ]
end 

to play
  set playing? true
  ifelse not game-over? [
    move-ball
    move-computer-paddle
  ]
  [
   setup
  ]
end 

to move-computer-paddle

  let target-ball one-of balls
  let my-heading 360
  let goal one-of patches with [ pycor = max-pycor ]

  ask patches with [ pycor = max-pycor ] [
    ask target-ball [
      if towards myself < my-heading [
        set my-heading towards myself
        set goal myself
      ]
    ]
  ]

  ask paddles with [ user? = false ] [
    if ( xcor < [pxcor] of goal ) [
      move-right computer-speed / 100
    ]
    if ( xcor > [pxcor] of goal ) [
      move-left computer-speed / 100
    ]
  ]
end 

to move-ball
  ask balls [
    let next-patch patch-ahead 1
    let paddle-patches nobody
    ifelse next-patch != nobody [

      ifelse ( paddle-ahead? = true ) [
        bounce
      ]
      [
        fd ball-speed / 100
      ]
    ]
    [
      if ( pycor = min-pycor ) [
        lose-game
      ]
      if ( pycor = max-pycor ) [
        win-game
      ]
      if ( pxcor = max-pxcor or pxcor = min-pxcor ) [
        bounce
      ]
    ]
  ]
end 

to lose-game
  set game-over? true
  set computer-points computer-points + 1
end 

to win-game
  set game-over? true
  set user-points user-points + 1
end 

;; this procedure checks the coordinates and makes the turtles
;; reflect according to the law that the angle of reflection is
;; equal to the angle of incidence

to bounce  ;; turtle procedure
  ; check: hitting left or right wall?
  if ( patch-ahead 1 = nobody  )
    ; if so, reflect heading around x axis
    [ set heading (- heading) ]
  ; check: hitting top or bottom wall?
  if ( paddle-ahead? )
    ; if so, reflect heading around y axis
    [ set heading (180 - heading) ]
end 

to-report paddle-ahead?
  let paddle-patches patches in-radius ([size] of one-of paddles / 2)
  ifelse ( heading > 270  or heading < 90 ) [
    set paddle-patches paddle-patches with [ pycor = [pycor] of myself + 1 ]
  ]
  [
    set paddle-patches paddle-patches with [ pycor = [pycor] of myself - 1 ]
  ]
  ifelse ( any? paddles-on paddle-patches ) [
    report true
  ]
  [
    report false
  ]
end 

to move-left [ speed ]
  if ( game-over? = false and playing? = true ) [
    set heading -90
    fd speed
  ]
end 

to move-right [ speed ]
  if ( game-over? = false and playing? = true ) [
    set heading 90
    fd 1
  ]
end 

There are 2 versions of this model.

Uploaded by When Description Download
Uri Wilensky almost 14 years ago Pong HubNet Download this version
Uri Wilensky almost 14 years ago Pong HubNet Download this version

Attached files

No files

This model does not have any ancestors.

This model does not have any descendants.