Starflight

Share your advanced PureBasic knowledge/code with the community.
User avatar
Hroudtwolf
Addict
Addict
Posts: 803
Joined: Sat Feb 12, 2005 3:35 am
Location: Germany(Hessen)
Contact:

Starflight

Post by Hroudtwolf »

Code updated For 5.20+

Everybody haves the starwars fever.
Myself, too :D

It not perfectly. Its just an example how to do it.

Code: Select all

;Starflight
;2005 by Hroudtwolf
;PureBasic-Lounge.de
;
;
;Moving:
;left,right,up and down
;
;Speed
;increase = +
;decrease = -

Declare InitStarFlight (max.l,width.l,height.l)
Declare DisplayStarFlight (x.l,y.l,speed.l,flag.l)


If InitSprite() = 0 Or InitKeyboard() = 0
  MessageRequester("Fehler", "Kann DirectX oder höher nicht initialisieren.", 0)
  End
EndIf

InitStarFlight (100,800,600)

If OpenScreen(800, 600, 16, "Starflight")
  
  Repeat
    
    FlipBuffers()
    ClearScreen(RGB(0,0,0))
    DisplayStarFlight (horz.l,vert.l,speed.l,1)
    
    
    
    ExamineKeyboard()
    If  KeyboardPushed(#PB_Key_Left):horz.l=horz.l-2:EndIf
    If  KeyboardPushed(#PB_Key_Up):vert.l=vert.l-2:EndIf
    If  KeyboardPushed(#PB_Key_Right):horz.l=horz.l+2:EndIf
    If  KeyboardPushed(#PB_Key_Down):vert.l=vert.l+2:EndIf
    If  KeyboardPushed(#PB_Key_Add) And speed.l<200:speed.l=speed.l+1:EndIf
    If  KeyboardPushed(#PB_Key_Subtract) And speed.l>0:speed.l=speed.l-1:EndIf
    
    Delay (10)
  Until x > 500 Or KeyboardPushed(#PB_Key_Escape)
  
  CloseScreen()
EndIf

End   












Procedure InitStarFlight (max.l,width.l,height.l)
  Global width199.l,height199.l,aktionszeit.l,aktion.l
  width199.l=width.l
  height199.l=height.l
  Structure sterndaten
    hrichtung.l
    vrichtung.l
    geschwindigkeitx.l
    geschwindigkeity.l
    x.l
    y.l
    z.l
  EndStructure
  Global NewList sternenflucht.sterndaten()
  For x=1 To max.l
    AddElement (sternenflucht())
    sternenflucht()\x= width199.l/2-(Random(20)-1)
    sternenflucht()\y=height199.l/2-(Random(20)-10)
    sternenflucht()\z=50
    sternenflucht()\geschwindigkeitx=1+Random(5)
    sternenflucht()\geschwindigkeity=1+Random(5)
    sternenflucht()\hrichtung=1+Random(1)
    sternenflucht()\vrichtung=1+Random(1)
  Next x
  x=0:y=0
  For z=1 To 500
    ResetList (sternenflucht())
    While NextElement(sternenflucht())
      If sternenflucht()\hrichtung=1:sternenflucht()\x=sternenflucht()\x-sternenflucht()\geschwindigkeitx:EndIf
      If sternenflucht()\hrichtung=2:sternenflucht()\x=sternenflucht()\x+sternenflucht()\geschwindigkeitx:EndIf
      If sternenflucht()\vrichtung=1:sternenflucht()\y=sternenflucht()\y-sternenflucht()\geschwindigkeity:EndIf
      If sternenflucht()\vrichtung=2:sternenflucht()\y=sternenflucht()\y+sternenflucht()\geschwindigkeity:EndIf
      If sternenflucht()\x<0 Or sternenflucht()\x>width199.l-1 Or sternenflucht()\y<0 Or sternenflucht()\y>height199.l-1
        sternenflucht()\x=(width199.l/2)+x
        sternenflucht()\y=(height199.l/2)+y
        sternenflucht()\geschwindigkeitx=1+Random(5)
        sternenflucht()\geschwindigkeity=1+Random(5)
        sternenflucht()\hrichtung=1+Random(1)
        sternenflucht()\vrichtung=1+Random(1)
        sternenflucht()\z=100
        If sternenflucht()\x <0:sternenflucht()\x=0:EndIf
        If sternenflucht()\y <0:sternenflucht()\y=0:EndIf
        If sternenflucht()\x >(width199.l)-1:sternenflucht()\x=width199.l-1:EndIf
        If sternenflucht()\y >(height199.l)-1:sternenflucht()\y=height199.l-1:EndIf
        sternenflucht()\z=sternenflucht()\z+3
        If sternenflucht()\z>255:sternenflucht()\z=255:EndIf
      EndIf
      farbe.l=sternenflucht()\z
    Wend
  Next z
EndProcedure














Procedure DisplayStarFlight (x.l,y.l,speed.l,flag.l)
  If aktion.l=0:aktionszeit.l=GetTickCount_()+(180-speed.l):aktion.l=1:EndIf
  If aktion.l=1 And GetTickCount_()>aktionszeit.l:aktion.l=2:EndIf
  If speed.l>180
    aktion.l=2
  EndIf
  If StartDrawing (ScreenOutput ())
    ResetList (sternenflucht())
    While NextElement(sternenflucht())
      If aktion.l=2 And speed.l>0
        If sternenflucht()\hrichtung=1:sternenflucht()\x=sternenflucht()\x-sternenflucht()\geschwindigkeitx:EndIf
        If sternenflucht()\hrichtung=2:sternenflucht()\x=sternenflucht()\x+sternenflucht()\geschwindigkeitx:EndIf
        If sternenflucht()\vrichtung=1:sternenflucht()\y=sternenflucht()\y-sternenflucht()\geschwindigkeity:EndIf
        If sternenflucht()\vrichtung=2:sternenflucht()\y=sternenflucht()\y+sternenflucht()\geschwindigkeity:EndIf
        If sternenflucht()\x<0 Or sternenflucht()\x>width199.l-1 Or sternenflucht()\y<0 Or sternenflucht()\y>height199.l-1
          sternenflucht()\x=(width199.l/2)+x.l
          sternenflucht()\y=(height199.l/2)+y.l
          sternenflucht()\geschwindigkeitx=1+Random(5)
          sternenflucht()\geschwindigkeity=1+Random(5)
          sternenflucht()\hrichtung=1+Random(1)
          sternenflucht()\vrichtung=1+Random(1)
          sternenflucht()\z=100
        EndIf
        If sternenflucht()\x <0:sternenflucht()\x=0:EndIf
        If sternenflucht()\y <0:sternenflucht()\y=0:EndIf
        If sternenflucht()\x >(width199.l)-1:sternenflucht()\x=width199.l-1:EndIf
        If sternenflucht()\y >(height199.l)-1:sternenflucht()\y=height199.l-1:EndIf
        sternenflucht()\z=sternenflucht()\z+3
        If sternenflucht()\z>255:sternenflucht()\z=255:EndIf
      EndIf
      farbe.l=sternenflucht()\z
      If flag.l=0
        Plot (sternenflucht()\x,sternenflucht()\y,RGB(farbe.l,farbe.l,farbe.l))
      Else
        Circle (sternenflucht()\x,sternenflucht()\y,flag.l,RGB(farbe.l,farbe.l,farbe.l))
      EndIf
    Wend
    StopDrawing ()
    If aktion.l=2:aktion.l=0:EndIf
  EndIf
EndProcedure

PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Starflight

Post by PB »

Doesn't compile... I get this error:

Error at line 133 : The specified output is NULL (0 value)

And no, I didn't try to debug it... that's your job. ;)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
Hroudtwolf
Addict
Addict
Posts: 803
Joined: Sat Feb 12, 2005 3:35 am
Location: Germany(Hessen)
Contact:

Post by Hroudtwolf »

I 've no bug in this code.
It works perfectly.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Was it supposed to move? I just saw a frozen image on the screen. (But I have a crappy ProSavage DDR)
Num3
PureBasic Expert
PureBasic Expert
Posts: 2812
Joined: Fri Apr 25, 2003 4:51 pm
Location: Portugal, Lisbon
Contact:

Post by Num3 »

Trond wrote:Was it supposed to move? I just saw a frozen image on the screen. (But I have a crappy ProSavage DDR)
The "+" key you must hit, young Jedi....

Then the arrow keys you must too, for particle move .... ;)
User avatar
Hroudtwolf
Addict
Addict
Posts: 803
Joined: Sat Feb 12, 2005 3:35 am
Location: Germany(Hessen)
Contact:

Post by Hroudtwolf »

press the "+" for increase the speed,
press the "-" for decrease the speed,
use the cursorkeys to move arround the screen.

This spaceship can't flight without started motors ;-)
akee
Enthusiast
Enthusiast
Posts: 498
Joined: Wed Aug 18, 2004 9:52 am
Location: Penang, Malaysia

Post by akee »

reminds me of the LucasArts DOS based game named X-Wing. :wink:
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

Post by NoahPhense »

Nifty..

- np
Anubis
New User
New User
Posts: 6
Joined: Fri Jun 03, 2005 11:32 am
Location: California - USA

Post by Anubis »

Note: For those of us who use laptops.... :D

Be sure to enable your 'Numeric' keypad and use it's [+] & [-] keys,

Otherwise you cannot 'start your engines' with the regular [+] & [-] keys!

"Warp 8, Mr. Scott :!: "
/\NUBIS
Post Reply