Classic Pong v1.0

Miscellaneous Forums/Blitz Showcase/Classic Pong v1.0

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

I don't know if this is of any help, but I once wrote some code to get a (what I thought was a) sonic boom effect. It also looks a bit like a water ripple effect. It is done by using copyRect to copy a piece of the background to a spot a bit further on, giving the ripple effect. This is done for a number of spots that move outwards in a circle.

However I noticed in my own game that this effect is also quite heavy (probably because of all the calls to copyRect). (The trick is to make the objects move fast or restrict their life time, and especially their size.) Otherwise just see it as a funny demo. The code still needs a bitmap (with some image on it) as large as the screen ("somebitmap.png").

; "sonic boom" effect demo by Foppy

Const SCREENWIDTH  = 800
Const SCREENHEIGHT = 600

Graphics(SCREENWIDTH,SCREENHEIGHT)
SetBuffer(BackBuffer())

Global g_gfx_background = LoadImage("somebitmap.png")

; this is the distance used when copying from source to target location (see sonic draw function)
Const DISPLACEMENT = 4

;#####################################################################################################
; SONIC TYPE DEFINITION AND RELATED FUNCTIONS
;#####################################################################################################

Type SONIC
	Field f_x#
	Field f_y#
	Field f_size
	Field f_dx#
	Field f_dy#
	Field f_life
End Type

Function SONIC_launch (p_x#,p_y#,p_size,p_dx#,p_dy#,p_life)
	sonic.SONIC  = New SONIC
	
	sonic\f_x    = p_x
	sonic\f_y    = p_y
	sonic\f_size = p_size
	sonic\f_dx   = p_dx
	sonic\f_dy   = p_dy
	sonic\f_life = p_life
End Function

Function SONIC_actionAll ()
	For sonic.SONIC  = Each SONIC
		sonic\f_x    = sonic\f_x + sonic\f_dx
		sonic\f_y    = sonic\f_y + sonic\f_dy
		sonic\f_life = sonic\f_life - 1
		If (sonic\f_life <= 0) Then
			Delete(sonic)
		End If
	Next
End Function

Function SONIC_drawAll ()
	For sonic.SONIC = Each SONIC
	
		; using copyRect() we copy what is under the sonic object to a
		; spot a bit further away, in the direction the object is moving
		
		sourceX# = sonic\f_x
		sourceY# = sonic\f_y
		targetX# = sourceX + sonic\f_dx * DISPLACEMENT
		targetY# = sourceY + sonic\f_dy * DISPLACEMENT
		
		CopyRect(sourceX,sourceY,sonic\f_size,sonic\f_size,targetX,targetY)
	Next
End Function

Function SONIC_launchWave (p_centerX,p_centerY,p_count,p_size,p_life,p_speed#)
	x# = p_centerX - p_size/2.0
	y# = p_centerY - p_size/2.0
	
	; here we launch a collection of sonics from one location, each of them
	; moving in a different (calculated) direction, so that we get an outwards
	; moving circle
	
	For i = 1 To p_count
		angle# = 360.0/p_count * i
		dx#    = Cos(angle) * p_speed
		dy#    = Sin(angle) * p_speed
		
		SONIC_launch(x,y,p_size,dx,dy,p_life)
	Next
End Function

;#####################################################################################################
; MAIN LOOP
;#####################################################################################################

Color(255,255,255)

While(Not(KeyHit(1)))
	; move all "sonics"
	SONIC_actionAll()
	
	; check user input
	If (MouseHit(1)) Then
		; launch wave of "sonics" when left mouse button is hit, use random values
		SONIC_launchWave(MouseX(),MouseY(),Rand(100,200),Rand(5,10),Rand(20,50),Rnd(1,2))
	End If

	Cls
	
	; draw background
	DrawImage(g_gfx_background,0,0)
	
	; draw all "sonics"
	SONIC_drawAll()

	Plot(MouseX(),MouseY())

	Flip
Wend

Delete Each SONIC

End