Starfield for games

On monday’s afternoon waiting a C++ lesson, I surf on the web. But there aren’t a lot of news, so in a fully bother I go on a flash game’s website and I play a game with Starship, asteroid… I’m sure you know that kind of game 😉 And I ask myself about it mechanism.

After some research I discover the term “Starfield” but no explanation nowhere about how it works, therefore I decide to programme one !
So in fact a Starfield is a perpetual movement of Stars in a restrected field. I know this a little short 😀
The idea is to have a movement in your field and only there, so if a Star go away it should be deleted or puted back. And in fact that the field itself which is moving on the stage.

There is all the code below, but look at the application before !
Click on the animation and use arrow’s keyboard for moving the Starship. In this example the Starship never moves, it’s the Starfield !!

Continue reading Starfield for games

Recursion with factorial

A recursive function is a procedure or subroutine, implemented in a programming language, whose implementation references itself.
It is really useful, try to make a program like Tower of Hanoi with an iterative algorithm… good luck !

So I will give you an easy method to create a recursive algorithm, I choose the example of factorial because of it simplicity.
Mathematical’s reminder : 4! = 4 * 3 * 2 * 1 = 24.

There are 3 steps :
– parameter(s) : identify all the parameters that you need to resolve the problem. They will decrease at each call of the function. In the factorial’s case, it is the factorial’s number.

function factorialCalculation(factorial:uint):uint

– exception’s case : this is when the procedure stops to call itself.

if (factorial == 1) { return 1; }

– recursive’s case : where recursive call is implemented, don’t forget to decrease (generally) parameter(s).

return factorial * factorialCalculation(factorial - 1);

Now all the code :

var factorial:uint = 4;
var resul:uint = 0;
 
resul = factorialCalculation(factorial);
 
function factorialCalculation(factorial:uint):uint {
 
	if (factorial == 1) {
		return 1;
	} else {
		return factorial*factorialCalculation(factorial-1);
	}
}

Be careful : recursive function are really heavy. Factorial’s case is just an easy example, it should be implemented as an interative function !

And for the fun with a ternary operation :

function factorialCalculation(factorial:uint):uint {
 
	return (factorial == 1) ? 1 : factorial * factorialCalculation(factorial - 1);
}

😀

Setting-up MAMP for Php & Ruby on Rails with MySQL

If you want to develop php on localhost with you Mac, you sould use MAMP. But it doesn’t display error. To change that, just go in bin/php/php5.6.2/conf/php.ini and change display_errors = “Off” by “On”. Easy, but if we don’t know it could take time.

Now if you want to use Ruby on Rails on MAMP with MySQL instead of SQLite you need to download and install MAMP’s libraries.
After that, you should install Mysql for Ruby using Gem, do the command line in your terminal :

sudo env ARCHFLAGS="-arch x86_64" gem install mysql -- --with-mysql-config=/Applications/MAMP/Library/bin/mysql_config

And finally, you will have “gem installed”. To create a new Rails’s project with MySQL :  rails -d mysql myProject. Next open the file config/database.yml and add the line : socket: /Applications/MAMP/tmp/mysql/mysql.sock below host.
Don’t forget to put your password !

Now you can start working 😉

Simple BevelFilter

Some months ago, I was at Turbulent Média Inc. in Montréal, Canada as a flash trainee. During this internship I discovered the BevelFilter class.
At this time, I’m creating my Portfolio (it will be online soon), and I reuse an effect discovered there for buttons.
Here is my button.

And now the simple code without any Tween !

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package {
 
   import flash.display.Sprite;
   import flash.events.Event;
   import flash.filters.BevelFilter;
   import flash.filters.BitmapFilter;
 
   public class Bevel extends Sprite {
 
       private var _effect:BevelFilter;
       private var _filter:BitmapFilter;
       private var _myFilters:Array;
       private var _angle:uint;
 
       public function Bevel():void {
 
	    btn.addEventListener(Event.ENTER_FRAME, _animation);
	}
 
	private function _animation(evt:Event):void {
 
	   _effect = new BevelFilter(5, _angle, 0xFFFFFF, 1, 0x000000, 10, 10, 1);
	   _filter = _effect;
	   _myFilters = [];
	   _myFilters.push(_filter);
	   btn.filters = _myFilters;
 
	    _angle +=2;
 
	    if (_angle > 360) {
		_angle = 0;
	    }
         }
    }
}

Mathematicals and AS3 = Biomorphs

A biomorph is a shape resembling that of a living organism (such as bacteria), though not necessarily of biotic origin. It is created with Mathematicals functions, such as fractal.
Based on the work of C. Pickover, I have made a computer program with AS3 that you can try below. I used the as3mathlib because of Complex Numbers.

Biomorphs – the computing time could be long, wait a moment.

Here is a part of the code. All comments are welcome 🙂

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
private function calculate():void {
 
  var r:Number = 0;
  var n:uint;
 
  for (var abs:Number = -3; abs < 3; abs += 0.01) {
 
    for (var ord:Number = -2.5; ord < 2.5; ord += 0.01) {
 
    n = 0;
    r = 0;
 
    z  = new Complex(abs, ord);
 
      while ((n < 11) && (r < 10)) {
 
      z = f(z, A, B, C, D, E, F, G);
      r = Complex.modulo(z);
 
      if ((n == 10) || (r > 10)) {
 
        putPixels(Complex.abs(z), abs, ord);
      }
      n++;
      }
    }
  }
  showBitmap();
}
 
private function f(z:Complex, A:Complex, B:Complex, C:Complex, D:Complex, E:Complex, F:Complex, G:Complex):Complex {
 
  var i:Complex = new Complex(0, 1);
 
  var inter1:Complex = Complex.adds(Complex.power(z, A), B);
  var inter2:Complex = Complex.adds(Complex.mult(C, i), Complex.mult(D, Complex.cos(z)));
  var inter3:Complex = Complex.adds(Complex.mult(E, Complex.sin(z)), Complex.mult(F, Complex.exp(z)));
  var inter4:Complex = Complex.mult(G, Complex.exp(z));
 
  return Complex.adds(Complex.adds(inter1, inter2), Complex.adds(inter3, inter4));
}
 
private function putPixels(h:Complex, abs:Number, ord:Number):void {
 
  abs = Math.round((abs + 3) * 100);
  ord = Math.round((ord + 2.5) * 100);
 
  var biomorphe:Number = Complex.norm(h) / 2;
 
  if (biomorphe > 1500000) {
    monBitmap.setPixel(abs, ord, 0xFFFFFF);
  } else if (biomorphe > 15000) {
  monBitmap.setPixel(abs, ord, 0x0099CC);
  } else if (biomorphe > 150) {
  monBitmap.setPixel(abs, ord, 0x993333);
  } else {
  monBitmap.setPixel(abs, ord, 0xFFFF00);
  }
}

Hello World !

Hi, welcome on my blog !

My name is Aymeric Lamboley, I’m a french student in Computer Science at the UTBM, a french engineering school !

On this blog I will speak about Multimedia, Programming, Mac and why not music, movies, video games or anything else.

I hope you will enjoy information on this blog !

Best Regards,
Aymeric