Code: Select all
If KeyboardPushed(#PB_Key_Space)
If playcount = 0
PlaySound(#phaser)
playcount = 40
EndIf
EndIf
If playcount > 0
DEC playcount
EndIf
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