Page 3 of 4

OK

Posted: Sun Jul 27, 2003 8:22 am
by LJ
OK Hypervox, found a fairly simple solution. Create a count down variable. The length is adjustable. When the laser is fired, playcount = 30. Playcount then begins counting down each iteration of the main loop. The laser sound will not play again until the playcount variable = 0. It's a little ugly, but does the job in a simple fashion not so far beyond a beginner (as opposed to an API solution that may be a bit to confusing). If you don't like the DEC command, disable Inline Assembly option in compiler options and replace with playcount = playcount - 1

Code: Select all

    If KeyboardPushed(#PB_Key_Space)
      If playcount = 0
        PlaySound(#phaser)
        playcount = 40
      EndIf
    EndIf
    If playcount > 0
      DEC playcount
    EndIf
Here is your complete code with above routine added:

Code: Select all

; fighter
; (C) Shaun Raven, use freely 

;//////////////////////////////////////////////////////////////
;- Initialisation
;//////////////////////////////////////////////////////////////
;- Window Constants
;
#Window_0 = 0

;- Screen constants
#SCREEN_WIDTH = 640
#SCREEN_HEIGHT = 480
playcount = 0
;- Window Title
Title$ = "Fighter Demo 2"

;////////////////////////////////////////////////////////////
;- Subroutines
;////////////////////////////////////////////////////////////
;-Constants: 

#NUM_STARS = 100
#PLANE_1 = 1
#PLANE_2 = 2
#PLANE_3 = 3

#player1 = 4
#snapshot = 5
#voidhum = 6
#phaser = 7

Global BLACK.l
Global WHITE.l
Global LT_GREY.l 
Global DK_GREY.l 

;-Structures
Structure Frame
  x1.w      ;position of frame top left x
  y1.w      ;position of frame top left y
  x2.w      ;position of frame bottom right x
  y2.w      ;position of frame bottom right y
EndStructure

Structure star_type
  x.w       ;position of star - x
  y.w       ;position of star - y
  plane.b   ;plane of star
  colour.l  ;colour of star
EndStructure

Structure Player
  x.w       ;position of player - x
  y.w       ;position of player - y
  width.w   ;width of sprite
  height.w  ;height of sprite
  speed.w   ;speed of player
  fps.w     ;frames per second of player
  frameno.w ;current frame number
  frames.Frame[3] ;array of frames to animate
EndStructure




;-Globals
Dim stars.star_type(#NUM_STARS)
Global player_1.Player
Global velocity_1.b
Global velocity_2.b 
Global velocity_3.b 
Global done.b 

velocity_1 = 1
velocity_2 = 2
velocity_3 = 3
done = 0

BLACK = RGB(0,0,0)
WHITE = RGB(255,255,255)
LT_GREY = RGB(175,175,175)
DK_GREY = RGB(100,100,100)
Procedure Init_Stars()
  ;
  ; initialise stars
  ;
  For x = 0 To #NUM_STARS -1
    ; initialise star position
    stars(x)\x = Random(#SCREEN_WIDTH - 1)
    stars(x)\y = Random(#SCREEN_HEIGHT - 1)
    ;initialise star plane & colour
    Select Random(2)
      Case 0
        stars(x)\plane = 1
        stars(x)\colour = DK_GREY
        
      Case 1
        stars(x)\plane = 2
        stars(x)\colour = LT_GREY
      
      Case 2
        stars(x)\plane = 3
        stars(x)\colour = WHITE
    EndSelect
         
  Next
 

EndProcedure

Procedure InitPlayer()
  ;
  ; Load player variable
  ;
  ; load sprite and set player's initial x,y and speed
  result = LoadSprite(#player1,"fighter.bmp",0)
    
  If result = 0
    MessageRequester("b","ARGH",0)
  EndIf

  player_1\speed = 0
  player_1\width = 60
  player_1\height = 60
  player_1\x = (#SCREEN_WIDTH - player_1\width) /2
  player_1\y = (#SCREEN_HEIGHT - player_1\height) /2

  ; now load the frame data (note: not the bitmap, just the cordinates
  startpointx = 0
  startpointy = 0
  
  For x = 0 To 2
    player_1\frames[x]\x1 = startpointx
    player_1\frames[x]\y1 = startpointy
    player_1\frames[x]\x2 = startpointx + 60
    player_1\frames[x]\y2 = startpointy
    startpointx = startpointx + 60

    
  Next
  player_1\frameno = 0
EndProcedure

Procedure AnimatePlayer()

  ;
  ; Animate the player
  ;
  ; Clip the sprite
  ;
  
  CurrFrame = player_1\frameno

  
  ClipSprite(#player1, player_1\frames[CurrFrame]\x1, player_1\frames[CurrFrame]\y1, player_1\width ,player_1\height)
  DisplayTransparentSprite(#player1,player_1\x,player_1\y)
  CurrFrame = CurrFrame + 1
  If CurrFrame > 2
    CurrFrame = 0
  EndIf
  player_1\frameno = CurrFrame
EndProcedure


If InitSprite() = 0 Or InitKeyboard() = 0 Or InitSound() = 0
  MessageRequester("Error","Can't open DirectX",0)
  End
EndIf

If OpenWindow(#Window_0, 0, 0, #SCREEN_WIDTH, #SCREEN_HEIGHT,  #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_TitleBar | #PB_Window_ScreenCentered , Title$)
  
  ;If CreateGadgetList(WindowID())
  ;
  ;EndIf
EndIf

If OpenWindowedScreen(WindowID(),0,0,#SCREEN_WIDTH, #SCREEN_HEIGHT,0,0,0)
    Init_Stars()
    InitPlayer()
    SetFrameRate(45)
    LoadSound(#voidhum,"voidhum.wav")
    PlaySound(#voidhum,1)
    LoadSound(#phaser,"phaser.wav")
  ;//////////////////////////////////////////////////////////////
  ;- GAME LOOP
  ;//////////////////////////////////////////////////////////////
Repeat

    ;///////////////////////////////////////////////////
    ;- Input
    ;///////////////////////////////////////////////////
    ;check keyboard For input
    ;if escape pressed, leave program,
    ;if + or - pressed, change speed of starfield
    ExamineKeyboard()
    If KeyboardPushed(#PB_Key_Escape) 
       done = 1
    EndIf
    
    ; save screenshot
    If KeyboardPushed(#PB_Key_F1)
      GrabSprite(#snapshot,0,0,#SCREEN_WIDTH,#SCREEN_HEIGHT)
      SaveSprite(#snapshot,"screenshot.bmp")
    EndIf
    
    If KeyboardPushed(#PB_Key_Equals)
      velocity_1 = velocity_1 + 1
      velocity_2 = velocity_2 + 2
      velocity_3 = velocity_3 + 3
        
    EndIf
    If KeyboardPushed(#PB_Key_Minus)
      velocity_1 = velocity_1 - 1
      velocity_2 = velocity_2 - 2
      velocity_3 = velocity_3 - 3
    EndIf
    If KeyboardPushed(#PB_Key_Up)
      player_1\y = player_1\y - 5

    EndIf
    If KeyboardPushed(#PB_Key_Down)
      player_1\y = player_1\y + 5
    EndIf
    If KeyboardPushed(#PB_Key_Left)
      player_1\x = player_1\x - 5
      
    EndIf
    If KeyboardPushed(#PB_Key_Right)
      player_1\x = player_1\x + 5

    EndIf
    If KeyboardPushed(#PB_Key_Space)
      If playcount = 0
        PlaySound(#phaser)
        playcount = 40
      EndIf
    EndIf
    If playcount > 0
      DEC playcount
    EndIf
    ; make sure player doesn't go off screen
    If player_1\x > (#SCREEN_WIDTH - player_1\width)
      player_1\x = (#SCREEN_WIDTH - player_1\width)
    EndIf
    If player_1\x < 0 
      player_1\x = 0
    EndIf
    If player_1\y > (#SCREEN_HEIGHT - player_1\height)
      player_1\y = (#SCREEN_HEIGHT - player_1\height)
    EndIf
    If player_1\y < 0
      player_1\y = 0
    EndIf
    
    Event = WindowEvent()
    If Event = #PB_EventCloseWindow
      done = 1
    EndIf
    ;//////////////////////////////////////////////////////////////
    ;- Game logic,Transformations & Rendering      
    ;//////////////////////////////////////////////////////////////
    ClearScreen(0,0,0)
    If StartDrawing(ScreenOutput())

        For i = 0 To #NUM_STARS - 1
          ; erase the star

          Plot(stars(i)\x  ,stars(i)\y  ,BLACK)
          
          ;move the star, and test for off-screen condition
          
          ;each star is on a different plane, so test which plane the star
          ;is in so that proper velocity can be used
          Select stars(i)\plane
            Case #PLANE_1 ;slowest plane
              stars(i)\y = stars(i)\y + velocity_1
              
            Case #PLANE_2 ;mid speed plane
              stars(i)\y = stars(i)\y + velocity_2
          
            Case #PLANE_3 ;high speed plane
              stars(i)\y = stars(i)\y + velocity_3

          
          EndSelect
          
          ; test If star has gone off screen
          If stars(i)\y > (#SCREEN_HEIGHT - 1)
            stars(i)\y = (stars(i)\y - #SCREEN_HEIGHT)
          ElseIf stars(i)\y < 0 ; off left edge?
            stars(i)\y = (#SCREEN_HEIGHT + stars(i)\y)
          EndIf
          
          Plot(stars(i)\x,stars(i)\y,stars(i)\colour)
          
        Next
        
        ; Draw instructions
        Locate(0,460)
        DrawText(Str(player_1\x) + "," + Str(player_1\y))
        
        
        StopDrawing()
        AnimatePlayer()
          
        FlipBuffers()
        
        
      EndIf


  Until done = 1
EndIf
;///////////////////////////////////////////////////////
;- Tidy up here if required
;///////////////////////////////////////////////////////
StopSound(#voidhum)
End 

Posted: Sun Jul 27, 2003 1:30 pm
by Searhin
@ hypervox

Thanx again for your great work :D

finally found the time to check out your latest tutorial - and learnt to handle sprites (never done before, always boring app's :wink: ).

btw: is the "fps" offset in the Player structure for use in a later tutorial? you wrote "because our sprite animation may be slower than our frame rate, we need to store it", but the sprite gets animated in each event loop regardless of player_1\fps or the screen refresh rate. just wondering...

Another point i noticed: you can press two keys simultaneously but not three. E.g. up and left arrow keys lets the ship fly diagonally, any arrow key together with space moves the ship and fires at the same time. But when pressing a third key, the appropriate command isn't executed. Instead after a short while there is an error beep.
Any idea? Some windows or DirectX limits?


@ LJ
nice work-around. simple but efficient.

Greets,
Searhin

Posted: Mon Jul 28, 2003 7:22 am
by hypervox
@ LJ

Thanks - I'll put it in the next tutorial

@ Searhin

Yup - I'd thought I'd better include it as I just KNOW I'm gonna need it :wink: As for the 3 button keypress issue, I'll play around and see if it seems to be PB or not...

Posted: Sun Aug 03, 2003 12:40 am
by Kevin
This looks great hypervox, just the tutorials I wanted... well almost!

Will you be doing any more focusing on platforms - simple jumping and landing stuff?


Great work and much appreciated. thanks.



kev.

Posted: Sun Aug 03, 2003 10:14 pm
by hypervox
This is the current list of tutorials I'm planning....

Space Invaders Clone (basic shoot-em-ups)
  • More about sounds sprites and animation
    Basic Collisions
    Multiple Enemies, and basic AI
title to be announced (Platforms)
  • Tiles
When the 3D engine has collision detection, I may do some tutorials based on that. If you want to see a particular area covered, I'm open to suggestions :wink:

Sounds great!

Posted: Mon Aug 04, 2003 9:03 pm
by LJ
Sounds great!! Can't wait! And thank you for postponing a 3D game tutorial until some things get worked out in the 3D engine. For some of us, none of the 3D commands work because of the InitEngine3D() not initializing bug.

I've got Maya Complete for creating awesome 3D graphics and so if you need some cheezy, amateur, your dog could draw better than me, 3D models for a future tutorial, let me know. This gives me a reason to dust off the old Maya book and practice modeling again.

Posted: Mon Aug 04, 2003 10:13 pm
by hypervox
:lol:

I'm a blender / truespace man myself, but I know what you mean... Most of the model I create look almost neolithic!

RTS

Posted: Mon Aug 04, 2003 10:14 pm
by Archaikz
Hey hypervox thanks for the previous tutorials. I would really like to see rts(games like starcraft) style movement and such. Such as selecting units,moving them. Just a suggestion. thanks

Blender and TrueSpace

Posted: Tue Aug 05, 2003 5:55 am
by LJ
@Hypervox,

Blender and TrueSpace!!! Those were the best in the day. Especially Blender! I loved that little program. I liked the marketing literature of the company also, Not a Number (NaN), that was the bomb. Too bad development stopped on it. Last I heard some users were trying to pool together some $$ to pay the author (I'm thinking he was the brains behind Neo-Geo or something??) to keep going. I couldn't believe that Blender was free for so long, very incredible.

TrueSpace, I had that also. I liked it alot, have nothing bad to say about it and the price was very reasonable also. What version of TrueSpace do you have? How is that company doing by the way? I sorta broke off from TrueSpace about 5 years ago.

Posted: Tue Aug 05, 2003 4:48 pm
by hypervox
Blender is now open source, and at version 2.28 (2.25 if you want the game engine) - it's also completly free - my personal favourite (especially as it can export to ogre format with the right scripts :) I'd recommend this to anyone designing models at the moment.

I have truespace 4.3, but it's at 6.5 i think.

Posted: Tue Aug 05, 2003 5:41 pm
by gas01ine
I really like the tutorials on hypervox' site :) downloaded all of 'em :D

I am doing a platformer right now and got stuck so maybe someone here can help me:
It's this darn gavity-thing! It's harder than i thought, I ended up with an in-depht physics book yesterday... lol
But how would you code that if Mario(r) drops he would do that physically correct?

I solved the jumping somehow with a sin() and it looks and "feels" quite well but dropping is impossible for me... any suggestions? (or even a tutorial...?!?)

Thanx in advance

Hmmmm....

Posted: Wed Aug 06, 2003 5:38 am
by LJ
Check this out and tell me what you think:

http://www.tokamakphysics.com/

"The Tokamak API exposes two different type of classes. The Interface class are classes of object which must be created and freed through a Tokamak API function, or member function of a Tokamak class. The other, Peripheral classes, are ordinary C++ class which are completely manage by the users.

Note: The Tokamak API is written in C++ and object-oriented design. But it keeps the use of virtual class functions to very minimum. Internally, there are no virtual functions at all. The rationale behind this design decision is that on some platform, virtual function calls are very costly, due to limited cache space. Thus Tokamak does not suffer from virtual function call overhead."

Best of all, it's free. Looks awesome. Maybe we can get one of the PureBasic Guru's to check out this web site and show us how to call the Tokamak API commands from inside Purebasic. :P

Lj

Hypervox

Posted: Sun Aug 24, 2003 8:01 am
by LJ
Hypervox,

I'm needing my bi-weekly fix to your addictive game tutorials. How's your next one coming along? Great stuff.

Lj

Posted: Tue Aug 26, 2003 9:49 am
by hypervox
Sorry All, been on holiday :wink:

Posted: Wed Sep 24, 2003 9:10 pm
by blueznl
what site? i get an error that the page is down... :-(