def rotate(self): '''rotates facing to match direction''' frame = self.curFrame self.oldCenter = frame.get_rect().center #find the angle between default (RIGHT) and current direction angle = tools.calcAngle(self.direction, logic.RIGHT) #round the angle to integer if float angle = int(round(angle,0)) #print 'current angle for {0}: '.format(self.name), angle #make sure it's not over 360, in order to avoid extra rotation. self.curRotation = angle while self.curRotation > 359: self.curRotation -= 360 #print self.curRotation #drawing will be rounded self.curRotation= self.roundTo(self.curRotation,2) #self.drawable = pygame.transform.rotate(frame, self.curRotation) self.doRotate(self.curRotation)
def eventHandler(): """handles events to the pygame screen""" #degree = tools.calcAngle(actors.player.rect.center, pygame.mouse.get_pos()) #actors.player.doRotate(degree-90) #degree = actors.player.calcAngle(actors.player.getPos(), pygame.mouse.get_pos()) #actors.player.doRotate(degree) #print 'degree difference between player and mouse:', degree, 'degrees' #rotate player's frame in relation to mouse: for event in pygame.event.get(): if event.type == pygame.MOUSEBUTTONDOWN: print 'Button: {0}, Position: {1}'.format(event.button, event.pos) #print 'player direction: {0} and pos: {1}'.format(actors.player.direction, actors.player.getPos()) if event.type == pygame.QUIT: global gameRunning gameRunning= 0 print 'caught close command' elif event.type == pygame.KEYDOWN or event.type== pygame.KEYUP: key_event(event) elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 3: print 'event.pos ', event.pos #actors.player.pos = event.pos #actors.player.go(False) #mouse and player po mpos = event.pos ppos = actors.player.getPos() print '\nmpos {0}, ppos {1}'.format(mpos, ppos) #sub mpos from ppos sub = tools.V.subtract(mpos, ppos) print 'sub pos: {0}'.format(sub) #normalize sub norm = tools.V.norm(sub) print 'norm pos: {0}'.format(norm) #calculate the angle angle = tools.calcAngle(ppos, mpos) print 'angle between ppos and mpos: {0}'.format(angle) #player direction = norm actors.player.direction = norm actors.player.rotate() elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 4: bob.x, bob.y = event.pos elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: print event.button print 'mouse pressed' if stats.mousePressed: actors.player.go(False) #take pos as vector: mousePos = (x,y) = event.pos circlePos = actors.player.getPos() #print mousePos, circlePos #in relation to circle, directions the vector away from them. #subtract current selfx from x and same for selfy and y direction= tuple([a - b for a, b in zip(mousePos, circlePos)]) print direction, 'is direction' home = [a /200 for a in direction] print 'home', home #normalize home normed = tools.V.norm(home) #print 'normalized', normed #new direction is normed actors.player.changeDir(normed) actors.player.go(True) stats.mousePressed = True #draw a line from player.pos to mouse.pos pygame.draw.rect(visuals.screen, visuals.BLACK, actors.player.rect) pygame.draw.line(visuals.screen, visuals.BLACK, actors.player.rect.center, mousePos)