Programming 2D Tutorials For Purebasic 4.51

Just starting out? Need help? Post your questions and find answers here.
Swos2009
Enthusiast
Enthusiast
Posts: 112
Joined: Sat Nov 08, 2008 8:19 pm

Programming 2D Tutorials For Purebasic 4.51

Post by Swos2009 »

Hello everyone

It is from John P. Logsdon's book "Programming 2D Scrolling Games in 2005 as it used purebasic 3.93 and I have fixed most of the code for Purebasic 4.51....

So far....

Chapter 3 - Hello World - Done
Chapter 4 - Hello World2 - Done
Chapter 5 - Loop - Done
Chapter 6 - Arrays - Nearly Done ..Just one Code to fixed
Chapter 7 - Arrays Struct - Nearly Done...Just one code to fixed
Chapter 8 - Memory - Done
Chapter 9 - Procedure - Not Done
Chapter 10 - File 0/1 - Not Done
Chapter 11 - 2D Graphics - Done
Chapter 12 - Sprites - Not Done
Chapter 13 - Sprite Anim - Done
Chapter 14 - Collisions - Not Done
Chapter 15 - Inputs - Not Done
Chapter 16 - Sounds - Not Done
Chapter 17 - Timer - Not Done
Chapter 19 - ZOrdering - Done
Chapter 20 - Map - Not Done

This is Chapter 6 - ex6-5VariableLengths.pb

