A newbie question

Blitz3D Forums/Blitz3D Beginners Area/A newbie question

Arno94(Posted 2003) [#1]
Hi,

As said in the topic title, I am a newbie so my question could be fun for dinos :)

I want to make a tetris like for my fun.

In this example, left arrow allows to go to the left, right arrow to the right and down arrow allows to drop the piece. I've no problem with this.

The problem is for the rotate key (up arrow). In fact, I have four BMP for my piece. The first is the "normal" piece, the second is the normal piece rotated@90°, the third is rotated@180° and the last is rotated@270°.

So, I decided to write my code like this : if uparrow is pressed and the piece is normal, then change the normal piece into the rotated@90 one. If uparrow is pressed and the piece is the rotated@90 one, then change the rotated@90 by the rotated@180 etc.

I hope you follow what I mean.

This is the code :

Forme_T=LoadImage ("data/skins/defaut/t.bmp")
Forme_T_R1=LoadImage ("data/skins/defaut/t_r1.bmp")
Forme_T_R2=LoadImage ("data/skins/defaut/t_r2.bmp")
Forme_T_R3=LoadImage ("data/skins/defaut/t_r3.bmp")

While Not KeyHit (1)

Cls

DrawImage (Fond2j,0,0); Draw a background image

Text 0,0,"Tetris New Version 0.002"; A great title, isn't it ? :p

DepartPieceY=DepartPieceY+PieceMvtY

If DepartPieceY>=490
DepartPieceY=490
PieceMvty=0
EndIf

If Forme_T=Forme_T And KeyHit(Fleche_Haut)=1 Then ;

Forme_T=Forme_T_R1

ElseIf Forme_T=Forme_T_R1 And KeyHit(Fleche_Haut)=1 Then

Forme_T=Forme_T_R2

ElseIf Forme_T=Forme_T_R2 And KeyHit(Fleche_Haut)=1 Then

Forme_T=Forme_T_R3

ElseIf Forme_T=Forme_T_R3 And KeyHit(Fleche_Haut)=1 Then

Forme_T=Forme_T

EndIf

If KeyHit(Fleche_Gauche)=1

DepartPieceX=DepartPieceX-30

EndIf

If KeyHit(Fleche_Droite)=1

DepartPieceX=DepartPieceX+30

EndIf

If KeyHit(Fleche_Bas)=1

PieceMvty=50

EndIf

If DepartPieceX<=50
DepartPieceX=50
EndIf

If DepartPieceX>=260
DepartPieceX=260
EndIf

DrawImage (Forme_T,DepartPieceX,DepartPieceY)

Flip

Wend


So, everything works fine but the rotation. When I press the key, the piece rotate (Forme_T becomes Forme_T_R1) but if I press the key again, nothing happens...

Please, can you help me ?


JazzieB(Posted 2003) [#2]
I would store your images in an array and then have a 'direction' variable that keeps track of which one to show.

Set up your images like this:

Dim t(3)
Global dir=0

t(0)=LoadImage("t0.bmp")
t(1)=LoadImage("t1.bmp")
t(2)=LoadImage("t2.bmp")
t(3)=LoadImage("t3.bmp")


Then when checking for the rotation key, just add 1 to dir and check if it's higher than 3. If so, reset it to 0...

If KeyHit(200) Then
  dir=dir+1
  If dir>3 Then dir=0
EndIf


The above can also be made a lot shorter by the use of the Mod operator.

If KeyHit(200) Then dir=(dir+1) Mod 4


Hope that helps.


Arno94(Posted 2003) [#3]
I just tried your code and it now works perfectly, thank you very much for the speed and the quality of your ansnwer.

For the Mod Operator, I'll see later :)

I'll soon have other questions I guess...

Thank you again JazzieB


Sir Gak(Posted 2003) [#4]
JazzieB's answer was excellent. I wanted to add a thought, just on general principle, on why you were having problems with your orginal code.

You needed to have a separate variable to store the current image handle. In your original line below:

If Forme_T=Forme_T And KeyHit(Fleche_Haut)=1 Then ;

you had problems immediately. You see, you loaded the default image handle into Forme_T. Then, you re-used that variable to store the handles for the other images. The moment you assigned a different image handle into Forme_T (such as: Forme_T=Forme_T_R3), you lost the handle for the default image.