วันอาทิตย์ที่ 22 กันยายน พ.ศ. 2556

A2 Submit

ชื่อเกม : Egg
รูปแบบเกม : smooth & step

code รันได้บน Program Processing

การออกแบบฟังก์ชัน : http://com5630264.blogspot.com/2013/08/game-egg.html

Global Variable ที่ใช้ในโปรแกรม : http://com5630264.blogspot.com/2013/09/global-variable.html

เกม : (STEP) http://com5630264.blogspot.com/2013/09/game-egg_15.html
     : (SMOOTH) http://com5630264.blogspot.com/2013/09/game-egg-smooth.html

Global Variable ที่ใช้ในโปรแกรม

Smooth

float a = 250;
float b = 250;
float posX = a;
float posY = b;
float[] x1=new float[100];
float[] x2=new float[100];
float[] y1=new float[100];
float[] y2=new float[100];
int i=1;
int sum=0;
int miss1=0;
int miss2=0;
int take=0;

Step

int xSS=0;
float[] x1=new float[100];
float[] x2=new float[100];
float[] y1=new float[100];
float[] y2=new float[100];
int i=1;
int sum=0;
int miss1=0;
int miss2=0;
int count=0;
int take=0;

Game Egg (Step)



void setup() {
size(500, 500);
background(255);
textSize(30);
fill(0);

text("Click to play", 160, 250);
}
int xSS=0;
float[] x1=new float[100];//egg left
float[] x2=new float[100];
float[] y1=new float[100];//egg left
float[] y2=new float[100];
int i=1;
int sum=0;
int miss1=0;
int miss2=0;
int count=0;
int take=0;
void draw () {
if (take==0) {

if (mousePressed && mouseButton==LEFT) {
take=1;
i=1;
sum=0;
miss1=0;
miss2=0;
count=0;
SetEgg();
xSS=0;
}
}
if (take==1) {
BG();
frameRate(90);
strokeWeight (3);
fill(255);
drawBody();
fill(255, 228, 196);
i=0;
while (i<x1.length) {
if (x1[i]<=160) {
if (count%30==0) {
x1[i]=x1[i]+25;
}
}
if (x2[i]>340) {
if (count%30==0) {
x2[i]=x2[i]-25;
}
}
if (x1[i]>=90) {
if (count%30==0) {
y1[i]=y1[i]+13;
}
}
if (x2[i]<=400) {
if (count%30==0) {
y2[i]=y2[i]+13;
}
}
ellipse(x1[i], y1[i], 20, 25);
ellipse(x2[i], y2[i], 20, 25);
///////chack get point
if (y1[i]>=225&&y1[i]<=240) {
if (xSS==1) {
sum++;
x1[i]=1000;
y1[i]=1000;///Delete egg
}
}
if (y2[i]>=225&&y2[i]<=240) {
if (xSS==2) {
sum++;
x2[i]=1000;
y2[i]=1000;///Delete egg
}
}
if (y1[i]>=329&&y1[i]<=342) {
if (xSS==3) {
sum++;
x1[i]=1000;
y1[i]=1000;///Delete egg
}
}
if (y2[i]>=329&&y2[i]<=342) {
if (xSS==4) {
sum++;
x2[i]=1000;
y2[i]=1000;///Delete egg
}
}

/////check miss point
if (y1[i]>=252&&y1[i]<=276) {
miss1++;
y1[i]=600;
}
if (y2[i]>=252&&y2[i]<=276) {
miss2++;
y2[i]=600;
}
if (y1[i]>=351&&y1[i]<=373) {
miss1++;
y1[i]=600;
}
if (y2[i]>=351&&y2[i]<=373) {
miss2++;
y2[i]=600;
}
if (miss1==1) {
breakEgg(160);
}
if (miss2==1) {
breakEgg(340);
}
if (miss1==2) {
breakEgg(160);
}
if (miss2==2) {
breakEgg(340);
}
if (miss1==3) {
breakEgg(160);
}
if (miss2==3) {
breakEgg(340);
}

fill(255, 228, 196);
text("Score"+" "+sum, 50, 50);
text("Miss", 200, 50);
i++;
if ((miss1+miss2)>=3) {
textSize(50);
fill(255, 0, 0);
text("Game over", 120, 150);
fill(255);
textSize(30);
take=0;
}
}

if (keyPressed) {
if (key == 'q' || key == 'Q') {
xSS=1;
}
if (key == 'e' || key == 'E') {
xSS=2;
}
if (key == 'z' || key == 'Z') {
xSS=3;
}
if (key == 'c' || key == 'C') {
xSS=4;
}
}
switch(xSS) {
case 1:
fill(255);
drawArms (-35, 53, 67, -85, 22, 18, 20);
break;
case 2:
fill(255);
drawArms (+35, 53, 67, 85, 22, 18, 20);
break;
case 3:
fill(255);
drawArms (-35, 73, 87, -85, 122, 118, 120);
break;
case 4:
fill(255);
drawArms (35, 73, 87, 85, 122, 118, 120);
break;
default :
fill(255);
break;
}
count++;
drawChicken();
if (miss1+miss2>0) {
int m1=0;
int k=0;
while (k<miss1+miss2) {
fill(255, 228, 196);
arc(290+m1, 38, 25, 30, 0, PI);
fill(0);
triangle(290+m1, 38, 278+m1, 38, 284+m1, 45);
triangle(290+m1, 38, 302+m1, 38, 296+m1, 45);
k++;
m1=m1+30;
}
}
}
}
void BG()
{
background(0);
drawTree();
drawChute();
drawBush();
}
void drawChute() {
stroke(255);
fill(255, 0, 0);
//left
quad(85, 200, 85, 215, 150, 260, 150, 245);
quad(85, 310, 85, 325, 150, 370, 150, 355);
//right
quad(415, 200, 415, 215, 350, 260, 350, 245);
quad(415, 310, 415, 325, 350, 370, 350, 355);
fill(0, 255, 255);
//right
rect(415, 200, 85, 15);
rect(415, 310, 85, 15);
//left
rect(0, 200, 85, 15);
rect(0, 310, 85, 15);
rect(0, 330, 20, 50);
rect(25, 330, 20, 50);
rect(50, 330, 20, 50);
rect(480, 330, 20, 50);
rect(455, 330, 20, 50);
rect(430, 330, 20, 50);
}
void drawTree() {
noStroke();
fill(139, 69, 19);
rect(475, 50, 30, 55);
fill(124, 252, 0);
rect(420, 0, 80, 45);
ellipse(410, 5, 40, 30);
ellipse(420, 25, 30, 30);
ellipse(435, 40, 30, 30);
ellipse(460, 45, 30, 30);
ellipse(490, 45, 30, 30);
}
void drawBush() {
noStroke();
fill(124, 252, 0);
ellipse(0, 380, 30, 50);
ellipse(25, 380, 30, 50);
ellipse(50, 380, 50, 40);
ellipse(80, 380, 30, 30);
ellipse(500, 380, 30, 50);
ellipse(475, 380, 30, 50);
ellipse(450, 380, 50, 40);
ellipse(420, 380, 30, 30);
}
void drawChicken() {
fill(255, 0, 0);
//cockscomb
ellipse(431, 140, 15, 15);
ellipse(445, 140, 15, 15);
ellipse(455, 144, 15, 15);
ellipse(431, 250, 15, 15);
ellipse(445, 250, 15, 15);
ellipse(455, 254, 15, 15);
ellipse(70, 140, 15, 15);
ellipse(56, 140, 15, 15);
ellipse(46, 144, 15, 15);
ellipse(70, 250, 15, 15);
ellipse(56, 250, 15, 15);
ellipse(46, 254, 15, 15);
stroke(255);
//Chicken's mouth
triangle(415, 160, 425, 140, 450, 160);
triangle(415, 270, 425, 250, 450, 270);
triangle(50, 160, 75, 140, 84, 160);
triangle(50, 270, 75, 250, 84, 270);

fill(255, 255, 0);
rect(425, 140, 30, 25);
rect(425, 165, 65, 35);
rect(425, 250, 30, 25);
rect(425, 275, 65, 35);
rect(45, 140, 30, 25);
rect(10, 165, 65, 35);
rect(45, 250, 30, 25);
rect(10, 275, 65, 35);
noStroke();
//Chicken's eye
fill(255);
ellipse(433, 150, 12, 12);//1
ellipse(433, 260, 12, 12);//2
ellipse(67, 150, 12, 12);//3
ellipse(67, 260, 12, 12);//4
fill(0);
ellipse(430, 152, 5, 5);//1
ellipse(430, 262, 5, 5);//2
ellipse(70, 152, 5, 5);//3
ellipse(70, 262, 5, 5);//4

fill(255, 0, 0);
ellipse(490, 167, 26, 26);
ellipse(494, 182, 17, 17);
ellipse(496, 194, 14, 14);

ellipse(490, 278, 26, 26);
ellipse(494, 293, 17, 17);
ellipse(496, 304, 14, 14);

ellipse(10, 167, 26, 26);
ellipse(6, 182, 17, 17);
ellipse(4, 194, 14, 14);

ellipse(10, 278, 26, 26);
ellipse(6, 293, 17, 17);
ellipse(4, 304, 14, 14);
}
void drawArms (int p, int k, int j, int l, int u, int y, int h) {
fill (0, 0, 255);
quad((width/2)+p, (height/2)+k, (width/2)+p, (height/2)+j, (width/2)+l, (height/2)+u, (width/2)+l, (height/2)+y);
ellipse ((width/2)+l, (height/2)+h, 15, 10);
}
void drawBody() {
noStroke ();
fill (0, 0, 255);
triangle((width/2)-20, (height/2)-10, (width/2)-30, (height/2)-40, (width/2), (height/2)-20); //ears
triangle((width/2)+20, (height/2)-10, (width/2)+30, (height/2)-40, (width/2), (height/2)-20);
fill (255);
triangle((width/2)-20, (height/2)-10, (width/2)-30, (height/2)-40, (width/2)-15, (height/2)-20); //in ears
triangle((width/2)+20, (height/2)-10, (width/2)+30, (height/2)-40, (width/2)+15, (height/2)-20);
fill (0, 0, 255);
ellipse ((width/2)-10, (height/2)+130, 10, 70); //legs
ellipse ((width/2)+10, (height/2)+130, 10, 70);
rect ((width/2)+20, (height/2)+100, 15, 10, 10);//tail
ellipse (width/2, (height/2)+70, 70, 100); //body
fill (102, 204, 255);
ellipse (width/2, (height/2)+70, 40, 70); //in body
ellipse ((width/2)-10, height-85, 20, 10); //feet
ellipse ((width/2)+10, height-85, 20, 10);
fill (0, 0, 255);
ellipse (width/2, (height/2), 60, 60); //haed
noStroke ();
fill (255);
ellipse ((width/2)-10, (height/2)-15, 15, 20);//eyes
ellipse ((width/2)+10, (height/2)-15, 15, 20);
fill (0);
ellipse ((width/2)-10, (height/2)-13, 10, 15);//in eyes
ellipse ((width/2)+10, (height/2)-13, 10, 15);
fill (204, 255, 255);
arc((width/2), (height/2)+20, 40, 40, PI, 2*PI);//mouth
arc((width/2), (height/2)+20, 40, 20, 0, PI);//mouth
fill (153);
ellipse (width/2, (height/2)+10, 25, 25); //nose
stroke (102);
strokeWeight (1.8);
line (width/2, (height/2)-5, width/2, (height/2)+4);
curve((width/2)-3, (height/2)-20, width/2, (height/2)+4, (width/2)-9, (height/2)+12, (width/2)-6, (height/2)-10);
curve((width/2)+3, (height/2)-20, width/2, (height/2)+4, (width/2)+9, (height/2)+12, (width/2)+6, (height/2)-10);
noStroke();
fill (0);
ellipse (width/2, (height/2), 15, 10);
stroke (0, 0, 255);
strokeWeight (3);
curve((width/2)-25, (height/2)+20, (width/2)-20, (height/2), (width/2)-40, (height/2)+5, (width/2)-35, (height/2)+10);//Mustache
curve((width/2)+25, (height/2)+20, (width/2)+20, (height/2), (width/2)+40, (height/2)+5, (width/2)+35, (height/2)+10);
curve((width/2)-25, (height/2)+30, (width/2)-20, (height/2)+10, (width/2)-40, (height/2)+15, (width/2)-35, (height/2)+20);
curve((width/2)+25, (height/2)+30, (width/2)+20, (height/2)+10, (width/2)+40, (height/2)+15, (width/2)+35, (height/2)+20);
noStroke();
strokeWeight (0);
}
void SetEgg() {
x1[0]=random(-200, -150);
x2[0]=0-x1[0]+random(100, 200)+500;
y1[0]=185;
y2[0]=290;
while (i<x1.length) {
x1[i]=0-(x2[i-1]-500+random(0, 100));
x2[i]=0-x1[i]+random(50, 200)+500;
if (i%2==0) {
y1[i]=185;
y2[i]=185;
}
else {
y1[i]=295;
y2[i]=295;
}
i=i+1;
}
}



