|
|
|
|
|
|
|
|
|
|
Duck Shoot
|
By: Kevin Picone Added: March 24th, 2008
|
Category: All,Games,Demo,Beginners
|
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.
;*=-----------------------------------------------------------------------------=*
;
; >> 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())
Continue
endif
; If it's not dead yet, flash it
SpriteDrawMode Spr,2+4096
SpriteAlphaAddColour Spr,rndrgb()
endif
Next
endif
; Draw All of the Sprites to the Screen
DrawAllSprites
; Ask PB to calc the number of Ducks left in the Ducks() list
NumberOfDucks=GetListSize(Ducks())
; Draw the Players Score
CenterText 200,10,"Score:"+Digits$(Score,8)
; Draw the Number of the ducks left
CenterText 600,10,"Ducks:"+Digits$(NumberOfDucks,2)
; Refresh the display
Sync
; repeat the game loop until the number of ducks is bellow 1.
Until NumberOfDucks<1
;
xpos#=GetScreenWidth()/2
Ypos#=GetScreenHeight()*0.4
centertext Xpos#,Ypos#,"You Win"
centertext Xpos#,Ypos#+40,"Press Any Key To Play Again"
Sync
Waitkey
; Loop back to the DO statement to play the game again
loop
;*=-----------------------------------------------------------------------------=*
; >> Add Duck <<
;*=-----------------------------------------------------------------------------=*
Function AddDuck(Xpos#,Ypos#)
OldPos=GetListPos(Ducks())
Ducks= new tObject
; Create sprite
Spr=NewSprite(xpos#,Ypos#,DuckImage)
SpriteDrawMode Spr,2
Ducks.Sprite=Spr
Ducks.HitPoints=1
SetListPos Ducks(),OldPos
EndFunction
;*=-----------------------------------------------------------------------------=*
; >> Kill (a duck) <<
;*=-----------------------------------------------------------------------------=*
Function Kill(Me.tobject)
if GetSpriteStatus(me.sprite)
DeleteSprite Me.Sprite
endif
; Kill this link in the list
me=Null
EndFunction
;*=-----------------------------------------------------------------------------=*
; >> Make Duck Image <<
;*=-----------------------------------------------------------------------------=*
; This function creates an Image that looks like a duck
;*=-----------------------------------------------------------------------------=*
Function MakeDuckImage()
cls rgb(0,0,0)
Yellow=Rgb(255,255,0)
xpos#=100
Ypos#=100
Width#=45
; Ducks Beck
Tric Xpos#-40,Ypos#-30,Xpos#-60,Ypos#-25,Xpos#-40,ypos#-20,rgb(255,110,100)
; Ducks Body
EllipseC xpos#,Ypos#,Width#,25,true,Yellow
; Ducks Head
EllipseC xpos#-28,ypos#-28,Width#/3,11,true,Yellow
; Ducks eye
EllipseC xpos#-30,ypos#-30,4,3,true,222
Dotc xpos#-32,ypos#-30,rgb(100,100,100)
Dotc xpos#-31,ypos#-30,rgb(200,200,200)
ThisImage=NewFXimage(110,100)
; grab this section of the screen and copy to our image
GetImage THisImage,40,40,150,140
; remove the alpha channel
rgbmaskimage ThisImage,rgb(255,255,255)
EndFunction ThisIMage
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.
|
|
|
Download:
Login to Download
|
|
Release Type:
The source code & tutorials found on this site are released as license ware for PlayBasic Users. No Person or Company may redistribute any file (tutorial / source code or media files) from this site, without explicit written permission.
|
|
|
|
|
|
|
|
|
|
|
|