almost there

Monkey Forums/Monkey Beginners/almost there

dubbsta(Posted 2016) [#1]
getting the hang of this, im stuck but feel close. doing a tetris clone and my problem is in the shape class. all the commented out code was to render each shape separately, and worked great, but i wanted to be able to put the shapes in a list so im trying to keep the shape maker in one method. so i added more parameters to the constructor the only problem i seem to be having is how to pass the shape fields{t:int[][])into the block parameter
ive tried different combinations but cant find the right one

edit: just had a thought should each shape have its own sub class then called to the array? will give a try



didnt think about it but if commenting out would help more i will.


dubbsta(Posted 2016) [#2]
took some hours but got it!!


Gerry Quinn(Posted 2016) [#3]
You may want to copy the array for each shape into an individual shape object, so you can freely rotate it etc. without worrying about corrupting the original data (of course there are lots of ways to do this). Your arrays would be in a 'ShapeFactory' object that you can ask to spit out a new Shape of a given kind at any time. Or you could skip the factory class altogether and have Shape initialisers or constructors that fill in the fields of a newly-created shape (I'd probably do it the latter way).

(Just a way of thinking about it - if it's working fine now don't worry!).


dubbsta(Posted 2016) [#4]
yeah was thinking of something like that but wasnt sure how to implement. would i be possible rotate each piece in a matrix, i will be looking into a shape factory though
this is my current code

ps. my code is a little bit neater but the codebox shifts things around a bit
Import mojo

Global gcube:Image
global rcube:Image
global bcube:Image
global pkcube:Image
Global ycube:Image

Class shape 
  Field piece:Int[][]
  Field color:int
  Field img:Image
  
  Field x:Int ,y:Int
  Field rowmax:Int
  Field colmax:Int
  Field gravity:Int 
  
  
				Method New(img:Image,x:Int,y:Int,
						  rowmax:Int,colmax:Int,piece:int[][])
				 	  
				 Self.rowmax = rowmax
				 Self.colmax =colmax
				 Self.img= img
			     Self.x = x
			     Self.y = y
			     Self.piece = piece
			    End 
            Method Draw()
            
              For Local i:Int = 0 To rowmax
             	For Local j:Int = 0 To colmax   
             	   If piece[j][i] = 1
             	     DrawImage(img,x+i*26,y+j*25,0,.5,.5)
             	   End 
             	 End 
              End 
            End
            
           ' Method t()
           '           [[1,1,1],
           '           [0,1,0]]
           'End 
            
            Method move(gravity:Int)
             Self.gravity = gravity
             
               If KeyHit(KEY_RIGHT)
                  x+=26
               Elseif KeyHit(KEY_LEFT)
               	  x-=26
               End 
               
                ' y+=gravity 
               
            End 
            
        
End class


Class drop

  Field size:float
  Field gravity:Float
  Field droplet:Image
  Field x:Int,y:Int


		Method New(x:Int,y:Int,droplet:Image)
			
		    Self.droplet = droplet
		  	Self.x = x 
		  	Self.y = y
		    Self.size = size
		    gravity = Rnd(5.0,8.0)
		    size = Rnd(0.2,.5)	
		End 
		
		Method fall()
		    Self.gravity = gravity
		  	y+= gravity	
		End 
		
		Method Draw()
			DrawImage(droplet,x,y,0,size,size)
		End 
		
End Class	




Class bg 

  Field x:Int
  Field y:Int
  Field speed:Int
  Field bg1:Image
  Field name:String
 
  
		Method New(x:Int,y:Int,bg1:Image)
		  Self.x = x
		  Self.y = y 
		  Self.bg1 = bg1
		End 
		
		Method scroll(speed:Int)
		
		   Self.speed = speed
		   x = x + speed
		   
		End 
		
		Method create(name:String)
		Self.name = name
		  bg1 = LoadImage("bg.png ")
		  
		End   
		  
		Method Draw()
		
		  DrawImage (bg1, x,y)
		
		End
		 
End Class
   
   
   
Class test2 Extends App
  Field ScreenHeight = 480
  Field ScreenWidth = 640 
  
  Field shapePicker:Int = 2
  Field background:bg 
  Field bg1:Image  
  Field bg2:bg  
  Field droplet:Image  
  Field rain:drop
  
  Field bgList:List<bg> = New List<bg>() 
  Field dropList:List<drop> = New List<drop>()
  
  Field L:shape
  Field I:shape
  Field T:shape
  Field Z:shape
  Field O:shape
  
  Field shapeList:List<shape> = New List<shape>()
  
  
		Method OnCreate()
		  SetUpdateRate(60)
		  
		  				  
				  ycube = LoadImage("yellowcubesmall.png")		   
				  pkcube = LoadImage("pinkcubesmall.png")									 
				  rcube = LoadImage("redcubesmall.png")
				  bcube = LoadImage("bluecubesmall.png")
				  gcube = LoadImage("greencubesmall.png")			
				  
		 For Local i:Int = 0 To 4
		    
	     End   
	     
		        If shapePicker = 1 
		 		  shapeList.AddLast(New shape(gcube,150, 150,2,1,[[1,1,1],[0,1,0]]))  'T
		 		Elseif shapePicker = 2
		 		  shapeList.AddLast(New shape(ycube,ScreenWidth/2, 0,2,1,[[1,1,0],[0,1,1]]))  'Z
		 		Elseif shapePicker = 3 
		 		  shapeList.AddLast(New shape(bcube,250, 250,0,3,[[1],[1],[1],[1]]))  'I
		 		Elseif shapePicker = 4 
		 		  shapeList.AddLast(New shape(pkcube,250, 250,1,1,[[1,1],[1,1]]))     'O
		 		Elseif shapePicker = 5  
		 		  shapeList.AddLast(New shape(rcube,250, 250,1,2,[[1,0],[1,0],[1,1]]))'L
		 		End 	  	                           	        
				 
		  
		   'PlayMusic ("rain.mp3",1)

		   rain = New drop
		   droplet = LoadImage("drop.png") 
		   

		   background  = New bg
		   bg1 = LoadImage("bg2.png")
		  ' background.create("bg.png")
		   bg2 = New bg(0,0,bg1)
		   bgList.AddLast(New bg(0,0,bg1))   
		  
		End Method 
		
		
		Method OnUpdate() 
		
			For Local i:= Eachin shapeList
			
			   i.move(2)
			   
			End 
		
		 dropList.AddLast(New drop(Rnd(ScreenWidth),0,droplet))
		 
		  For Local rain:drop = Eachin dropList
			rain.fall()
		  End 
		  
			  	For Local rain:drop = Eachin dropList
			     If rain.y > 520
			     
			      dropList.RemoveFirst()
			      
			     End 
		 		End  
	 
		End 
		
		
		Method OnRender()
		  Cls(0,0,0)
		   
		   'bg2.Draw()
		   
		   For Local rain:drop = Eachin dropList
		       rain.Draw()
		   End 
		    
		   For Local shape:= Eachin shapeList		   
		       shape.Draw()
		    End 
		    SetColor(255,66,255)

		      SetAlpha .7
		    DrawRect(10,10,222,222)	
		    DrawOval(34,13,30,34)
		    
		    T.Draw()
		    Z.Draw() 
		    O.Draw()
		    I.Draw()
		    L.Draw()
           'nothing renders past here???
		   
		  
		 	
		   
		End 
		
End 

Function Main()

 	New test2()
 	
End 



quick thing, the bottom i couldnt render anything after the x.Draw's i had to put the DrawRect before them, is there a reason why???


Gerry Quinn(Posted 2016) [#5]
Just drawing the shapes rotated won't work - they won't know when they are colliding / overlapping non-empty squares on the board (assuming a norrmal Tetris-style game). You need to be able to tell that if a shape falls one square, it will be over (say) (3,4), (4, 4), (4, 5) and (4, 6), and then check whether any of these squares are full, in which case it must now stop falling and 'stick' to the board (you add the shape's current squares to the board, and discard the shape). Then you check the board for horizontal lines, and collapse it if there are any. Then you release the next shape at the top of the play area. And so on. Every so many shapes, increase the fall speed a bit. No room to release a shape? Game over!

(Again, that's plain old Tetris,you could be panning something different, but it will be the same kind of logic.)

I don't think you ever initialised T, Z, O, I and L.


dubbsta(Posted 2016) [#6]
ok sounds good, yes t,z,o,i,l are good they are initialized at OnCreate, they render fine the problem is anything drawn after that line wont render i dont know why


Gerry Quinn(Posted 2016) [#7]
They don't render at all. You add a new shape to your list. But nowhere does it sayT = New Shape(...). T and the others are null as far as I can see. So when T.Draw is called, the program crashes.

Also, you have
For Local i:Int = 1 To 4
End


There's some error there.


dubbsta(Posted 2016) [#8]
Ohhhhh yes you're right I changed the whole thing not needing those calls anymore...doe