Posted: Sat Jan 07, 2017 2:27 pm Post subject: Problems with my tetris game

HI guys im making a tetris game but i dont know how to stack blocks on top of each other and avoid collision plz help.

Sponsor Sponsor

Insectoid

Posted: Sun Jan 08, 2017 3:18 am Post subject: RE:Problems with my tetris game

What have you tried so far?

jason98wang

Posted: Mon Jan 09, 2017 9:42 pm Post subject: RE:Problems with my tetris game

um... everything i tried using what dot colour but it doesn't seem to work. My main problem is how the blocks will stack on top of each other and how the blocks fall down again after a row is cleared

Insectoid

Posted: Tue Jan 10, 2017 10:23 am Post subject: RE:Problems with my tetris game

There are a number of collision detection tutorials on this site that you may want to take a look at. The Turing Walkthrough may be helpful.

I suggest you start small- try to make two squares stack on top of each other before worrying about complex Tetris shapes.

jason98wang

Posted: Tue Jan 10, 2017 11:00 am Post subject: RE:Problems with my tetris game

bro helo me lol

Insectoid

Posted: Tue Jan 10, 2017 2:13 pm Post subject: RE:Problems with my tetris game

I can't help you if you won't try.

jason98wang

Posted: Wed Jan 11, 2017 10:33 am Post subject: RE:Problems with my tetris game

im currently using what dot colour to do the program but im not sure how to detect the bottom of the block

jason98wang

Posted: Thu Jan 12, 2017 7:40 pm Post subject: RE:Problems with my tetris game

currently i have been able to move the blocks around but my problem is after the blocks move the previous one isnt deleted, is there a way i can fix that?

Insectoid

Posted: Fri Jan 13, 2017 10:52 am Post subject: RE:Problems with my tetris game

It's hard to tell you what the problem is without seeing your code.

jason98wang

Posted: Sun Jan 15, 2017 1:26 am Post subject: RE:Problems with my tetris game

setscreen ("graphics:300;600")
var x : int := 90

var y : int := 600 - 30
var position : int := 1
var randBlock : int
var x_Right, x_Left, y_Bottom1, y_Bottom2, num_blocks_per_row, highscore : int
var gameover, stop : boolean
drawfillbox(300,600,-300,0,black)

%naming variables for different shapes
%This is also setting up how to draw each shape in each position

%Checking if the any blocks has reached the top row
process endgame
for x1 : 0 .. 300
if whatdotcolor (x1, 600) not= black then
gameover := true
exit when gameover
end if
end for
end endgame

%clearing the rows after the whole row is filled
process filledrows
for y1 : 0 .. 600
num_blocks_per_row := 0
for x1 : 0 .. 300
if whatdotcolor (x1, y1) not= black then
num_blocks_per_row := num_blocks_per_row + 1
if num_blocks_per_row = 10 then
drawfillbox (x1, y1, x1 + 30, y1 - 30, black)
% highscore := highscore + 100
end if
end if
end for
end for
end filledrows

%I-Block
var Block0 := Pic.FileNew ("Block0.bmp")
var Block1 := Pic.FileNew ("Block1.bmp")
Block0 := Pic.Scale (Block0, 30, 120)
Block1 := Pic.Scale (Block1, 120, 30)
%rotations
procedure I_Block
if position = 1 then
Pic.Draw (Block1, x, y, picMerge)
x_Left := x
x_Right := x + 120
y_Bottom1 := y
elsif position = 2 then
Pic.Draw (Block0, x + 60, y - 90, picMerge)
x_Left := x
x_Right := x + 30
y_Bottom1 := y
elsif position = 3 then
Pic.Draw (Block1, x, y, picMerge)
x_Left := x
x_Right := x + 120
y_Bottom1 := y
elsif position = 4 then
Pic.Draw (Block0, x + 60, y - 90, picMerge)
x_Left := x
x_Right := x + 30
y_Bottom1 := y
end if
end I_Block

