Hello,
I would like to create a game, where for example I can click on a cube and it opens a browser like the internet explorer. But at the Game engine I cant find a mouse click event like that. So I have to do it with python, but I´m an absolute beginner with that. Can anyone help me with that?
Thanks a lot!
So I´ve starting scripting and my script is like this:
import bpy
import webbrowser
scene=bpy.context.scene
scene.objects.keys()
if (’<Button-1>’):
![file:///C:\Users\CyHo\AppData\Local\Temp\msohtmlclip1\01\clip_image001.gif](file:///C:\Users\CyHo\AppData\Local\Temp\msohtmlclip1\01\clip_image001.gif) webbrowser.open(‘http://www.google.com’)
When i click on the button run script it is opening the internet explorer, but when I´m playing the game there is nothing happening when I press the left mouse button…
Can someone help me?
Because you’re not declaring the statement correctly. At the moment your script is checking if ‘<Button-1>’ is there, which it is - thus it opens Internet Explorer. You need to declare an ‘if’ statement which also checks whether the button has been pressed. I’m not too familiar with the Blender API, however assuming that ‘<Button-1>’ is either pressed or not it should return a True or False.
This isn’t correct so you’ll need to do some reading up, however this should be the idea…
from bge import logic
mouse = logic.mouse
if ('<Button-1>'):
if mouse.events[logic.('<Button-1>')] == True:
webbrowser.open('http://www.google.com')
Better yet you should create a definition which checks so it can be called as a function.
def button_open_link(self, button, link):
if button:
if mouse.events[logic.button] == True:
return webbrowser.open(link)
Then assuming you had a button called ‘BUTTON_X_5’ and you wanted to run the definition with this you could do:
self.button_open_link('BUTTON_X_5', 'http://www.google.com')
…this would take the button and link you want to open and it’ll execute the function when you call it.
Have you had a look at https://www.codecademy.com/learn, maybe it’s worth doing the Python course to get a better understanding of loops and declaring statements.