Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
138 views
in Technique[技术] by (71.8m points)

python - Mouse motion function isn't working for me

I have been trying to follow an older tutorial for pyglet, and it has been going good for the most part, but I have run into a problem with seemingly no answer, and that is that the camera doesn't move when I move the mouse.

The code here:

from pyglet.gl import *

from pyglet.window import key

import math

class Model:
    
    def get_tex(self, file):
        tex= pyglet.image.load(file).get_texture()
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)

        return pyglet.graphics.TextureGroup(tex)
    
    def __init__(self):

        self.top = self.get_tex('grass_top.png')
        self.side = self.get_tex('grass_side.png')
        self.bottom = self.get_tex('dirt.png')

        self.batch= pyglet.graphics.Batch()

        tex_coords = ('t2f',(0,0,1,0,1,1,0,1,))
        
        x,y,z = 0,0,-1
        X,Y,Z = x+1, y+1, z+1

        self.batch.add(4,GL_QUADS, self.side, ('v3f',(x,y,z, X,y,z, X,Y,z, x,Y,z)),tex_coords)

    def draw(self):
        self.batch.draw()



class Player:
    def __init__(self):
        self.pos = [0,0,0]
        self.rot = [0,0]

    def mouse_motion(self,dx,dy):
        dx/=8; dy/=8; self.rot[0]+=dy; self.rot[1]-=dx
        if self.rot[0]>90: self.rot[0] = 90
        elif self.rot[0]<-90: self.rot[0] = -90

    def update(self,dt,keys):
        s= dt*10
        rotY = self.rot[1]/180*math.pi
        dx, dz = s*math.sin(rotY),s*math.cos(rotY)
        if keys[key.W]: self.pos[0] += dx; self.pos[2] -= dz
        if keys[key.S]: self.pos[0] -= dx; self.pos[2] += dz
        if keys[key.A]: self.pos[0] -= dz; self.pos[2] -= dx
        if keys[key.D]: self.pos[0] += dz; self.pos[2] += dx        
        
        if keys[key.SPACE]: self.pos[1] += s
        if keys[key.LCTRL]: self.pos[1] -= s

class Window(pyglet.window.Window):

    def Projection(self):glMatrixMode(GL_PROJECTION); glLoadIdentity()
    def Model(self):glMatrixMode(GL_MODELVIEW); glLoadIdentity()
    def set3d(self):self.Projection();gluOrtho2D(0, self.width,0,self.height);self.Model()
    def set3d(self):self.Projection();gluPerspective(70, self.width/self.height,0.05,1000);self.Model()

    def setLock(self,state): self.lock = state; self.set_exclusive_mouse(state)
    lock = False; mouse_lock = property(lambda self:self.lock,setLock)
    
    def __init__(self,*args,**kwargs):
        super().__init__(*args,**kwargs)
        self.set_minimum_size(width=300, height=200)
        self.keys = key.KeyStateHandler()
        self.push_handlers(self.keys)
        pyglet.clock.schedule(self.update)

        self.model = Model()
        self.player = Player()

    def on_mouse_motion(self,x,y,dx,dy):
        if self.mouse_lock: self.player.mouse_motion(dx,dy)
        
              
    def on_key_press(self,KEY,MOD):
        if KEY == key.ESCAPE: self.close()
        elif KEY == key.E: self.mouse_lock = not self.mouse_lock

    def update(self, dt):
        self.player.update(dt,self.keys)

    def on_draw(self):
        self.clear()
        self.set3d()
        #glRotatef(-30, 1, 0,0)
        x,y,z = self.player.pos
        glTranslatef(-x,-y,-z)
        self.model.draw()
if __name__ == '__main__':
    window= Window(width=400, height=300, caption='Lame Game', resizable=True)
    glClearColor(0.5,0.4,1,1)
    pyglet.app.run()
question from:https://stackoverflow.com/questions/65848274/mouse-motion-function-isnt-working-for-me

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The only thing I can think of to recommend is to look at the latest pyglet docs and try to re-write the code, and if you run into any problems there ask for help again.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...