need help with grid based game

Monkey Forums/Monkey Beginners/need help with grid based game

dubbsta(Posted 2017) [#1]
at this point im trying to put random pieces on the grid, now its only putting jellybeans
not to sure how to use the candylist on this one. any help, pointers or any books to read would be great thanks.

Strict 

Import mojo


Global screenWidth:Int = 640
Global screenHeight:Int = 480 
 
 
Class Board
  Field x:Int 
  Field y:Int 
  Field cols:Int = 8
  Field rows:Int = 8 
  Field offset:Int = 1
  Field lvl:Int[][]
  Field size:Int = 40
  Field gridOffset:Int 
  
	  		Method New()
	  			
	  			gridOffset = (screenWidth -((size + 2) * rows)) / 2
	  			x = gridOffset
	  			y = 100
	  		End  
	  		
	  		Method level:Int()
	  		
	  		
	  			Return 0 
	  		End 
	
			Method Draw:Int()
				
				For Local j:Int = 0 until cols
					For Local i:Int = 0 Until rows
					'white border
				SetColor( 255 ,255, 255)
				DrawRect( x+i*(size+2), y+j*(size+2), size+4, size+4)
				
					'inner boxes
				SetColor( 255, 100, 100)
				DrawRect( x+2+i*(size+2), y+2+j*(size+2), size, size)
					End 
				End 
			
				Return 0 
			End 

End 

Class Pieces Extends Board
 
  
  Field candy:Image
  Field jellyBean:Int  
  Field gumDrop:Int  
  Field lolliPop:Int  
  Field mint:Int  
  Field mix:Int  

	  		Method New(frame:int)
	  			candy = LoadImage("size1/candy.png",40,40,16,Image.MidHandle)
	  			
	  				 Seed = Millisecs()
		  			 jellyBean  = Rnd(0,3)
					 gumDrop  = Rnd(4,7)
					 lolliPop  = Rnd(8,11)
					 mint  = Rnd(12,15)
					 mix  = Rnd(1,4)
	  		End  
	  		
	  		Method pickCandy:Int()	  
		  			  jellyBean  = Rnd(0,4)
					  gumDrop  = Rnd(4,8)
					  lolliPop  = Rnd(8,12)
					  mint  = Rnd(12,16)
					  mix  = Rnd(1,5)
				    
	  			Select mix 
	  				Case 1
	  				mix = jellyBean
	  				Case 2
	  				mix = gumDrop
	  				Case 3 
	  				mix = lolliPop
	  				Case 4 
	  				mix = mint
	  			End 
	  		
	  			Return 0 
	  		End 
	
			Method Draw:Int()
				 
				'need to make candyList
				'For loacal spread:= Eachin candyList
				For Local j:Int = 0 until cols 
					For Local i:Int = 0 Until rows 
						DrawImage(candy,x+(size+4)/2+i*(size+2),y+(size+4)/2+j*(size+2),0,1,1,mix)
					End
				End 
				'End 
				Return 0 
			End 

End 

Class crushcandy Extends App

  Field board:Board
  Field pieces:Pieces
  Field candyList:List<Pieces> = New List<Pieces>

		Method OnCreate:Int() 
		 
			  SetUpdateRate(3)
			 
			  board = New Board
			  pieces = New Pieces
			  candyList.AddLast(New Pieces(pieces.mix))
			 
			Return 0 		
		End 
		
		
		Method OnUpdate:Int()
		 
			pieces.pickCandy()
			
		
			Return 0 
		End 
		   
		
		Method OnRender:int()
			Cls(40,35,34)
			 
			 board.Draw()
			 SetColor(255,255,255)
			
			 For Local piece:= Eachin candyList
			 	piece.Draw()
			 End 
			Return 0 
		End 
End 



Function Main:Int ()

	New  crushcandy()
	
	Return 0
End 




Gerry Quinn(Posted 2017) [#2]
You need to re-think everything here.

jellybean, mint etc. are constants, 0, 1, 2... One would normally use Const rather than defining actual fieldfs, but fields set to those values will work anyway.

To pick one at random, do: Local chosenSweet:Int = Rnd( 0, 4 ). Gives an integer that is 0, 1, 2 or 3.

Then chosenSweet is an integer denoting a particular sweet and it will probably be in a 2D array of ints that say what is in every square of the board:

0 1 1 3
2 3 2 2
3 1 2 0
1 0 1 3

...an array of ints that represents your current board.

Also, Piece extending Board doesn't really make sense.


dubbsta(Posted 2017) [#3]
hahaha i have no idea what im doing. can you tell me what to read for things like this, i tried finding tutorials on match 3 games but i cant translate it to monkey and alot of unity stuff out there. any books or algos i should learn in particular for this. i want to nail grid games and platformers and ill be a hap hap happy man!


Pakz(Posted 2017) [#4]
dubbsta, look at this book here :

http://eloquentjavascript.net/

It is a free online book (html) that teaches javascript. Javascript is somewhat different than monkeyx but in the book it teaches a lot on programming itself(games also). I have been going through it and am learning technical things that get mentioned on the monkey and blitz forums.

There is a chapter on how to program a platformer game.

Monkey also compiles to javascript(hmtl5) and if you use that then knowing something about javascript might be valuable. The book is not short in size and I find it is not that very difficult. (Might require re-reads of the chapters though)


dubbsta(Posted 2017) [#5]
Thanks pakz might take a hiatus to do some reading


Gerry Quinn(Posted 2017) [#6]
The rule is, start small and keep it simple. Don't feel the need to go crazy using classes until you know what they are doing for you.

What is a grid game, fundamentally? It has a lot in common with other games such as Tetris. There is a 'simulated' world consisting of a grid of square cells which - in the simplest case - each have an integral value associated with them, which might be represented by a colour. See if you can make this and render it, just using DrawRect() in a particular colour. Then write methods to show something at a particular square (Maybe draw a circle on it in a special colour) and to tell which grid cell you clicked on with the mouse. Now you have a basic substrate for a grid game.

Invent a game with the simplest rules you can think of. Are some 'colours' special? Maybe an empty cell is 0, ordinary colours (sweets) run from 1 upward. In some games it makes calculations easier if there is a special 'wall' value.

Suppose the game is Bejeweled. You want to swap cells, and remove any 3 in a row. First step: write code to make a list of all 3 in a row the same colour. You'll have to decide what data structure you will use for this list.

Now you need code to replace every cell in such a list with an empty cell. And you need code to fill the empty cells. In real Bejeweled, all the sweets will fall down if there are spaces below them, but if this seems complicated, you could start by just filling empty squares with random sweets. [What if the new sweets make rows? You have to decide how to approach this.]

Okay, we're ready to make a game. Write code that selects a square when you click on it, When you click on it again it should be de-selected. When you click on another square, the game checks that it is next to the first square, then swaps the sweets in the two squares and makes a list of rows. If the squares are not adjacent or the list of rows made is empty, the move is rejected. Otherwise it is made. Now you have a primitive Match-3 game.

The remainder is simply elaboration :D


dubbsta(Posted 2017) [#7]
good tips gerry, i need to put thoughts on paper before hitting the keys, i think that might help alot. i caught myself over complicating a few times with lists and other stuff and didnt work, then went back with simple commands and simple math and boom.