1

I'm trying to use Vpython 3 to create the game brick. I'm new to programming but so far I've discovered how to make a ball move around and I have discovered how to use key events to move my paddle in the game. Here is my program so far:

from visual import *

ball = sphere(pos=(-5,0), radius=0.2, color=color.cyan)
wallR = box(pos=(3,0), size=(.2,12), color=color.green)
wallL = box(pos=(-7,0), size=(.2,12), color=color.green)
wallT = box(pos=(-2,6), size=(10,.2), color=color.green)
wallB = box(pos=(-2,-6), size=(10,.2), color=color.green)
paddle = box(pos=(0,-5), width=0, height=.2, length=1.5)


ball.velocity = vector(10,10)
deltat = 0.005
t = 0


while t<100:
    pi=3.14
    angle=pi/2
    rate(50)
    if ball.pos.x < wallL.pos.x or  ball.pos.x > wallR.pos.x:
        ball.velocity.x = -ball.velocity.x
    if ball.pos.y > wallT.pos.y or  ball.pos.y < wallB.pos.y:
        ball.velocity.y = -ball.velocity.y
    ball.pos = ball.pos + ball.velocity*deltat
    t = t + deltat
    movement=scene.kb.getkey()
    if movement=='right':
        paddle.pos.x+=1
    if movement=='left':
        paddle.pos.x-=1

My problem is that my ball only moves when I press the left or right arrow key and I want it to move at all times. I assume that it does this because the scene.kb.getkey() freezes everything for some reason. How do I get it to let the ball keep going while checking to see if a key has been pressed? (please explain it very simply, I am a beginner).

4

1 回答 1

1

我想通了,通过在移动scene.kb.getkey()之前的if语句中使用scene.kb.keys,我能够让球和我的桨同时移动。但是,如果有更好的方法可以做到这一点,我仍然希望得到反馈。

于 2013-11-06T18:54:06.320 回答