Saltar para o conteúdo

Panda3D/Exemplos/Eventos

Origem: Wikilivros, livros abertos por um mundo aberto.

Este código faz mover o texto ao pressionar certas teclas do mouse ou teclado:

import direct.directbase.DirectStart
from direct.gui.OnscreenText import OnscreenText
from direct.showbase.DirectObject import DirectObject

texto = OnscreenText("arrow_left - esquerda\narrow_right - direita\nmouse1 - cima\nmouse2 - baixo\na (pressionar) - direita e cima\na (soltar) - centralizar")

x = 0.0
y = 0.0
	
def pressionouTeclaArrowLeft():
	global x
	global y
	x -= 0.01
	texto.setPos(x, y)
	
def pressionouTeclaArrowRight():
	global x
	global y
	x += 0.01
	texto.setPos(x, y)
	
 
def pressionouMouse1():
	global x
	global y
	y += 0.01
	texto.setPos(x, y)
	
def pressionouMouse2():
	global x
	global y
	y -= 0.01
	texto.setPos(x, y)

def pressionouTeclaA():
	global x
	global y
	x += 0.01
	y += 0.01
	texto.setPos(x, y)

def soltouTeclaA():
	global x
	global y
	x = 0.0
	y = 0.0
	texto.setPos(x, y)

directObject = DirectObject()
	
directObject.accept("arrow_left", pressionouTeclaArrowLeft)
directObject.accept("arrow_right", pressionouTeclaArrowRight)
directObject.accept("mouse1", pressionouMouse1)
directObject.accept("mouse2", pressionouMouse2)
directObject.accept("a", pressionouTeclaA)
directObject.accept("a-up", soltouTeclaA)

run()

Mantendo a tecla pressionada

[editar | editar código-fonte]
import direct.directbase.DirectStart
from direct.showbase.DirectObject import DirectObject
from direct.task.Task import Task


spaceDown = 0

def setSpaceDown():
	global spaceDown
	spaceDown = 1

def setSpaceUp():
	global spaceDown
	spaceDown = 0

directObject = DirectObject()
directObject.accept("space", setSpaceDown)
directObject.accept("space-up", setSpaceUp)


def taskTeclaSpace(task):
	global spaceDown
	if spaceDown == 0:
		print "Tecla space não está sendo pressionada"
	else:
		print "Tecla space está sendo pressionada"
	return task.cont

taskMgr.add(taskTeclaSpace, "Task da tecla space")


run()