%L-Block
var Block2 := Pic.FileNew ("Block2.bmp")
var Block3 := Pic.FileNew ("Block3.bmp")
Block2 := Pic.Scale (Block2, 30, 60)
Block3 := Pic.Scale (Block3, 60, 30)
%rotations
procedure L_Block
if position = 1 then
Pic.Draw (Block2, x + 30, y - 30, picMerge)
Pic.Draw (Block3, x + 30, y - 60, picMerge)
x_Left := x
x_Right := x + 60
y_Bottom1 := 60
elsif position = 2 then
Pic.Draw (Block2, x, y - 30, picMerge)
Pic.Draw (Block3, x + 30, y, picMerge)
x_Left := x
x_Right := x + 90
y_Bottom1 := y - 30
y_Bottom2 := y
elsif position = 3 then
Pic.Draw (Block2, x + 30, y - 60, picMerge)
Pic.Draw (Block3, x, y, picMerge)
x_Left := x
x_Right := x + 30
y_Bottom1 := y - 60
y_Bottom2 := y
elsif position = 4 then
Pic.Draw (Block3, x, y - 30, picMerge)
Pic.Draw (Block2, x + 60, y - 30, picMerge)
x_Left := x
x_Right := x + 90
y_Bottom1 := y - 30
y_Bottom2 := y
end if
end L_Block

%O-Block
var Block4 := Pic.FileNew ("Block4.bmp")
Block4 := Pic.Scale (Block4, 60, 60)
procedure O_Block
if position < 5 then
Pic.Draw (Block4, x + 30, y - 30, picMerge)
x_Left := x
x_Right := x + 60
y_Bottom1 := y - 30
end if
end O_Block

%S-Block
var Block5 := Pic.FileNew ("Block5.bmp")
var Block6 := Pic.FileNew ("Block6.bmp")
Block5 := Pic.Scale (Block5, 60, 30)
Block6 := Pic.Scale (Block6, 30, 60)
%rotations
procedure S_Block
if position = 1 then
Pic.Draw (Block5, x, y, picMerge)
Pic.Draw (Block5, x + 30, y - 30, picMerge)
x_Left := x
x_Right := x + 90
y_Bottom1 := y - 30
y_Bottom2 := y
elsif position = 2 then
Pic.Draw (Block6, x + 60, y - 30, picMerge)
Pic.Draw (Block6, x + 30, y - 60, picMerge)
x_Left := x
x_Right := x + 60
y_Bottom1 := y - 60
y_Bottom2 := y
elsif position = 3 then
Pic.Draw (Block5, x, y, picMerge)
Pic.Draw (Block5, x + 30, y - 30, picMerge)
x_Left := x
x_Right := x + 90
y_Bottom1 := y - 30
y_Bottom2 := y
elsif position = 4 then
Pic.Draw (Block6, x + 60, y - 30, picMerge)
Pic.Draw (Block6, x + 30, y - 60, picMerge)
x_Left := x
x_Right := x + 60
y_Bottom1 := y - 60
y_Bottom2 := y
end if
end S_Block

%T-Block
var Block7 := Pic.FileNew ("Block7.bmp")
var Block8 := Pic.FileNew ("Block8.bmp")
var Block9 := Pic.FileNew ("Block9.bmp")
var Block9copy := Pic.FileNew ("Block9copy.bmp")
Block9copy := Pic.Scale (Block9copy, 30, 30)
Block7 := Pic.Scale (Block7, 90, 30)
Block8 := Pic.Scale (Block8, 30, 90)
Block9 := Pic.Scale (Block9, 30, 30)
%rotations
procedure T_Block
if position = 1 then
Pic.Draw (Block9, x + 30, y, picMerge)
Pic.Draw (Block7, x, y - 30, picMerge)
x_Left := x
x_Right := x + 90
y_Bottom1 := y
elsif position = 2 then
Pic.Draw (Block8, x + 30, y - 60, picMerge)
Pic.Draw (Block9copy, x + 60, y - 30, picMerge)
x_Left := x
x_Right := x + 30
y_Bottom1 := y - 60
y_Bottom2 := y - 30
elsif position = 3 then
Pic.Draw (Block7, x, y, picMerge)
Pic.Draw (Block9, x + 30, y - 30, picMerge)
x_Left := x
x_Right := x + 90
y_Bottom1 := y - 30
y_Bottom2 := y
elsif position = 4 then
Pic.Draw (Block8, x + 60, y - 60, picMerge)
Pic.Draw (Block9copy, x + 30, y - 30, picMerge)
x_Left := x
x_Right := x + 60
y_Bottom1 := y - 60
y_Bottom2 := y - 30
end if
end T_Block

