I am making a Tetris like game in order to learn Blitz Basic - this is my first real attempt so please forgive the n00b code.
I have got quite a long way (although my code is very messy). I am having problems with my shapes - they are made up of 4 cubes with 1 as the parent entity. If a non-parent cube hits a wall then it goes behind another one of the cubes. If I put collision detection on the actual cubes to prevent this, it has strange effects on my shape as it moves.
Take a look - you'll quickly see what I mean.
Any suggestions on how to fix this most appreciated. Also suggestions on better ways to do things would be useful.
Thanks, here's my code:
[CODE]
;;;;;;;;;;;;;;;;;;;;
; ;
; Main starts here ;
; ;
;;;;;;;;;;;;;;;;;;;;
Graphics3D 800,600
SetBuffer BackBuffer()
Camera = CreateCamera()
CameraViewport Camera,0,0,GraphicsWidth(),GraphicsHeight()
Light = CreateLight()
Dim Block(4)
Block(1) = InitGame()
SeedRnd = MilliSecs()
ShapeType = Rnd(7)
Block(2) = NewShape(ShapeType,2)
Block(3) = NewShape(ShapeType,3)
Block(4) = NewShape(ShapeType,4)
Timer = CreateTimer(5)
Dim Grid(11,20)
While Not KeyHit(1)
Frames = WaitTimer(Timer)
For k = 1 To Frames
x = 0
y = -2
StopCollision = 0
Collision = 0
LCollision = 0
RCollision = 0
For c = 1 To 4
If EntityCollided(Block(c),1)
Collision = 1
If CollisionNX(Block(c),1) > 0
LCollision = 1
End If
If CollisionNX(Block(c),1) < 0
RCollision = 1
End If
If CollisionNY(Block(c),1) = 1
StopCollision = 1
End If
End If
Next
If KeyHit(203)
If LCollision = 0
x = -2
End If
End If
If KeyHit(205)
If RCollision = 0
x = 2
End If
End If
;If KeyHit(200)
; TurnEntity Block(1),0,0,90
; For a = 1 To 4
; TurnBack = 0
; If EntityCollided(Block(a),1)
; TurnBack = 1
; End If
; If TurnBack = 1
; TurnEntity Block(1),0,0,-90
; End If
; Next
;End If
If StopCollision = 1
For a = 1 To 4
If Not a = 1
AdjX = EntityX(Block(1)) + EntityX(Block(a))
AdjY = EntityY(Block(1)) + EntityY(Block(a))
Else
AdjX = EntityX(Block(1))
AdjY = EntityY(Block(1))
End If
ValX = AdjX/2 + 6
ValY = AdjY/2 + 10
Grid(ValX,ValY) = PlaceBlock()
PositionEntity Grid(ValX,ValY),AdjX,AdjY,30
EntityType Grid(ValX,ValY),1
Next
FreeEntity Block(1)
For r = 1 To 20
Full = 1
For c = 1 To 11
If Grid(c,r) = 0
Full = 0
End If
Next
If Full = 1
For c = 1 To 11
FreeEntity Grid(c,r)
Grid(c,r) = 0
Next
For s = r + 1 To 20
For c = 1 To 11
If Not Grid(c,s) = 0
TranslateEntity Grid(c,s),0,-2,0
Grid(c,s-1) = Grid(c,s)
Grid(c,s) = 0
End If
Next
Next
End If
Next
Block(1) = NewBlock()
ShapeType = Rnd(7)
Block(2) = NewShape(ShapeType,2)
Block(3) = NewShape(ShapeType,3)
Block(4) = NewShape(ShapeType,4)
Else
TranslateEntity Block(1),x,y,0
End If
UpdateWorld
Next
RenderWorld
; o = 10
; q = 10
;
; For r = 1 To 20
;
; For c = 1 To 11
;
; If Grid(c,r) = 0
;
; Text p,q,"0"
;
; Else
;
; Text p,q,"X"
;
; End If
;
; p = p + 10
;
; Next
;
; q = q + 10
;
; p = 10
;
; Next
Flip
Wend
;;;;;;;;;;;;;;;;;;;;
; ;
; Main ends here ;
; ;
;;;;;;;;;;;;;;;;;;;;
Function InitGame()
For a = 0 To 20 Step 2
x = -10 + a
BottomWall = CreateCube()
PositionEntity BottomWall,x,-20,30
EntityType BottomWall,1
Next
For a = 0 To 40 Step 2
y = 20 - a
LeftWall = CreateCube()
PositionEntity LeftWall,-12,y,30
EntityType LeftWall,1
RightWall = CreateCube()
PositionEntity RightWall,12,y,30
EntityType RightWall,1
Next
aBlock = NewBlock()
Return aBlock
End Function
Function NewShape(ShapeType,BlockNo)
aBlock = CreateCube(Block(1))
EntityRadius aBlock,0.5
EntityType aBlock,2
EntityColor aBlock,100,100,100
Select ShapeType
Case 1
Select BlockNo
Case 2
PositionEntity aBlock,0,2,0
Case 3
PositionEntity aBlock,0,4,0
Case 4
PositionEntity aBlock,0,6,0
End Select
Case 2
Select BlockNo
Case 2
PositionEntity aBlock,2,0,0
Case 3
PositionEntity aBlock,0,2,0
Case 4
PositionEntity aBlock,0,4,0
End Select
Case 3
Select BlockNo
Case 2
PositionEntity aBlock,-2,0,0
Case 3
PositionEntity aBlock,0,2,0
Case 4
PositionEntity aBlock,0,4,0
End Select
Case 4
Select BlockNo
Case 2
PositionEntity aBlock,0,2,0
Case 3
PositionEntity aBlock,0,4,0
Case 4
PositionEntity aBlock,2,2,0
End Select
Case 5
Select BlockNo
Case 2
PositionEntity aBlock,0,2,0
Case 3
PositionEntity aBlock,2,2,0
Case 4
PositionEntity aBlock,2,0,0
End Select
Case 6
Select BlockNo
Case 2
PositionEntity aBlock,2,0,0
Case 3
PositionEntity aBlock,2,2,0
Case 4
PositionEntity aBlock,4,2,0
End Select
Case 7
Select BlockNo
Case 2
PositionEntity aBlock,0,2,0
Case 3
PositionEntity aBlock,-2,2,0
Case 4
PositionEntity aBlock,2,0,0
End Select
End Select
Collisions 2,1,1,1
Return aBlock
End Function
Function NewBlock()
aBlock = CreateCube()
EntityRadius aBlock,0.5
PositionEntity aBlock,0,22,30
EntityType aBlock,2
EntityColor aBlock,150,100,100
Return aBlock
End Function
Function PlaceBlock()
aBlock = CreateCube()
EntityRadius aBlock,0.5
EntityType aBlock,1
EntityColor aBlock,100,100,100
Return aBlock
End Function
[/CODE]
I have got quite a long way (although my code is very messy). I am having problems with my shapes - they are made up of 4 cubes with 1 as the parent entity. If a non-parent cube hits a wall then it goes behind another one of the cubes. If I put collision detection on the actual cubes to prevent this, it has strange effects on my shape as it moves.
Take a look - you'll quickly see what I mean.
Any suggestions on how to fix this most appreciated. Also suggestions on better ways to do things would be useful.
Thanks, here's my code:
[CODE]
;;;;;;;;;;;;;;;;;;;;
; ;
; Main starts here ;
; ;
;;;;;;;;;;;;;;;;;;;;
Graphics3D 800,600
SetBuffer BackBuffer()
Camera = CreateCamera()
CameraViewport Camera,0,0,GraphicsWidth(),GraphicsHeight()
Light = CreateLight()
Dim Block(4)
Block(1) = InitGame()
SeedRnd = MilliSecs()
ShapeType = Rnd(7)
Block(2) = NewShape(ShapeType,2)
Block(3) = NewShape(ShapeType,3)
Block(4) = NewShape(ShapeType,4)
Timer = CreateTimer(5)
Dim Grid(11,20)
While Not KeyHit(1)
Frames = WaitTimer(Timer)
For k = 1 To Frames
x = 0
y = -2
StopCollision = 0
Collision = 0
LCollision = 0
RCollision = 0
For c = 1 To 4
If EntityCollided(Block(c),1)
Collision = 1
If CollisionNX(Block(c),1) > 0
LCollision = 1
End If
If CollisionNX(Block(c),1) < 0
RCollision = 1
End If
If CollisionNY(Block(c),1) = 1
StopCollision = 1
End If
End If
Next
If KeyHit(203)
If LCollision = 0
x = -2
End If
End If
If KeyHit(205)
If RCollision = 0
x = 2
End If
End If
;If KeyHit(200)
; TurnEntity Block(1),0,0,90
; For a = 1 To 4
; TurnBack = 0
; If EntityCollided(Block(a),1)
; TurnBack = 1
; End If
; If TurnBack = 1
; TurnEntity Block(1),0,0,-90
; End If
; Next
;End If
If StopCollision = 1
For a = 1 To 4
If Not a = 1
AdjX = EntityX(Block(1)) + EntityX(Block(a))
AdjY = EntityY(Block(1)) + EntityY(Block(a))
Else
AdjX = EntityX(Block(1))
AdjY = EntityY(Block(1))
End If
ValX = AdjX/2 + 6
ValY = AdjY/2 + 10
Grid(ValX,ValY) = PlaceBlock()
PositionEntity Grid(ValX,ValY),AdjX,AdjY,30
EntityType Grid(ValX,ValY),1
Next
FreeEntity Block(1)
For r = 1 To 20
Full = 1
For c = 1 To 11
If Grid(c,r) = 0
Full = 0
End If
Next
If Full = 1
For c = 1 To 11
FreeEntity Grid(c,r)
Grid(c,r) = 0
Next
For s = r + 1 To 20
For c = 1 To 11
If Not Grid(c,s) = 0
TranslateEntity Grid(c,s),0,-2,0
Grid(c,s-1) = Grid(c,s)
Grid(c,s) = 0
End If
Next
Next
End If
Next
Block(1) = NewBlock()
ShapeType = Rnd(7)
Block(2) = NewShape(ShapeType,2)
Block(3) = NewShape(ShapeType,3)
Block(4) = NewShape(ShapeType,4)
Else
TranslateEntity Block(1),x,y,0
End If
UpdateWorld
Next
RenderWorld
; o = 10
; q = 10
;
; For r = 1 To 20
;
; For c = 1 To 11
;
; If Grid(c,r) = 0
;
; Text p,q,"0"
;
; Else
;
; Text p,q,"X"
;
; End If
;
; p = p + 10
;
; Next
;
; q = q + 10
;
; p = 10
;
; Next
Flip
Wend
;;;;;;;;;;;;;;;;;;;;
; ;
; Main ends here ;
; ;
;;;;;;;;;;;;;;;;;;;;
Function InitGame()
For a = 0 To 20 Step 2
x = -10 + a
BottomWall = CreateCube()
PositionEntity BottomWall,x,-20,30
EntityType BottomWall,1
Next
For a = 0 To 40 Step 2
y = 20 - a
LeftWall = CreateCube()
PositionEntity LeftWall,-12,y,30
EntityType LeftWall,1
RightWall = CreateCube()
PositionEntity RightWall,12,y,30
EntityType RightWall,1
Next
aBlock = NewBlock()
Return aBlock
End Function
Function NewShape(ShapeType,BlockNo)
aBlock = CreateCube(Block(1))
EntityRadius aBlock,0.5
EntityType aBlock,2
EntityColor aBlock,100,100,100
Select ShapeType
Case 1
Select BlockNo
Case 2
PositionEntity aBlock,0,2,0
Case 3
PositionEntity aBlock,0,4,0
Case 4
PositionEntity aBlock,0,6,0
End Select
Case 2
Select BlockNo
Case 2
PositionEntity aBlock,2,0,0
Case 3
PositionEntity aBlock,0,2,0
Case 4
PositionEntity aBlock,0,4,0
End Select
Case 3
Select BlockNo
Case 2
PositionEntity aBlock,-2,0,0
Case 3
PositionEntity aBlock,0,2,0
Case 4
PositionEntity aBlock,0,4,0
End Select
Case 4
Select BlockNo
Case 2
PositionEntity aBlock,0,2,0
Case 3
PositionEntity aBlock,0,4,0
Case 4
PositionEntity aBlock,2,2,0
End Select
Case 5
Select BlockNo
Case 2
PositionEntity aBlock,0,2,0
Case 3
PositionEntity aBlock,2,2,0
Case 4
PositionEntity aBlock,2,0,0
End Select
Case 6
Select BlockNo
Case 2
PositionEntity aBlock,2,0,0
Case 3
PositionEntity aBlock,2,2,0
Case 4
PositionEntity aBlock,4,2,0
End Select
Case 7
Select BlockNo
Case 2
PositionEntity aBlock,0,2,0
Case 3
PositionEntity aBlock,-2,2,0
Case 4
PositionEntity aBlock,2,0,0
End Select
End Select
Collisions 2,1,1,1
Return aBlock
End Function
Function NewBlock()
aBlock = CreateCube()
EntityRadius aBlock,0.5
PositionEntity aBlock,0,22,30
EntityType aBlock,2
EntityColor aBlock,150,100,100
Return aBlock
End Function
Function PlaceBlock()
aBlock = CreateCube()
EntityRadius aBlock,0.5
EntityType aBlock,1
EntityColor aBlock,100,100,100
Return aBlock
End Function
[/CODE]