Deleting Types.
Blitz3D Forums/Blitz3D Beginners Area/Deleting Types.
| ||
| Ok I've just made my Types for my BreakOut game. Got it to loop and create them. Type Box Field X,Y,Alive End Type ;Box For X = 30 To 60 Step 15 For Y = 15 To 150 Step 15 Box.Box = New Box Box\X = X Box\Y = Y Box\Alive = 1 Next Next Then display them. For Box.Box = Each Box ; Show Box If Box\Alive = 1 Then DrawImage BoxFN,Box\Y,Box\X Next But using this code I've created myself. When a Object is hit, instead of it removing the one it hit. It removes the last object from memory created. For Box.Box = Each Box If Box\Alive = 1 And Ball\Direction = 4 And Box\X + ImageHeight(BoxFN) + 1 > Ball\X Then Ball\Direction = 3 : Box\Alive = 0 ; Bottom If Box\Alive = 1 And Ball\Direction = 1 And Box\X + ImageHeight(BoxFN) + 1 > Ball\X Then Ball\Direction = 2 : Box\Alive = 0 ; Bottom Next How can I get it to remove the Object it hit. And not the last Object in the que?, Thank you all in advance. |
| ||
| Ok that seems confusing, and the code is getting rather large to just copy and paste it all. So if you all would find it easier for me to cut down the code let me know, and if the above was just dribble I'll reply with more explaining. :D |
| ||
For Box.Box = Each Box If Box\Alive = 1 And Ball\Direction = 4 And Box\X + ImageHeight(BoxFN) + 1 > Ball\X Then Ball\Direction = 3 : Box\Alive = 0 ; Bottom If Box\Alive = 1 And Ball\Direction = 1 And Box\X + ImageHeight(BoxFN) + 1 > Ball\X Then Ball\Direction = 2 : Box\Alive = 0 ; Bottom if Box\Alive=0 delete Box Next |
| ||
| Ok Ben thanks for that piece of code, but urm unfortunitly it doesn't seem to be working. Or I've done it wrong. :\ 1 = Box Exists, 0 = Box Doesn't Exist! Just incase. :) |
| ||
| Just copy and paste all you code. Can see whats wrong with it better then :) |
| ||
| Ok if you say so, lol: ; 1 = True : 0 = False. ; Setup Window. ;------------------------------------------------------------------------------------------------------- AppTitle "Cubies: BreakIn! (beta)" Local ScreenWidth = 180 Local ScreenHeight = 180 Graphics ScreenWidth,ScreenHeight,32,2 ; Load Images into Memory, assign Transparency. ;------------------------------------------------------------------------------------------------------- WideScreenFN = LoadImage("WideScreen.bmp") : MaskImage WideScreenFN,255,255,255 BoxFN = LoadImage("Box.bmp") : MaskImage BoxFN ,255,255,255 BallFN = LoadImage("Ball.bmp") : MaskImage BallFN,255,255,255 PadFN = LoadImage("Pad.bmp") : MaskImage PadFN ,255,255,255 ImageFont = LoadAnimImage("ImageFont.bmp",15,15,0,10) : MaskImage ImageFont,255,255,255 ; Objects/Values ;------------------------------------------------------------------------------------------------------- Type WideScreen Field X,Y End Type ;WideScreen For X = 0 To 165 Step 165 WideScreen.WideScreen = New WideScreen WideScreen\X = X WideScreen\Y = 0 Next Type Box Field X,Y,Alive End Type ;Box For X = 30 To 60 Step 15 For Y = 15 To 150 Step 15 Box.Box = New Box Box\X = X Box\Y = Y Box\Alive = 1 Next Next Type Ball Field X,Y,Direction,Speed End Type Ball.Ball = New Ball : Ball\X = 127 : Ball\Y = 90 : Ball\Direction = 4 : Ball\Speed = 1 ;Ball Type Pad Field X,Y End Type Pad.Pad = New Pad : Pad\X = 135 : Pad\Y = 67 ;Pad ; Local Variables. ;------------------------------------------------------------------------------------------------------- Local Score = 3000 Local Lives = 5 ; Buffer/Refresh Colour ;------------------------------------------------------------------------------------------------------- SetBuffer BackBuffer() ClsColor 250,250,250 ;------------------------------------------------------------------------------------------------------- ;------------------------------------------------------------------------------------------------------- While Not KeyDown(1) ; Calculate Ball Moving. If Ball\Direction = 1 Then Ball\Y = Ball\Y + Ball\Speed : Ball\X = Ball\X - Ball\Speed ; Top Right If Ball\Direction = 2 Then Ball\Y = Ball\Y + Ball\Speed : Ball\X = Ball\X + Ball\Speed ; Bottom Right If Ball\Direction = 3 Then Ball\Y = Ball\Y - Ball\Speed : Ball\X = Ball\X + Ball\Speed ; Bottom Left If Ball\Direction = 4 Then Ball\Y = Ball\Y - Ball\Speed : Ball\X = Ball\X - Ball\Speed ; Top Left ; Calculate Wall Collision Detection. If Ball\Direction = 1 And Ball\X < ImageHeight(WideScreenFN) + 1 Then Ball\Direction = 2 ; Top If Ball\Direction = 4 And Ball\X < ImageHeight(WideScreenFN) + 1 Then Ball\Direction = 3 ; Top If Ball\Direction = 1 And Ball\Y + ImageWidth(BallFN) > ScreenWidth - 1 Then Ball\Direction = 4 ; Right If Ball\Direction = 2 And Ball\Y + ImageWidth(BallFN) > ScreenWidth - 1 Then Ball\Direction = 3 ; Right If Ball\Direction = 2 And Ball\X + ImageHeight(BallFN) > ScreenHeight - ImageHeight(WideScreenFN) - 1 Then Ball\Direction = 1 ; Top If Ball\Direction = 3 And Ball\X + ImageHeight(BallFN) > ScreenHeight - ImageHeight(WideScreenFN) - 1 Then Ball\Direction = 4 ; Top If Ball\Direction = 4 And Ball\Y < 1 Then Ball\Direction = 1 ; Left If Ball\Direction = 3 And Ball\Y < 1 Then Ball\Direction = 2 ; Left ; Calculate Pad Collision Detection. If Ball\Direction = 2 And Ball\X + ImageHeight(BallFN) > Pad\X - 1 And Ball\Y + ImageWidth(BallFN) > Pad\Y And Ball\Y < Pad\Y + ImageWidth(PadFN) Then Ball\Direction = 1 ; Top If Ball\Direction = 3 And Ball\X + ImageHeight(BallFN) > Pad\X - 1 And Ball\Y + ImageWidth(BallFN) > Pad\Y And Ball\Y < Pad\Y + ImageWidth(PadFN) Then Ball\Direction = 4 ; Top For Box.Box = Each Box If Box\Alive = 1 And Ball\Direction = 4 And Box\X + ImageHeight(BoxFN) + 1 > Ball\X Then Ball\Direction = 3 : Box\Alive = 0 ; Bottom If Box\Alive = 1 And Ball\Direction = 1 And Box\X + ImageHeight(BoxFN) + 1 > Ball\X Then Ball\Direction = 2 : Box\Alive = 0 ; Bottom If Box\Alive = 0 Delete Box Next Cls ; Show Main Graphics. For WideScreen.WideScreen = Each WideScreen DrawImage WideScreenFN,WideScreen\Y,WideScreen\X Next ; Show WideScreen For Box.Box = Each Box ; Show Box If Box\Alive = 1 Then DrawImage BoxFN,Box\Y,Box\X Next DrawImage BallFN,Ball\Y,Ball\X ; Show Ball DrawImage PadFN,Pad\Y,Pad\X ; Show Pad ; Show Score. For A = 0 To 9 If Score1 = A Then DrawImage ImageFont,165,0,A If Score2 = A Then DrawImage ImageFont,150,0,A If Score3 = A Then DrawImage ImageFont,135,0,A If Score4 = A Then DrawImage ImageFont,120,0,A If Score5 = A Then DrawImage ImageFont,105,0,A Next ; Show Lives. For A = 0 To 9 If Lives1 = A Then DrawImage ImageFont,165,165,A If Lives2 = A Then DrawImage ImageFont,150,165,A Next Flip ; Move Pad Left:Right If KeyDown(203) And Pad\Y => 1 Then Pad\Y = Pad\Y - 1 ; Pad Left If KeyDown(205) And Pad\Y <= ScreenWidth - ImageWidth(PadFN) - 1 Then Pad\Y = Pad\Y + 1 ; Pad Right Wend End :\ |
| ||
| 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
|
| ||
| Hmm, your code is pretty hard to read because you've stuck lots of things on the one line :S I don't think i can see what's wrong tho.... But i do notice that your not checking for collisions between the ball and the box, your meerly comparing x and y co-ords. |
| ||
| Yeah, thats how I do my collision detection. By using the X and Y co-ords. Works perfect. But all I want to do is remove the box I hit rather than the last box created. How would I do that? |
| ||
if Box\Alive = 1 And Ball\Direction = 4 And Box\X + ImageHeight(BoxFN) + 1 > Ball\X Then Ball\Direction = 3 : Box\Alive = 0 ; Bottom The problem with this is any box that is beyond a certain x co-ord, will be deleted. You don't know which box your colliding with. |
| ||
| Yep, hence the post. So any suggestions? |
| ||
Try replacing the collision detection with thisFor Box.Box = Each Box If Box\Alive =1 Then If ImagesCollide(BallFN,Ball\Y,Ball\X,BoxFN,Box\Y,Box\X) Then Box\Alive=0 Select Ball\Direction Case 1: Ball\Direction = 4 Case 2: Ball\Direction = 3 Case 3: Ball\Direction = 2 Case 4: Ball\Direction = 1 End Select EndIf EndIf Next |
| ||
| Wow thats some nifty coding there Coorae, Cheers alot. :D |