By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,097 Members | 1,161 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,097 IT Pros & Developers. It's quick & easy.

Flickering

Bob Ross
100+
P: 119
I am trying to make a very simple game involving two lines flying round shooting each other. The trouble I am having is that I am using a loop to handle the controls which includes a screen refresh. This is making the lines (players) on the screen to flicker constantly.

Any help to stop this would be greatly appreciated.

Here is my code
Expand|Select|Wrap|Line Numbers
  1. Dim Player1angle As Single
  2. Dim Player1ratio As Double 'Y/X or (y2-y1)/(x2-x1)
  3. Dim P1Xhigher As String
  4. Dim P1YHigher As String
  5. Dim P1X As Double
  6. Dim P1Y As Double
  7. Dim Player2angle As Double
  8. Const Pi As Single = 3.14159
  9. Const Rad As Single = Pi / 180
  10. Private Const D_THETA = 3.14159265 / 12
  11. Private Theta As Single
  12. Private R1 As Single
  13. Private R2 As Single
  14. Private Cx1 As Single
  15. Private Cy1 As Single
  16. Dim TmeToEnd As Boolean
  17. Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
  18. Dim Keys(255) As Boolean
  19.  
  20.  
  21. Private Sub StartButton_Click()
  22. Player1Controls.Visible = False
  23. Player2Controls.Visible = False
  24. Title.Visible = False
  25. StartButton.Visible = False
  26. Timer1.Enabled = True
  27. Call Main
  28. End Sub
  29.  
  30.  
  31. Private Sub Timer1_Timer()
  32. 'Player1.Y1 = Player1.Y1 + 10
  33. 'Player1.Y2 = Player1.Y2 + 10
  34. Player2.Y1 = Player2.Y1 + 10
  35. Player2.Y2 = Player2.Y2 + 10
  36.  
  37.  
  38. If P2Bullet.Visible = True Then
  39.  
  40. End If
  41.  
  42. End Sub
  43.  
  44. Public Function ArcTan(x As Double) As Double
  45.  
  46.   'Inverse Tangent
  47.  
  48.     On Error Resume Next
  49.         ArcTan = Atn(x) '* (180 / Pi)
  50.     On Error GoTo 0
  51.  
  52. End Function
  53.  
  54. Public Function DegToRad(Degrees As Single) As Single
  55.  
  56.   'DegToRad = Degrees / 180 * Pi
  57.  
  58.     DegToRad = Degrees * Rad
  59.  
  60. End Function
  61.  
  62. Public Function RadToDeg(Radians As Single) As Single
  63.  
  64.   'RadToDeg = Radians * 180 / Pi
  65.  
  66.     RadToDeg = Radians / Rad
  67.  
  68. End Function
  69.  
  70. Public Sub Main()
  71. Do
  72.  
  73. 'Show and refresh the form
  74.  
  75. Me.Refresh
  76.  
  77.       If Keys(vbKeyUp) Then
  78.         dx = Player1.X2 - Player1.X1
  79.         dy = Player1.Y2 - Player1.Y1
  80.         R1 = Sqr(dx * dx + dy * dy) + 100
  81.         P1Y = (dy / R1)
  82.         P1X = (dx / R1)
  83.         Player1.X1 = Player1.X1 - (5 * P1X)
  84.         Player1.X2 = Player1.X2 - (5 * P1X)
  85.         Player1.Y1 = Player1.Y1 - (5 * P1Y)
  86.         Player1.Y2 = Player1.Y2 - (5 * P1Y)
  87.  
  88.       End If
  89.  
  90.       If Keys(vbKeyDown) Then
  91.         dx = Player1.X2 - Player1.X1
  92.         dy = Player1.Y2 - Player1.Y1
  93.         R1 = Sqr(dx * dx + dy * dy) + 100
  94.         Cx1 = Player1.X1
  95.         Cy1 = Player1.Y1
  96.         P1Y = (dy / R1)
  97.         P1X = (dx / R1)
  98.         Player1.X1 = Player1.X1 + (5 * P1X)
  99.         Player1.X2 = Player1.X2 + (5 * P1X)
  100.         Player1.Y1 = Player1.Y1 + (5 * P1Y)
  101.         Player1.Y2 = Player1.Y2 + (5 * P1Y)
  102.       End If
  103.  
  104.       If Keys(vbKeyLeft) Then
  105.         dx = Player1.X2 - Player1.X1
  106.         dy = Player1.Y2 - Player1.Y1
  107.         R1 = Sqr(dx * dx + dy * dy)
  108.         Cx1 = Player1.X1
  109.         Cy1 = Player1.Y1
  110.         Theta = Theta - (D_THETA / 100)
  111.  
  112.         Player1.X2 = Cx1 + Cos(Theta) * R1
  113.         Player1.Y2 = Cy1 + Sin(Theta) * R1
  114.       End If
  115.  
  116.       If Keys(vbKeyRight) Then
  117.         dx = Player1.X2 - Player1.X1
  118.         dy = Player1.Y2 - Player1.Y1
  119.         R1 = Sqr(dx * dx + dy * dy)
  120.         Cx1 = Player1.X1
  121.         Cy1 = Player1.Y1
  122.         Theta = Theta + (D_THETA / 100)
  123.  
  124.         Player1.X2 = Cx1 + Cos(Theta) * R1
  125.         Player1.Y2 = Cy1 + Sin(Theta) * R1
  126.       End If
  127.  
  128. If P1Bullet.Visible = True Then
  129.         dx = P1Bullet.X2 - P1Bullet.X1
  130.         dy = P1Bullet.Y2 - P1Bullet.Y1
  131.         R1 = Sqr(dx * dx + dy * dy) + 100
  132.         Cx1 = Player1.X1
  133.         Cy1 = Player1.Y1
  134.         P1Y = (dy / R1)
  135.         P1X = (dx / R1)
  136.         P1Bullet.X1 = P1Bullet.X1 + (5 * P1X)
  137.         P1Bullet.X2 = P1Bullet.X2 + (5 * P1X)
  138.         P1Bullet.Y1 = P1Bullet.Y1 + (5 * P1Y)
  139.         P1Bullet.Y2 = P1Bullet.Y2 + (5 * P1Y)
  140. End If
  141.  
  142. DoEvents
  143.  
  144.  
  145. Loop Until TmeToEnd
  146. End Sub
  147.  
  148. Private Sub Form_Unload(Cancel As Integer)
  149.  
  150. 'Mark the program to end
  151. TimeToEnd = True
  152.  
  153. End Sub
  154.  
  155. Private Sub Form_KeyDown(KeyCode As Integer, Shift As _
  156.     Integer)
  157.    'The key KeyCode is pressed now...
  158.    Keys(KeyCode) = True
  159. End Sub
  160.  
  161. Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
  162.    'The key KeyCode is NOT pressed now...
  163.    Keys(KeyCode) = False
  164. End Sub
  165.  
Mar 19 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.