Courses/CS 461/Winter 2006/Nghia Phan/Nghia Phan/Week 5

From CSWiki

Jump to: navigation, search

Contents

[edit] Shooting Bubbles

A little game that I like to play. Hope you guys like it!

[edit] Screen Shot

Image:Nghia_phan_week5_1.PNG
Image:Nghia_phan_week5_2.PNG
Image:Nghia_phan_week5_3.PNG

[edit] Instructions of how to play

1) Click on new game
2) Click play
3) Move mouse to the view
4) To shoot the bubble, left-click on the mouse
5) Good luck and have fun

[edit] Download

Shooting Bubbles Version 1.0

[edit] Code

;;******************************************************************************************************************************
;;******************************************************************************************************************************
GLOBALS
[
  Bubble_Size
  Bubble_Color_Code
  Kill_List
  Speed
  Main_Bubble_Is_Not_Moving?
  Main_Bubble_Direction
  Main_Bubble_Id
]

BREEDS
[
  Bubbles
  Direction_Guides
]
;;******************************************************************************************************************************
;;******************************************************************************************************************************




;;******************************************************************************************************************************
;;******************************************************************************************************************************
to new_game
  ca
  set Bubble_Size 3
  set Bubble_Color_Code (list 5 15 25 35 45 55 65 75 85 95 105 115 125 135)
  set Kill_List (list)
  set Speed 0.001
  set Main_Bubble_Is_Not_Moving? true
  set-default-shape Bubbles "circle"
  set-default-shape Direction_Guides "arrow"
  
  ;;Layout bubbles
  if (Layout = "Triangle")
  [
    layout_triangle
  ]
  
  ;;Create main bubble
  make_bubbles  0 (- screen-edge-y + 2)
  make_direction_guides 0 (- screen-edge-y + 2)
end
;;******************************************************************************************************************************
;;******************************************************************************************************************************




;;******************************************************************************************************************************
;;******************************************************************************************************************************
to layout_triangle  
  let bubble_xcor_orginal 0
  let max_left_xcor 0
  let max_right_xcor 0
  let bubble_xcor 0
  let bubble_ycor (screen-edge-y - 2)
  
  ;;For each row, the number of bubbles must equal to row's number
  let row_num 1
  let num_bubble 1
  
  repeat (Level * 10)
  [  
    make_bubbles bubble_xcor bubble_ycor
    
    ;;If row's number equal to number of bubbles, then move to next row
    ;;Else, continue adding bubbles to current row
    ifelse (row_num = num_bubble)
    [
      set row_num (row_num + 1)
      set num_bubble 1
      set bubble_xcor_orginal (bubble_xcor_orginal - 2)
      set bubble_xcor bubble_xcor_orginal
      set bubble_ycor (bubble_ycor - 2)
      set max_left_xcor bubble_xcor 
    ]
    [
      set num_bubble (num_bubble + 1)
      set bubble_xcor (bubble_xcor + 4)
      set max_right_xcor bubble_xcor
    ]
  ]
  
  ;;Create more bubbles to make a complete shape in the last row
  repeat (row_num - num_bubble + 1) 
  [
    make_bubbles bubble_xcor bubble_ycor
    set bubble_xcor (bubble_xcor + 4)
  ]
end
;;******************************************************************************************************************************
;;******************************************************************************************************************************




;;******************************************************************************************************************************
;;******************************************************************************************************************************
to make_bubbles [cur_xcor cur_ycor]
  create-custom-Bubbles 1
  [      
    set size Bubble_Size
    set color random-one-of Bubble_Color_Code
    setxy cur_xcor cur_ycor
  ]
end

;;------------------------------------------------------------------------------------------------------------------------------

to make_direction_guides [cur_xcor cur_ycor]
  create-custom-Direction_Guides 1
  [
    set size Bubble_Size
    set color white
    setxy cur_xcor cur_ycor
    jump Bubble_Size
  ]
  
  ifelse (Guides)
  [
    ask Direction_Guides
    [
      showturtle
    ]
  ]
  [
    ask Direction_Guides
    [
      hideturtle
    ]
  ]
end
;;******************************************************************************************************************************
;;******************************************************************************************************************************




;;******************************************************************************************************************************
;;******************************************************************************************************************************
to play
  if (mouse-inside? and Main_Bubble_Is_Not_Moving?)
  [
    if (count Bubbles-at 0 (- screen-edge-y + 2) = 0)
    [
      make_bubbles  0 (- screen-edge-y + 2)
    ]
    
    ask Direction_Guides
    [
      setxy 0 (- screen-edge-y + 2)
      
      ;;Direction guide cannot points below ball's current x-axis
      ifelse (mouse-ycor < (- screen-edge-y + 2))
      [
        set heading towardsxy-nowrap mouse-xcor (- screen-edge-y + 3)
      ]
      [
        set heading towardsxy-nowrap mouse-xcor mouse-ycor
      ]
      
      set Main_Bubble_Direction heading
      jump (Bubble_Size)
    ]
    
    ifelse (Guides)
    [
      ask Direction_Guides
      [
        showturtle
      ]
    ]
    [
      ask Direction_Guides
      [
        hideturtle
      ]
    ]
  ]
  
  if (mouse-down? and Main_Bubble_Is_Not_Moving?)
  [  
    set Main_Bubble_Is_Not_Moving? false
    
    ask Direction_Guides
    [
      hideturtle
    ]
    
    ask Bubbles-at 0 (- screen-edge-y + 2)
    [
      set heading Main_Bubble_Direction
      set Main_Bubble_Id who
    ]

    shoot_bubbles
  ]
end

;;------------------------------------------------------------------------------------------------------------------------------

to shoot_bubbles
  
    ask turtle Main_Bubble_Id
    [
      ;;Bouds to left/right of the wall  
      if (pxcor-of patch-ahead 1 >= screen-edge-x or pxcor-of patch-ahead 1 <= (- screen-edge-x))
      [
        set heading (- heading)
      ]
      
      ;;Meet top of the wall or other bubbles
      ifelse ((pycor-of patch-ahead 2 >= screen-edge-y) or ((count Bubbles in-radius (Bubble_Size - 0.5)) > 1))
      [
        set Main_Bubble_Is_Not_Moving? true
        
        ask Direction_Guides
        [
          showturtle
        ]
      ]
      [
        fd Speed
      ]
    ]
    
    ifelse (Not Main_Bubble_Is_Not_Moving?)
    [
      shoot_bubbles
    ]
    [
      add_member_to_kill_list Main_Bubble_Id
      
      if (length Kill_List > 2)
      [
        foreach Kill_List
        [
          ask turtle ?
          [
            die
          ]
        ]
      ]
      
      set Kill_List []
    ]
    
end

;;------------------------------------------------------------------------------------------------------------------------------

to add_member_to_kill_list [curr_id]

  set Kill_List fput curr_id Kill_List
  
  ask turtle curr_id
  [
    ask Bubbles in-radius (Bubble_Size) with [( (self != myself) and (color = color-of myself) )]
    [
      if (Not member? who Kill_List) 
      [
        add_member_to_kill_list who
      ]
    ]
  ]
  
end
;;******************************************************************************************************************************
;;******************************************************************************************************************************

Personal tools