-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path신호등
96 lines (80 loc) · 2.36 KB
/
신호등
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
int currentState = 1; // 현재 신호 상태 (0: 빨강, 1: 초록, 2: 노랑)
void setup() {
size(400, 600);
noCursor(); // 기본 마우스 포인터 숨기기
}
void draw() {
background(255);
drawRoad();
drawCrosswalk();
drawTrafficLight(); // 신호등 그리기
drawCar(mouseX, mouseY); // 마우스 위치에 자동차 그리기
// 도로 중심(x 좌표 200)과 마우스 위치 간의 거리 계산
float distance = dist(200, height / 2, mouseX, mouseY);
// 거리에 따라 신호등 상태 변경
if (distance < 50) {
currentState = 0; // 빨간불
} else if (distance < 150) {
currentState = 2; // 노란불
} else {
currentState = 1; // 초록불
}
}
void drawRoad() {
// 도로 그리기
fill(50);
rect(150, 0, 100, height);
}
void drawCrosswalk() {
// 횡단보도 그리기
int crosswalkX = 150; // 횡단보도의 시작 X 좌표
int crosswalkY = height / 2 - 15; // 횡단보도의 시작 Y 좌표
int crosswalkHeight = 30; // 횡단보도의 높이
float stripeWidth = 9.1; // 줄무늬의 너비
int numStripes = 11; // 줄무늬의 개수
fill(255); // 흰색 설정
noStroke(); // 테두리 없음
for (int i = 0; i < numStripes; i++) {
rect(crosswalkX + i * stripeWidth * 2, crosswalkY, stripeWidth, crosswalkHeight);
}
}
void drawTrafficLight() {
// 신호등 그리기
fill(200);
rect(300, 50, 50, 150);
// 빨간불
if (currentState == 0) {
fill(255, 0, 0);
} else {
fill(100, 0, 0);
}
ellipse(325, 75, 30, 30);
// 노란불
if (currentState == 2) {
fill(255, 255, 0);
} else {
fill(100, 100, 0);
}
ellipse(325, 125, 30, 30);
// 초록불
if (currentState == 1) {
fill(0, 255, 0);
} else {
fill(0, 100, 0);
}
ellipse(325, 175, 30, 30);
}
void drawCar(float x, float y) {
// 자동차가 도로 안에 위치하도록 제한
x = constrain(x, 150 + 20, 150 + 100 - 20);
// 자동차 몸체
fill(255, 200, 0); // 노란색으로 설정
rect(x - 20, y - 10, 40, 20, 5); // 라운드 코너를 가진 사각형
// 창문
fill(150, 200, 255); // 연한 파란색으로 설정
rect(x - 15, y - 8, 15, 12, 3); // 라운드 코너를 가진 작은 사각형
// 휠
fill(0); // 검정색
ellipse(x - 12, y + 7, 8, 8); // 왼쪽 바퀴
ellipse(x + 12, y + 7, 8, 8); // 오른쪽 바퀴
}