วันอังคารที่ 20 พฤษภาคม พ.ศ. 2557

Chapter 2. Extending our Functional Test Using the unittest Module

Chapter 2. Extending our Functional Test Using the unittest Module

จากไฟล์ functional_test.py
from selenium import webdriver

browser = webdriver.Firefox()

# Edith has heard about a cool new online to-do app. She goes
# to check out its homepage
browser.get('http://localhost:8000')

# She notices the page title and header mention to-do lists
assert 'To-Do' in browser.title

# She is invited to enter a to-do item straight away

# She types "Buy peacock feathers" into a text box (Edith's hobby
# is tying fly-fishing lures)

# When she hits enter, the page updates, and now the page lists
# "1: Buy peacock feathers" as an item in a to-do list

# There is still a text box inviting her to add another item. She
# enters "Use peacock feathers to make a fly" (Edith is very methodical)

# The page updates again, and now shows both items on her list

# Edith wonders whether the site will remember her list. Then she sees
# that the site has generated a unique URL for her -- there is some
# explanatory text to that effect.

# She visits that URL - her to-do list is still there.

# Satisfied, she goes back to sleep

browser.quit()





ทำการรัน serverและไฟล์ test

$python manage.py runserver 
$python funtional_tests.py
Traceback (most recent call last):
  File "Functional_tests.py", line 10, in <module>
     assert 'To-Do' in browser.title
AssertionError 

     เมื่อรันแล้วโปรแกรมจะแสดงว่ามีการ Error คือ ชื่อ title ของบราวเซอร์ไม่ตรงกับคำว่า 'To-Do'
จากนั้นทำการแก้ไขไฟล์ funtional_tests.py

from selenium import webdriver
import unittest

class NewVisitorTest(unittest.TestCase):  #1

    def setUp(self):  #2
        self.browser = webdriver.Firefox()

    def tearDown(self):  #3
        self.browser.quit()

    def test_can_start_a_list_and_retrieve_it_later(self):  #4
        # Edith has heard about a cool new online to-do app. She goes
        # to check out its homepage
        self.browser.get('http://localhost:8000')

        # She notices the page title and header mention to-do lists
        self.assertIn('To-Do', self.browser.title)  #5
        self.fail('Finish the test!')  #6

        # She is invited to enter a to-do item straight away
        #[...rest of comments as before]

if __name__ == '__main__':  #7
    unittest.main()  #8
     #1
 ทำการรันไฟล์นี้อีกครั้ง 

