Simple 1st Camera rotation around X and Y axis

Hi, everybody
Finally I decided to try playing a little bit with Blender Game Engine and Python and I already have being for more than 5 hours with this. I managed to create a simple scene (skydome and a platform) and I intend to make the camera act like a 1st person game view. I want it to be able to turn around the horizontal axis and also rotate around the vertical axis (like when you look upward and downward)
stc=1
So, I added an XYZ empty (this is Blender 2.6.6), and set the parent to the camera (which I suppose doesn’t matter for the resolution of my problem, but anyway, in case it has something to do). Now, in order to make the camera rotate when I move the mouse, I created this simple script (a “frankenscript” made out from lot of pieces I found on the Internet) and I assigned it to the Mouse sensor (Movement event):

import GameLogic
import Rasterizer

# Mouse sensivity                                                                          #
mouseSensivity = 0.0005


controller = GameLogic.getCurrentController()
mouseSensor = controller.sensors["Mouse"]

rotCamera = controller.actuators["rotCamera"]


# Size of the game window                                                              #
screenWidth = Rasterizer.getWindowWidth()
screenHeight = Rasterizer.getWindowHeight()


# Get mouse movement coordinates                                                  #
def mouseMove():
    # Distance moved from screen center                                            #
    x = screenWidth/2 - mouseSensor.position[0] 
    y = screenHeight/2 - mouseSensor.position[1]


    # Intialize mouse                                                                        #
    if hasattr(GameLogic, 'init') == False:
        x = 0
        y = 0
        GameLogic.init = True


    return (x, y)


# Get mouse position                                                                       #
mousePosition = mouseMove()


# Amount, direction and sensitivity                                                     #
leftRight = mousePosition[0] * mouseSensivity
upDown = -mousePosition[1] * mouseSensivity


# Set the rotation values
rotCamera.dRot = [upDown, leftRight, 0.0]


# Use them
controller.activate(rotCamera)


# Center mouse in game window
Rasterizer.setMousePosition(int(screenWidth/2), int(screenHeight/2))




The camera rotates but at some moment the angle goes weird, like in this image


It’s like it also rotates around the Z axis (or the axis that is parallel to the camera). If I change upDown by 0.0 in “# Set the rotation values” I can rotate correctly around the horizontal axis (but I won’t be able to look up and down). I don’t know if it is the script or some properties of the camera. Could you give me some tips?

Thanks