Posted: Mon Jan 16, 2017 8:56 pm Post subject: RE:Problems with my tetris game
i Have tried what you told me to do but currently this is still bugging up, with the animation as well as the blocks stacking, can you help me please
Sponsor Sponsor
Insectoid
Posted: Tue Jan 17, 2017 10:50 am Post subject: RE:Problems with my tetris game
All right, here's a bit of code to help you get started.
code:
var grid : array 0 .. 9, 0 .. 50 of int
var blockX, blockY : int
%Here we just set the grid to all 0.
for x : 0 .. 9
for y : 0 .. 49
grid (x, y) := 0
end for
grid (x, 50) := 1 %set the bottom to all 1's, to make a floor
end for
%This checks if grid(x, y) is full or not
%a 1 means that spot is full. A 0 means that spot is taken.
function checkBlock (x : int, y : int) : int
if grid (x, y) > 0 then
result 1
else
result 0
end if
end checkBlock
%This just draws everything. It's the only code that draws anything, and drawing things is all it does. It draws upside down, because I'm too lazy to fix it.
%It just loops over every cell in our grid array. If it finds a '1', it drawfills a red box there. Otherwise, it just draws a black box. Then it draws a green box at (blockX, blockY).
proc drawGrid
for x : 0 .. 9
for y : 0 .. 50
if checkBlock (x, y) = 1 then
Draw.FillBox (10 * x, 10 * y, 10 * x + 10, 10 * y + 10, red)
end if
Draw.Box (10 * x, 10 * y, 10 * x + 10, 10 * y + 10, black)
end for
end for
Draw.FillBox (10 * blockX, 10 * blockY, 10 * blockX + 10, 10 * blockY + 10, green)
end drawGrid
%That's all we need to make it work. Here's the main loop where it all comes together:
loop
%First, we draw everything. Doesn't have to be first. Could be last. Up to you.
drawGrid
%Here, we check if the block can move down. If it can, it does. If it can't, it sets that spot to filled (1) and then resets to the top.
if checkBlock (blockX, blockY + 1) = 0 then
blockY := blockY + 1 %This moves the block down
else
grid (blockX, blockY) := 1 %this sets the spot to '1' ie 'occupied'
blockY := 0 %this moves the block back to the top
end if
delay (10)
cls %erase EVERYTHING. Everything will get re-drawn at the top of the loop anyway!
end loop
It's quite flashy so if you've got epilepsy don't run it. Don't worry about that though, we can fix it later. Trying to fix that now will only cause you more problems.
Notice that the function drawGrid ONLY draws things. There is no game logic. We can change that function all we want, and the game will still work. All of the logic functions are nice and clean- things only move by 1 at a time, collision detection is one super simple line of code, etc. The game itself is easy to write, easy to read, easy to debug. The draw function might be a bit of a mess, but at least the mess is contained there instead of sprawled over the entire codebase.
jason98wang
Posted: Tue Jan 17, 2017 4:25 pm Post subject: RE:Problems with my tetris game
This is my current updated program can you help me fix the problem
setscreen ("graphics:300;600")
var grid : array 0 .. 10, 0 .. 20 of int
var dely : int
var turn : int
var blockType : int
var rightturn, leftturn, bottom : int
var chars : array char of boolean
dely := 500
for x : 0 .. 10
for y : 0 .. 20
grid (x, y) := 0
end for
end for
%1 means block occupuy that positino
%0 means its an empty block
%Making blocks fall
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%End of procedures%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
loop
x := 10
y := 30
turn := 1
blockType := 1%Rand.Int (1, 6)
bottom := 3
rightturn := 23
leftturn := 0
if blockType = 1 then
loop
locate (1,1)
put array_x," ", x
exit when grid (array_x, array_y - 1) = 1 or y = bottom
L_block_turn_White
dely := 200
y := y - 1
L_block_turn
Input.KeyDown (chars)
if chars (KEY_UP_ARROW) then
if turn <= 4 then
turn := turn + 1
else
turn := 1
end if
elsif chars (KEY_RIGHT_ARROW) then
put x
if x < rightturn then
x := x + 1
end if
elsif chars (KEY_LEFT_ARROW) then
put x
if x > leftturn then
array_x := array_x - 1
end if
elsif chars (KEY_DOWN_ARROW) then
dely := 50
end if
elsif blockType = 3 then
%put array_y
loop
exit when grid (array_x, array_y - 1) = 1 or y = bottom
S_block_turn_White
dely := 200
y := y - 1
S_block_turn
Input.KeyDown (chars)
if chars (KEY_UP_ARROW) then
if turn <= 4 then
turn := turn + 1
else
turn := 1
end if
elsif chars (KEY_RIGHT_ARROW) then
if x < rightturn then
x := x + 1
end if
elsif chars (KEY_LEFT_ARROW) then
if x > leftturn then
x := x - 1
end if
elsif chars (KEY_DOWN_ARROW) then
dely := 50
end if
loop
exit when grid (array_x, array_y - 1) = 1 or y = bottom
O_block_turn_White
dely := 200
y := y - 1
O_block_turn
Input.KeyDown (chars)
if chars (KEY_UP_ARROW) then
if turn <= 4 then
turn := turn + 1
end if
turn := 1
elsif chars (KEY_RIGHT_ARROW) then
if x < rightturn then
x := x + 1
end if
elsif chars (KEY_LEFT_ARROW) then
if x > leftturn then
x := x - 1
end if
elsif chars (KEY_DOWN_ARROW) then
dely := 50
end if
grid (array_x, array_y) := 1 %this spot is now full.
grid (array_x + 1, array_y + 1) := 1
grid (array_x, array_y + 1) := 1
grid (array_x + 1, array_y) := 1
end if
end loop
jason98wang
Posted: Tue Jan 17, 2017 5:30 pm Post subject: RE:Problems with my tetris game
Hi, im wordering how are you gonna rid of falling blocks? because if you use cls then won't the landed blocks reset?
Insectoid
Posted: Tue Jan 17, 2017 6:21 pm Post subject: RE:Problems with my tetris game
No, they won't. cls is a draw command. It doesn't have any effect on the game itself at all. It just draws things.
Everything 'in the game' happens on the grid array. Every single block in the game is represented there. When you rotate something, it gets rotated in the array. When you delete a row, it is deleted from the array. Not from the screen.
The draw procedure draws everything. It looks at the array, and the score, etc and draws them to the screen. Anything you want drawn gets drawn here. We look at cell (1,1), if it's full, we draw it. If not, we don't. Then we look at cell (1, 2), then (1, 3), and so on. It looks at everything, and decides, based on what it sees, what to draw.
Things change pretty quickly in games. What we drew to the screen isn't accurate for very long, because as soon as we drew it, we started changing things. Maybe a row got deleted, maybe the user pressed a key, maybe the score went up. We already have a procedure that looks at EVERYTHING, and decides what to draw. So instead of writing another function to erase the parts we don't want, we just erase everything and draw it all again. Maybe something changed, maybe it didn't. We don't know, and we don't have to.
jason98wang
Posted: Wed Jan 18, 2017 1:12 am Post subject: RE:Problems with my tetris game