Snowing Effect in Monkey X

Since Christmas is coming soon, I decided to share my snowing effect code I wrote last year. The original code was written in BlitzMax. This year I have already made new Christmas intro in Monkey X. You may want to take look at the snowing effect of the Christmas video of the last year.

Import mojo2

Function Main()
	New MyApp
End

Class snowFlake
	Field x:Float, y:Float
	Field dx:Float
	Field r:Int
	Field yspeed:Float
	Field flakeAngle:Float
End Class

Class MyApp Extends App

	Field gfxBG:Image
	Field canvas:Canvas
	
	Global flakeList := New List<snowFlake>
	
	Method OnCreate()

		SetDeviceWindow(640,480,0)		
		canvas = New Canvas
		
		gfxBG = Image.Load("bg.jpg",.0,.0)
			
		For Local i = 0 To 149
			Local flake:snowFlake = New snowFlake
			flake.x = Rnd(50,640-50)
			flake.r = Rnd(2,8)
			flake.y = -flake.r * 2
			flake.yspeed = Rnd(0.7,3)
			flake.flakeAngle = Rnd(0,359)
			flakeList.AddLast(flake)
		Next
		
		SetUpdateRate(30)
		
	End
	
	Method OnUpdate()
		
	End
	
	Method OnRender()
		
		canvas.PushMatrix()
		canvas.Scale(DeviceWidth() / 640.0, DeviceHeight() / 480.0)
		canvas.SetAlpha 1
		canvas.SetBlendMode(BlendMode.Opaque)
		canvas.DrawImage(gfxBG,0,0)
		
		For Local flake:snowFlake = Eachin flakeList
		
			flake.x = flake.x + flake.dx
			flake.y = flake.y + flake.yspeed
			
			canvas.DrawCircle flake.x, flake.y, flake.r
			flake.dx = Cos(flake.flakeAngle) * 4 * Rnd(0.2,1)

			If flake.y >= 480 + flake.r * 2 Then
				flake.r = Rnd(2,8)
				flake.y = -flake.r * 2
			Endif

			flake.flakeAngle = flake.flakeAngle + 1
		
		Next
		
		canvas.PopMatrix()
		canvas.Flush()

		
	End
	
End Class

Examine the code and you’ll get the idea quickly. Implementation in other programming languages should be quit straight forward. Just use background picture of your own.

Feel free to use the code.

Screenshot of the program:

I will soon publish my new Merry Christmas 2017 and Happy New Year 2018 video. And… It’s also written in Monkey X.

What if Smart Screen blocks my product?

What if Smart Screen prevents my product to run on your computer? You can check the file before running it with some antivirus scanner and as you see, that there’s nothing harmful, just run the program. See the video (I have Finnish system, but I have added needed translations):

In case you ran into any problems, just contact me by e-mail and we’ll find a solution together.

Image courtesy of TAW4 at FreeDigitalPhotos.net

At the time I’m writing this update to this blog post, I’m pleased to say that I have the OV code signing certificate for Windows. This doesn’t prevent the smart screen at first, but in time it may, if the product gets enough downloads.

Unique Random Integers part II

Last year I wrote about my clumsy implementation of unique random integers system, I’ve used in my Memorable Ladies games. In those the speed isn’t a critical factor. It’s sufficient that the idea works. 🙂

In this post the idea behind unique random integers is the same, but implemented in a faster way.

In the implementation I have used an array that consists of all desired integers and each is drawn in a way that the same integer doesn’t get drawn again. Let’s take a look at the code (I have used Monkey2 programming language in the example):

Namespace myapp

#Import "<std>"

Using std..

Function Main()

	Local index:Int
	Local amountNumbers:Int
	Local randomNumber:Int
	Local numbers:=New Int[](1,3,5,7,9,11,13,15,17,19,21,23)

	While (numbers.Length <> 0)
		
		amountNumbers = numbers.Length	
		index = Int(amountNumbers * Rnd(0,1)) Mod amountNumbers
		randomNumber = numbers[index]
		
		For Local i:Int = index To amountNumbers - 2
			numbers[i] = numbers[i+1]
		Next
		
		amountNumbers = amountNumbers - 1
		numbers = numbers.Resize(amountNumbers)	
				
		Print "Random number:" + randomNumber

	End While
	
End

Image courtesy of nonicknamephoto at FreeDigitalPhotos.net

First an array is initialized with desired integers. Then an random integer from the array is fetched using random index inside the array.  After that the numbers after the drawn number are moved to “left”. For the end the amount of integers is decreased by 1 and the array is resized by the new amount of integers. That’s it.

This way one gets always different random integer from the set of desired integers.

Furthermore, if one would like to get, for example, number four (4) to be drawn three times, one could just include 4 three times in the array.

Depending on what one is doing, one could keep a copy of the original array for future usage.

Although I have used Monkey2 as example programming language, it should be easy to implement similar implementation to many other programming languages quite straight forwardly.