void breakEgg(int x)
{
fill(255, 204, 153);
stroke(1);
arc(x, 450, 20, 15, PI/2, TWO_PI-PI/2);
arc(x+10, 450, 20, 15, TWO_PI-PI/2, TWO_PI+PI/2);
fill(255, 100, 0);
ellipse(x+5, 455, 10, 10);
fill(0);
noStroke();
//left
triangle(x, 450, x, 442, x-5, 446);
triangle(x, 450, x, 458, x-5, 454);
//right
triangle(x+10, 450, x+10, 442, x+15, 446);
triangle(x+10, 450, x+10, 458, x+15, 454);
}


function ที่แต่ละคนรับผิดชอบ

นายชนานันน์ พงศ์กีรติกานต์ (56-010126-3004-3)
SetEgg() 
breakEgg(int x)

นางสาวสาริศา ปิ่นทอง (56-010126-3026-4)

drawTree()
drawChute()
drawBush()
drawChicken()

นางสาวอัญธิกา หนองบัว (56-010126-3028-1)

drawBody()
drawArms()

Abstract Class



void setup(){
  size(140, 30);
  background (255, 0, 102);
  Shape s= new Square("Shape a",4.5d);
  text((s.ToString()), 10, 15);
}
abstract class Shape{
  String Name;
  abstract double Perimeter();
  String GetName(){
     return Name;
  }
 String ToString(){
    return GetName() + " Square = " + Perimeter();
  }
}
class Square extends Shape{
  double h;
  Square(String n,double h){
    Name = n;
    h = h;
  }
  double Perimeter(){
  return 4*h;
  }
}

