1

I'm trying to create a dot that moves around the screen, bounces off the edges, and curves in a random direction every 50 frames or so.

What I've done so for is get a ball to move constantly and bounce off of the screen edges. Please note that this uses PsychoPy:

win = visual.Window(size=(1600, 900), fullscr=False, screen=0, allowGUI=False, allowStencil=False, units='pix',
monitor='testMonitor', colorSpace=u'rgb', color=[0.51,0.51,0.51])

keys = event.BuilderKeyResponse()

dot1 = visual.Circle(win=win, name='dot1',units='pix',
    radius=10, edges=32,
    ori=0, pos=(0,0),
    lineWidth=1, lineColor='red', lineColorSpace='rgb',
    fillColor='red', fillColorSpace='rgb',
    opacity=1,interpolate=True)

x_change = 10
y_change = 10

while True:
    dot1.pos+=(x_change,y_change)

    if dot1.pos[0] > 790 or dot1.pos[0] < -790:
        x_change = x_change * -1
    if dot1.pos[1] > 440 or dot1.pos[1] < -440:
        y_change = y_change * -1

    dot1.draw()
    win.flip()

    if event.getKeys(keyList=["escape"]):
        core.quit()

I imagine that this will require some trig, which I barely understand. Could anyone point me in the right direction? What variables will I need, and how should they be manipulated?

4

1 回答 1

2

一般的策略是这样的:计算循环内的帧数,x_changey_change在所需帧上改变新角度(例如,每 50 帧)。我将明确使用角度和速度来设置x_changey_change使用三角函数的值:

# New stuff:
import numpy as np
frameN = 50  # To set angle in first loop iteration
speed = 14  # initial speed in whatever unit the stimulus use.
angle = 45  # initial angle in degrees
x_change = np.cos(angle) * speed  # initial
y_change = np.sin(angle) * speed  # initial

while True:
    # More new stuff: Change direction angle (in degrees)
    if frameN == 50:
        angle_current = np.rad2deg(np.arctan(y_change / float(x_change)))  # float to prevent integer division
        angle_change = np.random.randint(-180, 180)  # change interval to your liking or set to a constant value or something
        angle = angle_current + angle_change  # new angle

        x_change = np.cos(angle) * speed
        y_change = np.sin(angle) * speed
        frameN = 0
    frameN += 1

    dot1.pos+=(x_change,y_change)

    if dot1.pos[0] > 790 or dot1.pos[0] < -790:
        x_change = x_change * -1
    if dot1.pos[1] > 440 or dot1.pos[1] < -440:
        y_change = y_change * -1


    dot1.draw()
    win.flip()

    if event.getKeys(keyList=["escape"]):
        core.quit()

更多随机性的选项:

  • 您可以控制速度(例如设置speed = np.random.randint(1, 20)
  • 您可以控制下次更改角度的框架(frameN = np.random.randint(40, 60)
  • 如上所述,您可以更改角度变化的间隔。
于 2014-10-26T10:08:34.540 回答