$python funtional_tests.pyF
======================================================================
FAIL: test_can_start_a_list_and_retrieve_it_later (__main__.NewVisitorTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "functional_tests.py", line 18, in test_can_start_a_list_and_retrieve_it_later
    self.assertIn('To-Do', self.browser.title)  #5
AssertionError: 'To-Do' not found in u'Welcome to Django'

----------------------------------------------------------------------
Ran 1 test in 5.698s

FAILED (failures=1)

    เมื่อรันแล้วจะพบว่ามีการ Error คือชื่อ title ของbrowserไม่ตรงกับที่เราต้องการคือ 'To-Do'
และ browser จะเปิดขึ้นและปิดไปทันที เนื่องจากในคลาสมีการกำหนดให้มีการ setUp และ tearDown อยู่ด้วย โดยฟังก์ชั่น setUp นั้นเป็นการรันโปรแกรมก่อน และฟังก์ชั่น tearDown เป็นคำสั่งปิด browser จะทำหลังจากที่มีการ  test เสร็จแล้ว ดังนั้น browser จึงปิดเร็วมาก
    จึงมีการแก้ไขไฟล์โดยการเพิ่มคำสั่งเข้าไปในฟังก์ชั่น setUp 
 
[...]
def setUp(self):
    self.browser = webdriver.Firefox()
    self.browser.implicitly_wait(3)

def tearDown(self):
[...]

     เมื่อรันแล้วจะพบว่ามีการ Error เหมือนเดิม แต่ช่วงเวลาในการปิด-เปิด จะนานขึ้นมาเป็น 3 วินาที จากคำสั่ง implicitly_wait(3) เป็นคำสั่งให้ browser รอ 3 วินาที

     ต่อไปเป็นคำสั่ง git diff เป็นคำสั่งไว้สำหรับดูว่าได้กระทำกับไฟล์อะไรอย่างไรบ้าง
$ git diff
diff --git a/.gitignore b/.gitignore
index 49ef255..3ba11e5 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,3 @@
 db.sqlite3
+__pycache__
+*.pyc
diff --git a/functional_tests.py b/functional_tests.py
index df73c70..b6322b7 100644
--- a/functional_tests.py
+++ b/functional_tests.py
@@ -1,6 +1,26 @@
 from selenium import webdriver
+import unittest
 
-browser = webdriver.Firefox()
-browser.get('http://localhost:8000')
+class NewVisitorTest(unittest.TestCase):  #1
 
-assert 'Django' in browser.title
+    def setUp(self):  #2 
+        self.browser = webdriver.Firefox()
:


และทำการ commit โดยพิมพ์ไปว่า First FT specced out in comments, and now uses unittest

$git commit -a
$git log
commit d69c2f03402ae2fec3afc49c08d244d5bb6bbd23
Author: jutamas <jutamas@jutamas.(none)>
Date:   Wed May 21 11:04:59 2014 +0700

    Fist FT specced out in comments, and now uses unittest

commit c7ec8f9cc6c4bf59c8d8dbc51520df2d3c50de2a
Author: jutamas <jutamas@jutamas.(none)>
Date:   Tue May 20 11:40:50 2014 +0700

    First commit: First FT and basic Django config

 




วันจันทร์ที่ 19 พฤษภาคม พ.ศ. 2557

Python Global

Python Global

ตัวอย่างคำสั่งที่ 1

answer = 0

def calculator(x,y):
    answer = x + y
    print "answer in function = ",answer
    return answer

calculator(5,6)
print "answer out function = ",answer

เมื่อรันโปรแกรมแล้วจะได้ผลลัพธ์ดังนี้


    จากตัวอย่างคำสั่ง จะเห็นได้ว่าผลลัพที่ค่าเท่ากับ 11 เกิดจากการส่งค่าไปทำการบวกกันในฟังก์ชั่น calculator() และเก็บค่าไว้ในตัวแปร answer เมื่อรันโปรแกรมจึงแสดงค่า 11 ออกมา ในขณะที่ผลลัพธ์ที่มีค่าเท่ากับ 0 นั้นเป็นนำค่าจากตัวแปร answer ที่อยู่นอกฟังก์ชั่นมาแสดงผล



Python Array

Python Array

ตัวอย่างคำสั่งที่ 1


def calculator(): #function for calculator
    x =[5,6]  
    answer = x[0]+x[1] #answer for x[0]+x[1]
    print "5+6 = ",answer    #print answer

calculator() #cal function

เมื่อรันโปรแกรมจะได้ผลลัพธ์ดังนี้


    จากตัวอย่าง ในฟังก์ชั่น calculator()  มีการประกาศตัวแปรแบบ Array คือ x = [5,6] ทำให้มีการประกาศตัวแปรน้อยลง และมีการเก็บค่าไว้ในตัวแปร answer เมื่อนำค่า x[0]+x[1] และทำการเรียกฟังก์ชั่น จะได้ค่าดังรูป

ตัวอย่างคำสั่งที่ 2

def calculator(): #function for calculator
    x =[5,6,7]  
    for i in range(3):
        answer = x[0]+x[i] #answer for x[0]+x[i]
     print "5 +",x[i],"=",answer    #print answer    

calculator() #cal function

เมื่อรันโปรแกรมจะได้ผลลัพธ์ดังนี้


     จากตัวอย่าง ในฟังก์ชั่น calculator() มีการประกาศตัวแปร Array คือ x = [5,6,7] และนำมาวน for loop เพื่อนำค่า x ตำแหน่งที่ i มาบวกกับ x[0] และ print ค่าออกมา เมื่อเรียกใช้ฟังก์ชั่น calculator()



Python Function

Python Function

ตัวอย่างคำสั่งที่ 1

def calculator(): #function for calculator
    x = 5
    y = 6    
    answer = x+y #answer for x+y
    print "5+6 = ",answer    #print answer

calculator() #cal function

เมื่อรันโปรแกรมจะได้ผลลัพธ์ดังนี้


   จากตัวอย่างคำสั่ง เป็นโปรแกรมที่มีการเรียกใช้ฟังก์ชั่น calculator() โดยฟังก์ชั่นนี้เป็นฟังก์ชั่นที่มีการประกาศตัวแปรสองตัวคือ x = 5 และ y = 6 และนำค่า x และ y มาบวกกันและเก็บค่าไว้ในตัวแปร answer และทำการเรียกฟังก์ชั่น calculator
    ในส่วนของรันโปรแกรม โดยเปิด terminal ขึ้นมาและหา directory ของไฟล์ที่จะทำการรัน และ พิมพ์ python ตามด้วยชื่อไฟล์.py เมื่อกด enter แล้วจะแสดงผล 5+6 = 11

ตัวอย่างคำสั่งที่ 2

def calculator(x,y): #function for calculator
    answer = x+y #Answer for x+y
    return answer  #return answer

print "x+y = ",calculator(5,6) #call function for calculator value x=5 , y=6

เมื่อรันโปรแกรมจะได้ผลลัพธ์ดังนี้


    จากตัวอย่างคำสั่ง เป็นโปรแกรมที่มีการ print ค่า x+y โดยการส่งค่าไปในฟังก์ชั่น calculator() 2 ค่า และค่า 2 ค่านั้นจะเข้าไปทำในฟังก์ชั่น โดยทำการบวกกันและเก็บค่าไว้ในตัวแปร answer และ return ค่า answer ไปยัง print "5+6 = ",calculator(5,6) ก็จะได้ค่าเท่ากับ 11

ตัวอย่างคำสั่งที่ 3

def calculator(x,y):#function for calculator    
    answer = x+y 
    return answer    

print "5+6 = ",calculator(5,6) #call function for calculator value x=5 , y=6
print "2+3 = ",calculator(2,3) #call function for calculator value x=2 , y=3

เมื่อรันโปรแกรมจะได้ผลลัพธ์ดังนี้


    จากตัวอย่างคำสั่ง เป็นการเรียกฟังก์ชั่น 2 รอบโดยส่งค่าไปสองค่าคือ 5,6 และ 2,3 เมื่อรันโปรแกรมแล้วจะให้ดังรูป


วันพฤหัสบดีที่ 15 พฤษภาคม พ.ศ. 2557

Array

Array


    ตัวอย่างคำสั่งที่ 1 คำสั่งที่ไม่ใช้ Array

int A = 0;
int B = 1;
int C = 2;
int D = 3;
print(A,B,C,D);

    เมื่อรันโปรแกรมจะได้ผลลัพธ์ดังนี้





   
ตัวอย่างคำสั่งที่ 2 คำสั่งที่ใช้ Array

int[] A = {0,1,2,3};
int i;

for(i=0; i<4; i++){
  print(A[i]," ");
}

    เมื่อรันโปรแกรมจะได้ผลลัพธ์ดังนี้





   
จากตัวอย่าง เมื่อรันโปรแกรมจะเห็นได้ว่าผลลัพธ์ของทั้งสองที่แสดงออกมานั้นเหมือนกัน  แต่คำสั่งไม่เหมือนกัน คือ ตัวอย่างที่  1 ไม่มีการใช้ Array ทำให้ต้องมีการประกาศตัวแปรหลายตัว เมื่อเทียบกับตัวอย่างที่ 2 มีการใช้ Array โดยประกาศตัวแปรแค่ 1 ตัวแต่สามารถใส่ค่าได้หลายค่า


    ตัวอย่างคำสั่งที่ 3 คำสั่งที่ไม่ใช้ Array

int numbers1 = 90;
int numbers2 = 150;
int numbers3 = 30;

int a = numbers1 + numbers2;
int b = numbers2 + numbers3;

print("Variable a = ",a);
print("\nVariable b = ",b);

    เมื่อรันโปรแกรมจะได้ผลลัพธ์ดังนี้





   
ตัวอย่างคำสั่งที่ 4 คำสั่งที่ใช้ Array

int[] numbers = { 90, 150, 30 };

int a = numbers[0] + numbers[1]; 
int b = numbers[1] + numbers[2];

print("Variable a = ",a);
print("\nVariable b = ",b);

    เมื่อรันโปรแกรมจะได้ผลลัพธ์ดังนี้





   
จากตัวอย่างคำสั่ง เมื่อรันโปรแกรมแล้วจะเห็นได้ว่าผลลัพธ์ของทั้งสองที่แสดงออกมานั้นเหมือนกัน แต่คำสั่งไม่เหมือนกัน ตัวอย่างที่ 3 เป็นคำสั่งที่ไม่ใช้ Array ส่วนตัวอย่างที่ 4 เป็นคำสั่งที่ 4 เป็นคำสั่งที่ใช้ Array การใช้ Array จะทำให้ประหยัดตัวแปร เพราะมีการประกาศตัวแปรน้อยกว่า

    ตัวอย่างคำสั่งที่ 5 คำสั่งที่ไม่ใช้ Array

void draw_table(int x, int y, int width, int height){  //function draw table
  line(x,y,x+width,y);
  line(x+10,y,x+10,y+height);
  line(width+x-10,y,width+x-10,y+height);
}

void setup() {
  background(255, 255, 255, 255);
  size(500, 500);
  draw_table(100,100,200,100); //call function for draw table
  draw_table(200,200,200,100);
  draw_table(50,50,200,100);
 }


    เมื่อรันโปรแกรมจะได้ผลลัพธ์ดังนี้



    ตัวอย่างคำสั่งที่ 6 คำสั่งที่ใช้ Array

void draw_table(int x, int y, int width, int height){  //function draw table
  line(x,y,x+width,y);
  line(x+10,y,x+10,y+height);
  line(width+x-10,y,width+x-10,y+height);
}

void setup() {
  background(255, 255, 255, 255);
  size(500, 500);
  int[] x = {100,200,50};
  int i;
  for(i=0; i<3; i++){
    draw_table(x[i],x[i],x[1],x[0]); //call function for draw table
  }  
}

    เมื่อรันโปรแกรมจะได้ผลลัพธ์ดังนี้


    จากตัวอย่างคำสั่ง เมื่อรันโปรแกรมแล้วจะเห็นได้ว่าผลลัพธ์ของทั้งสองที่แสดงออกมานั้นเหมือนกัน แต่คำสั่งไม่เหมือนกัน ตัวอย่างที่ 5 เป็นคำสั่งที่ไม่ใช้ Array ส่วนตัวอย่างที่ 6 เป็นคำสั่งที่ใช้ Array โดยกำหนดค่า x มา 3 ค่า และนำทั้ง 3 ค่ามาวนใน loop For เพื่อที่จะส่งค่าเข้าไปในฟังก์ชั่น draw_table ในทีนี้กำหนด loop ให้เท่ากับ 3 เมื่อรันโปรแกรมจึงมีรูปโต๊ะจำนวน 3 รูป ซึ่งรูปจะออกมาเหมือนกับตัวอย่างที่ 5 แต่คำสั่งต่างกัน คือ ในตัวอย่างที่ 5 นั้นจะให้การเรียกฟังก์ชั่น 3 ครั้งโดยกำหนดค่าที่จะส่งไปยังฟังก์ชั่นไว้เลย











วันพุธที่ 7 พฤษภาคม พ.ศ. 2557

Animation basketball

Animation basketball
คำสั่ง

int y = 0;
int check_hight = 0;

void setup() {
  size(500, 500);
  background(255, 255, 255, 255);
  frameRate(15);
  draw_human();
}

void draw_bas(int y){ //draw bas
  fill(234,120,12);
  noStroke();
  ellipse(160,175+y,30,30);

}

void draw_human(){ //draw human
  fill(0);
  stroke(5);
  ellipse(100,100,100,100);
  line(100, 150, 160, 160);
  line(100,150,100,200);
  line(85,200,125,200);
  line(85,200,80,220);
  line(125,200,130,220);
  fill(137,234,12);
  noStroke();
  rect(0,220,500,10);
  
}

void draw(){
  background(255);
  draw_human();
  draw_bas(y);
  
  if(check_hight == 0){ //check hight maximum
    y = y+1;
  }
  else{
    y = y-1;
  }
  if(y==30){
    check_hight = 1;  //check hight lower
  }
  if( y == 0){
    check_hight = 0; //check hight maximum
  }
}

จากคำสั่งเมื่อรันโปรแกรมจะเป็นรูปคนเล่นลูกบาสเกตบอลขึ้นลง โดยจากคำสั่งจะมีการบวก ค่า y ทีละ 1 รูปลูกบาสก็จะขึ้นไปเรื่อยๆ เมื่อค่า y ครบ 30 ค่า y จะมีการลบทีละ 1 รูปลูกบาสก็จะลงไปเรื่อยดังรูป




Animation piano

Animation piano
คำสั่ง

float x, xa, xb, xc;
float r,g,b;

void setup() {
  size(500, 500);
  background(255, 255, 255, 255);
  frameRate(5);
}

void draw_piano() {
  fill(0); //color black
  rect(90, 60, 330, 105); //draw body piano color black
  fill(255); //color white
  rect(100, 100, 10, 60); //draw key piano color white
  rect(110, 100, 10, 60);
  rect(120, 100, 10, 60);
  rect(130, 100, 10, 60);
  rect(140, 100, 10, 60);
  rect(150, 100, 10, 60);
  rect(160, 100, 10, 60);
  rect(170, 100, 10, 60);
  rect(180, 100, 10, 60);
  rect(190, 100, 10, 60);
  rect(200, 100, 10, 60);
  rect(210, 100, 10, 60);
  rect(220, 100, 10, 60);
  rect(230, 100, 10, 60);
  rect(240, 100, 10, 60);
  rect(250, 100, 10, 60);
  rect(260, 100, 10, 60);
  rect(270, 100, 10, 60);
  rect(280, 100, 10, 60);
  rect(290, 100, 10, 60);
  rect(300, 100, 10, 60);
  rect(310, 100, 10, 60);
  rect(320, 100, 10, 60);
  rect(330, 100, 10, 60);
  rect(340, 100, 10, 60);
  rect(350, 100, 10, 60);
  rect(360, 100, 10, 60);
  rect(370, 100, 10, 60);
  rect(380, 100, 10, 60);
  rect(390, 100, 10, 60);
  rect(400, 100, 10, 60);
}

void random_keyPiano(int newX, int newxa, int newxb, int newxc) {
  //random color
  r = random(255); 
  g = random(255);
  b = random(255);
  //draw random key piano and color
  fill(r,g,b);
  rect(newX, 100, 10, 60); 
  fill(b,g,r);
  rect(newxa, 100, 10, 60);
  fill(g,r,b);
  rect(newxb, 100, 10, 60);
  fill(r,b,g);
  rect(newxc, 100, 10, 60);

//draw key piano color black
  fill(0);
  rect(107, 100, 6, 35);
  rect(117, 100, 6, 35);
  rect(137, 100, 6, 35);
  rect(147, 100, 6, 35);
  rect(157, 100, 6, 35);
  rect(177, 100, 6, 35);
  rect(187, 100, 6, 35);
  rect(207, 100, 6, 35);
  rect(217, 100, 6, 35);
  rect(227, 100, 6, 35);
  rect(247, 100, 6, 35);
  rect(257, 100, 6, 35);
  rect(277, 100, 6, 35);
  rect(287, 100, 6, 35);
  rect(297, 100, 6, 35);
  rect(317, 100, 6, 35);
  rect(327, 100, 6, 35);
  rect(347, 100, 6, 35);
  rect(357, 100, 6, 35);
  rect(367, 100, 6, 35);
  rect(387, 100, 6, 35);
  rect(397, 100, 6, 35);
}

void draw() {

  x = random(100, 400);
  xa = random(100, 400);
  xb = random(100, 400);
  xc = random(100, 400);

  int newX = int(x);
  int newxa = int(xa);
  int newxb = int(xb);
  int newxc = int(xc);

  int modx = newX % 10;
  int modxa = newxa % 10;
  int modxb = newxb % 10;
  int modxc = newxc % 10;

  draw_piano();
  random_keyPiano(newX-modx, newxa-modxa, newxb-modxb, newxc-modxc);
}

จากคำสั่ง เมื่อรันโปรแกรมจะได้รูปเปียโน และที่คีย์จะมีการเปลี่ยนสีไปเรื่อยๆโดยการสุ่มสี ในโปรแกรมจะมีการจำลองเหมือนการกดคีย์ที่ละ 4 คีย์โดยการสุ่มเลข 100-400 เพราะรูปที่วาดไว้เริ่มที่ 100 - 400 และเมื่อสุ่มค่าได้แล้วนำค่ามา mod กับ 10 และเรียกใช้ฟังก์ชั่น random_piano โดยส่งค่าที่สุ่มได้ ลบกับค่าที่ mod ได้ก็ได้ค่าที่อยู่ในช่วง 100-400 โดยไม่มีเศษในหลักหน่วย






วันจันทร์ที่ 5 พฤษภาคม พ.ศ. 2557

การแปลงตัวแปรชนิด float เป็น int

การแปลงตัวแปรชนิด float เป็น int

ตัวอย่างเช่น
float x,y;
int tableWidth;
int tableHeight;
void setup(){
    size(500,500);
    background(255, 255, 255, 255);
    frameRate(5);
    tableWidth = 200;
    tableHeight = 100;
  
}
void draw_table(int x,int y){
    line(x,y,x+tableWidth,y);
    line(x+10,y,x+10,y+tableHeight);
    line(tableWidth+x-10,y,tableWidth+x-10,y+tableHeight);
}
void draw(){
    background(255, 255, 255, 255);
    x=random(500);
    y=random(500);
    int newX = int(x);
    int newY = int(y);
    draw_table(newX,newY);  
}

จากตัวอย่าง ในเริ่มแรกจะประกาศค่า x,y เป็นชนิด float แต่เมื่อเราต้องการจะใช้ตัวแปร x,y ในชนิด int โดย

float x,y;
 int newX = int(x);
 int newY = int(y);


ตัวอย่างการ Error ของโปรแกรม
float x,y;
int tableWidth;
int tableHeight;
void setup(){
    size(500,500);
    background(255, 255, 255, 255);
    frameRate(5);
    tableWidth = 200;
    tableHeight = 100;
  
}
void draw_table(int x,int y){
    line(x,y,x+tableWidth,y);
    line(x+10,y,x+10,y+tableHeight);
    line(tableWidth+x-10,y,tableWidth+x-10,y+tableHeight);
}
void draw(){
    background(255, 255, 255, 255);
    x=random(500);
    y=random(500);
    draw_table(x,y);  
}

    จากตัวอย่าง เมื่อรันโปรแกรมแล้ว โปรแกรมจะขึ้นแจ้งว่า

    เนื่องจากฟังก์ชั่น draw_table(x,y); มีการส่งตัวแปรไปเป็น x และ y ซึ่งมีการกำหนดชนิดเป็น float ตั้งแต่แรก แต่ในฟังก์ชั่น void draw_table(int x, int y) นำค่า x และ y มากำหนดใช้เป็นชนิด int จึงไม่สามารถนำมาใช้ได้ โปรแกรมจึงแจ้ง Error


ตัวแปร Global

ตัวแปร global
     จะกำหนดไว้นอกกลุ่มคำสั่งหรือนอกฟังก์ชั่น ใช้งานได้ทั้งโปรแกรม มีค่าเริ่มเท่ากับ 0 ในกรณีที่ไม่ได้กำหนดค่าเริ่มต้น
    ตัวอย่างเช่น

int x,y;
void setup(){
    size( 500,500 );
    background( 255, 255, 255, 255 );
    frameRate(5);
    x = 0;
    y = 50;
}

void draw_circle(int x,int y){
    fill( 245,222,15 );
    ellipse( x,y,100,100 );
}

void draw(){
    draw_circle( x,y );
    x = x + 100;
  
    draw_circle(x,y);
    if ( x == 500 ){
        x = 0;
        y = y + 50;
     }
    if( y == 500 ){
        x = 0;
        y = 0;
    }
}

     จากตัวอย่าง ตัวแปร x,y จะถูกประกาศนอกฟังก์ชั่น ทำให้สองตัวแปรนี้สามารถใช้ได้ทั้งโปรแกรมหรือใช้ได้ทุกฟังก์ชั่นในโปรแกรม จากโปรแกรมจะได้ดังรูป

    จากรูปเมื่อรันโปรแกรมแล้วรูปวงกลมจะปรากฏขึ้นทีละสองรูปและขยับตำแหน่งไปเรื่อยๆ โดยขยับไปทาง x ทีละ 100 และเมื่อขยับไปจน x = 500 จะให้เลื่อนลงไปทาง y ทีละ 50 วงกลมสองรูปก็จะขยับไปทางขวาและทำไปเรื่อยๆ

    ถ้าไม่มีการประกาศและเรียกใช้ตัวแปรแบบ Global จะไม่มีการอัปเดตค่า


void setup(){
    size(500,500);
    background(255, 255, 255, 255);
    frameRate(5);
    int x = 0;
    int y = 50;
}

void draw_circle(int x,int y){
    fill(245,222,15);
    ellipse(x,y,100,100);
}

void draw(){   
    int x = 0;
    int y = 50;
draw_circle(x,y); x = x +100; draw_circle(x,y); if(x == 500){ x = 0; y = y +50; } if(y == 500){ x = 0; y = 0; } }
  จากตัวอย่างคำสั่ง เมื่อรันโปรแกรมแล้ว รูปวงกลมสองรูปจะเกิดขึ้นในตำแหน่ง x = 0 , y = 50 และจะเกิดซ้ำอยู่ตรงที่เดิมไปเรื่อยๆ เพราะค่า x,y ไม่ได้มีการอัปเดตตลอดเวลา ทำให้เวลาโปรแกรมเรียกใช้ฟังก์ชั่น void draw() ทุกครั้งค่า x,y ก็จะเท่าเดิมทำให้รูปอยู่ที่เดิมดังรูป