0

我正在尝试编写一个显示加速轮的代码。只要用户按下“a”,车轮就会逆时针加速。问题是它转向正确的方向,但它没有加速。这是我正在使用的代码(在 PTB-3 和 Windows XP 中):

img=imread('c:\images.jpg');  
[yimg,ximg,z]=size(img);  
rot_spd = 1;  
larrow = KbName('a'); % modify this for Windows  
rarrow = KbName('b');  
[w,rect]=Screen('OpenWindow',0,[0 0 0]);  
sx = 400; % desired x-size of image (pixels)  
sy = yimg*sx/ximg; % desired y-size--keep proportional  
t = Screen('MakeTexture',w,img);  
bdown=0;  
th = 0; % initial rotation angle (degrees)  
HideCursor  
while(~any(bdown)) % exit loop if mouse button is pressed  
    [x,y,bdown]=GetMouse;  
    [keyisdown,secs,keycode] = KbCheck;    
    if(keycode(larrow))  
        th = th - rot_spd-1; % accelerate counterclockwise  
        th  
    end  
    if(keycode(rarrow))  
        th = th + rot_spd+1; % accelerate clockwise  
        th  
    end  
    destrect=[x-sx/2,y-sy/2,x+sx/2,y+sy/2];  
    Screen('DrawTexture',w,t,[],destrect,th);  
    Screen('Flip',w);   
end  
Screen('Close',w)   
ShowCursor   

如果有人知道它为什么不加速,我会非常感激。

4

1 回答 1

1
if(keycode(larrow))  
    th = th - rot_spd-1; % accelerate counterclockwise  
    th  
end  
if(keycode(rarrow))  
    th = th + rot_spd+1; % accelerate clockwise  
    th  
end 

该代码似乎只影响th一个“周期”。相反,您应该修改旋转速度,并让旋转速度影响角度。

尝试这个:

if(keycode(larrow))  
    rot_spd-=1; % accelerate counterclockwise  
    th  
end
if(keycode(rarrow))  
    rot_spd+=1; % accelerate clockwise  
    th  
end
the+=rot_speed;
于 2011-06-30T21:45:55.067 回答