News:

Building a 3D Ray Tracer  By stevmjon

Main Menu

Shape Confetti

Started by balaporte, June 24, 2007, 07:30:05 PM

Previous topic - Next topic

balaporte

OK, I said I'd start posting...

This bit of code uses mostly shape and color commands to display confetti dropping down from the top of the screen. It recycles back up at the top when it reaches the bottom. Hit ESC to exit.

This was really just a test to see if I could rotate and scale shapes to make them appear more "3D".

PlayBASIC Code: [Select]
; PROJECT : shape confetti
; AUTHOR : Brian Anthony LaPorte
; CREATED : 6/23/2007
; EDITED : 6/24/2007
; ---------------------------------------------------------------------

sw = 800
sh = 600
sd = 32
st = 2

OpenScreen sw,sh,sd,st

border = 32

black = RGB(0,0,0)
white = RGB(255,255,255)
backcolor = black

Type confettitype
shape
x#
y#
xspeed#
yspeed#
xscale#
yscale#
zscale#
angle#
spinrate#
fliprate#
color
blend
side
EndType

total = 200

Dim confetti(total) As confettitype

token = NewShape(4,4)

SetShapeVertex token,0,-16,-16
SetShapeVertex token,1,16,-16
SetShapeVertex token,2,16,16
SetShapeVertex token,3,-16,16

SetShapeEdge token,0,0,1
SetShapeEdge token,1,1,2
SetShapeEdge token,2,2,3
SetShapeEdge token,3,3,0

For a = 1 To total

confetti(a).shape = GetFreeShape()
CopyShape token,confetti(a).shape

confetti(a).x# = Rnd(sw)
confetti(a).y# = -(Rnd(sh))
confetti(a).xspeed# = (Rnd(50) + 1) * 0.01: If Rnd(2) = 1 Then confetti(a).xspeed# = -(confetti(a).xspeed#)
confetti(a).yspeed# = (Rnd(10) + 10) * 0.1
confetti(a).xscale# = 1.0
confetti(a).yscale# = (Rnd(100) + 1) * 0.01
confetti(a).zscale# = (Rnd(75) + 25) * 0.01
confetti(a).angle# = Rnd(360)
confetti(a).spinrate# = 1.0: If Rnd(2) = 1 Then confetti(a).spinrate# = -(confetti(a).spinrate#)
confetti(a).fliprate# = (Rnd(50) + 1) * 0.001
confetti(a).color = RndRGB()
confetti(a).blend = 0
confetti(a).side = RgbAlphaBlend(confetti(a).color,backcolor,confetti(a).blend)

RotateShapeXY confetti(a).shape,confetti(a).angle#,confetti(a).xscale#,confetti(a).yscale#
ScaleShape confetti(a).shape, confetti(a).zscale#

Next a

; MAIN LOOP -----------------------------------------------------------

Repeat

; Delta

FramesPerSecond = FPS()
DeltaTime# = 1000.0 / FramesPerSecond

delta# = delta# + DeltaTime#

If delta# > 10

For a = 1 To total

; move object
confetti(a).x# = confetti(a).x# + confetti(a).xspeed#
confetti(a).y# = confetti(a).y# + confetti(a).yspeed#
If confetti(a).y# > sh + border
confetti(a).x# = Rnd(sw)
confetti(a).y# = -(border)
confetti(a).xspeed# = (Rnd(50) + 1) * 0.01: If Rnd(2) = 1 Then confetti(a).xspeed# = -(confetti(a).xspeed#)
confetti(a).yspeed# = (Rnd(10) + 10) * 0.1
confetti(a).xscale# = 1.0
confetti(a).yscale# = (Rnd(100) + 1) * 0.01
confetti(a).zscale# = (Rnd(75) + 25) * 0.01
confetti(a).angle# = Rnd(360)
confetti(a).spinrate# = 1.0: If Rnd(2) = 1 Then confetti(a).spinrate# = -(confetti(a).spinrate#)
confetti(a).fliprate# = (Rnd(50) + 1) * 0.001
confetti(a).color = RndRGB()
confetti(a).blend = 0
confetti(a).side = RgbAlphaBlend(confetti(a).color,backcolor,confetti(a).blend)
EndIf

; rotate angle
confetti(a).angle# = WrapAngle(confetti(a).angle#,confetti(a).spinrate#)

; shine
If confetti(a).yscale# => 1.0 And confetti(a).blend = 0
confetti(a).blend = 65
confetti(a).side = RgbAlphaBlend(confetti(a).color,white,confetti(a).blend)
EndIf

If confetti(a).yscale# <= 1.0 And confetti(a).blend = 65
confetti(a).blend = 0
confetti(a).side = RgbAlphaBlend(confetti(a).color,backcolor,confetti(a).blend)
EndIf

; flip color
If confetti(a).yscale# < 0.0
If confetti(a).blend = 0
confetti(a).blend = 75
Else
confetti(a).blend = 0
EndIf
confetti(a).side = RgbAlphaBlend(confetti(a).color,backcolor,confetti(a).blend)
EndIf

; flip object
confetti(a).yscale# = confetti(a).yscale# + confetti(a).fliprate#
If confetti(a).yscale# < 0.0 Or confetti(a).yscale# > 1.0
confetti(a).fliprate# = -(confetti(a).fliprate#)
EndIf

; adjust shape
RotateShapeXY confetti(a).shape,confetti(a).angle#,confetti(a).xscale#,confetti(a).yscale#

Next a

; reset delta
delta# = 0
Login required to view complete source code



old version, has issues in modern versions of PB

