烟花烟火模拟程序

模拟放烟火的程序

从地面放到空中,爆炸后消失

FB源码(VFB源码,在勇芳编程群里下载)

Const Pi = 4 * Atn(1)
Dim Shared As Double TwoPi = 8 * Atn(1)
Dim Shared As Double RtoD = 180 / Pi   ' radians * RtoD = degrees
Dim Shared As Double DtoR = Pi / 180   ' degrees * DtoR = radians

ScreenRes 640,480,32

Type SPARK
    As Single px    'position
    As Single py
    As Single vx    'velocity
    As Single vy
    As ULong  c     'color
    As Integer a    'radius of explosion
    As Integer exploding  
End Type

Const N=100

Dim Shared As SPARK sparks(0 To N-1)
Dim Shared As Integer total

Sub drawSparks()
    Dim As Single dx,dy,angle,rr
    ScreenLock
    Cls
    If total<>0 Then
        For i As Integer = 0 To total-1: If i < total-N Then i=total-N
            With sparks(i Mod N)
                If .py < 240 Then .exploding = 1
                'random sparks within circle radius rr
                For j As Integer = 0 To 29
                    rr = Int(Rnd(1)*.a)  'gets wider each time
                    angle = Int(Rnd(1)*360)*DtoR
                    dx = Cos(angle)*rr
                    dy = Sin(angle)*rr
                    Circle(.px+dx,.py+dy),Int(Rnd(1)*2)+1,.c,,,,f
                Next j
                .px = .px + .vx
                .py = .py + .vy
                .vy = .vy + 0.1          'gravity
                If .exploding = 1 Then
                    .a  = .a + 1             'increase width
                End If
            End With
        Next i
    End If
    ScreenUnlock
End Sub

Dim As ULong r,g,b
Dim As Integer f    'red,green or blue sparks

Do

    If Int(Rnd(1)*50)=0 Then
        With sparks(total Mod N)
            .a = 2
            .px = 320
            .py = 430
            .vx = Int(Rnd(1)*5)-2
            .vy = -8
            .exploding = 0
            r = Int(Rnd(1)*255)
            g = Int(Rnd(1)*255)
            b = Int(Rnd(1)*255)
            'choose a bright color
            f = Int(Rnd(1)*3)
            If f = 0 Then r = 255
            If f = 1 Then g = 255
            If f = 2 Then b = 255
            .c = RGB(r,g,b)
        End With
        total += 1
    End If
   
    drawSparks()
    Sleep 5
Loop Until Len(Inkey)

你可能感兴趣的:(VisualFreeBasic)