Gummiband (Types in BlitzMax)

Übersicht BlitzMax, BlitzMax NG Codearchiv & Module

Neue Antwort erstellen

Markus2

Betreff: Gummiband (Types in BlitzMax)

BeitragMo, Mai 16, 2005 19:25
Antworten mit Zitat
Benutzer-Profile anzeigen
Einfach angucken 8)

Code: [AUSKLAPPEN]

'----------------------------------------------------------------------
'Gummiband , BlitzMax , M.Rauch 16.05.2005
'----------------------------------------------------------------------
Strict

'Screen mode
Const screenWidth   =800
Const screenHeight   =600
Graphics screenWidth,screenHeight,0,72
SetClsColor 255,255,255

'----------------------------------------------------------------------
'The amount of gravity acting on the beads
Const CGRAVITY:Float=0.04
'The decay of the X movement of the beads, 1=no decay, 0=complete decay
Const XDECAY:Float=0.97
'The decay of the Y movement of the beads, 1=no decay, 0=complete decay
Const YDECAY:Float=0.97
'The number of beads on the String take into account that each bead weighs the beads above it down
Const NUMBEADS:Int=10
'The elasticity of the X movement of the beads
Const XELASTICITY:Float=0.02
'The elasticity of the Y movement of the beads
Const YELASTICITY:Float=0.02
'The amount the bead movement decays when it hits a wall, 1=no decay, 0=complete decay
Const WALLDECAY:Float=0.9
'The between beads, before the String tenses And elasticity acts
Const SLACKNESS:Float=1.0
'The diameter of the beads
Const BEADSIZE:Float=6
Const BEADSIZEH:Float=3 'half size
'----------------------------------------------------------------------

'----------------------------------------------------------------------
Global beadDist:Float
Global beadDistX:Float
Global beadDistY:Float
Global distRatioX:Float
Global distRatioY:Float
'----------------------------------------------------------------------

'----------------------------------------------------------------------
Global beadList:TList=CreateList()

Type beadType
 Field no:Int
 Field X:Float
 Field Y:Float
 Field xMove:Float
 Field yMove:Float

 Field _link:TLink

 Method New()
  _link=beadList.addlast(Self)
 End Method

 Method After:beadType()
      Local t:TLink
      t=_link.NextLink()
      If t Return beadType(t.Value())
 End Method

 Method Draw()
  DrawOval Self.X-BEADSIZEH,self.Y-BEADSIZEH,BEADSIZE,BEADSIZE
 End Method

 Method Decay()
  Self.xMove=Self.xMove*XDECAY
  Self.yMove=Self.yMove*YDECAY
 End Method

 Method Gravity()
  Self.yMove=Self.yMove+CGRAVITY
 End Method

 Method Move()
  Self.X=Self.X+Self.xMove
  Self.Y=Self.Y+Self.yMove
 End Method

 Method xyMove(X:Float,Y:Float)
  Self.xMove=Self.xMove+X
  Self.yMove=Self.yMove+Y
 End Method

 Method WallBounce()
      If Self.X-(BEADSIZE+10)/2<0 Then
         Self.X=+(BEADSIZE+10)/2
         Self.xMove=-Self.xMove*WALLDECAY
      EndIf
      If Self.X+(BEADSIZE+10)/2>screenWidth Then
         Self.X=screenWidth-(BEADSIZE+10)/2
         Self.xMove=-Self.xMove*WALLDECAY
      EndIf
      If Self.Y-(BEADSIZE+10)/2<0 Then
         Self.yMove=-Self.yMove*WALLDECAY
         Self.Y=(BEADSIZE+10)/2
      EndIf
      If Self.Y+(BEADSIZE+10)/2>screenHeight Then
         Self.yMove=-Self.yMove*WALLDECAY
         Self.Y=screenHeight-(BEADSIZE+10)/2
      EndIf
 End Method

 Method ScreenLimit()
   If Self.X-(BEADSIZE+10.0)/2.0<0.0 Then Self.X=(BEADSIZE+10.0)/2.0
   If Self.X+(BEADSIZE+10.0)/2.0>screenWidth Then Self.X=screenWidth-(BEADSIZE+10.0)/2.0
   If Self.Y-(BEADSIZE+10.0)/2.0<0.0 Then Self.Y=(BEADSIZE+10.0)/2.0
   If Self.Y+(BEADSIZE+10.0)/2.0>screenHeight Then Self.Y=screenHeight-(BEADSIZE+10.0)/2.0
 End Method

EndType
'----------------------------------------------------------------------

'----------------------------------------------------------------------
Global bead:beadType
Local i:Int
For i=1 To NUMBEADS
 bead=New beadType
 bead.no=i
Next
'----------------------------------------------------------------------

While Not KeyHit(KEY_ESCAPE)

 Local x:Int
 Local beadlast:beadType
 Local beadnext:beadType

  Cls

  For bead = EachIn beadList
   If bead.no=1 Then
    'Position parent bead
    bead.X=MouseX()
    bead.Y=MouseY()
    bead.ScreenLimit

    'Draw parent bead
    SetColor 0,0,0
    bead.Draw
   Else     
      'Loop though other beads
      'Work out X And Y distance between the bead And the one before it
      beadDistX=bead.X-beadlast.X
      beadDistY=bead.Y-beadlast.Y
      'Work out total distance
      beadDist=Sqr(beadDistX^2+beadDistY^2)

      'If the beads are far enough apart, decrease the movement To create elasticity
      If beadDist>SLACKNESS Then
       bead.xyMove -XELASTICITY*beadDistX,-YELASTICITY*beadDistY
      EndIf

      'If bead is Not last bead
      If bead.no<NUMBEADS Then
        beadnext=bead.After()
         'Work out distances between the bead And the one after it
         beadDistX=bead.X-beadnext.X
         beadDistY=bead.Y-beadnext.Y
         beadDist=Sqr(beadDistX^2+beadDistY^2)

         'If the beads are far enough apart, decrease the movement To create elasticity
         If beadDist>1.0 Then
            bead.xyMove -XELASTICITY*beadDistX,-YELASTICITY*beadDistY
         EndIf
      EndIf

      'Decay the movement of the beads To simulate loss of energy
      bead.Decay

      'Apply gravity To bead movement
      bead.Gravity

      'Move beads
      bead.Move

      'If the beads hit a wall, make them bounce off of it
      bead.WallBounce

      'Draw bead And String from it To the one before it
      SetColor 0,0,0
      bead.Draw
      SetColor 200,100,0
      DrawLine bead.X,bead.Y,beadlast.X,beadlast.Y,False
   EndIf
   beadlast=bead
  Next

  Flip
Wend
End


 

ChristianH

BeitragMo, Mai 16, 2005 19:55
Antworten mit Zitat
Benutzer-Profile anzeigen
Hehe, das ist ja cool. Smile

Das Gummi wird aber ein wenig zu groß wenn man die Maus schnell hin und her bewegt.

MfG

Suco-X

Betreff: .......

BeitragMo, Mai 16, 2005 21:44
Antworten mit Zitat
Benutzer-Profile anzeigen
Gefällt mir gut. Sehr nett gemacht Exclamation
Mfg Suco
Intel Core 2 Quad Q8300, 4× 2500 MHz, 4096 MB DDR2-Ram, GeForce 9600GT 512 MB

Neue Antwort erstellen


Übersicht BlitzMax, BlitzMax NG Codearchiv & Module

Gehe zu:

Powered by phpBB © 2001 - 2006, phpBB Group