Interface



void setup() {
  size (150, 50);
  background (255, 0, 0);
  Moto Nokia = new Moto(false, false);
  Nokia.Calling();
  Nokia.Callend();
}
interface Phone {
  void Calling();
  void Callend();
}
class Moto implements Phone {
  boolean x, y;
  Moto(boolean aa, boolean ee) {
    this.x = aa;
    this.y = ee;
  }
  void Calling() {
    fill(255, 255, 0);
    text(("Nokia Calling= "+x), 20, 20);
  }
  void Callend() {
    fill(255, 255, 0);
    text(("Nokia Callend= "+y), 20, 35);
  }
}

UFO Sub_Class



class diagram 

Name:              UFO
Attribute:         x:int
                   y:int
                   r:int
Method:            UFO(e:int,f:int,t:int)
                   display()
                   click()


int count=0;
int r =0;

void setup () {
  size(400, 400);
}

Draw a = new Draw (50, 50, 0);


void draw() {
  background(0, 0, 255);
  a.display();
}

class Draw {
  int x;
  int y;
  int r;
  Draw(int e, int f, int t) {
    this.x=e;
    this.y=f;
    this.r=t;
  }


  void display() {

      if (r<120) {
      if (count==0) {

          x=x+5;
      }

      if (x==250) {
          count=1;
      }

      if (count==1) {
        y=y+5;
      }
      if (y==250) {
        count=2;
      }
      if (count==2) {
        x=x-5;
      }
      if (x==50) {
        count=3;
      }
      if (count==3) {
        y=y-5;
      }
      if (y==50) {
        count=0;
        r=r+1;
      }
      noStroke();
      fill(0, 255, 0);                                                 
      ellipse(x, y, 100, 130);
      fill(255, 255, 0);                                          
      ellipse(x, y+4, 150, 90);
    }
  }

}

