Author Topic: Help with making a tetris game in BASIC  (Read 4988 times)

0 Members and 1 Guest are viewing this topic.

Offline flyingfisch

  • I'm 1337 now!
  • Members
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1620
  • Rating: +94/-17
  • Testing, testing, 1...2...3...4...5...6...7...8..9
    • View Profile
    • Top Page Website Design
Help with making a tetris game in BASIC
« on: September 14, 2011, 11:52:50 am »
Hi

I am thinking about making tetris in casio-basic (using ASCII symbols). Here are my problems:

1. Is there an equation that would let me rotate an ASCII sprite?

2. How can I detect collisions?

3. How can I detect when a line is filled, so the program knows to delete that line.



Quote from: my dad
"welcome to the world of computers, where everything seems to be based on random number generators"



The Game V. 2.0

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Help with making a tetris game in BASIC
« Reply #1 on: September 14, 2011, 01:54:10 pm »
Rotation

I've made a few tetris games in Basic, let me share the way I did it.  First off, the way I stored my pieces is in 2 lists, 1 for X offset and 1 for Y offset.  Each list had 4 coordinates, 1 for each piece.  These values would look something like this:

Code: [Select]
X = {-1,-1,0,1}
Y = {-1,0,0,0}

Note that these coordinated would make some form of the L shape, to output them, we would do something like this:

Code: [Select]
For(I,1,4)
Output(X(I)+S,Y(J)+T)
End

It's important to see that I have 4 variables here, the two lists X and Y, and then the two variables S and T.  The lists hold the shape of the piece, while the variables hold the Location of the piece.  This is a good thing because this is what allows us to rotate our pieces quickly:

Code: [Select]
X->Z
Y->X
-Z->Y

That code operates on the lists, and rotates the shape clockwise, you can switch the negative sign to the Y instead of the Z to rotate the other direction.

Collisions

I am a bit unfamiliar with Casio homescreen text commands, so I will assume there is a way to detect whether a single space is filled.  Whether you utilize some sort of Pixel Test function, or use a Matrix, the logic is the same.  There are two conditions for a collision, you either move a piece into a wall, or rotate a piece into a wall.  Assuming we have a way to detect whether a position is filled, collision checking is simple.

First we merely move or rotate the object blindly, then loop through all positions of the new piece position and check if any of them are filled.  If any of them are, we reverse the object operations and resume the game.  Because of this, you will need a way to rotate both directions.

Line Detecting:

Because of the size of the tetris field, it might be difficult to check every line every time a piece is placed,  so instead we will check only the lines that possibly could have a completion, and these are the lines that the piece ends up in.  The piece is made of 4 blocks, which means at a maximum we will have to check 4 lines.  

When we check for lines, we take the Y locations of the object, remove duplicated, and sort them from top to bottom.  The we loop through each Y position from top to bottom.  For each Y position we check all of the blocks in that line, if any of them are empty, we skip this Y position and go to the next.  If we are at the last Y position, we move on to playing the game.

If, however, all of the blocks are filled, we move onto the line clearing algorithm.  It consists of mostly a line shifting algorithm, and this is how it works.  If a filled Line was detected at location Y, we go through every block in that line and replace it with the one above.  We then do that for every line above Y until we encounter an empty line, after which we are finished clearing and shifting.

Once we check and clear every line we need to, we can choose the next piece (which can be done from a lookup table) and start the next turn!



Does that all make sense?  Hopefully it helped :D Just ask if you have more questions ^^
« Last Edit: September 14, 2011, 03:04:13 pm by Builderboy »

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: Help with making a tetris game in BASIC
« Reply #2 on: September 14, 2011, 02:00:04 pm »
Here's an idea for checking line fills: Have a list with an element for each row, which tells how many spaces are empty in that row.  When a piece lands, loop through the piece's Y coordinates and subtract 1 from each of those rows. Then you can just check whether any of the elements are equal to 0.
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Help with making a tetris game in BASIC
« Reply #3 on: September 14, 2011, 02:25:08 pm »
Ah that's clever!  That would make things significantly faster during line checking ^^

Offline flyingfisch

  • I'm 1337 now!
  • Members
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1620
  • Rating: +94/-17
  • Testing, testing, 1...2...3...4...5...6...7...8..9
    • View Profile
    • Top Page Website Design
Re: Help with making a tetris game in BASIC
« Reply #4 on: September 14, 2011, 02:45:08 pm »
Quote
Code: [Select]
For(I,1,4)
Output(X(I)+S,Y(J)+T)
End

Ok, I got it to display that, but the code is alittle different in casio-basic:

Code: [Select]
For 1-> I To 4
PlotOn List 1[I]+s,List 2[I]+T
Next