; PROJECT : shape confetti
; AUTHOR  : Brian Anthony LaPorte
; CREATED : 6/23/2007
; EDITED  : 6/24/2007
; ---------------------------------------------------------------------

sw = 800
sh = 600
sd = 32
st = 2

OpenScreen sw,sh,sd,st

border = 32

black = RGB(0,0,0)
white = RGB(255,255,255)
backcolor = black

Type confettitype
shape
x#
y#
xspeed#
yspeed#
xscale#
yscale#
zscale#
angle#
spinrate#
fliprate#
color
blend
side
EndType

total = 200

Dim confetti(total) As confettitype

token = NewShape(4,4)

SetShapeVertex token,0,-16,-16
SetShapeVertex token,1,16,-16
SetShapeVertex token,2,16,16
SetShapeVertex token,3,-16,16

SetShapeEdge token,0,0,1
SetShapeEdge token,1,1,2
SetShapeEdge token,2,2,3
SetShapeEdge token,3,3,0

For a = 1 To total

confetti(a).shape = GetFreeShape()
CopyShape token,confetti(a).shape

confetti(a).x# = Rnd(sw)
confetti(a).y# = Neg(Rnd(sh))
confetti(a).xspeed# = (Rnd(50) + 1) * 0.01: If Rnd(2) = 1 Then confetti(a).xspeed# = Neg(confetti(a).xspeed#)
confetti(a).yspeed# = (Rnd(10) + 10) * 0.1
confetti(a).xscale# = 1.0
confetti(a).yscale# = (Rnd(100) + 1) * 0.01
confetti(a).zscale# = (Rnd(75) + 25) * 0.01
confetti(a).angle# = Rnd(360)
confetti(a).spinrate# = 1.0: If Rnd(2) = 1 Then confetti(a).spinrate# = Neg(confetti(a).spinrate#)
confetti(a).fliprate# = (Rnd(50) + 1) * 0.001
confetti(a).color = RndRGB()
confetti(a).blend = 0
confetti(a).side = RgbAlphaBlend(confetti(a).color,backcolor,confetti(a).blend)

RotateShapeXY confetti(a).shape,confetti(a).angle#,confetti(a).xscale#,confetti(a).yscale#
ScaleShape confetti(a).shape, confetti(a).zscale#

Next a

; MAIN LOOP -----------------------------------------------------------

Repeat

; Delta

FramesPerSecond = FPS()
DeltaTime# = 1000.0 / FramesPerSecond

delta# = delta# + DeltaTime#

If delta# > 10

For a = 1 To total

; move object
confetti(a).x# = confetti(a).x# + confetti(a).xspeed#
confetti(a).y# = confetti(a).y# + confetti(a).yspeed#
If confetti(a).y# > sh + border
confetti(a).x# = Rnd(sw)
confetti(a).y# = Neg(border)
confetti(a).xspeed# = (Rnd(50) + 1) * 0.01: If Rnd(2) = 1 Then confetti(a).xspeed# = Neg(confetti(a).xspeed#)
confetti(a).yspeed# = (Rnd(10) + 10) * 0.1
confetti(a).xscale# = 1.0
confetti(a).yscale# = (Rnd(100) + 1) * 0.01
confetti(a).zscale# = (Rnd(75) + 25) * 0.01
confetti(a).angle# = Rnd(360)
confetti(a).spinrate# = 1.0: If Rnd(2) = 1 Then confetti(a).spinrate# = Neg(confetti(a).spinrate#)
confetti(a).fliprate# = (Rnd(50) + 1) * 0.001
confetti(a).color = RndRGB()
confetti(a).blend = 0
confetti(a).side = RgbAlphaBlend(confetti(a).color,backcolor,confetti(a).blend)
EndIf

; rotate angle
confetti(a).angle# = WrapAngle(confetti(a).angle#,confetti(a).spinrate#)

; shine
If confetti(a).yscale# => 1.0 And confetti(a).blend = 0
confetti(a).blend = 65
confetti(a).side = RgbAlphaBlend(confetti(a).color,white,confetti(a).blend)
EndIf

If confetti(a).yscale# <= 1.0 And confetti(a).blend = 65
confetti(a).blend = 0
confetti(a).side = RgbAlphaBlend(confetti(a).color,backcolor,confetti(a).blend)
EndIf

; flip color
If confetti(a).yscale# < 0.0
If confetti(a).blend = 0
confetti(a).blend = 75
Else
confetti(a).blend = 0
EndIf
confetti(a).side = RgbAlphaBlend(confetti(a).color,backcolor,confetti(a).blend)
EndIf

; flip object
confetti(a).yscale# = confetti(a).yscale# + confetti(a).fliprate#
If confetti(a).yscale# < 0.0 Or confetti(a).yscale# > 1.0
confetti(a).fliprate# = Neg(confetti(a).fliprate#)
EndIf

; adjust shape
RotateShapeXY confetti(a).shape,confetti(a).angle#,confetti(a).xscale#,confetti(a).yscale#

Next a

; reset delta
delta# = 0

EndIf

LockBuffer

Cls backcolor

Ink RGB(128,128,128)
Print "fps: " + Str$(FramesPerSecond)
Print "delta: " + Str$(DeltaTime#)

For a = 1 To total
Ink confetti(a).side
DrawShape confetti(a).shape,confetti(a).x#,confetti(a).y#,2
Next a

UnLockBuffer

Sync

If EscKey() Then quit = Yes

Until quit = Yes

kevin


  Excellent,  in fact deserves a place in the demo !     


balaporte

Thanks, hopefully more to come.