Class UFO



void setup() {
  size(500, 500);
}

float n = 0;
float x = 0;
float y = 0;
class Fly {
  float a;
  float b;

  Fly(float x, float y) {
    this.a = x;
    this.b = y;
  }
  void display(float x, float y) {

    noStroke();
    fill(255);
    ellipse(170+cos(x)*100, 200+sin(y)*150, 90, 130);
    fill(0);
    ellipse(170+cos(x)*100, 200+sin(y)*150, 150, 90);
    fill(255);
  }
}
boolean move=false;
void mouseClicked() {
  if (move==false) {
    move = true;
    stop();
  }
}

void draw() {
  background(102, 0, 153);
  Fly f = new Fly(0, 0);
  f.display(x, y);
  x = x + 0.01;
  y = y + 0.01;
}

Maxtric Calculation



void setup() {
  size (50, 150);
  background (255, 255, 0);
  int z = 10;
  int[][] M = {
    {
      1, 2, 3
    }
    , {
      4, 5, 6
    }
    , {
      0, 7, 8
    }
  };
  int[][] m = {
    {
      9, 8, 0
    }
    , {
      2, 1, 3
    }
    , {
      7, 5, 7
    }
  };
  int[][] Ans = new int[3][3];

  int i, j;
  for (i=0;i<M.length;i++) {
    for (j=0;j<M[i].length;j++) {
      Ans[i][j] = M[i][j]+m[i][j];
      fill (0);
      text((Ans[i][j]), 20, 10+z);
      z=z+15;
    }
  }
}

