我一直在开发一款类似于《愤怒的小鸟》的游戏,在这款游戏中,玩家将一枚炮弹射向目标,目的是将其摧毁。然而,我正在努力寻找一种方式来显示画布之前,初始启动参数输入和动画开始。当代码运行时发生的第一件事是它要求输入,但我想在这之前显示可视化。想知道是否有一种简单的方法可以做到这一点,代码如下。
scene = canvas(width=640, height=480, center=vector(8,5,0),range=8, background=color.blue)
ground = box(pos=vector(8,-5,0), length=16, height=10, width=0.5, color=color.green)
dist = 5 + random()*10
target = box(pos=vector(dist,1,0), length=0.5, height=2, width=0.5, color=color.red)
height = random() #determines random height of platform between 0 and 1 meters
platform = box(pos=vector(-2,height-0.25,0), length=4, height=0.5, width=0.5, color=color.white)
bird = sphere(pos=vector(0,height+0.3,0), radius=0.3, color=color.yellow)
hit = 0
while hit == 0: #loops until player hits target
#Take initial parameters from player
v0 = float(input('Input your launch velocity:'))
dtheta = float(input('Input your launch angle in degrees:'))
theta = np.radians(dtheta) #convert launch angle to radians
#Draw arrow for initial momentum
px = 0.1*v0*np.cos(theta) #calculate initial x momentum
py = 0.1*v0*np.sin(theta) #calculate initial y momentum
momentum = arrow(pos=bird.pos, axis=vector(px,py,0), shaftwidth=0.1, color=color.orange) #draw arrow representing projectiles initial momentum
# Calculate position of the projectile at a specified interval of time
# Code adapted from my submission to PHAS0007: Computing Session 8
#Define intial parameters
g = 9.81
x = x0 = 0
y = y0 = height
dt = 0.0001 #timestep
t = 0
#conditions that alllow looping when projectile is not in contact with target
while (y > 0 and (x < dist-0.25 or x > dist+0.25)) or (y > 2 and x >= dist-0.25 and x<= dist+0.25):
rate(5000) #set rate of animation
x = x0 + v0*t*np.cos(theta) # calculate new x position
y = y0 + v0*t*np.sin(theta)-0.5*g*t**2 # calculate new y position
bird.pos = vector(x,y,0) # redraw projectile at new position
px = 0.1*v0*np.cos(theta) #calculate x momentum
py = 0.1*v0*np.sin(theta)-0.1*g*t #calculate y momentum
momentum.pos = bird.pos #redraw momentum arrow in new position
momentum.axis = vector(px,py,0) #redraw momentum arrow direction
t += dt # increase timestep increment by dt
momentum.visible = False #removes momentum arrow after projectile motion is finished
#use inverse condition of the loop to determine and print whether the target was hit or not
if y >= 0 and y <= 2 and x >= dist-0.25 and x <= dist+0.25:
#calculate whether the target topples or not
trest = 0.5*100*g*0.5 #calculate the restoring torque
contact_t = 0.01 #define the time projectile is in contact with target
fapp = vector(px, py, 0) / contact_t #calculate applied force of projectile on target
tapp = cross(fapp, vector((dist+0.25)-x, -y, 0)) #calculate applied torque vector of projectile on target
tappm = mag(tapp) #calculate magnitude of applied torque vector
if tappm > trest: #target toppled
#redraw target to be toppled
target.pos = vector(dist+1.25,0.25,0)
target.length = 2
target.height =0.5
print('Target toppled, success!')
hit = 1
else: #target hit but not toppled
print('Target hit but not toppled, try again.')
#animate bird dropping to ground
t=0 #reset time counter
y0=y #set y0 equal to current y position
while y > 0.3:
rate(5000)
y = y0 - 0.5*g*t**2
bird.pos = vector(x,y,0)
t += dt
else:
print('Target Missed, try again.')
问题来源StackOverflow 地址:/questions/59380361/vpython-is-there-anyway-to-display-a-canvas-before-an-animation-begins
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。