วันอังคารที่ 27 สิงหาคม พ.ศ. 2556

(Game Manhole)Assignment 2

           GAME ที่เลือกคือ MANHOLE GAME หรือที่เรียกกันว่าเกมต่อสะพานหนึ่งใน Gold Series ของ GAME & WATCH ที่ถูกผลิตขึ้นมาโดยบริษัท  Nintendo 


                                      

                    สามารถดูตัวอย่างการเล่นได้ที่ : https://www.youtube.com/watch?v=TOmHDs87AZw


ชื่อเกม : Minion Manhole Game (MMG)

ประเภท : Smooth game


ออกแบบ Function

void setup(){
   size();   //ขนาดของตัวเกม
   background();   //สีพื้นหลังเกม
}

void draw(){
   drawFloor(x, y);  //วาดพื้น
   drawBall(a, b, r);  //วาดลูกบอลที่กลิ้ง
   drawMinion(c, d);  //วาดตัวมินเนี่ยน
}

void drawFloor(int x, int y){  //ฟังก์ชันที่ใช้วาดพื้น
   rect();   //พื้นเป็นสี่เหลี่ยม
}

void drawBall(int a, int b, int r){   //ฟังก์ชันที่วาดลูกบอลรับค่าเป็นพารามิเตอร์
   ellipse(a, b, r, r);   //ลูกบอลเป็นทรงกลม
}

void drawMinion(int c, int d){   //วาดตัวมินเนี่ยนและรับค่ามาเป็นพารามิเตอร์
   ellipse();//  วาดตัวและ ตาเป็นวงกลม
   arc();   //วาดปากเป็นส่วนของวงกลม
}

Function สำหรับใช้ในการสร้างเกมส์ 

drawBackground() // สร้าง ฟังก์ชั่นที่วาด พื้นหลัง ส่วนของดวงอาทิตย์และพื้นสีเขียว ไม่มีการรับค่า parameter ไม่มีการ return ค่า   

drawFloor(int x, int y) // สร้าง  ฟังก์ชั่นที่ใช้วาด ก้อนอิฐ รับค่า parameter x,y ไม่มีการ return ค่า  


drawMinion(int c, int d) // สร้าง ฟังก์ชั่นที่ใช้วาด  Minious รับค่า parameter c,d ไม่มีการ return ค่า 

 

drawBall(int a, int b, int r) // สร้าง ฟังก์ชั่นที่ใช้วาด Ball รับค่า parameter a ,b,c เพื่อกำหนดตำแหน่งและขนาดของลูกบอล   ใช้กำหนดการเคลื่อนที่ของบอลด้วย ไม่มีการ return ค่า   


moveArm () //สร้าง ฟังก์ชั่นกำหนดการขยับแขนของ Minious ให้ขยับออกไปรับลูกบอล ไม่มีการรับค่า parameter ไม่มีการ return ค่า   


scoreBall() // สร้างฟังก์ชั่นที่ใช้นับคะแนน เมื่อรับบอลได้ ไม่มีการรับค่า parameter และไม่มีการ return ค่า

   

drawGrass() // สร้าง ฟังก์ชั่นที่ใช้วาด พื้นหลังของภาพโดยเป็นส่วนที่วาดหญ้า ไม่มีการรับค่า parameter ไม่มีการ return ค่า




ผู้รับผิดชอบ


นางสาว ชลิตา กาญจนาเมือง

drawMinious();

scoreBall();


นางสาว ปนัดดา อ่อนพลับ


drawBackground();


drawGrass();


moveArm ();


นางสาว ธีระนันท์ เย็นไธสง


drawFloor();


drawBall();




วันพุธที่ 7 สิงหาคม พ.ศ. 2556

Array : Hard

Question... (Pie chart)Write a class that displays a pie chart of the data that is passed to its method as an array.
                เขียนโค้ดแสดงแผนภูมิวงกลมโดยการใช้ array

