473,406 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Flickering

Bob Ross
119 100+
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
0 877

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

Similar topics

3
by: Shoaib | last post by:
I have an IFRAME in HTML page whose source is set to some xml page.XML page gets displayed properly. But when i open some other IFRAME on top that IFRAME displaying xml, there is lot of flickering...
6
by: Joaquin Grech | last post by:
Hi I did alot of research on this on the web and msdn and I couldn't find anything. I have a listview showing as a grid (table looking, with rows and columns and no images at all, only text)....
1
by: Jack Smash | last post by:
Hi, I'm using a UserControl object for all the graphics handling in my application. However, if I select an image and move it around, I get flickering. So in order to get rid of the flickering,...
2
by: John Lee | last post by:
Hi, I have a windows application that uses the listview to display about 50 items in detailed view - 4 columns. The first column is static and other columns will be updated in 100-1000ms - it...
2
by: John Lee | last post by:
Thanks Jay for your response. I tried your code and it still flickering a lot. To demonstrate it, you can grab a listview, create 3 columns - name, value, timestamp, in form_load event to add 50...
8
by: benben | last post by:
I created a form and overrided OnPaint, OnClick and OnResize methods. My OnPaint calls base.OnPaint then repaints the whole screen. The screen flickers a lot! It didn't happen when the app was...
0
by: Imran | last post by:
Hi , I have an mdi applicaton. When user switch between child forms they are flickering like crazy! When a Child Form loads, you can see it in it's original size (the size from Design-time) then...
5
by: n00b | last post by:
I have some forms with maybe around 30 controls on each and anytime I load these forms different parts of it start flickering though not all of them at once and not the same ones everytime. the...
6
by: Mark Thompson | last post by:
I have a problem with my Visual Basic .NET 2003 application which I have so far been unable to resolve. I am trying to get an OpenGL control that I have created working properly as a control on...
1
by: sj | last post by:
Flickering Subform! I am developing a Quotation system in Access 03. At entry, users enter data thru' a form with subform. However, the subform keep flicker when I run the system on computer...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.