Basic noob question

Monkey Forums/Monkey Programming/Basic noob question

Gary Leeds(Posted 2011) [#1]
Sorry for asking what to you all will probably be a simple obvious answer but it has had me stumped for an hour or so

I want to do a card based game and thought I would start with a simple routine to pick a card from the deck and display it. I have a single PNG file that is 13 image wide by 5 deep, the last row of 13 are card backs

I pick a number between 0 and 51 and display the animation frame chosen

The problem is I cannot get it to run once, the cards are constantly being picked and displayed.

The code I am using is as follows
Import mojo

	Global card
	
Class ShowCard Extends App
	Field deck:Image

	Method OnCreate ()
		deck = LoadImage ("cards.png", 79, 123, 65,Image.DefaultFlags )
		SetUpdateRate 60
	End


	Method OnRender ()
		DrawImage deck,0,0,Rnd(0,52)
	End

End

Function Main()
		New ShowCard

End

what I am after is a basic load the PNG, pick a card, display the card and repeat when the mouse is clicked. I looked at the getting started with Monkey document but this seems to be very basic stuff. I have some experience with BlitzMAX but the way I coded stuff with BM seems very different to Monkey

Hope someone can help

Gary


muddy_shoes(Posted 2011) [#2]
The answer is that OnRender is called every frame and not only when the user presses the mouse button.

If you want the card to change when the user clicks their mouse then you need to create a separate variable for the current card and update it based on an input check. The OnUpdate call would probably be best suited for that.

So, something like:

Class ShowCard Extends App
    Field deck:Image
    Field pickedCard:Int

    Method OnCreate ()
        deck = LoadImage ("cards.png", 79, 123, 65,Image.DefaultFlags )
        SetUpdateRate 60
    End

    Method OnUpdate()
        If( MouseHit( MOUSE_LEFT) )
            pickedCard = Rnd(0,52)
        End
    End

    Method OnRender ()
        DrawImage deck,0,0,pickedCard
    End

End



Gary Leeds(Posted 2011) [#3]
Right, that makes sense cheers

Just another couple of quick questions. If I have say 3 screens in the game (say title screen, main game and menu) would I just have a game state of 1 2 and 3 and use a select command on the OnRender method to choose which to draw? Or is there a better way?

Am I right in thinking the best way to look at everything is Main() really does nothing, the OnCreate() is there to set everything up variable and graphic wise, OnUpdate is the program that can call other functions and OnRender is the draw function, I dont have to manually flip the screen like in Blitz Max where I did a draw routine like

	Cls
		draw_reels()
		DrawImage background,0,0-y_offset,0
		but.display()
		alp.display()
		lam.display()
	Flip


Also with the lack of threading in Monkey which I used to take care of things like screen touches without having to manually check them could I use the Onrender routine to do the same? I have a few lines like


If MouseX()>33 And MouseX()<33+60 And MouseY()>1223-y_offset And MouseY()<1313-y_offset And MouseDown(1) Then button_state[5]=1 Else button_state[5]=0



to set the states when drawing graphics and to set up timers to automatically advance to the next frame in an animation

Thanks again for your time

Gary


muddy_shoes(Posted 2011) [#4]
If I have say 3 screens in the game (say title screen, main game and menu) would I just have a game state of 1 2 and 3 and use a select command on the OnRender method to choose which to draw? Or is there a better way?


"Better" is a bit subjective. What you suggest would work fine, but I'd be more likely to create a GameState class that has methods for update and render and then swap the active instance.

Am I right in thinking the best way to look at everything is Main() really does nothing, the OnCreate() is there to set everything up variable and graphic wise, OnUpdate is the program that can call other functions and OnRender is the draw function, I dont have to manually flip the screen


It's true that most Main() functions will do little more that instantiate the game App instance but there's nothing to stop you doing other things. I tend to think of the App methods as points in the application lifecycle and update sequence where you can do some work rather than absolute dictation of what you should do. The only real limitation is that graphics operations on the mojo canvas won't work outside of the OnRender call. However, what you say is more or less right.

Also with the lack of threading in Monkey which I used to take care of things like screen touches without having to manually check them could I use the Onrender routine to do the same?


I'd use the OnUpdate, but I imagine it would work in the OnRender. One thing I would suggest about your technique is that it looks like you're coming at it in a very top-down and procedural way. If that's your preference then feel free to ignore me, but I'd go the route of creating some simple UI classes to wrap up those input checks and responses. If you're not familiar with OO techniques then that might be something to read up on as part of learning Monkey.