Answer...
                 void setup(){
                     size(300,300);
                     noStroke();
                     noLoop();
                }
              
                int[] angles={20,25,45,45,45,90,60,30};
                int[] cB={#FF0509,#FF05B9,#7D05FF,#056FFF,#00FFF9,#03FF00,#F6FF00,#F7B407};
                float start = 0;
                int i = 0;
                float diameter=200;

              void piechart(){  
                  while( i < angles.length){
                      fill(cB[i]);
                      arc(width/2, height/2, diameter, diameter, start, start+radians(angles[i]));
                      start+= radians(angles[i]);
                      i=i+1;
                  }
              }

              void draw(){
                  background(0);
                  piechart();
              }

             การแสดงผล:


.
source : Java for students
Douglas Bell and Mike Parr. -- 2nd ed. page 253
ISBN : 0-13010922-3

Function:Hard

Question...Write a method which returns the inch equivalent of its centimetre parameter.An example invocation is:
               เขียนฟังก์ชันที่มีการรีเทิร์นค่านิ้ว โดยกำหนด เซนติเมตรในฟังชั่นพารามิเตอร์
                float inches = inchEquivalent(2.5f);

               Multiply centimetres by 0.394 to calculate inches.
               หาเซนติเมตรโดย 0.394 ในการคำนวณนิ้ว

Answer...

void setup(){
  inchEquivalent(2.5);
  println("Equivalent "+inchEquivalent(2.5)+"inch");
}
float inchEquivalent(float x){
  float n;
  n = x*0.394;
  return n;
}



source : Java for students
Douglas Bell and Mike Parr. -- 2nd ed. page 61
ISBN : 0-13010922-3
การแสดงผล


loops.:hard

Question...  ( Wall )Write a program to draw a wall of bricks, as shown in figure8.10.  Use the libraly method drawRect to draw each brick.This is invoked like this:
                 
                 
                   g. drawRect(x,y,width,height);

                  All the parameters are integers,representing a pixel number.x,y are the coordinates of the top left of the rectangle. Hint: use nested loops.
               

Answer....

               void setup(){}
               void draw() {
                  int flats=5;
                  int floors=4;
                  int yCoord=10;
                  int xCoord;
               for (int floor=0;floor<floors;floor++) {
                  xCoord=0;
                  for (int flat=0;flat<flats;flat++) {
                     rect(xCoord, yCoord, 18, 10);
                     rect(xCoord+9, yCoord+10, 18, 10);
                     xCoord=xCoord+20;
                  }
                  yCoord=yCoord+20;
                }
              }



source : Java for students
Douglas Bell and Mike Parr. -- 2nd ed. page 136
ISBN : 0-13010922-3
การแสดงผล



วันอังคารที่ 6 สิงหาคม พ.ศ. 2556

Variable : Easy

Question... assign an initial value to a variable:
                  กำหนดตัวแปรเริ่มต้นคือ
         
                  int totalSeconds = 2549;

                  and convert this to hours, minutes and seconds. Do an example with pen and paper before you write the program.
                  และให้แปลงเป็นชั่วโมง,นาที,และวินาที โดยทำตัวอย่างกับกระดาษก่อนที่จะเขียนโปรแกรม

Answer...
                int totalSeconds = 2549;//ตัวแปรที่โจทย์กำหนดมาให้
                int hours=0;//กำหนดตัวแปรเพื่อหาชั่วโมง
                int minutes=0;//กำหนดตัวแปรเพื่อหาค่านาที
                int seconds=0;//กำหนดตัวแปรเพื่อหาค่าวินาที

                hours = totalSeconds/3600;//คำนวณเพื่อแปลงค่าหาชั่วโมง
                minutes = totalSeconds/60;//คำนวณเพื่อแปลงค่าหานาที
                seconds = totalSeconds%60;//คำนวณเพื่อแปลงค่าหาวินาที

                println("hours is "+hours);//แสดงค่าของชั่งโมง
                println("minutes is "+minutes);//แสดงค่าของนาที
                println("seconds is "+seconds);//แสดงค่าของวินาที

               แสดงผล  =  hours is 0
                                     minutes is 42
                                     seconds is 29

source : Java for students
Douglas Bell and Mike Parr. -- 2nd ed. page 45
ISBN : 0-13010922-3

Variable : Medium

Question...  Assume that two students take a java exam, and the results are assigned to two variables:
                  ให้นักเรียนแสดงตัวอย่างการทำและผลจากตัวแปรสองตัว

                  int mark1 = 44;
                  int mark2 = 51;
                 
                  Write a program which calculates and displays the average mark as a float value. Check your answer with a calculator.
                  เขียนโปรแกรมในการคำนวณและแสดงผลค่าเฉลี่ยของ mark เป็นค่าลอยๆเป็นจำนวนที่มีจุดทศนิยม เช็คคำตอบกับเครื่องคิดเลข

Answer...

                  int mark1 = 44;
                  int mark2 = 51;
                  int plusmark =0;//กำหนดตัวแปรผลบวก
                  float averagemark = 0;//กำหนดตัวแปร average

                 plusmark = mark1+mark2;//คำนวณผลบวกของค่าที่ให้มา
                 averagemark = plusmark/2;//การหาค่าเฉลี่ย average
             
                 println("averagemark is "+averagemark);//การแสดงผล

                 แสดงผล = averagemark is 47

source : Java for students
Douglas Bell and Mike Parr. -- 2nd ed. page 44
ISBN : 0-13010922-3



         

วันจันทร์ที่ 5 สิงหาคม พ.ศ. 2556

Function :Medium

Question...Create a method  drawFamily  which  draws two adults and two children  (assumed  to  be  half  as  tall as  the  adults).Make  use  of  your  drawPerson, and  decide  on  your  own  parameter.

สร้าง drawFamily  มีผู้ใหญ่สองคนและเด็กสองคน(ความสูงเป็นครึ่งหนึ่งของผู้ใหญ่) ทำโดยใช้ฟังก์ชั่น drawPerson ของคุณ และกำหนดตัวแปรของคุณเอาเอง

Answer...


.....
void setup(){
  size(200,200);
}
  int a=0;//กำหนดเพื่อให้ภาพขยับไปทางด้านซ้าย
  int b=0;//กำหนดเพื่อให้ภาพมีตำแหน่งเลื่อนลง
  int y= 160;//จุดต่ำสุดของภาพ
  int y1= 120;

 
void drawAdult(int a,int b, int r,int l){
  strokeWeight(8);//ขนาดของเส้นที่ใช้ในการวาด
  line(40+a,50+b,40+a,y1+l);//ลำตัวของคน
  line(40+a,y1+l,20+a,y);//ขาข้างซ้ายของคน
  line(40+a,y1+l,60+a,y);//ขาข้างขวาของคน
  line(20+a,80+l,60+a,80+b);//แขนของคน
  strokeWeight(2); //ขนาดของเส้นที่ใช้ในการวาด
  ellipse(40+a,50+b,r,r);//หัวของคน
}


void draw(){
  background(0,random(123),random(250));//พื้นหลังเป็นแบบสุ่ม
  drawAdult(0,0,50,0);//ผู้ใหญ่ด้านซ้ายสุด
  drawAdult(40,0,50,0);//ผู้ใหญ่ด้านตรงกลาง
  drawAdult(80,45,30,20);//เด็กตรงกลาง
  drawAdult(120,45,30,20);//เด็กด้านริมขวาสุด
 

}


source : Java for students
Douglas Bell and Mike Parr. -- 2nd ed. page 61
ISBN : 0-13010922-3