I had to use the plot on graphing function since Locate (the equivelant of TI's "output") won't except negative numbers.

Quote
Code: [Select]
X->Z
X = Y
Y = -Z

I don't understand what you are saying here, sorry.

Quote
Collisions

I am a bit unfamiliar with Casio homescreen text commands, so I will assume there is a way to detect whether a single space is filled.  Whether you utilize some sort of Pixel Test function, or use a Matrix, the logic is the same.  There are two conditions for a collision, you either move a piece into a wall, or rotate a piece into a wall.  Assuming we have a way to detect whether a position is filled, collision checking is simple.

First we merely move or rotate the object blindly, then loop through all positions of the new piece position and check if any of them are filled.  If any of them are, we reverse the object operations and resume the game.  Because of this, you will need a way to rotate both directions.

We have PixlTest() which tests for pixels and we have matrices. I think that matrices would probably be faster, but I'll have to figure out how to get them to work with this.


Quote
Line Detecting:

Because of the size of the tetris field, it might be difficult to check every line every time a piece is placed,  so instead we will check only the lines that possibly could have a completion, and these are the lines that the piece ends up in.  The piece is made of 4 blocks, which means at a maximum we will have to check 4 lines. 

When we check for lines, we take the Y locations of the object, remove duplicated, and sort them from top to bottom.  The we loop through each Y position from top to bottom.  For each Y position we check all of the blocks in that line, if any of them are empty, we skip this Y position and go to the next.  If we are at the last Y position, we move on to playing the game.

If, however, all of the blocks are filled, we move onto the line clearing algorithm.  It consists of mostly a line shifting algorithm, and this is how it works.  If a filled Line was detected at location Y, we go through every block in that line and replace it with the one above.  We then do that for every line above Y until we encounter an empty line, after which we are finished clearing and shifting.

Once we check and clear every line we need to, we can choose the next piece (which can be done from a lookup table) and start the next turn!

Since I'll probably be using matrices, this should be easy, now. I think I'll use calc's method, though

My only problem is that, using pixels and the graph screen, every shape will be 1 pixel thick. Is there an equation for how to change each shapes thickness, or will I just have to use more coordinates, OR, can you giv me a way to do this without any egative numbers, so I can use ASCII for the shapes on the homescreen.



« Last Edit: September 14, 2011, 02:46:28 pm by flyingfisch »



Quote from: my dad
"welcome to the world of computers, where everything seems to be based on random number generators"



The Game V. 2.0

Offline flyingfisch

  • I'm 1337 now!
  • Members
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1620
  • Rating: +94/-17
  • Testing, testing, 1...2...3...4...5...6...7...8..9
    • View Profile
    • Top Page Website Design
Re: Help with making a tetris game in BASIC
« Reply #5 on: September 14, 2011, 03:02:28 pm »
never mind about negatives, I was wrong, thats not the prob.

S and T had to be 2 not 1.
« Last Edit: September 14, 2011, 03:27:37 pm by flyingfisch »



Quote from: my dad
"welcome to the world of computers, where everything seems to be based on random number generators"



The Game V. 2.0

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Help with making a tetris game in BASIC
« Reply #6 on: September 14, 2011, 05:46:44 pm »
Glad you got that part figured out, and sorry about the rotating code, I was thinking half in Java and Half in Ti-Basic, it's all fixed now x.x

Offline flyingfisch

  • I'm 1337 now!
  • Members
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1620
  • Rating: +94/-17
  • Testing, testing, 1...2...3...4...5...6...7...8..9
    • View Profile
    • Top Page Website Design
Re: Help with making a tetris game in BASIC
« Reply #7 on: September 14, 2011, 06:36:31 pm »
OK, thanks. I made a program, but I think I'll start from scratch and perfect it.(I've been embellishing too much and some stuff won't work now.) Thanks for all your help Bboy!!!



Quote from: my dad
"welcome to the world of computers, where everything seems to be based on random number generators"



The Game V. 2.0

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Help with making a tetris game in BASIC
« Reply #8 on: September 14, 2011, 06:39:47 pm »
No problem :D Making Tetris in Basic is quite an undertaking, it's a very quick game to make in Basic ^^

Offline flyingfisch

  • I'm 1337 now!
  • Members
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1620
  • Rating: +94/-17
  • Testing, testing, 1...2...3...4...5...6...7...8..9
    • View Profile
    • Top Page Website Design
Re: Help with making a tetris game in BASIC
« Reply #9 on: September 14, 2011, 09:05:16 pm »
Yeah. I'm realizing that! Man, those programmers in the 80's must have been good!



Quote from: my dad
"welcome to the world of computers, where everything seems to be based on random number generators"



The Game V. 2.0