Just a little help with mouse/ keyboard controllers

Hello! New to the whole game making. I’m trying to make a very simple game. You mouse over one of the several blocks, click and drag it, and then drop it. I got this to work with left click. However it would select all the blocks, so I was gonna just try and give each block it’s own keyboard key. (Q,W,E,R… and so on.) However, I can’t get my script to work with keyboard buttons. I downloaded someone’s file, and I learned it worked with both mouse and keyboard buttons. I just need a bit of help here.

My script, which is also from another video I watched:
Code:
import bge

cont = bge.logic.getCurrentController()
owner = cont.owner

mouse = cont.sensors[“Mouse”]
width = bge.render.getWindowWidth()
width = width/2

pos = mouse.position

posX = width-pos[0]
posX = posX0.02-1
owner.localPosition.x = posX

height = bge.render.getWindowHeight()
height = height/2

posZ = height-pos[1]
posZ = posZ*0.02
owner.localPosition.z = posZ
(Here on the top is the downloaded files pic, and the one on the bottom is my own.)
/uploads/default/original/4X/b/6/6/b66c8a2bf38ffb0a17defa646caea5b8ddedb3d3.jpgd=1454811308&thumb=1/uploads/default/original/4X/3/3/e/33eb5a0cc3e14a922f106fc5c870ceea04337046.pngd=1454811619&thumb=1

Anyways, I think I just need to tweak the logic nodes or the python. Thanks for any help! :smiley: (So, I know it looks like I just copied and pasted other peoples code. That is pretty much what I did. I’ve done a bunch of messing around with the code and logic, but I still haven’t figured it out yet…)
(I pasted this from where I wrongly posted it, just in case someone see’s both.)

Here is a demo and some code that will allow players to move objects with their mice.

Sensors you will need are:

Always - with the left ‘’’ checked
Mouse - rename to “Click”
Mouse - change ‘Left click’ to ‘Mouse over any’ and rename it “MouseOver”

Here is some code that will help you out:

import bge
bge.render.showMouse(1) #renders mouse

cont = bge.logic.getCurrentController()
own = cont.owner
scene = bge.logic.getCurrentScene()

cubeObj = scene.objects[“Cube”]
sphereObj = scene.objects[“Sphere”]

keyboard = bge.logic.keyboard.events

mousePos = cont.sensors[“MouseOver”].hitPosition #as integer tuple (gets where in the global space mouse touches an object)
mouseOverObject = str(cont.sensors[“MouseOver”].hitObject) #as KX_Object (convert to string so it can be compared to other strings)
click = cont.sensors[“Click”].positive #as boolean (True while left mouse is pressed, False when left mouse is released)

if mouseOverObject == “Cube”:if click:
[INDENT=2]cubeObj.localPosition.y = mousePos[1]
cubeObj.localPosition.z = mousePos[2][/INDENT]

if mouseOverObject == “Sphere” and click:sphereObj.localPosition.y = mousePos[2]
sphereObj.localPosition.z = mousePos[1]

if keyboard[bge.events.QKEY] == bge.logic.KX_INPUT_JUST_ACTIVATED:print(“A has been pressed”)
#put code here when ‘Q’ is pressed
if keyboard[bge.events.WKEY] == bge.logic.KX_INPUT_ACTIVE:print(“W is being held”)
#put code here when ‘W’ is held down

And a reference image:


Here is the .blend file if you need it: MouseKeyboardTutorial.blend (498 KB)

Hope this helped!

Here I made a demo for you.

For this code to work in your project you will need the following sensors:

Always - with the left ‘’’ checked
Mouse - named “Click”
Mouse - change ‘Left click’ to ‘Mouse over any’ and rename “MouseOver”

Here is the code:

import bgebge.render.showMouse(1) #renders mouse

cont = bge.logic.getCurrentController()
own = cont.owner
scene = bge.logic.getCurrentScene()

cubeObj = scene.objects[“Cube”]
sphereObj = scene.objects[“Sphere”]

keyboard = bge.logic.keyboard.events

mousePos = cont.sensors[“MouseOver”].hitPosition #as integer tuple (gets where in the global space mouse touches an object)
mouseOverObject = str(cont.sensors[“MouseOver”].hitObject) #as KX_Object (convert to string so it can be compared to other strings)
click = cont.sensors[“Click”].positive #as boolean (True while left mouse is pressed, False when left mouse is released)

if mouseOverObject == “Cube”:
if click:
cubeObj.localPosition.y = mousePos[1]
cubeObj.localPosition.z = mousePos[2]

if mouseOverObject == “Sphere” and click:
sphereObj.localPosition.y = mousePos[2]
sphereObj.localPosition.z = mousePos[1]

if keyboard[bge.events.QKEY] == bge.logic.KX_INPUT_JUST_ACTIVATED:
print(“A has been pressed”)
#put code here when ‘Q’ is pressed
if keyboard[bge.events.WKEY] == bge.logic.KX_INPUT_ACTIVE:
print(“W is being held”)
#put code here when ‘W’ is held down

And a reference image for you:

[ATTACH=CONFIG]423172[/ATTACH]

Demo file here: [ATTACH]423173[/ATTACH]

:open_mouth: Wow! I got it working! Kinda stinks that I have to add to the code for every object, but I’ll live. Unless there’s away around that? Also, when I’m moving one object and it goes over another, they switch. A click and hold or something would fix that right?
Or, I could just use keyboard presses as well. I got the buttons to work, it just moves the object to the center of the screen. I’m guessing it’s because none of the mouse functions are called until that point, leaving it with a default value of… Well, whatever the center of the screen is. Surely there’s some way to do it… I just don’t know enough about coding or Python, and even Blender, to figure out the HOW.
Anyways, thanks so much! I still could make it work, albeit a bit glitchy.

:open_mouth: I got it working! I just have to set the background as an object, that way Mouse is over any object is always true. Then, holding the keyboard button moves it! Great!

Still have to add code for every object, but I think I can deal with that. Now I just gotta set it up. And maybe even a save and reset button. Saw a post about using empties? I’ll keep working on it. I’ll give you a .blend and a video once it all gets going! Thanks so much!