This is a small collection of mostly game example source codes. These source codes are made available to help PlayBasic programmers kick start their game programming journey.
Looking for more source code / tutorials & media, then remember to visit the PlayBasic Resource board on our forums.
The aim of this sample is build some simple 'run away from the player' styled behavior into our dumb game actors, which are represented by simple blobs.
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:
;*=-----------------------------------------------------------------------------=* ;; >> 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 lessSetFps60; Create the Images Media ManuallyGlobal 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 SupportDim 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#=100For Xpos#=0to DuckRowWidth-1Step DuckWidth
AddDuck(Xpos#,Ypos#)nextRepeat; Clear the Screen
C1=rgb(10,0,100)
C2=rgb(150,50,200)ShadeBox0,0,GetScreenWidth(),GetScreenHeight(),C1,c1,c2,c2
; ----------------------------------------------------; Move all the Ducks to the left; ----------------------------------------------------
DuckSpeed=10-GetLIstSize(Ducks())ForEach 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 aroundif(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 positionMOveSpriteX Spr,DuckRowWidth
endifnext; ----------------------------------------------------; Player; ----------------------------------------------------; Check if the player has fired ?ifMouseButton()=1and 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 spritesForEach Ducks(); Get this ducks sprite
Spr=Ducks.Sprite
; Did the mouse coordinate hit this sprite ? ifpointHitSpritePixels(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())Continueendif; If it's not dead yet, flash itSpriteDrawMode Spr,2+4096SpriteAlphaAddColour Spr,rndrgb()endifNextendif; Draw All of the Sprites to the ScreenDrawAllSprites; Ask PB to calc the number of Ducks left in the Ducks() list
NumberOfDucks=GetListSize(Ducks()); Draw the Players ScoreCenterText200,10,"Score:"+Digits$(Score,8); Draw the Number of the ducks leftCenterText600,10,"Ducks:"+Digits$(NumberOfDucks,2); Refresh the displaySync; repeat the game loop until the number of ducks is bellow 1. Until NumberOfDucks<1;
xpos#=GetScreenWidth()/2
Ypos#=GetScreenHeight()*0.4centertext Xpos#,Ypos#,"You Win"centertext Xpos#,Ypos#+40,"Press Any Key To Play Again"SyncWaitkey; Loop back to the DO statement to play the game againloop;*=-----------------------------------------------------------------------------=* ; >> 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=1SetListPos Ducks(),OldPos
EndFunction;*=-----------------------------------------------------------------------------=* ; >> Kill (a duck) <<;*=-----------------------------------------------------------------------------=* Function Kill(Me.tobject)ifGetSpriteStatus(me.sprite)DeleteSprite Me.Sprite
endif; Kill this link in the list
me=NullEndFunction;*=-----------------------------------------------------------------------------=* ; >> Make Duck Image <<;*=-----------------------------------------------------------------------------=* ; This function creates an Image that looks like a duck ;*=-----------------------------------------------------------------------------=* Function MakeDuckImage()clsrgb(0,0,0)
Yellow=Rgb(255,255,0)
xpos#=100
Ypos#=100
Width#=45; Ducks BeckTric Xpos#-40,Ypos#-30,Xpos#-60,Ypos#-25,Xpos#-40,ypos#-20,rgb(255,110,100); Ducks BodyEllipseC xpos#,Ypos#,Width#,25,true,Yellow
; Ducks HeadEllipseC xpos#-28,ypos#-28,Width#/3,11,true,Yellow
; Ducks eyeEllipseC xpos#-30,ypos#-30,4,3,true,222Dotc 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.
[INDENT]Thesius XIII is one of the more recent example games and is shipped as part of the PlayBasic package. The game is an unofficial follow up to a game I wrote on the Amiga called Thesius XII.
[BR][BR]
A (very) simple torch flame demo. Shows 100 flaming torches on screen. As you can see by my screenshot, I was able to run this at 450+FPS.
This is a simplified version of my flame routine in my Sorcery game. It's not doing anything big or clever, and it's not particularly realistic, but it gives a pretty good impression of a fire (changing the colours in the animation strip might make it look more realistic, but I needed a cartoon-styled flame to fit my game).
This example creates a sinus bob chain (image for you PC folks ) , as seen in countless old school demo's. The demo keeps adding bobs until either the Frame drops bellow 30, or the max of 1000 is reached.
In 800*600*16 (full screen) my 800mhz Duron machine can push 900 odd bobs.
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.