I try to correct it but it wont run :(

Code: Select all

; Initialize the sprite and keyboard systems and a 640x480, 16-bit screen
If InitSprite() = 0 Or InitKeyboard() = 0 Or OpenScreen(640,480,16,"Array Test") = 0
   MessageRequester("Error!", "Unable to Initialize Environment", #PB_MessageRequester_Ok)
   End
EndIf

; Set up the vertical control variable
TextY = 0

; set up our flag value for seeing if we're done or not
FinishedListing = 0
 
Restore NameData
 
StartDrawing(ScreenOutput())
     ; while we're NOT finished
     While FinishedListing = 0
            ; read a name from the data segment
            Read Name.s             
            ; if that name = STOP, then we're done
            If Name = "STOP"
                FinishedListing = 1
            Else
                ; otherwise, show the name we read              
                DrawText(0,TextY,Name)
                TextY = TextY + 16
            EndIf
      Wend

      ; display a message so the user knows how to exit       
      DrawText(0,400,"Press any key to exit")
StopDrawing()

FlipBuffers() ;show the output to the user
 
; wait for any key to be pressed
Repeat 
   ExamineKeyboard()
Until KeyboardReleased(#PB_Key_All)

End  ; end the program

; Here is our data area
DataSection 
    NameData:
        Data.s "John","Joe","Mark","George"
        Data.s "Sally","Betty","Lorelei","Anne"
        Data.s "Fido","Spot","Killer","Tank"
        Data.s "Millennium Hawk","Tea Fighter","Zap-Wing","Dead Star"
        Data.s "STOP"
EndDataSection
LuCiFeR[SD]
666
666
Posts: 1033
Joined: Mon Sep 01, 2003 2:33 pm

Re: Programming 2D Tutorials For Purebasic 4.51

Post by LuCiFeR[SD] »

very slight modification to the code... but should point you in the right direction.

Code: Select all

EnableExplicit

; Initialize the sprite and keyboard systems and a 640x480, 16-bit screen
If InitSprite() = 0 Or InitKeyboard() = 0 Or OpenScreen(640,480,16,"Array Test") = 0
   MessageRequester("Error!", "Unable to Initialize Environment", #PB_MessageRequester_Ok)
   End
EndIf

;Define strings
Define.s name

; Define integer variables
Define.i TextY,FinishedListing


; Set up the vertical control variable
TextY = 0

; set up our flag value for seeing if we're done or not
FinishedListing = 0

Restore NameData

StartDrawing(ScreenOutput())
     ; while we're NOT finished
     While FinishedListing = 0
            ; read a name from the data segment
            Read.s Name  ;- <== Read string data
            ; if that name = STOP, then we're done
            If Name = "STOP"
                FinishedListing = 1
            Else
                ; otherwise, show the name we read              
                DrawText(0,TextY,Name)
                TextY = TextY + 16
            EndIf
      Wend

      ; display a message so the user knows how to exit       
      DrawText(0,400,"Press any key to exit")
StopDrawing()

FlipBuffers() ;show the output to the user

; wait for any key to be pressed
Repeat 
   ExamineKeyboard()
Until KeyboardReleased(#PB_Key_All)

End  ; end the program

; Here is our data area
DataSection 
    NameData:
        Data.s "John","Joe","Mark","George"
        Data.s "Sally","Betty","Lorelei","Anne"
        Data.s "Fido","Spot","Killer","Tank"
        Data.s "Millennium Hawk","Tea Fighter","Zap-Wing","Dead Star"
        Data.s "STOP"
EndDataSection
Swos2009
Enthusiast
Enthusiast
Posts: 112
Joined: Sat Nov 08, 2008 8:19 pm

Re: Programming 2D Tutorials For Purebasic 4.51

Post by Swos2009 »

Thanks LuCiFeR[SD] :)

Chapter 3 - Hello World - Done
Chapter 4 - Hello World2 - Done
Chapter 5 - Loop - Done
Chapter 6 - Arrays - Done
Chapter 7 - Arrays Struct - Nearly Done...Just one code to fixed
Chapter 8 - Memory - Done
Chapter 9 - Procedure - Not Done
Chapter 10 - File 0/1 - Not Done
Chapter 11 - 2D Graphics - Done
Chapter 12 - Sprites - Not Done
Chapter 13 - Sprite Anim - Done
Chapter 14 - Collisions - Not Done
Chapter 15 - Inputs - Not Done
Chapter 16 - Sounds - Not Done
Chapter 17 - Timer - Not Done
Chapter 19 - ZOrdering - Done
Chapter 20 - Map - Not Done

I have fixed some of it but I cant seem to get ArrayOfStructures. working.....Could anyone explain to me why got error?

Code: Select all

; Initialize the sprite and keyboard systems and a 640x480, 16-bit screen
If InitSprite() = 0 Or InitKeyboard() = 0 Or OpenScreen(640,480,16,"App Title") = 0
   MessageRequester("Error!", "Unable to Initialize Environment", #PB_MessageRequester_Ok)
   End
EndIf

; setup our structure
Structure Ships
           Name.s                    ; name of this ship
           LaserPower.b              ; 1-20 points per hit
           Armor.b                   ; 75-125 points depending on the ship
           ShieldPower.b             ; 50-100 points added on to Armor
           TopSpeed.b                ; 2 - 4 depending on ship type
EndStructure

; dimension our fighters
Dim Fighter.Ships(1)

; go to the ShipNameData section
Restore ShipNameData

; use a standard array looping style
For i = 0 To 1
     ; read the data
     Read.s Fighter.Ships(i)\Name
 Next

; go to the ShipSpecsData section
Restore ShipSpecsData

; use a standard array looping style
For i = 0 To 1
  ; read the data
    ; I got some error :(
    Read.s Fighter.Ships(i)\LaserPower
    Read.i Fighter.Ships(i)\Armor
    Read.s Fighter.Ships(i)\ShieldPower ; << Why Error?
    Read.s Fighter.Ships(i)\TopSpeed     ; << Why Error?
Next

; Set up the vertical control variable
TextY.w = 0

StartDrawing(ScreenOutput())
    ; Step through the ships array and print
    For i = 0 To 1 
      
        ShipText.s = "Ship Name: " + Fighter.Ships(i)\Name
        DrawText(0,TextY.w,ShipText.s)
        TextY.w = TextY.w + 16
        
        ShipText.s = "Laser Power: " + Str(Fighter.Ships(i)\LaserPower)
        DrawText(0,TextY,ShipText.s)
        TextY.w = TextY.w + 16
      
        ShipText.s = "Armor: " + Str(Fighter.Ships(i)\Armor)
        DrawText(0,TextY.w,ShipText.s)
        TextY.w = TextY.w + 16
      
        ShipText.s = "Shield Power: " + Str(Fighter.Ships(i)\ShieldPower)
        DrawText(0,TextY.w,ShipText.s)
        TextY.w = TextY.w + 16
          
        ShipText.s = "Top Speed: " + Str(Fighter.Ships(i)\TopSpeed)
        DrawText(0,TextY.w,ShipText.s)
        TextY.w = TextY.w + 32
    Next

     ; display a message so the user knows how to exit        
    DrawText(0,400,"Press any key to exit")

StopDrawing()
 
FlipBuffers()  ; show the output to the user

; wait for any key to be pressed
Repeat 
   ExamineKeyboard()
Until KeyboardReleased(#PB_Key_All)

End  ; end the program

; Here is our data area
DataSection 
    ShipNameData:
        Data.s "Kliazian Raptor", "Weltic Cruiser"

    ShipSpecsData:
        Data.b 15,125,50,3
        Data.b 20,100,75,4
EndDataSection
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Programming 2D Tutorials For Purebasic 4.51

Post by Demivec »

Use a read that matches the data.

For 'Data.b' use:

Code: Select all

Read.b
LuCiFeR[SD]
666
666
Posts: 1033
Joined: Mon Sep 01, 2003 2:33 pm

Re: Programming 2D Tutorials For Purebasic 4.51

Post by LuCiFeR[SD] »

As Demivec says, you are just trying to read the incorrect datatype. You just assumed it would be ".i" as I used that to fix the previous example. In this particular case you need to change that to ".b", which is a byte.

The PureBasic reference manual is invaluable in assisting you to adapt earlier source versions. And I would recommend having a good read of the Variables, Types and Operators & data sections :)
Swos2009
Enthusiast
Enthusiast
Posts: 112
Joined: Sat Nov 08, 2008 8:19 pm

Re: Programming 2D Tutorials For Purebasic 4.51

Post by Swos2009 »

thanks for links LuCiFeR[SD] :)

I really do hate pointer but pointer can be useful when having same item but different name I guess....

I get the error saying Cant Assign a value to a structure and what does it really mean?

Code: Select all

; Initialize the sprite and keyboard systems and a 640x480, 16-bit screen
If InitSprite() = 0 Or InitKeyboard() = 0 Or OpenScreen(640,480,16,"App Title") = 0
   MessageRequester("Error!", "Unable to Initialize Environment", #PB_MessageRequester_Ok)
   End
EndIf

; setup our structure
Structure Ships
           *Next.Ships       ; pointer -> next ship in list
           *Previous.Ships   ; pointer <- previous ship in list
           Name.s            ; Name of the ship
           WeaponsType.w     ; 0=basic, 1=advanced
           Shields.w         ; 200-400
           Armor.w           ; 200-400
EndStructure

; Define a pointer to the Structure
DefType.Ships *Ship

; setup a new list for the Freighters
NewList Fighter.Ships()


;And now let's populate two Fighters by hand, using pointers
*Ship = AddElement(Fighter())
If *Ship <> 0
   *Ship\Name = "Fast Fighter"
   *Ship\WeaponsType = 1
   *Ship\Shields= 300
   *Ship\Armor = 200
EndIf

*Ship = AddElement(Fighter())
If *Ship <> 0
   *Ship\Name = "Mid Fighter"
   *Ship\WeaponsType = 0
   *Ship\Shields= 400
   *Ship\Armor = 400
EndIf

ClearScreen(0)          ; clear the screen to black
   
; Set up the vertical control variable
TextY = 0
   
StartDrawing(ScreenOutput())
     ; point to the first element in the Fighter List
     *Ship = FirstElement(Fighter())
     
     ; While it's a valid element
     While *Ship <> 0
          
           MissileText.s = "Name: " + *Ship\Name 
           DrawText(0,TextY,MissileText)
           TextY = TextY + 16
          
           MissileText.s = "Weapons Type: " + Str(*Ship\WeaponsType)
           DrawText(0,TextY,MissileText)
           TextY = TextY + 16            
        
           MissileText.s = "Shields: " + Str(*Ship\Shields)
           DrawText(0,TextY,MissileText)
           TextY = TextY + 16            
         
           MissileText.s = "Armor: " + Str(*Ship\Armor)
           DrawText(0,TextY,MissileText)
           TextY = TextY + 32            
           
           ; now point to the next element
           *Ship = *Ship\Next

     Wend
        
     ; display a message so the user knows how exit        
     DrawText(0,400,"Press any key to Exit")

StopDrawing()

FlipBuffers()  ; show the output to the user

; wait for any key to be pressed
Repeat 
   ExamineKeyboard()
Until KeyboardReleased(#PB_Key_All)

End  ; end the program
Chapter 7 is nearly Done before I move to fixed Chapter 9 :)
LuCiFeR[SD]
666
666
Posts: 1033
Joined: Mon Sep 01, 2003 2:33 pm

Re: Programming 2D Tutorials For Purebasic 4.51

Post by LuCiFeR[SD] »

'DefType' renamed to 'Define' . simple fix :)

Code: Select all

; Initialize the sprite and keyboard systems and a 640x480, 16-bit screen
If InitSprite() = 0 Or InitKeyboard() = 0 Or OpenScreen(640,480,16,"App Title") = 0
   MessageRequester("Error!", "Unable to Initialize Environment", #PB_MessageRequester_Ok)
   End
EndIf

; setup our structure
Structure Ships
           *Next.Ships       ; pointer -> next ship in list
           *Previous.Ships   ; pointer <- previous ship in list
           Name.s            ; Name of the ship
           WeaponsType.w     ; 0=basic, 1=advanced
           Shields.w         ; 200-400
           Armor.w           ; 200-400
EndStructure

; Define a pointer to the Structure
Define.Ships *Ship  ;- <= - Changed: 'DefType' renamed to 'Define' 

; setup a new list for the Freighters
NewList Fighter.Ships()


;And now let's populate two Fighters by hand, using pointers
*Ship = AddElement(Fighter())
If *Ship <> 0
   *Ship\Name = "Fast Fighter"
   *Ship\WeaponsType = 1
   *Ship\Shields= 300
   *Ship\Armor = 200
EndIf

*Ship = AddElement(Fighter())
If *Ship <> 0
   *Ship\Name = "Mid Fighter"
   *Ship\WeaponsType = 0
   *Ship\Shields= 400
   *Ship\Armor = 400
EndIf

ClearScreen(0)          ; clear the screen to black
   
; Set up the vertical control variable
TextY = 0
   
StartDrawing(ScreenOutput())
     ; point to the first element in the Fighter List
     *Ship = FirstElement(Fighter())
     
     ; While it's a valid element
     While *Ship <> 0
          
           MissileText.s = "Name: " + *Ship\Name 
           DrawText(0,TextY,MissileText)
           TextY = TextY + 16
          
           MissileText.s = "Weapons Type: " + Str(*Ship\WeaponsType)
           DrawText(0,TextY,MissileText)
           TextY = TextY + 16            
        
           MissileText.s = "Shields: " + Str(*Ship\Shields)
           DrawText(0,TextY,MissileText)
           TextY = TextY + 16            
         
           MissileText.s = "Armor: " + Str(*Ship\Armor)
           DrawText(0,TextY,MissileText)
           TextY = TextY + 32            
           
           ; now point to the next element
           *Ship = *Ship\Next

     Wend
        
     ; display a message so the user knows how exit        
     DrawText(0,400,"Press any key to Exit")

StopDrawing()

FlipBuffers()  ; show the output to the user

; wait for any key to be pressed
Repeat 
   ExamineKeyboard()
Until KeyboardReleased(#PB_Key_All)

End  ; end the program
Swos2009
Enthusiast
Enthusiast
Posts: 112
Joined: Sat Nov 08, 2008 8:19 pm

Re: Programming 2D Tutorials For Purebasic 4.51

Post by Swos2009 »

Wow ..Just one simple fixed lol... Thanks LuCiFeR[SD] :)
I will work on Chapter 9 Next :) :mrgreen:
Swos2009
Enthusiast
Enthusiast
Posts: 112
Joined: Sat Nov 08, 2008 8:19 pm

Re: Programming 2D Tutorials For Purebasic 4.51

Post by Swos2009 »

I thought I have try to fixed the code but I dont know it isnt working :(

This is Procedures Library

Myprocedures.PB

Code: Select all

; Sample library just to show how to set one up.
 ;****************************************************

 ;****************************************************
 ;  Procedure: AddNumbers()
 ;     Author: John Logsdon
 ;   Last Upd: 8/23/2004
 ;    Purpose: add two numbers and return the result
 ;       Args: Two numbers
 ;    Returns: Byte - Sum of the two numbers sent 
 ;   Comments: None
 ;****************************************************
 Procedure.b AddNumbers(iNumber1.b, iNumber2.b)
     iSum.b = iNumber1 + iNumber2
     ProcedureReturn(iSum)
 EndProcedure

 ;****************************************************
 ;   Procedure: AvgNumbers()
 ;      Author: John Logsdon
 ;   Last Upd: 8/23/2004
 ;    Purpose: Find the average of 3 floats
 ;       Args: Two floats
 ;    Returns: Sum of the two numbers sent
 ;   Comments: None
 ;****************************************************
 Procedure.f AvgNumbers(float1.f, float2.f, float3.f)
     avg.f = (float1 + float2 + float3) / 3
     ProcedureReturn(avg)
 EndProcedure

 ;****************************************************
 ;  Procedure: WaitKey()
 ;     Author: John Logsdon
 ;   Last Upd: 8/23/2004
 ;    Purpose: Wait for a keypress
 ;       Args: n/a
 ;    Returns: n/a
 ;   Comments: None
 ;****************************************************
 Procedure WaitKey()
    Repeat 
       ExamineKeyboard()
    Until KeyboardReleased(#PB_Key_All)
 EndProcedure
Then to code your own procedures

Ex9-1Procedures.PB

Code: Select all

; Initialize the sprite and keyboard systems and a 640x480, 16-bit screen
If InitSprite() = 0 Or InitKeyboard() = 0 Or OpenScreen(640,480,16,"App Title") = 0
   MessageRequester("Error!", "Unable to Initialize Environment", #PB_MessageRequester_Ok)
   End
EndIf

 Declare.b AddNumbers(iNumber1.b, iNumber2.b)
 Declare.s ConcatString(String1.s, String2.s)
 Declare.f AvgNumbers(float1.f, float2.f, float3.f)
 Declare.w GetArray(Location.w)
 Declare SetupShips()
 Declare.w GetShip(ShipID.b)
 Declare WaitKey()
 
 ; make a dummy array and fill it with values
 
; Why Error from here to 
;============================================================   
Dim ArrayValues.l(4)                                            Global 
Dim ArrayValues.l(4)
  ArrayValues(0) = 100
  ArrayValues(1) = 200
  ArrayValues(2) = 300
  ArrayValues(3) = 400
  ArrayValues(4) = 500
 
  ; define a type for Ships
  Structure Ships
      ShipID.b   ; what's it's ID?
      ShipName.s ; the name?
      Speed.b    ; top speed (3-7)
  EndStructure
 
  NewList Ship.Ships()                                         Global  
  NewList Ship.Ships()
;============================================================   
; There 
 
 ; add a few ships.  Note that this procedure does NOT
 ; return a value!
 SetupShips()

 ; call AddNumbers procedure and place the returned-value
 ; into the variable "byteValue"
 byteValue.b = AddNumbers(10,20)

 ; call ConcatString procedure and place the returned-value
 ; into the variable "stringValue"
 stringValue.s = ConcatString("How","dy")

 ; call AvgNumbers procedure and place the returned-value
 ; into the variable "floatValue"
 floatValue.f = AvgNumbers(1.495,3.772,11.1935)
 
 ; call GetArray procedure and place the returned-value
 ; into the variable "arrayValue"
 arrayValue.w = GetArray(3)

 ; call GetShip procedure and place the returned-value
 ; into the variable "ShipLocation"
 ShipLocation.w = GetShip(Random(9))
 ; now make sure we have that element selected
 SelectElement(Ship(),ShipLocation)

 ; Set up the vertical control variable
 TextY = 0

 StartDrawing(ScreenOutput()) 
 ; display returned values
 
 DrawText(0,TextY,"byteValue = " + Str(byteValue))
 TextY = TextY + 16
 
 DrawText(0,TextY,"stringValue = " + StringValue)
 TextY = TextY + 16
 
 DrawText(0,TextY,"floatValue = " + StrF(floatValue.f))
 TextY = TextY + 16
 
 DrawText(0,TextY,"arrayValue = "+ Str(arrayValue))
 TextY = TextY + 32
 
 DrawText(0,TextY,"** Ship Info **")
 TextY = TextY + 16
 
 DrawText(0,TextY,"ID = " + Str(Ship()\ShipID))
 TextY = TextY + 16
 
 DrawText(0,TextY,"Name = " + Ship()\ShipName)
 TextY = TextY + 16
 
 DrawText(0,TextY,"Speed = " + Str(Ship()\Speed))
 StopDrawing()

 ; show the output to the users
 FlipBuffers()

 ; wait for a keypress
 WaitKey ()

 ; end the program
 End


 ;****************************************************
 ;             P R O C E D U R E S
 ;****************************************************
 
 ;****************************************************
 ;  Procedure: AddNumbers()
 ;     Author: John Logsdon
 ;   Last Upd: 8/23/2004
 ;    Purpose: add two numbers and return the result
 ;       Args: Two numbers
 ;    Returns: Byte - Sum of the two numbers sent 
 ;   Comments: None
 ;****************************************************
 Procedure.b AddNumbers(iNumber1.b, iNumber2.b)
     iSum.b = iNumber1 + iNumber2
     ProcedureReturn(iSum)
 EndProcedure

 ;****************************************************
 ;  Procedure: ConcatString$()
 ;     Author: John Logsdon
 ;   Last Upd: 8/23/2004
 ;    Purpose: concatonates two strings
 ;       Args: Two strings
 ;    Returns: the resultant string
 ;   Comments: None
 ;****************************************************
 Procedure.s ConcatString(String1.s, String2.s)
     ProcedureReturn(String1.s + String2.s)
 EndProcedure

 ;****************************************************
 ;   Procedure: AvgNumbers()
 ;      Author: John Logsdon
 ;   Last Upd: 8/23/2004
 ;    Purpose: Find the average of 3 floats
 ;       Args: Two floats
 ;    Returns: Sum of the two numbers sent
 ;   Comments: None
 ;****************************************************
 Procedure.f AvgNumbers(float1.f, float2.f, float3.f)
     avg.f = (float1 + float2 + float3) / 3
     ProcedureReturn(avg)
 EndProcedure

 ;****************************************************
 ;   Procedure: GetArray()
 ;      Author: John Logsdon
 ;   Last Upd: 8/23/2004
 ;    Purpose: Get a particular array value
 ;       Args: location in the array
 ;    Returns: The array value
 ;   Comments: None
 ;****************************************************
 Procedure.w GetArray(Location.w)
     ProcedureReturn(ArrayValues(Location))
 EndProcedure

 ;****************************************************
 ;   Procedure: SetupShips()
 ;      Author: John Logsdon
 ;   Last Upd: 8/23/2004
 ;    Purpose: adds a few ships to the Structure
 ;       Args: N/A
 ;    Returns: N/A
 ;   Comments: None
 ;****************************************************
 Procedure SetupShips()
     For i = 0 To 9
         If AddElement(Ship()) <> 0
            Ship()\ShipID = i
            Ship()\ShipName = "Ship" + Str(i)
            Ship()\Speed = Random(4) + 3
         EndIf
     Next
 EndProcedure

 ;****************************************************
 ;  Procedure: GetShip()
 ;     Author: John Logsdon
 ;   Last Upd: 8/23/2004
 ;    Purpose: locates a ship and returns its index
 ;       Args: Ship's ID
 ;    Returns: the Index entry for the Ship
 ;   Comments: None
 ;****************************************************
 Procedure.w GetShip(ShipID.b)
     ForEach Ship()
         If Ship()\ShipID = ShipID.b
            Break 
         EndIf
     Next
     ProcedureReturn(ListIndex(Ship()))
 EndProcedure

 ;****************************************************
 ;  Procedure: WaitKey()
 ;     Author: John Logsdon
 ;   Last Upd: 8/23/2004
 ;    Purpose: Wait for a keypress
 ;       Args: n/a
 ;    Returns: n/a
 ;   Comments: None
 ;****************************************************
 Procedure WaitKey()
    Repeat 
       ExamineKeyboard()
    Until KeyboardReleased(#PB_Key_All)
 EndProcedure
Could anyone fixed the code above Before I go on fixed on next tutorial :)
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Programming 2D Tutorials For Purebasic 4.51

Post by Demivec »

change:

Code: Select all

;============================================================   
Dim ArrayValues.l(4)                                            Global
Dim ArrayValues.l(4)
ArrayValues(0) = 100
ArrayValues(1) = 200
ArrayValues(2) = 300
ArrayValues(3) = 400
ArrayValues(4) = 500

  ; define a type for Ships
Structure Ships
  ShipID.b   ; what's it's ID?
  ShipName.s ; the name?
  Speed.b    ; top speed (3-7)
EndStructure

NewList Ship.Ships()                                         Global 
NewList Ship.Ships()
;============================================================ 
to:

Code: Select all

;============================================================   
Global Dim ArrayValues.l(4)
ArrayValues(0) = 100
ArrayValues(1) = 200
ArrayValues(2) = 300
ArrayValues(3) = 400
ArrayValues(4) = 500

  ; define a type for Ships
Structure Ships
  ShipID.b   ; what's it's ID?
  ShipName.s ; the name?
  Speed.b    ; top speed (3-7)
EndStructure

Global NewList Ship.Ships()
;============================================================ 
Lost
User
User
Posts: 63
Joined: Fri Dec 19, 2008 12:24 am
Location: Tasmania

Re: Programming 2D Tutorials For Purebasic 4.51

Post by Lost »

Cool, I only just stumbled upon this topic. I bought this book, so thanks for updating the code.
Swos2009
Enthusiast
Enthusiast
Posts: 112
Joined: Sat Nov 08, 2008 8:19 pm

Re: Programming 2D Tutorials For Purebasic 4.51

Post by Swos2009 »

So far

Chapter 3 - Hello World - Done
Chapter 4 - Hello World2 - Done
Chapter 5 - Loop - Done
Chapter 6 - Arrays - Done
Chapter 7 - Arrays Struct - Done
Chapter 8 - Memory - Done
Chapter 9 - Procedure - Done
Chapter 10 - File 0/1 - Not Done - This is the next one I will be doing, try to fixed it and If I couldnt then I will post the code on here :)
Chapter 11 - 2D Graphics - Done
Chapter 12 - Sprites - Not Done
Chapter 13 - Sprite Anim - Done
Chapter 14 - Collisions - Not Done
Chapter 15 - Inputs - Not Done
Chapter 16 - Sounds - Not Done
Chapter 17 - Timer - Not Done
Chapter 19 - ZOrdering - Done
Chapter 20 - Map - Not Done

:)
Swos2009
Enthusiast
Enthusiast
Posts: 112
Joined: Sat Nov 08, 2008 8:19 pm

Re: Programming 2D Tutorials For Purebasic 4.51

Post by Swos2009 »

So far....I have done

Chapter 3 - Hello World - Done
Chapter 4 - Hello World2 - Done
Chapter 5 - Loop - Done
Chapter 6 - Arrays - Done
Chapter 7 - Arrays Struct - Done
Chapter 8 - Memory - Done
Chapter 9 - Procedure - Done
Chapter 10 - File 0/1 - Done
Chapter 11 - 2D Graphics - Done
Chapter 12 - Sprites - Done
Chapter 13 - Sprite Anim - Done
Chapter 14 - Collisions - Done
Chapter 15 - Inputs - 1 problem
Chapter 16 - Sounds - Done
Chapter 17 - Timer - Done
Chapter 19 - ZOrdering - Done
Chapter 20 - Map - 1 problem

Can anyone Check this one but you need joystick to test it!!

Code: Select all

; setup our Screen Width and Height here for easier tracking
#ScreenWidth  = 640
#ScreenHeight = 480

; Initialize the sprite and keyboard, joystick, and a 640x480, 16-bit screen
If InitSprite() = 0 Or InitKeyboard() = 0 Or InitJoystick() = 0 Or OpenScreen(#ScreenWidth,#ScreenHeight,16,"App Title") = 0
   MessageRequester("Error!", "Unable to Initialize Environment", #PB_MessageRequester_Ok)
   End
EndIf

LastButton.s = "None"
OtherButton.s = "None"
 
; Keep going until the user hits a key
Repeat
   ; clear the screen
   ClearScreen(0)

   Button.s = ""
   If ExamineJoystick()
      ; check to see if there has been any movement on X
      X_Movement = JoystickAxisX()
      If X_Movement < 0
         X_JoystickMovement.s = "Left"
      Else   
         If X_Movement > 0
            X_JoystickMovement.s = "Right"
         Else
            X_JoystickMovement.s = ""
         EndIf
      EndIf

      ; check to see if there has been any movement on Y
      Y_Movement = JoystickAxisY()
      If Y_Movement < 0
         Y_JoystickMovement.s = "Up"
      Else   
         If Y_Movement > 0
            Y_JoystickMovement.s = "Down"
         Else
            Y_JoystickMovement.s = ""
         EndIf
      EndIf

      ; check to see if any of the buttons have been pressed
      For Buttons = 1 To 10
          If JoystickButton(Buttons)
             If Button.s = ""
                Button.s = Str(Buttons) 
             Else
                Button.s = Button.s + ", " + Str(Buttons) 
             EndIf
          EndIf
      Next

   EndIf

   ; put up text for exiting
   StartDrawing(ScreenOutput())
  
     DrawText(0,0,"Buttons Pressed: " + Button.s)
    
     DrawText(0,20,"X Movement = " + X_JoystickMovement.s )
  
     DrawText(0,20,"Y Movement = " + Y_JoystickMovement.s )
    
     DrawText(0,460,"Press any key To quit")
   StopDrawing()

   FlipBuffers()

   ExamineKeyboard()
Until KeyboardReleased(#PB_Key_All)
End
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Programming 2D Tutorials For Purebasic 4.51

Post by Demivec »

Corrections:

Code: Select all

;add the joystick# to each of the joystick commands so they look like so:
ExamineJoystick(0)
JoystickAxisX(0)
JoystickAxisY(0)
JoystickButton(0, Buttons)

;change the y postition for the text statement displaying the 'Y Movement' to 40 so it doesn't overwrite the 'X movement' display
DrawText(0,40,"Y Movement = " + Y_JoystickMovement.s )
Swos2009
Enthusiast
Enthusiast
Posts: 112
Joined: Sat Nov 08, 2008 8:19 pm

Re: Programming 2D Tutorials For Purebasic 4.51

Post by Swos2009 »

thanks Demivec and I got one more things to do on the map tutorials and The Programming 2D Tutorials For Purebasic 4.51 will be finished as I will put all the code in the Winrar Zip for everyone to download it :mrgreen:
Post Reply