如何在没有抖动的情况下独立移动形状?

时间:2017-02-20 23:42:31

标签: game-physics velocity shapes smallbasic

这是完整的代码:

GraphicsWindow.Clear()
GraphicsWindow.CanResize = "false"
GraphicsWindow.Height = Desktop.Height-200
GraphicsWindow.Width = Desktop.Width-200

scount = Math.GetRandomNumber(25)
For s = 1 To scount
  Shx[s] = Math.GetRandomNumber(GraphicsWindow.Width-100)
  Shy[s] = Math.GetRandomNumber(GraphicsWindow.Height-100)
  shsize[s] = Math.GetRandomNumber(50)
  Sh[s] = Shapes.AddEllipse(shsize[s],shsize[s])
  Shapes.Move(Sh[s],Shx[s],Shy[s])
EndFor

loop:
For s = 1 to scount
  op[s] = Math.GetRandomNumber(2)
  If op[s] = 1 Then
    vel[s] = .5
  EndIf
  If op[s] = 2 Then
    vel[s] = -.5
  EndIf
  Shx[s] = Shx[s] + vel[s]
  Shy[s] = Shy[s] + vel[s]  
  Shapes.Move(Sh[s],Shx[s],Shy[s])
EndFor
Goto loop

我的猜测是问题在于:

op[s] = Math.GetRandomNumber(2)
  If op[s] = 1 Then
    vel[s] = .5
  EndIf
  If op[s] = 2 Then
    vel[s] = -.5
  EndIf

我需要做些什么才能使形状在独立的方向上移动而不会抖动?

2 个答案:

答案 0 :(得分:0)

我说:

op[s] = Math.GetRandomNumber(2)
 If op[s] = 1 Then
vel[s] = .5
 EndIf
If op[s] = 2 Then
 vel[s] = -.5
EndIf

在初始化循环中,形状不再抖动。我的猜测是" vel"变量每次都被重新分配给形状,导致抖动。

答案 1 :(得分:0)

你可以做的另一件事是每秒做帧。让所有对象进行数学运算然后更新它。所有的数学运算完成之后运行tHis就好了 通常每秒60帧,所以1/60,以获得每帧秒数。你还需要一个计时器来检查经过的时间,这样你就可以给计算机时间在适当的时间移动每个形状。如果您需要更多代码,我很乐意提供一些代码。

  GraphicsWindow.Clear()
GraphicsWindow.CanResize = "false"
GraphicsWindow.Height = Desktop.Height-200
GraphicsWindow.Width = Desktop.Width-200

scount = Math.GetRandomNumber(25)
For s = 1 To scount
  Shx[s] = Math.GetRandomNumber(GraphicsWindow.Width-100)
  Shy[s] = Math.GetRandomNumber(GraphicsWindow.Height-100)
  shsize[s] = Math.GetRandomNumber(50)
  Sh[s] = Shapes.AddEllipse(shsize[s],shsize[s])
  Shapes.Move(Sh[s],Shx[s],Shy[s])
   op[s] = Math.GetRandomNumber(2)
EndFor

loop:
Time = Clock.ElapsedMilliseconds
For s = 1 to scount

  If op[s] = 1 Then
    vel[s] = .5
  EndIf
  If op[s] = 2 Then
    vel[s] = -.5
  EndIf
  Shx[s] = Shx[s] + vel[s]
  Shy[s] = Shy[s] + vel[s]  
  Shapes.Move(Sh[s],Shx[s],Shy[s])
EndFor
frames()
Goto loop

Sub frames
     timeend = Clock.ElapsedMilliseconds
  TextWindow.WriteLine((timeend - Time )/1000)
  framelength = 60
  Timeperframe = 1/framelength
  while (((timeend - Time )/1000) <  Timeperframe)
    timeend = Clock.ElapsedMilliseconds

  EndWhile
  Time = Clock.ElapsedMilliseconds

  endsub