Deleting Types.

Blitz3D Forums/Blitz3D Beginners Area/Deleting Types.

Here is a basic breakout game that does nothing but let you hit blocks and delete them. The blocks are made with types and shows you how to delete them.

You will need to substitute in some graphics or let me know where to e-mail them to you.

Global GRAPHWIDTH = 800
Global GRAPHHEIGHT = 600


Graphics GRAPHWIDTH,GRAPHHEIGHT
AutoMidHandle True
SetBuffer BackBuffer()

SeedRnd MilliSecs()

;load graphics
Global playerimage = LoadImage("gfx/player.bmp")   ;player paddle
Global playerimage1 = LoadImage("gfx/player1.bmp") ;alternate player image
Global ballimage = LoadImage("gfx/ball.bmp")       ;game ball
Global block1image = LoadImage("gfx/block1.bmp")   ;block type 1

;load sounds
Global ballmisssnd = LoadSound("sounds/ballmisssnd.wav")  ;if ball gets by the player sound
Global ballwallsnd = LoadSound("sounds/ballwallsnd.wav")  ;if ball hits the wall sound
Global ballpadsnd = LoadSound("sounds/ballpadsnd.wav")    ;if ball hits the paddle sound
Global blocksnd = LoadSound("sounds/blocksnd.wav")      ;if ball hits a block sound

;creates a player type
Type paddle
	Field x,y
End Type

;creates a ball type
Type ball
	Field x,y
End Type

;creates a block type
Type block
	Field x,y
End Type

;creates the player paddle in memory and sets coordinates to the bottom/middle of the screen
Global player.paddle = New paddle
player\x = GRAPHWIDTH/2
player\y = GRAPHHEIGHT-10

;creates a new ball at a semi-random starting loaction
Global gameball.ball = New ball
gameball\x = Rnd(50,250)
gameball\y = Rnd(200,230)

;offsets where we start drawing the blocks on the screen, 1st block starts at 100,100
xoffset = 100
yoffset = 100

For y=0 To 4
	For x=0 To 12
	b.block = New block
	b\x = x*51 + xoffset
	b\y = y*19 + yoffset
	Next
Next

Global plyrspeed = 7  ;sets how fast the player paddle will move
Global ballspeedx = 5 ;sets how fast ball moves in x direction
Global ballspeedy = 5 ;sets how fast ball moves in y direction
Global padfliptimer   ;sets how long the player paddle image changes when ball hits it
Global Plyr_Score = 0 ;keeps the players score

Const ESCKEY = 1, LEFTKEY = 203, RIGHTKEY = 205, PAUSE = 25  ;set up constants for control keys

;;;;;;;;;;;;;;;
;MAIN LOOP
;;;;;;;;;;;;;;;
While Not KeyDown(ESCKEY)

UpdatePaddle()  ;function to update player paddle position
UpdateBall()    ;function to update ball movement and collisions
DrawBlocks()    ;function to draw blocks
DrawHUD()       ;function to draw the HUD

Flip:Cls  ;flip to the backbuffer to display all graphics and clear the new backbuffer
Wend


Function DrawHUD()  ;draws the walls around the outside of the screen

	Color 32,5,175 ;set wall color
	Rect 0,0,GRAPHWIDTH,10  ;draw top wall
	Rect 0,0,10,GRAPHWIDTH  ;draw left wall
	Rect GRAPHWIDTH-10,0,10,GRAPHHEIGHT  ;draw right wall
	Color 255,0,0  ;make the score red
	Text 50,25,"Score: " + Plyr_Score  ;display the score in upper left corner

End Function

;;;;;;;;;;;;;;
;Function UpdatePaddle()
;moves player paddle, keeps it on the screen
;;;;;;;;;;;;;;
Function UpdatePaddle()

;sets player left and right boundries, makes adjusting graphics, walls and player wraparound easier
plyrbndleft = ImageWidth(playerimage)/2+10
plyrbndright = GRAPHWIDTH-ImageWidth(playerimage)/2-10

;if left arrow is pressed, move player left and keep it onscreen
If KeyDown(LEFTKEY)
	player\x = player\x - plyrspeed
	If player\x < plyrbndleft Then player\x = plyrbndleft
End If

;if right arrow is pressed, move player left and keep it onscreen
If KeyDown(RIGHTKEY)
	player\x = player\x + plyrspeed
	If player\x > plyrbndright Then player\x = plyrbndright
End If

	;if the padfliptimer (ball hits paddle) is true, draw alternate paddle or else draw normal paddle
If padfliptimer > MilliSecs()
	DrawImage playerimage1,player\x,player\y  ;draw the alternate player paddle
Else
	DrawImage playerimage,player\x,player\y  ;draw the player paddle
EndIf

End Function

;;;;;;;;;;;;;;;;;
;Function UpdateBall()
;Updtes ball position and checks for collisions
;;;;;;;;;;;;;;;;;
Function UpdateBall()

;sets ball left/right/top boundries
boundleft = 18
boundright = GRAPHWIDTH-18
boundtop = 18

;if the ball collides with a block, change ball direction, delete block, play sound, update score
For e.block = Each block
	If ImagesCollide(ballimage,gameball\x,gameball\y,0,block1image,e.block\x,e.block\y,0)
		ballspeedy =- ballspeedy
		Delete e.block
		PlaySound(blocksnd)
		Plyr_Score = Plyr_Score + 10
	EndIf
Next

;if the ball collides with the paddle, change ball direction, play sound and start padfliptimer
If ImagesCollide(playerimage,player\x,player\y,0,ballimage,gameball\x,gameball\y,0) 
	ballspeedy =- ballspeedy
	PlaySound(ballpadsnd)
	padfliptimer = MilliSecs() + 100
EndIf
	
;if the ball hits a wall, reverse direction and play bounce sound
If gameball\y < boundtop 
	ballspeedy =- ballspeedy
	PlaySound(ballwallsnd)
ElseIf gameball\x < boundleft Or gameball\x > boundright 
	ballspeedx =- ballspeedx
	PlaySound(ballwallsnd)
EndIf
	
;if the ball goes past player, reset it's speed and send a new ball out
If gameball\y > GRAPHHEIGHT 
	ballspeedx=5 ballspeedy=5 gameball\x=Rnd(50,250)  gameball\y=Rnd(200,230)
	Plyr_Score = Plyr_Score - 100  ;subtract 100 points for missing the ball
	If Plyr_Score < 0 Then Plyr_Score = 0 ;don't let score get below 0
	PlaySound(ballmisssnd)
EndIf
	
;update ball movement
gameball\x = gameball\x + ballspeedx
gameball\y = gameball\y + ballspeedy
	
DrawImage ballimage, gameball\x,gameball\y  ;draw the ball

End Function

;;;;;;;;;;;;;;;;;
;Function DrawBlocks()
;draws the blocks
;;;;;;;;;;;;;;;;;
Function DrawBlocks()

	;draws each block on the screen
	For newblk.block = Each block
		DrawImage block1image,newblk.block\x,newblk.block\y
	Next
	
End Function