%Z-Block
var Block10 := Pic.FileNew ("Block10.bmp")
var Block11 := Pic.FileNew ("Block11.bmp")
Block10 := Pic.Scale (Block10, 30, 60)
Block11 := Pic.Scale (Block11, 60, 30)
%rotations
procedure Z_Block
if position = 1 then
Pic.Draw (Block11, x + 30, y, picMerge)
Pic.Draw (Block11, x, y - 30, picMerge)
x_Left := x
x_Right := x + 90
y_Bottom1 := y - 30
y_Bottom2 := y
elsif position = 2 then
Pic.Draw (Block10, x + 60, y - 60, picMerge)
Pic.Draw (Block10, x + 30, y - 30, picMerge)
x_Left := x
x_Right := x + 60
y_Bottom1 := y - 60
y_Bottom2 := y
elsif position = 3 then
Pic.Draw (Block11, x + 30, y, picMerge)
Pic.Draw (Block11, x, y - 30, picMerge)
x_Left := x
x_Right := x + 90
y_Bottom1 := y - 30
y_Bottom2 := y
elsif position = 4 then
Pic.Draw (Block10, x + 60, y - 60, picMerge)
Pic.Draw (Block10, x + 30, y - 30, picMerge)
x_Left := x
x_Right := x + 60
y_Bottom1 := y - 60
y_Bottom2 := y
end if
end Z_Block
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
randBlock := Rand.Int (1, 6)
process falling123 (randBlock : int)
loop
if randBlock = 1 then
I_Block
elsif randBlock = 2 then
L_Block
elsif randBlock = 3 then
O_Block
elsif randBlock = 4 then
S_Block
elsif randBlock = 5 then
T_Block
elsif randBlock = 6 then
Z_Block
end if
end loop
end falling123

process falling
loop
if whatdotcolor (x + 40, y - 30 - 30) = black or whatdotcolor (x + 70, y - 30 - 30) = black then
if randBlock = 1 then
delay (100)
drawfillbox (x, y + 30, x + 120, y + 60, black)
y := y - 30
elsif randBlock = 2 then
delay (100)
drawfillbox (x + 30, y + 30, x + 90, y + 60, black)
drawfillbox (x + 60, y + 30, x + 90, y + 60, black)
y := y - 30
elsif randBlock = 3 then
delay (100)
drawfillbox (x + 30, y + 30, x + 90, y + 60, black)
y := y - 30
elsif randBlock = 4 then
delay (100)
drawfillbox (x, y + 30, x + 90, y + 60, black)
y := y - 30
elsif randBlock = 5 then
delay (100)
drawfillbox (x, y + 30, x + 90, y + 60, black)
y := y - 30
elsif randBlock = 5 then
delay (100)
drawfillbox (x, y + 30, x + 90, y + 60, black)
y := y - 30
elsif randBlock = 6 then
delay (100)
drawfillbox (x, y - 30, x + 90, y + 60, black)
y := y - 30
end if
end if
end loop
end falling
fork falling
%first loop where everything happens
loop
randBlock := Rand.Int (1, 6)
x := 90
y := 600 - 10
loop
exit when y = 20
fork falling123 (randBlock)
var chars : array char of boolean
Input.KeyDown (chars)
if chars (KEY_UP_ARROW) then
if y not= 101 then
position += 1
end if
y := y - 30
end if
if chars (KEY_RIGHT_ARROW) then
if y not= 101 then
x := x + 30
end if
end if
if chars (KEY_LEFT_ARROW) then
if y not= 101 then
x := x - 30
end if
end if
if chars (KEY_DOWN_ARROW) then
%if not= 101 then
y := y - 30
%end if
end if
end loop
end loop

