|
-
Oct 12th, 2003, 12:27 PM
#1
Thread Starter
Fanatic Member
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:
Private Sub Command1_Click()
Dim T1 As Long, T2 As Long
myBackBuffer = CreateCompatibleDC(GetDC(0))
myBufferBMP = CreateCompatibleBitmap(GetDC(0), 320, 256)
SelectObject myBackBuffer, myBufferBMP
BitBlt myBackBuffer, 0, 0, 320, 256, 0, 0, 0, vbWhiteness
mySprite = LoadGraphicDC(App.Path & "\sprite1.bmp")
mySprite1 = LoadGraphicDC(App.Path & "\mask.bmp")
cmdTest.Enabled = False
T2 = GetTickCount
Do
DoEvents
T1 = GetTickCount
If (T1 - T2) >= 0.15 Then
BitBlt myBackBuffer, SpriteX - 1, SpriteY - 1, 32, 32, 0, 0, 0, vbBlackness
BitBlt myBackBuffer, SpriteX - 1, SpriteY - 1, 32, 32, 0, 0, 0, vbSrcAnd
BitBlt myBackBuffer, SpriteX, SpriteY, 32, 32, mySprite, 0, 0, vbSrcAnd
BitBlt myBackBuffer, SpriteX, SpriteY, 32, 32, mySprite1, 0, 0, vbSrcPaint
BitBlt Me.hdc, 0, 0, 320, 256, myBackBuffer, 0, 0, vbSrcCopy
SpriteX = SpriteX + 1
SpriteY = SpriteY + 1
T2 = GetTickCount
End If
If SpriteX = 320 Then
SpriteX = SpriteX - 1
SpriteY = SpriteY - 1
ElseIf SpriteY = 256 Then
SpriteX = SpriteX - 1
SpriteY = SpriteY - 1
End If
Loop
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.
-
Oct 12th, 2003, 01:38 PM
#2
Not NoteMe
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:
'Somewhere out of loop set SpriteXVeolcity and SpriteYVelocity to 1 (or whatever)
SpriteX = SpriteX + SpriteXVelocity
SpriteY = SpriteY + SpriteYVelocity
If SpriteX >= 360 Then SpriteXVelocity = -abs(SpriteXVelocity)
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. 
-
Oct 12th, 2003, 02:52 PM
#3
So Unbanned
I'd suggest organizing it all in a type.
VB Code:
Private Type tSprite
X as Single
Y as Single
Vx as Single
Vy as Single
End Type
Private Sprite as tSprite
-
Oct 12th, 2003, 03:39 PM
#4
Thread Starter
Fanatic Member
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:
'Somewhere out of loop set SpriteXVeolcity and SpriteYVelocity to 1 (or whatever)
SpriteX = SpriteX + SpriteXVelocity
SpriteY = SpriteY + SpriteYVelocity
If SpriteX >= 360 Then SpriteXVelocity = -abs(SpriteXVelocity)
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.
-
Oct 12th, 2003, 03:47 PM
#5
So Unbanned
VB Code:
Private Type tSprite
X as Single
Y as Single
Vx as Single
Vy as Single
End Type
Private Sprite as tSprite
Private Sub Command1_Click()
Dim T1 As Long, T2 As Long
myBackBuffer = CreateCompatibleDC(GetDC(0))
myBufferBMP = CreateCompatibleBitmap(GetDC(0), 320, 256)
SelectObject myBackBuffer, myBufferBMP
BitBlt myBackBuffer, 0, 0, 320, 256, 0, 0, 0, vbWhiteness
mySprite = LoadGraphicDC(App.Path & "\sprite1.bmp")
mySprite1 = LoadGraphicDC(App.Path & "\mask.bmp")
cmdTest.Enabled = False
T2 = GetTickCount
Do
DoEvents
T1 = GetTickCount
If (T1 - T2) >= 0.15 Then
BitBlt myBackBuffer, Sprite.X - 1, Sprite.Y - 1, 32, 32, 0, 0, 0, vbBlackness
BitBlt myBackBuffer, Sprite.X - 1, Sprite.Y - 1, 32, 32, 0, 0, 0, vbSrcAnd
BitBlt myBackBuffer, Sprite.X, Sprite.Y, 32, 32, mySprite, 0, 0, vbSrcAnd
BitBlt myBackBuffer, Sprite.X, Sprite.Y, 32, 32, mySprite1, 0, 0, vbSrcPaint
BitBlt Me.hdc, 0, 0, 320, 256, myBackBuffer, 0, 0, vbSrcCopy
Sprite.X = Sprite.X + Sprite.Vx
Sprite.Y = Sprite.Y + Sprite.Vy
T2 = GetTickCount
End If
If Sprite.X >= 320 Then Sprite.Vx = -1
If Sprite.Y >= 256 Then Sprite.Vy = -1
If Sprite.X =< 0 Then Sprite.Vx = 1
If Sprite.Y =< 0 Then Sprite.Vy = 1
End If
Loop
End Sub
-
Oct 12th, 2003, 04:42 PM
#6
Thread Starter
Fanatic Member
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:
Private Function MoveBall()
Dim T1 As Long, T2 As Long
Sprite.Vx = 1.5
Sprite.Vy = 1.5
myBackBuffer = CreateCompatibleDC(GetDC(0))
myBufferBMP = CreateCompatibleBitmap(Picture1.hdc, 344, 392)
SelectObject myBackBuffer, myBufferBMP
BitBlt myBackBuffer, 0, 0, 344, 392, 0, 0, 0, vbWhiteness
mySprite = LoadGraphicDC(App.Path & "\sprite1.bmp")
mySprite1 = LoadGraphicDC(App.Path & "\mask.bmp")
T2 = GetTickCount
Do
DoEvents
T1 = GetTickCount
If (T1 - T2) >= 0.15 Then
BitBlt myBackBuffer, Sprite.X - 1, Sprite.Y - 1, 32, 32, 0, 0, 0, vbBlackness
BitBlt myBackBuffer, Sprite.X - 1, Sprite.Y - 1, 32, 32, 0, 0, 0, vbSrcAnd
BitBlt myBackBuffer, Sprite.X, Sprite.Y, 32, 32, mySprite, 0, 0, vbSrcAnd
BitBlt myBackBuffer, Sprite.X, Sprite.Y, 32, 32, mySprite1, 0, 0, vbSrcPaint
BitBlt Me.hdc, 0, 0, 344, 392, myBackBuffer, 0, 0, vbSrcCopy
Sprite.X = Sprite.X + Sprite.Vx
Sprite.Y = Sprite.Y + Sprite.Vy
T2 = GetTickCount
End If
If Sprite.X >= 344 Then Sprite.Vx = -1.5
If Sprite.Y >= 392 Then Sprite.Vy = -1.5
If Sprite.X <= 0 Then Sprite.Vx = 1.5
If Sprite.Y <= 0 Then Sprite.Vy = 1.5
Loop
End Function
Last edited by hothead; Oct 12th, 2003 at 04:45 PM.
-
Oct 13th, 2003, 02:13 AM
#7
So Unbanned
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|