def __init__(self, filename): # If we're not going to use this function ANYWHERE ELSE # why expose it? Make it a local function. def loadImage(filename): image = pygame.image.load(filename) if image.get_alpha() is None: image = image.convert() else: image = image.convert_alpha() return image self.image = loadImage(filename) self.angle = vector2D(1, 0) self.position = vector2D(0, 0)
def __init__(self,com): self.MD = MecanumBase() self.cmdbuf = self.MD.stop() self.cmd = 0 self.cmdstr = [u'停止',u'平移',u'旋转'] self.seropened = False self.ser = None self.mp = (0,0) self.effective = 0 self.driving = False self.center = vector2D(216,213) self.END =0 pygame.init() self.scr = pygame.display.set_mode((537,429),0,32) self.background = pygame.image.load(script_path+'//'+'background.jpg').convert() self.label = label(self.scr,(460,30),'left',text=u'端口:',textsize=13,forecolor=(0,0,0,0),textcolor=(255,255,0),textfont='stsong') self.textbox = textbox(self.scr,(460+self.label.getsize()[0],30),anchor='left',siz = (32,20),textcolor=(255,0,0),forecolor=(200,200,200,255),cusorcolor=(255,0,0)) self.textbox.focused = 1 self.label2 = label(self.scr,(460,60),'left',text=u'未连接',textsize=13,forecolor=(0,0,0,0),textfont='stsong',textcolor=(255,255,0),borderon=1,bordercolor = (255,255,255,255)) self.openbt = button(self.scr,(460,90),'left',(60,25),textsize = 15,bordercolor=(0,0,255),forecolor=(200,200,200,255),text = u'连接',textcolor=(255,255,0),textfont='stsong') self.cmdlabel = label(self.scr,(460,120),'left',text=u'停止',textsize=13,forecolor=(0,0,0,0),textcolor=(255,255,0),borderon=1,bordercolor = (255,255,255,255),textfont='stsong') self.transbt = button(self.scr,(460,150),'left',(60,25),textsize = 15,bordercolor=(0,0,255),forecolor=(200,200,200,255),text = u'平移',textcolor=(255,255,0),textfont='stsong') self.rotatebt = button(self.scr,(460,180),'left',(60,25),textsize = 15,bordercolor=(0,0,255),forecolor=(200,200,200,255),text = u'旋转',textcolor=(255,255,0),textfont='stsong') self.stopbt = button(self.scr,(216,213),'center',(60,60),textsize = 30,bordercolor=(0,0,255),forecolor=(200,200,200,0),text = u'停止',textcolor=(255,255,0),textfont='stsong') self.cursorlabel = label(self.scr,(0,0),'lefttop',text='',textsize=15,forecolor=(0,0,0,255),textcolor=(255,255,255),textfont='stsong',borderon = 0,visible = False) self.clk = pygame.time.Clock()
def __init__(self, resolution=(640,480)): pygame.init() self.screen = pygame.display.set_mode(resolution) self.clock = pygame.time.Clock() self.mysprite = imagedata.sprite("test2.png") self.mysat = imagedata.sprite("test3.png") self.mysatvec = vector2D(50,0)
def __init__(self, parent, xpos = 0, ypos = 0, angle = 0.0, xsize = 1.0, ysize = 1.0, **args): self.childlist = [] self.parent = parent if parent is not None: parent.addNode(self) self.id = sgnode.ids sgnode.ids += 1 sgnode.nodelist.append(self) # Setup base node properties self.__position = vector2D( xpos, ypos ) self.__angle = vector2D() self.__angle.angle = angle self.__size = vector2D( xsize, ysize ) # Here we'll do specific startup stuff. doStartup(**args)
def run(self): while True: ev = pygame.event.get() for e in ev: if e.type == QUIT: self.END = 1 self.scr.blit(self.background,(0,0)) self.label.update() self.label2.update() self.cmdlabel.update() self.cursorlabel.update() self.textbox.update(ev) self.openbt.update(ev,self.openbtcallback) self.transbt.update(ev,self.transbtcallback) self.rotatebt.update(ev,self.rotatebtcallback) self.stopbt.update(ev,self.stopbtcallback) if self.seropened: if not self.driving: for e in ev: if e.type == MOUSEMOTION: self.mp = e.pos self.mpv = vector2D(self.mp[0],self.mp[1])-self.center if self.mpv.Len>55 and self.mpv.Len<210: a1,a2 = self.direct_measure(self.mpv) self.cursorlabel.visible = 1 self.cursorlabel.position = self.mp if self.cmd == 1: self.cursorlabel.text = u'平移 x:'+str(int(self.mpv.X))+' y:'+str(int(self.mpv.Y)) elif self.cmd == 2: self.cursorlabel.text = u'旋转 '+str(int(a2))+u'度' else: self.cursorlabel.visible = False else: self.cursorlabel.visible = 0 elif e.type == MOUSEBUTTONUP: self.mp = e.pos self.mpv = vector2D(self.mp[0],self.mp[1])-self.center if self.mpv.Len>55 and self.mpv.Len<210: a1,a2 = self.direct_measure(self.mpv) if self.cmd == 1: self.cmdbuf = self.MD.translateV(VELTRANSLATE,int(a1)) self.lstclk = time.clock() self.endclk = self.lstclk+4*self.mpv.Len/VELTRANSLATE self.driving = True elif self.cmd == 2: self.cmdbuf = self.MD.rotateV(-VELROTATE*np.sign(a2)) self.lstclk = time.clock() self.endclk = self.lstclk+abs(a2)/VELROTATE self.driving = True else: self.cmdbuf = self.MD.stop() else: pygame.draw.circle(self.scr,(255,0,0),self.mp,5) if time.clock()-self.endclk>0: self.driving = False self.cmdbuf = self.MD.stop() self.cursorlabel.visible=False self.ser.write(self.cmdbuf) pygame.display.update() self.clk.tick(30) if self.END: break if self.seropened: self.cmdbuf = self.MD.stop() for i in range(20): self.ser.write(self.cmdbuf) time.sleep(0.05) self.ser.close() self.ser.close() pygame.quit()