Results 1 to 7 of 7

Thread: What's wrong with this code? [SOLVED]

  1. #1

    Thread Starter
    Fanatic Member hothead's Avatar
    Join Date
    Mar 2002
    Location
    Missouri
    Posts
    692

    What's wrong with this code? [SOLVED]

    I have no problems getting the ball to wrap to the other side of the screen, but now I'm trying to get it to bounce like it does in a Breakout-type game. The code snippet below doesn't work, the ball just sticks to the bottom of the screen.

    NOTE: Right now I'm testing the code in a command button's click procedure inside a practice program, just to see if I can get it working.

    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim T1 As Long, T2 As Long
    3.     myBackBuffer = CreateCompatibleDC(GetDC(0))
    4.     myBufferBMP = CreateCompatibleBitmap(GetDC(0), 320, 256)
    5.     SelectObject myBackBuffer, myBufferBMP
    6.     BitBlt myBackBuffer, 0, 0, 320, 256, 0, 0, 0, vbWhiteness
    7.     mySprite = LoadGraphicDC(App.Path & "\sprite1.bmp")
    8.     mySprite1 = LoadGraphicDC(App.Path & "\mask.bmp")
    9.     cmdTest.Enabled = False
    10.     T2 = GetTickCount
    11.     Do
    12.         DoEvents
    13.         T1 = GetTickCount
    14.         If (T1 - T2) >= 0.15 Then
    15.             BitBlt myBackBuffer, SpriteX - 1, SpriteY - 1, 32, 32, 0, 0, 0, vbBlackness
    16.             BitBlt myBackBuffer, SpriteX - 1, SpriteY - 1, 32, 32, 0, 0, 0, vbSrcAnd
    17.             BitBlt myBackBuffer, SpriteX, SpriteY, 32, 32, mySprite, 0, 0, vbSrcAnd
    18.             BitBlt myBackBuffer, SpriteX, SpriteY, 32, 32, mySprite1, 0, 0, vbSrcPaint
    19.             BitBlt Me.hdc, 0, 0, 320, 256, myBackBuffer, 0, 0, vbSrcCopy
    20.             SpriteX = SpriteX + 1
    21.             SpriteY = SpriteY + 1
    22.             T2 = GetTickCount
    23.         End If
    24.         If SpriteX = 320 Then
    25.             SpriteX = SpriteX - 1
    26.             SpriteY = SpriteY - 1
    27.         ElseIf SpriteY = 256 Then
    28.             SpriteX = SpriteX - 1
    29.             SpriteY = SpriteY - 1
    30.         End If
    31.     Loop
    32. End Sub

    The code works fine when I take it out of the If SpriteX = 320/If SpriteY = 256 statements, so it must be the correct code.
    Last edited by hothead; Oct 12th, 2003 at 07:10 PM.

  2. #2
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051
    I suggest having 2 lines that add the ball's velocity, then have seperate if statements that alter the ball's velocity. The way you're doing it is that if the ball goes out of bounds it moves back, but then just moves fowards again because you alter the balls position, not it's veolcity. Try something like this:

    VB Code:
    1. 'Somewhere out of loop set SpriteXVeolcity and SpriteYVelocity to 1 (or whatever)
    2. SpriteX = SpriteX + SpriteXVelocity
    3. SpriteY = SpriteY + SpriteYVelocity
    4. If SpriteX >= 360 Then SpriteXVelocity = -abs(SpriteXVelocity)
    5. If SpriteY >= 256 Then SpriteYVelocity = -abs(SpriteYVelocity)
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  3. #3
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    I'd suggest organizing it all in a type.

    VB Code:
    1. Private Type tSprite
    2.  X as Single
    3.  Y as Single
    4.  Vx as Single
    5.  Vy as Single
    6. End Type
    7.  
    8. Private Sprite as tSprite

  4. #4

    Thread Starter
    Fanatic Member hothead's Avatar
    Join Date
    Mar 2002
    Location
    Missouri
    Posts
    692
    Originally posted by SLH
    I suggest having 2 lines that add the ball's velocity, then have seperate if statements that alter the ball's velocity. The way you're doing it is that if the ball goes out of bounds it moves back, but then just moves fowards again because you alter the balls position, not it's veolcity. Try something like this:

    VB Code:
    1. 'Somewhere out of loop set SpriteXVeolcity and SpriteYVelocity to 1 (or whatever)
    2. SpriteX = SpriteX + SpriteXVelocity
    3. SpriteY = SpriteY + SpriteYVelocity
    4. If SpriteX >= 360 Then SpriteXVelocity = -abs(SpriteXVelocity)
    5. If SpriteY >= 256 Then SpriteYVelocity = -abs(SpriteYVelocity)

    At first I wasn't sure what you were talking about, but I experimented with it a bit, and found I could change the direction at which the ball travels by tweaking the values of SpriteXVelocity and SpriteYVelocity, which is kinda cool!

    However, it still doesn't bounce when it hits the sides. It still sticks to the bottom.

  5. #5
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    VB Code:
    1. Private Type tSprite
    2.  X as Single
    3.  Y as Single
    4.  Vx as Single
    5.  Vy as Single
    6. End Type
    7.  
    8. Private Sprite as tSprite
    9.  
    10. Private Sub Command1_Click()
    11.     Dim T1 As Long, T2 As Long
    12.     myBackBuffer = CreateCompatibleDC(GetDC(0))
    13.     myBufferBMP = CreateCompatibleBitmap(GetDC(0), 320, 256)
    14.     SelectObject myBackBuffer, myBufferBMP
    15.     BitBlt myBackBuffer, 0, 0, 320, 256, 0, 0, 0, vbWhiteness
    16.     mySprite = LoadGraphicDC(App.Path & "\sprite1.bmp")
    17.     mySprite1 = LoadGraphicDC(App.Path & "\mask.bmp")
    18.     cmdTest.Enabled = False
    19.     T2 = GetTickCount
    20.     Do
    21.         DoEvents
    22.         T1 = GetTickCount
    23.         If (T1 - T2) >= 0.15 Then
    24.             BitBlt myBackBuffer, Sprite.X - 1, Sprite.Y - 1, 32, 32, 0, 0, 0, vbBlackness
    25.             BitBlt myBackBuffer, Sprite.X - 1, Sprite.Y - 1, 32, 32, 0, 0, 0, vbSrcAnd
    26.             BitBlt myBackBuffer, Sprite.X, Sprite.Y, 32, 32, mySprite, 0, 0, vbSrcAnd
    27.             BitBlt myBackBuffer, Sprite.X, Sprite.Y, 32, 32, mySprite1, 0, 0, vbSrcPaint
    28.             BitBlt Me.hdc, 0, 0, 320, 256, myBackBuffer, 0, 0, vbSrcCopy
    29.             Sprite.X = Sprite.X + Sprite.Vx
    30.             Sprite.Y = Sprite.Y + Sprite.Vy
    31.             T2 = GetTickCount
    32.         End If
    33.         If Sprite.X >= 320 Then Sprite.Vx = -1
    34.         If Sprite.Y >= 256 Then Sprite.Vy = -1
    35.         If Sprite.X =< 0 Then Sprite.Vx = 1
    36.         If Sprite.Y =< 0 Then Sprite.Vy = 1
    37.         End If
    38.     Loop
    39. End Sub

  6. #6

    Thread Starter
    Fanatic Member hothead's Avatar
    Join Date
    Mar 2002
    Location
    Missouri
    Posts
    692
    Ok, I got it working real good now, but apparently the ball is leaving a little trail behind. This trail appears to be invisible at the default values, but as I increase the values (to get more speed out of the game) I see this trail more the higher the speed goes.

    It looks like a chevron trail. Perhaps I'm missing some BitBlt data, or I didn't create my masks correctly?

    NOTE: The code is now a function called MoveBall, and this is the code as it appears:

    VB Code:
    1. Private Function MoveBall()
    2.     Dim T1 As Long, T2 As Long
    3.     Sprite.Vx = 1.5
    4.     Sprite.Vy = 1.5
    5.     myBackBuffer = CreateCompatibleDC(GetDC(0))
    6.     myBufferBMP = CreateCompatibleBitmap(Picture1.hdc, 344, 392)
    7.     SelectObject myBackBuffer, myBufferBMP
    8.     BitBlt myBackBuffer, 0, 0, 344, 392, 0, 0, 0, vbWhiteness
    9.     mySprite = LoadGraphicDC(App.Path & "\sprite1.bmp")
    10.     mySprite1 = LoadGraphicDC(App.Path & "\mask.bmp")
    11.     T2 = GetTickCount
    12.     Do
    13.         DoEvents
    14.         T1 = GetTickCount
    15.         If (T1 - T2) >= 0.15 Then
    16.             BitBlt myBackBuffer, Sprite.X - 1, Sprite.Y - 1, 32, 32, 0, 0, 0, vbBlackness
    17.             BitBlt myBackBuffer, Sprite.X - 1, Sprite.Y - 1, 32, 32, 0, 0, 0, vbSrcAnd
    18.             BitBlt myBackBuffer, Sprite.X, Sprite.Y, 32, 32, mySprite, 0, 0, vbSrcAnd
    19.             BitBlt myBackBuffer, Sprite.X, Sprite.Y, 32, 32, mySprite1, 0, 0, vbSrcPaint
    20.             BitBlt Me.hdc, 0, 0, 344, 392, myBackBuffer, 0, 0, vbSrcCopy
    21.             Sprite.X = Sprite.X + Sprite.Vx
    22.             Sprite.Y = Sprite.Y + Sprite.Vy
    23.             T2 = GetTickCount
    24.         End If
    25.         If Sprite.X >= 344 Then Sprite.Vx = -1.5
    26.         If Sprite.Y >= 392 Then Sprite.Vy = -1.5
    27.         If Sprite.X <= 0 Then Sprite.Vx = 1.5
    28.         If Sprite.Y <= 0 Then Sprite.Vy = 1.5
    29.     Loop
    30. End Function
    Last edited by hothead; Oct 12th, 2003 at 04:45 PM.

  7. #7
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    Could be your screen's refresh rate.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width