-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
4.2.1 Ghost.js
61 lines (59 loc) · 1.78 KB
/
4.2.1 Ghost.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Constants for main ghost body
var HEAD_RADIUS = 70;
var BODY_WIDTH = HEAD_RADIUS * 2;
var BODY_HEIGHT = 120;
var NUM_FEET = 3;
var FOOT_RADIUS = (BODY_WIDTH) / (NUM_FEET * 2);
var BODY_COLOR = Color.red;
// Constants for eyes
var PUPIL_RADIUS = 8;
var PUPIL_LEFT_OFFSET = 16;
var PUPIL_RIGHT_OFFSET = 40;
var EYE_RADIUS = 20;
var EYE_OFFSET = 28;
var EYE_COLOR = Color.white;
var PUPIL_COLOR = Color.blue;
/* This program draws a ghost on the canvas. */
function start(){
var centerX = getWidth()/2;
var centerY = getHeight()/2;
drawGhost(centerX,centerY, Color.red);
var footMiddle = new Circle(FOOT_RADIUS);
footMiddle.setColor(Color.RED);
add(footMiddle);
var foot2 = new Circle(FOOT_RADIUS);
foot2.setPosition(centerX + FOOT_RADIUS*2, centerY + BODY_HEIGHT + FOOT_RADIUS/8);
foot2.setColor(Color.RED);
add(foot2);
var foot3 = new Circle(FOOT_RADIUS);
foot3.setPosition(centerX - FOOT_RADIUS*2, centerY + BODY_HEIGHT + FOOT_RADIUS/8);
foot3.setColor(Color.RED);
add(foot3);
}
//function which basically draws the ghost.
function drawGhost(centerX, centerY, color){
drawBody(centerX, centerY, color);
drawEye(centerX-EYE_OFFSET, centerY);
drawEye(centerX+EYE_OFFSET, centerY);
}
//function draws eye
function drawEye(x,y){
drawCircle(x, y, EYE_RADIUS, Color.white);
drawCircle(x+PUPIL_LEFT_OFFSET, y, PUPIL_RADIUS, Color.blue);
}
function drawBody(x,y,col){
drawCircle(x,y,HEAD_RADIUS,col);
drawRect(x-HEAD_RADIUS, y, BODY_WIDTH, BODY_HEIGHT, col);
}
function drawCircle(x,y,rad,col){
var circle = new Circle(rad);
circle.setPosition(x,y);
circle.setColor(col);
add(circle);
}
function drawRect(x,y,w,h,col){
var rect = new Rectangle(w,h);
rect.setPosition(x,y);
rect.setColor(col);
add(rect);
}