-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path중력장 공튀기기 게임
194 lines (161 loc) · 5.67 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
'''
glowscript에 이 코트 복사하고 실행하면 게임을 즐기실수 있습니다
최대한 오랜시간 버텨야 점수가 높습니다.
좌우 방향키이용
빨간볼 ->공의 속도가 빨라짐
파란볼 ->공의 속도가 느려짐
하얀볼->중력을 가지고있는 공 가까이가면 공의 궤도가 휘어진다
'''
GlowScript 2.7 VPython
ball = sphere (color = color.green, radius = 0.4, make_trail=True, retain=200)
ball.v = vector (-0.7, -0.9,0)
bar_r = 9
G = 6.67e-11
#bar's angle = t
bounce_c=0
time=0
t = -pi/2
b1 = sphere(pos=vector(0,0,0), radius=0.2, color=color.red)
b2 = sphere(pos=vector(0,0,0), radius=0.2, color=color.blue)
b3 = sphere(pos=vector(0,0,0), radius=0.2, color=color.yellow)
b4 = sphere(pos= vec(5,0,0), radius = 0.3,color = color.white,mass=13000000000)
b1.opacity = 0
b2.opacity = 0
b3.opacity = 0
b4.opacity = 0
flag =0
flag1 =0
#gameover
over_cnt=0
wtext_game_over = text(text = "GAME OVER", pos = vec(0,0,0), align = 'center', color = color.red)
wtext_game_over.length = 50
wtext_game_over.height = 10
wtext_game_over.visible = False
bar_pos = vec(bar_r*cos(t),bar_r*sin(t),0)
bar = box(pos = bar_pos, size = vec(0.1,3,1), axis=-vec(5*cos(t),5*sin(t),0), color = color.white)
#the display
scene.autoscale = True
scene.center = ball.pos
def balls_colli(a,b): #a == ball b == item
global b1,b2,b3,b4,it,flag,b1_c,b2_c,b3_c,bar_r
if(mag(a.pos-b.pos) < a.radius + b.radius):
if(b == b1 and b1.opacity == 1):
b1.opacity = 0
ball.v = ball.v *1.5
ball.color = color.red
if(b == b2 and b2.opacity == 1):
b2.opacity = 0
ball.v = ball.v *2/3
ball.color = color.blue
if(b == b3 and b3.opacity == 1):
b3.opacity = 0
bar_r = bar_r - 1
bar.pos = vec(bar_r*cos(t),bar_r*sin(t),0)
if(b == b4 and b4.opacity == 1):
b4.opacity = 1
it = 0
flag = 0
def keyInput(evt):
#The key value is input by the user's keyboard.
s = evt.key
if s == 'a':
global bar_pos
global t
t = t - pi/2*(1/30)
bar.pos = vec(bar_r*cos(t),bar_r*sin(t),0)
bar.axis = vec(0,0,0)+vec(cos(t),sin(t),0)
bar.size = vec(0.1,3,1)
else if s == 'd':
global bar_pos
global t
t = t + pi/2*1/30
bar.pos = vec(bar_r*cos(t),bar_r*sin(t),0)
bar.axis = vec(0,0,0)+vec(cos(t),sin(t),0)
bar.size = vec(0.1,3,1)
scene.bind('keydown', keyInput)
dt = 0.01
it = 0
while True:
rate(600)
time=time+1/600
#gameover
if over_cnt ==1 and flag1 == 0 :
print(over_cnt+2)
flag1 = 1
if over_cnt ==2 and flag1 == 1:
print(over_cnt)
flag1 = 2
if over_cnt ==3 and flag1 == 2 :
wtext_game_over.visible = True
print(over_cnt-2)
print('score')###############################
print(bounce_c/time*1000)
break
it = it + dt
if(b4.opacity==0):
ball.pos = ball.pos + ball.v*dt
ball.pos.f = ball.pos + ball.v*dt
bar_axis = hat(cross(bar.axis,vec(0,0,1)))
bar_axis_ball_hori = dot(ball.pos,bar_axis)*bar_axis
bar_axis_ball_hor_mag = mag(dot(ball.pos,bar_axis)*bar_axis)
bar_axis_ball_verti_mag = mag(ball.pos - bar_axis_ball_hori)
bar_axis_ballf_hori = dot(ball.pos.f,bar_axis)*bar_axis
bar_axis_ballf_hor_mag = mag(dot(ball.pos.f,bar_axis)*bar_axis)
bar_axis_ballf_verti_mag = mag(ball.pos.f - bar_axis_ballf_hori)
else:
acc = -G*b4.mass/mag(ball.pos - b4.pos)**2*norm(ball.pos - b4.pos)
ball.v = ball.v + acc*dt
ball.pos = ball.pos + ball.v*dt
ball.pos.f = ball.pos + ball.v*dt
bar_axis = hat(cross(bar.axis,vec(0,0,1)))
bar_axis_ball_hori = dot(ball.pos,bar_axis)*bar_axis
bar_axis_ball_hor_mag = mag(dot(ball.pos,bar_axis)*bar_axis)
bar_axis_ball_verti_mag = mag(ball.pos - bar_axis_ball_hori)
bar_axis_ballf_hori = dot(ball.pos.f,bar_axis)*bar_axis
bar_axis_ballf_hor_mag = mag(dot(ball.pos.f,bar_axis)*bar_axis)
bar_axis_ballf_verti_mag = mag(ball.pos.f - bar_axis_ballf_hori)
if (bar_r-bar_axis_ballf_verti_mag < ball.radius+0.05 and bar_r-bar_axis_ballf_verti_mag > ball.radius+0.05-0.04 and mag(ball.pos.f-bar.pos)<bar_r and bar_axis_ballf_hor_mag<1.5):
#수평성분
c_hat = bar_axis
ball_hori = dot(ball.v, c_hat)*c_hat
ball_verti = ball.v - ball_hori
ball.v = ball_hori - ball_verti
bounce_c = bounce_c + 1
if(mag(ball.pos)>bar_r+3):
ball.pos = vec(0,0,0)
over_cnt+=1
balls_colli(ball,b1)
balls_colli(ball,b2)
balls_colli(ball,b3)
balls_colli(ball,b4)
if(it >= 5.0 and flag == 0):
a=-3+random()*6
b=-3+random()*6
c=random()
if c < 0.3333:
b1.pos=vec(a,b,0)
b1.opacity=1
flag = 1
else if c < 0.6666666:
b2.pos=vec(a,b,0)
b2.opacity=1
flag = 1
else if c < 0.667:
b3.pos=vec(a,b,0)
b3.opacity=1
flag = 1
else if c < 1:
b4.pos=vec(a,b,0)
b4.opacity=1
flag = 1
if(it >=20.0):
b1.opacity = 0
b2.opacity = 0
b3.opacity = 0
b4.opacity = 0
it = 0
flag = 0
# if not (bar.pos.y+3*sin(t) > ball.pos.y > bar.pos.y-3*sin(t)):
# ball.p.y = -ball.p.y
# if not (side > ball.pos.z > -side):
# ball.p.z = -ball.p.z