Duck Shoot (Shooting Gallery)

Started by kevin, March 24, 2008, 12:39:16 AM

Previous topic - Next topic

kevin

 Duck Shoot

    This example is a simple shooting gallery. The game displays a row of ducks (moving right to left) and all the player has to do is pick them off with the mouse. Not very exciting or visually appealing, but does show the basic mechanics of a game.



PlayBASIC Code: [Select]
;*=-----------------------------------------------------------------------------=*   
;
; >> Duck Shoot (sitting ducks) <<
;
; By Kevin Picone
;
; Built Using PlayBasic V1.62/V1.63.
;
; Copyright 2008 by Kevin Picone All Rights Reserved.
;
;*=-----------------------------------------------------------------------------=*
;
; About:
; ======
;
; This example is a simple shooting gallery. The game displays a row of
; ducks (moving right to left) and all the player has to do is pick them off
; with the mouse. Not very exciting or visually appealing, but does show the
; basic mechanics of a game.
;
;
; Controls:
; ========
;
; Mouse = Aim / Fire
; ESC = EXIT
;
;
;*=-----------------------------------------------------------------------------=*



; Tell PB to the limit this program to 60 frames per second or less
SetFps 60


; Create the Images Media Manually
Global DuckImage=MakeDuckImage()

; Get the Width of the Duck Image
DuckWidth=GetImageWidth(DuckIMage)



; Define our Object type.
Type tObject
Sprite ; the Sprite this object uses
HitPoints ; The Number of Hit Points this object has
EndType

; decalre the Variables Ducks() with Linked List Support
Dim Ducks as tObject List




;*=-----------------------------------------------------------------------------=*
; >> Main Loop <<
;*=-----------------------------------------------------------------------------=*


; Start program Main Loop.
Do


;Reset Players Score
Score=0

; Spawn a row of Ducks to shoot

DuckRowWidth=10*DuckWidth

Ypos#=100
For Xpos#=0 to DuckRowWidth-1 Step DuckWidth
AddDuck(Xpos#,Ypos#)
next


Repeat

; Clear the Screen
C1=rgb(10,0,100)
C2=rgb(150,50,200)
ShadeBox 0,0,GetScreenWidth(),GetScreenHeight(),C1,c1,c2,c2


; ----------------------------------------------------
; Move all the Ducks to the left
; ----------------------------------------------------

DuckSpeed=10-GetLIstSize(Ducks())

For Each Ducks()

Spr=Ducks.Sprite
SpriteDrawMode Spr,2

; Move this duck sprite to the left
MoveSpriteX spr,-(4+DuckSpeed)

; check if the sprite needs to be reset to the right hand side
; so that it wraps around
if (getSpriteX(spr)+DuckWidth)<0
; if the right hand side of the sprite is off
; the screen, then we move the sprite to it's far right position
MOveSpriteX Spr,DuckRowWidth
endif

next


; ----------------------------------------------------
; Player
; ----------------------------------------------------

; Check if the player has fired ?
if MouseButton()=1 and LastShotTime<Timer()

; Set the last shot time to 250 milliseconds ahead
; this limit the player to 4 shots per second
LastShotTime=Timer()+250

; Use the mouses Position as the gun sight (so it's almost impossible to miss ;) )

mx=mousex()
my=mousey()

; Check for a Pixel impact between the mouse position and any of our ducks sprites
For Each Ducks()

; Get this ducks sprite
Spr=Ducks.Sprite

; Did the mouse coordinate hit this sprite ?
if pointHitSpritePixels(mx,my,Spr,1)=true

; if so, subtract one from this ducks hit point counter
Ducks.Hitpoints=Ducks.Hitpoints-1
; Check if the hitspoints are bellow 1.
if Ducks.Hitpoints<1

; Add some score to the player
Score=Score+1000

; now since this duck is dead, so lets remove it from list
; and continue on with this loop
Kill(Ducks())
Login required to view complete source code



   Outline of how this PlayBASIC code works:


   The program begins by setting the frame rate to 60 frames per second using the SetFps function. This determines how often the program updates and redraws the screen.

   Next, the program creates an image for the ducks using the MakeDuckImage function. This function creates and returns an image that can be used to display the ducks on the screen.

   The program then gets the width of the duck image using the GetImageWidth function. This value will be used later to position the ducks on the screen.

   The program then defines a custom data type called tObject, which represents an object in the game (in this case, a duck). The tObject type has two fields: Sprite, which is a handle to the sprite that represents the object on the screen, and HitPoints, which is the number of hit points that the object has.

   The program then declares a variable called Ducks as a list of tObjects, using the List keyword. This will be used to store all of the ducks in the game.

   The program enters the main game loop, which will run until the player exits the game.

   Inside the main game loop, the program resets the player's score to 0 using the Score variable.

   The program then creates a row of ducks to shoot at. It does this by looping through a range of X positions on the screen, spaced apart by the width of the duck image. For each X position, the program calls the AddDuck function to create a new duck at that position. The AddDuck function creates a new tObject and adds it to the Ducks list.

   The program enters a second loop, which will run until all of the ducks have been shot.

   Inside the second loop, the program clears the screen using the ShadeBox function. This function fills the screen with a gradient of two colors.

   The program then moves all of the ducks to the left by a certain amount. It does this by looping through all of the ducks in the Ducks list and using the MoveSpriteX function to move the sprite for each duck to the left. If the duck's sprite has moved off the left side of the screen, the program moves the sprite back to the right side of the screen so that it appears to wrap around.


   The program checks to see if the player has fired their gun (by checking if the mouse button is down). If the player has fired, the program uses the CheckShot function to see if any of the ducks have been hit. The CheckShot function checks the mouse position against the position of each duck sprite, and if the mouse position is within the bounds of the duck sprite, it reduces the duck's hit points by 1 and returns true.

   If the CheckShot function returns true, the program increments the player's score by 1.

   The program then loops through all of the ducks in the Ducks list and draws them on the screen using the SpriteDraw function.

   The program then displays the player's score on the screen using the Print function.

   The program checks to see if the player has pressed the ESC key to exit the game. If the player has pressed ESC, the program breaks out of the second loop and returns to the main game loop.

   If the player has not pressed ESC, the program removes any ducks from the Ducks list that have 0 hit points. It does this by looping through the list and using the RemoveList function to remove each duck that has 0 hit points.

   If the Ducks list is empty (i.e. all of the ducks have been shot), the program breaks out of the second loop and returns to the main game loop.

   The main game loop then repeats. The program creates a new row of ducks and the player can shoot them again. This process continues until the player exits the game by pressing ESC.