OX-Table



void setup () {
  size (500, 500);
}
void draw () {
  drawt();
  drawOX();
}
void drawt() {
  stroke (0, 255, 0);
  fill(255, 0, 102);
  int i = 0;
  int [][] a = {
    {
      0, width/3, (width/3)*2, (width/3)*2, 0
    }
    , {
      0, height/3, (height/3)*2, 0, (width/3)*2
    }
  };
  rect(0, 0, width, height);
  strokeWeight (7);
  while (i<a[0].length) {
    rect(a[0][i], a[1][i], width/3, height/3);
    i=i+1;
  }
}
void drawOX() {
  stroke (255);
  strokeWeight(10);
  int Y=(width/3);
  int y=(width/3)/2;
  int x=(width/3)*2;
  int i=0;
  int I=0;
  int n=3;
  int N=2;
  while (i<n) {
    ellipse((width/3)/2, y, 100, 100);
    y = y+(height/3);
    i=i+1;
  }
  line (370, 470, 470, 370);
  line (470, 470, 370, 370);
  line (200, 300, 300, 200);
  line (300, 300, 200, 200);
}

Chess



int t[ ][ ] = {
  {
    0, 1, 2, 3, 4, 5, 6, 7
  }
  ,
  {
    0, 1, 2, 3, 4, 5, 6, 7
  }
};
int c[ ][ ]= {
  {
    2, 3, 4, 5, 6, 4, 3, 2
  }
  ,
  {
    1, 1, 1, 1, 1, 1, 1, 1
  }
  ,
  {
    0, 0, 0, 0, 0, 0, 0, 0
  }
  ,
  {
    0, 0, 0, 0, 0, 0, 0, 0
  }
  ,
  {
    0, 0, 0, 0, 0, 0, 0, 0
  }
  ,
  {
    0, 0, 0, 0, 0, 0, 0, 0
  }
  ,
  {
    1, 1, 1, 1, 1, 1, 1, 1
  }
  ,
  {
    2, 3, 4, 5, 6, 4, 3, 2
  }
};