jason98wang

Posted: Sun Jan 15, 2017 1:28 am Post subject: RE:Problems with my tetris game

this is my current code so far, ill private messgae you the images that you need in order to run it

Insectoid

Posted: Sun Jan 15, 2017 10:53 am Post subject: RE:Problems with my tetris game

First of all, you're using processes, which is just plain bad news, as explained here.

You need to replace all your processes with procedures. This might take some work, but it should fix a bunch of bugs automatically (even bugs you haven't discovered yet). I wouldn't even bother though, because you're probably going to end up re-writing almost the whole thing. It's easier than trying to save this.

You're making this much more complicated than it needs to be. Tetris is a simple game. It plays on a grid (10x50 or something, no?). The pieces are always lined up with the grid. Collisions are always between the cell on top hitting the cell on the bottom. You can only move left or right one cell at a time.

But you're writing this as if the screen was the game board. So instead of moving left or right by 1, now we're moving by 30, or 60. Now instead of checking 'x-1' for collisions, we're checking x-30. Instead of 'is cell (x, y) occupied' you're asking 'is location (x-30, y-30) the right color'. This is so much more complicated!

Turing (and most other programming languages) have a convenient way to represent grids- arrays!

var grid : array (0..9, 0..49) of boolean

Here we've created a 10x50 array to represent our game board. It's of boolean, which means every cell can be either 1 or 0. I've decided that a 0 means that cell is empty, and a 1 means that cell is full. Use a couple of loops to set every cell to 0, so the whole board is empty:

for x : 0..9
for y : 0..49
grid (x, y) := 0
end for
end for

Making something fall down is easy:

var x := 5
var y := 50

loop
y -= 1
delay (500)
if y = 0 then
grid (x, y) := 1 %this spot is now full.
exit
end loop
end loop

checking for collisions is easy too!

var x := 5
var y := 50

loop
if grid (x, y-1) = 0 then %check if square below is empty
y -= 1 %This is the same as y := y - 1
else
grid (x, y) := 1
exit
end if
delay (500)
end loop

Look at how simple that code is! It's easy to read, it's easy to write! But we still have to draw everything! Fortunately, that's easy too!

procedure drawEverything

for x: 0..9
for y : 0..49
if grid (x, y) = 1 then
Draw.FillBox (x*10, y*10, x*10+10, y*10+10, red)
end if
end for
end for

end drawEverything

We've scaled everything up here by 10 to draw it. Notice how we didn't have to touch any of our logic code? All we did was check if there was a 1 or zero in our grid, and draw a red box there. This means the game can look however you want. It can be flashy, plain, it can use pictures or Draw commands or even just plain text. It doesn't matter. All you need to change is the drawEverything procedure and as long as you don't touch the actual logic code, the game will still work.

Obviously I've only given you code snippets here and you'll need to fill in the rest, but hopefully you see how simple this can be. It will save you so much time to just start over. You'll only get frustrated trying to make your current code work.

And finally, don't be discouraged. You had to screw it up this bad to learn why doing it this way is bad. If you look far enough back in my post history here (I was in grade 10 once too), you'll see me doing exactly the same thing.

jason98wang

Posted: Sun Jan 15, 2017 11:26 am Post subject: RE:Problems with my tetris game

wait sorry i dont really understand can you reexplain it to me

jason98wang

Posted: Sun Jan 15, 2017 11:28 am Post subject: RE:Problems with my tetris game

do you think ill be able to finish before wednesday if i restart?

Insectoid

Posted: Sun Jan 15, 2017 12:06 pm Post subject: RE:Problems with my tetris game

I think you will. If you don't understand my post, re-read through it until you do. The biggest take-away is separating your game logic from what's drawn on the screen. Doing that will simplify your code so much.