EXAMPLE SOURCE CODES


     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.

Found #39 items in Games category

 Play Frogger

By: Kevin Picone Added: November 3rd, 2008

Category: All,Games,Beginners


This is a little remake of the classic coin-op video game FROGGER. We wrote the code purely as an example for a fellow PB forum member (in a few hours) so while it's not a complete remake of the original game, it does contain all of the basic mechanics of the game. At least from what I can remember, which isn't much.



Download: Login to Download




 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.



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 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




 High Score Library

By: Kevin Picone Added: December 7th, 2007

Category: All,Libraries,Games


This is library that can help you manage a high score table.
Download: Login to Download




 Perspective Platformer

By: Kevin Picone Added: November 19th, 2007

Category: All,Games


Perspective Platformer Demo (Using Perspective Sprites)

This is the standard platform demo from way back converted to use PB1.71c's perspective sprite rendering. Which is a feature of PB1.7x editions of PlayBasic. Perspective rendering has various rotation modes. In this one (perspective 2D), it will render the sprite scene down the z axis. So the camera can zoom in/out and spin on the Z axis but not on the X/Y axis. There's other rotation modes (i.e. full 3D) but this one is a lot simpler for 2D game scenes (no axis flipping! ) and therefore a lot more plug and play friendly!

Download

Get Perspective Platfomer Source Code


Video

This is a little 30second video capture (3.4 meg - DivX5.1) of the perspective platformer demo running in PB1.71. The demo includes 7 backdrop layers, particles, pixel perfect collision, alpha overlays etc All drawn from a top down perspective.

Download


Download: Login to Download




 Thesius XIII - Forest Blast

By: Kevin Picone Added: May 6th, 2007

Category: All,Games, 2.5D, Demo,Alpha Blending,Effects


[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]

Download: Login to Download




 Basic Text Adventure Framework

By: Kevin Picone Added: February 6th, 2007

Category: All,Games,Strings,AI

This example isn't a fully functional adventure game engine, but does include the bare bones of a user command parser, plus a few extras like an inventory and the start of the location system. While it's not finished, I'm not sure when i'll be able to find time to really write something decent. So feel free to continue it. Have fun !
Download: Login to Download




 Scramble / Terrain

By: Kevin Picone Added: November 13th, 2006

Category: All,Games,Scrolling


The idea with this snippet was to create an infinite scrolling (cough) landscape (cough) and have a ship fly along and blast and bomb it. So that's about all it does. But it looks pretty enough. Have Fun !

Requires: PB1.56 or higher


Download: Login to Download




 Carambole Billiards

By: Stef Added: August 17th, 2006

Category: All,Games


Simple Billiards simulation

Carambole Type (Carom)

The rules (simple):

With the your cue-ball (allways the white one) you must hit both other balls (yellow and red)

(Normally you play "Three cushion billiard"

Left-click the white ball (must stand still), move around the mouse - the cue-stick will appear - choose distance (the higher the more power) - aim - left-click again.

stef


Download: Login to Download




Viewing Page [3] of [5]



Want More Source Codes?:



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.

 

 
     
 
       

(c) Copyright 2002 / 2024 Kevin Picone , UnderwareDesign.com  - Privacy Policy   Site: V0.99a [Alpha]