void setup() {
  size(500, 500);
  for (int a=0 ; a<t[0].length ; a=a+1) {
    for (int b=0 ; b<t[1].length ; b++) {
      chanal(t[0][a]*(width/t[0].length), t[1][b]*(height/t[1].length));
      if ( a%2==0 && b%2==0 ) {
        chanal2(t[0][a]*(width/t[0].length), t[1][b]*(height/t[1].length));
      }
      if ( a%2==1 && b%2==1 ) {
        chanal2(t[0][a]*(width/t[0].length), t[1][b]*(height/t[1].length));
      }
    }
  }
  side();
}
void chanal(int x, int y) {
  strokeWeight(3);
  fill(255, 0, 0);
  noStroke();
  rect(x, y, width/t[0].length, height/t[1].length);
  noFill();
}
void chanal2(int x, int y) {
  strokeWeight(3);
  fill(0);
  rect(x, y, width/t[0].length, height/t[1].length);
  noFill();
  strokeWeight (3);
  stroke(153, 51, 0);
}
void side() {
  for (int i=0 ; i<c.length ; i++) {
    for (int j=0 ; j<c.length ; j++) {
      if (c[j][i]==1) {
        Prawn (((width/c[j].length)/2)+(i*(width/c[j].length)), ((height/c.length)/2)+(j*(height/c.length)));
      }
      if (c[j][i]==2) {
        Rook (((width/c[j].length)/2)+(i*(width/c[j].length)), ((height/c.length)/2)+(j*(height/c.length)));
      }
      if (c[j][i]==3) {
        Knight (((width/c[j].length)/2)+(i*(width/c[j].length)), ((height/c.length)/2)+(j*(height/c.length)));
      }
      if (c[j][i]==4) {
        Bishop (((width/c[j].length)/2)+(i*(width/c[j].length)), ((height/c.length)/2)+(j*(height/c.length)));
      }
      if (c[j][i]==5) {
        King (((width/c[j].length)/2)+(i*(width/c[j].length)), ((height/c.length)/2)+(j*(height/c.length)));
      }
      if (c[j][i]==6) {
        Queen (((width/c[j].length)/2)+(i*(width/c[j].length)), ((height/c.length)/2)+(j*(height/c.length)));
      }
    }
  }
}

void Rook(int x, int y) {
  fill(0, 0, 255);
  ellipse(x, y, (width/c.length)/1.2, (height/c.length)/1.2);
  fill(255);  
  text("Rook", x-(width/c.length)/4, y);
}
void Knight(int x, int y) {
  fill(0, 0, 255);
  ellipse(x, y, (width/c.length)/1.2, (height/c.length)/1.2);
  fill(255);  
  text("Knight", x-(width/c.length)/3, y+4);
}
void Bishop(int x, int y) {
  fill(0, 0, 255);
  ellipse(x, y, (width/c.length)/1.2, (height/c.length)/1.2);
  fill(255);  
  text("Bishop", x-(width/c.length)/3, y+4);
}
void Queen(int x, int y) {
  fill(0, 0, 255);
  ellipse(x, y, (width/c.length)/1.2, (height/c.length)/1.2);
  fill(255);  
  text("Queen", x-(width/c.length)/3, y+4);
}
void King(int x, int y) {
  fill(0, 0, 255);
  ellipse(x, y, (width/c.length)/1.2, (height/c.length)/1.2);
  fill(255);  
  text("King", x-(width/c.length)/4, y+4);
}
void Prawn(int x, int y) {
  fill(255, 255, 0);
  ellipse(x, y, (width/c.length)/1.2, (height/c.length)/1.2);
  fill(0);  
  text("Prawn", x-(width/c.length)/3, y+4);
}