打印本文 打印本文 关闭窗口 关闭窗口
Drawing & Animation III
作者:武汉SEO闵涛  文章来源:敏韬网  点击数4669  更新时间:2009/4/23 16:39:28  文章录入:mintao  责任编辑:mintao
p;  ''''Now draw from the beginning again
    BitBlt picBack.hdc, EndScroll, 0, GlueWidth, ABBackHeight, DCABBAck, 0, 0, vbSrcCopy
Else
    BitBlt picBack.hdc, 0, 0, ScrollWidth, ABBackHeight, DCABBAck, X, 0, vbSrcCopy
End If

''''Draw the first back ground
If XBack1 + ScrollWidth > Back1Width Then ''''We ned to glue at the beginnig again

    ''''Calculate the remaining width
    GlueWidth = XBack1 + ScrollWidth - Back1Width
    EndScroll = ScrollWidth - GlueWidth

    ''''Blit the first part
    BitBlt picBack.hdc, 0, ABBackHeight - Back1Height, EndScroll, Back1Height, _
           DCBack1M, XBack1, 0, vbSrcAnd

    BitBlt picBack.hdc, 0, ABBackHeight - Back1Height, EndScroll, Back1Height, _
           DCBack1, XBack1, 0, vbSrcPaint

    ''''Now draw from the beginning again
    BitBlt picBack.hdc, EndScroll, ABBackHeight - Back1Height, GlueWidth, _
           Back1Height, DCBack1M, 0, 0, vbSrcAnd

    BitBlt picBack.hdc, EndScroll, ABBackHeight - Back1Height, GlueWidth, _
           Back1Height, DCBack1, 0, 0, vbSrcPaint
Else

    BitBlt picBack.hdc, 0, ABBackHeight - Back1Height, ScrollWidth, Back1Height, _
           DCBack1M, XBack1, 0, vbSrcAnd

    BitBlt picBack.hdc, 0, ABBackHeight - Back1Height, ScrollWidth, Back1Height, _
           DCBack1, XBack1, 0, vbSrcPaint
End If

''''Draw the sprite
BitBlt picBack.hdc, XSprite, ABBackHeight - SpriteHeight, SpriteWidth, _
           SpriteHeight, DCSpriteM, 0, 0, vbSrcAnd

BitBlt picBack.hdc, XSprite, ABBackHeight - SpriteHeight, SpriteWidth, _
           SpriteHeight, DCSprite, 0, 0, vbSrcPaint

''''Draw the fore ground
If XFore + ScrollWidth > ForeWidth Then ''''We ned to glue at the beginnig again

    ''''Calculate the remaining width
    GlueWidth = XFore + ScrollWidth - ForeWidth
    EndScroll = ScrollWidth - GlueWidth
    ''''Blit the first part

    BitBlt picBack.hdc, 0, ABBackHeight - ForeHeight, EndScroll, ForeHeight, _
           DCForeM, XFore, 0, vbSrcAnd

    BitBlt picBack.hdc, 0, ABBackHeight - ForeHeight, EndScroll, ForeHeight, _
           DCFore, XFore, 0, vbSrcPaint

    ''''Now draw from the beginning again
    BitBlt picBack.hdc, EndScroll, ABBackHeight - ForeHeight, GlueWidth, ForeHeight, _
           DCForeM, 0, 0, vbSrcAnd

    BitBlt picBack.hdc, EndScroll, ABBackHeight - ForeHeight, GlueWidth, ForeHeight, _
           DCFore, 0, 0, vbSrcPaint
Else
    BitBlt picBack.hdc, 0, ABBackHeight - ForeHeight, ScrollWidth, ForeHeight, _
           DCForeM, XFore, 0, vbSrcAnd

    BitBlt picBack.hdc, 0, ABBackHeight - ForeHeight, ScrollWidth, ForeHeight, _
           DCFore, XFore, 0, vbSrcPaint
End If

''''Draw the back buffer onto the display
BitBlt Me.hdc, 0, 0, ScrollWidth, ABBackHeight, picBack.hdc, 0, 0, vbSrcCopy

Me.Refresh

''''Modify the positions.
X = (X Mod ABBAckWidth) + 1
XBack1 = (XBack1 Mod Back1Width) + 8
XFore = (XFore Mod ForeWidth) + 25

End Sub

The X position on the different layers is controlled by a separate variable. This enables us to move the layers with different speeds, which further enhances the illusion of depth and distance.

Star field background

Another often-used background is a star field background, which as the name implies simulates a moving star field. The most efficient method when it comes to speed is of course to make a bitmap and then draw it directly onto the gaming as the absolute background. Another and more fun method is to randomly generate dots and small circles on the drawing area.

The sample project STARFIELD in STARFIELD.ZIP demonstrates how to create a simple star field of small circles and dots. To represent a star we create a type called Star and declare it as follows:


Private Type Star
    X As Long
    Y As Long
    Speed As Long
    Size As Long
    Color As Long
 End Type

 

The X and Y members are the position of the star. The size is the diameter of the star in pixels. The Speed is the amount of pixels the star moves each turn. The color is the color of the star.

To represent the stars in this sample project we have an array of Star Types (called Stars). This array is initialized in the Load event of the form:

Private Sub Form_Load()

Dim I As Long

Randomize
''''Generate the 100 stars
For I = LBound(Stars) To UBound(Stars)
   
    Stars(I).X = Me.ScaleWidth * Rnd + 1
    Stars(I).Y = Me.ScaleHeight * Rnd + 1
    Stars(I).Size = MaxSize * Rnd + 1
    Stars(I).Speed = MaxSpeed * Rnd + 1
    Stars(I).Color = RGB(Rnd * 255 + 1, Rnd * 255 + 1, Rnd * 255 + 1)
Next I

End Sub

 


Each individual star is initialized to a random value within the bounds of the window.

The stars are drawn in the timer event, using the Ellipse API function.


Private Sub TimerStarField_Timer()

Dim I As Long

''''clear the form
BitBlt Me.hdc, 0, 0, Me.ScaleWidth, Me.ScaleHeight, 0, 0, 0, vbBlackness

For I = 0 To UBound(Stars)
   
    ''''Move the star
    Stars(I).Y = (Stars(I).Y Mod Me.ScaleHeight) + Stars(I).Speed
    ''''Relocate the X position
    If Stars(I).Y > Me.ScaleHeight Then
      Stars(I).X = Me.ScaleWidth * Rnd + 1
    End If
    ''''Set the color
    Me.FillColor = Stars(I).Color
    Me.ForeColor = Stars(I).Color
    ''''Draw the star
    Ellipse Me.hdc, Stars(I).X, Stars(I).Y, Stars(I).X + Stars(I).Size, Stars(I).Y + Stars(I).Size

Next I

End Sub

 

The BitBlt outside the loop makes the background of the window black, using the vbBlackness raster operation. The stars are moved using the usual operation. If a star goes out of bounds, compared to the scale height of the window, the X position of the star will be changed, in order to created more randomness. The Ellipse function simply takes two parameters, the first pair defining the upper-left 慶orner?of the circle, the second defining the lower-right 慶orner?

 

The Game Loop

So far we have been using the timer control to continuously execute the so-called game loop. The Game Loop is where everything is being controlle

上一页  [1] [2] [3] [4] [5] [6]  下一页

打印本文 打印本文 关闭窗口 关闭窗口