def point_camera(self): pos = mouse.get_rel() x_offset = pos[0] y_offset = pos[1] x_offset *= self.sensitivity y_offset *= self.sensitivity self.yaw += x_offset * self.sensitivity self.pitch += -y_offset * self.sensitivity if self.pitch > 89.0: self.pitch = 89.0 if self.pitch < -89.0: self.pitch = -89.0 direction_x = cos(radians(self.pitch)) * cos(radians(self.yaw)) direction_y = sin(radians(self.pitch)) direction_z = cos(radians(self.pitch)) * sin(radians(self.yaw)) self.camera_front = vector.normalise( np.array([direction_x, direction_y, direction_z], dtype=np.float32)) self.camera_front = Vector3(self.camera_front) view_matrix = self.look_at(self.camera_position, self.camera_position + self.camera_front) glUniformMatrix4fv(self.view_loc, 1, GL_FALSE, view_matrix)
def getRel(self): """Returns the new position of the mouse relative to the last call to getRel or getPos, in the same units as the :class:`~visual.Window`. """ if usePygame: relPosPix=numpy.array(mouse.get_rel()) * [1,-1] return self._pix2windowUnits(relPosPix) else: #NB getPost() resets lastPos so MUST retrieve lastPos first if self.lastPos is None: relPos = self.getPos() else: relPos = -self.lastPos+self.getPos()#DON't switch to (this-lastPos) return relPos
def updateMousePosition(self): self.mousePosition = self.mousePosition + Vector2(mouse.get_rel()) if self.mousePosition.x < 0: self.mousePosition.x = 0 elif self.mousePosition.x > self.screenSize.x - self.rect.width: self.mousePosition.x = self.screenSize.x - self.rect.width if self.mousePosition.y < 0: self.mousePosition.y = 0 elif self.mousePosition.y > self.screenSize.y - self.rect.h: self.mousePosition.y = self.screenSize.y - self.rect.h self.rect.x = self.mousePosition.x self.rect.y = self.mousePosition.y
def React(self, events, keys): # Process events. for event in events: if KEYDOWN == event.type: if K_UP == event.key: self.Player.Shoot() elif K_SPACE == event.key: self.Player.ShootBomb() elif MOUSEBUTTONDOWN == event.type: leftButton, middleButton, rightButton = mouse.get_pressed() if leftButton: self.Player.Shoot() elif middleButton: self.Player.ShootBomb() # Process pressed keys. if keys[K_LEFT]: self.Player.Move(Direction.Left) elif keys[K_RIGHT]: self.Player.Move(Direction.Right) rightMouseButton = mouse.get_pressed()[2] self.Player._shieldUp = bool(keys[K_DOWN] or rightMouseButton) # Process mouse input. mouseMovement = mouse.get_rel() self.Player.Move(Direction.Right, mouseMovement[0])
if __name__ == '__main__': pygame.init() SCREEN_SIZE = (800, 600) # initialize screen size SCREEN = display.set_mode(SCREEN_SIZE) # load screen triangle = Polygon([(0, 70), (110, 0), (110, 70)]) rhombus = Polygon([(0, 80), (20, 0), (80, 0), (60, 80)]) triangle.move_ip(200, 200) rhombus.move_ip(300, 300) font = Font(None, 24) r = mouse.get_rel() grab, other = None, None while 1: SCREEN.fill((0, 0, 0)) draw.polygon(SCREEN, (255, 0, 0), triangle.P, 1) draw.polygon(SCREEN, (0, 0, 255), rhombus.P, 1) mouse_pos = mouse.get_pos() for ev in event.get(): if ev.type == KEYDOWN: if ev.key == K_q: exit() if ev.type == MOUSEBUTTONDOWN: if grab: grab, other = None, None elif rhombus.collidepoint(mouse_pos): grab = rhombus other = triangle elif triangle.collidepoint(mouse_pos):
def mouse_relpos(self): return mouse.get_rel() def update(self):
def go(): # START # Fairly obvious, initialize the pygame library pygame.init() # Create the world in which all physical things reside. It is our coordinate system and universe. world = World(vector((20.0, 20.0, 2000.0)), vector((-10.0, 0.0, 10.0))) # Create startup cameras and make the world visible to them eyecam = Camera() eyecam.registerObject(world) scopecam = Camera() scopecam.registerObject(world) # Setup the program window (just a special case of a View) screen = Screen(eyecam, vector((1000,800,0)), -1.0) screen.display = pygame.display.set_mode((1000, 800)) # Setup the display surfaces to be drawn to screen and list them main_dim = vector((1000,800,0.0)) scope_dim = vector((240,240,0.0)) mainview = View(eyecam, main_dim, zero_vector, 1.0) scopeview = Scope(scopecam, scope_dim, (main_dim - scope_dim) / 2.0, 4.0) views = [ mainview, scopeview ] # Hide the cursor mouse.set_visible(False) # Let's set up some useful variables now = pygame.time.get_ticks() # The current program time shot_fired = False # chase_bullet = False hide_scope = True lock_mouse = False power = 100.0 #mainview.reposition(eyecam) scopeview.setPower(power) timescale = 1.0 tot_mag = 0.0 tot_dur = 0.0 frames = 0 if lock_mouse: mouse.set_pos((scope_dim / 2.0).xy()) while 1: last_t = now now = pygame.time.get_ticks() t_length = float(now - last_t) if world.has_hit: shot_end = now #break if world.bullet.position.z() > world.target.position.z() + 1000.0: #world.bullet.position = vector((world.bullet.position.x(), world.bullet.position.y(), world.target.position.z() - 0.01)) shot_end = now break #pass for event in pygame.event.get(): if event.type == pygame.MOUSEBUTTONDOWN: if event.dict['button'] == 4: power += 1.0 if event.dict['button'] == 5: power -= 1.0 power = scopeview.setPower(power) print power if event.type == pygame.QUIT: sys.exit() sx, sy = (0.5 * mainview.dimensions).xy() if lock_mouse: mx, my = mouse.get_rel() mouse.set_pos(sx, sy) r = (1.0 / (10000 * power))* (vector((sx - mx, sy - my, 0))) scopecam.rotation += r eyecam.rotation += r else: mx, my = mouse.get_pos() r = (1.0 / (1000 * power))* (vector((sx - mx, sy - my, 0))) scopecam.rotation = r eyecam.rotation = r world.bullet.rotation = scopecam.rotation scopeview.reposition(scopeview.camera) mainview.reposition(mainview.camera) b1, b2, b3 = mouse.get_pressed() if b1 and not shot_fired: shot_fired = True shot_start = now world.bullet.velocity = rotate(vector((0.0,0.0,800.0)), (-world.bullet.rotation - scopeview.tweaks)) print 'Start Velocity: ' + str(world.bullet.velocity.magnitude()) #mainview.camera.rotation = zero_vector mainview.setPower(50.0) #scopeview.reposition(view.camera) #scopeview.overlay = [] # hide scope world.movers += [world.bullet] # Move everything. The world does most of the work once we tell it to, # we just need to handle the less tangible stuff. world.moveObjects(timescale * (t_length / 1000.0)) # Bullet chasing if shot_fired: #scopecam.rotation = world.bullet.rotation if hide_scope: scopeview.visible = False bp = world.bullet.position lbp = world.bullet.last_position diff = bp - lbp mag = diff.magnitude() magxz = sqrt(diff.x()**2 + diff.z()**2) frames += 1 tot_mag += mag tot_dur += t_length * timescale if chase_bullet and mag > 0.0: h = arctan2(diff.x(), diff.z()) e = arcsin(diff.y() / mag) eyecam.rotation = vector((h, -e, 0)) eyecam.position = bp & vector((1,1,1)) mainview.setPower(10.0) mainview.reposition(mainview.camera) #print eyecam.position #scopeview.reposition(eyecam) # Grab each view's camera image, then draw them to the program window in sequence for view in views: view.updateDisplay() view.draw(screen.display) # fps = 1000.0 / (t_length + (1 if t_length == 0 else 0)) screen.drawOverlay((fps,)) pygame.display.flip() for view in views: view.updateDisplay() view.draw(screen.display, (screen.dimensions - view.dimensions) / 2.0) pygame.display.flip() print "Flight time: " + str(tot_dur / 1000.0) + " s" print "Impact energy: " + str(.5 * world.bullet.mass * world.bullet.velocity.magnitude() ** 2) + " J" print 'End Position' print world.bullet.position print 'End Velocity: ' + str(world.bullet.velocity.magnitude()) print "Average frame time: " + str((tot_dur / 1000.0) / float(frames)) print "Average FPS: " + str(float(frames) / tot_dur * 1000) print "Average distance per frame: " + str(tot_mag / float(frames)) pygame.time.delay(1000) pygame.display.quit() pygame.quit()
def update(self): """move the objects or tiles as needed and handle the expansion of the tile 2d list as needed based on the user panning the map.""" delta = mouse.get_rel() if (self.selected != None): self.selected.move(delta[0], delta[1]) if (self.scrolling): self.x_offset += delta[0] self.y_offset += delta[1] #if the tiles have gone to far to the left on the x append a column #~of tiles #Use the top right tile to get the x and y value #advance through the rows and add a column of tiles to the #~right top_left = self.tiles[0][0] bottom_right = self.tiles[len(self.tiles) - 1][len(self.tiles[0]) - 1] if (bottom_right.rect.right + self.x_offset < 650): for i, row in enumerate(self.tiles): right = self.tiles[i][len(self.tiles[i]) - 1] tile = Tile('resources/terrain/grass0.png', x=right.rect.right, y=right.rect.y) tile.in_bin = False row.append(tile) #if the tiles have gone too far up on the y append a row to tiles #~and fill with Tiles if (bottom_right.rect.bottom + self.y_offset < 500): row = [] for i in range(0, len(self.tiles[0])): bottom = self.tiles[len(self.tiles) - 1][i] tile = Tile('resources/terrain/grass0.png', x=bottom.rect.x, y=bottom.rect.y + Tile.HEIGHT) tile.in_bin = False row.append(tile) self.tiles.append(row) #if the tiles have gone too far right on the x advance throught the #~rows and insert a row of tiles on the left if (top_left.rect.x + self.x_offset > 100): for i, row in enumerate(self.tiles): left = self.tiles[i][0] tile = Tile('resources/terrain/grass0.png', x=left.rect.x - Tile.WIDTH, y=left.rect.y) tile.in_bin = False row.insert(0, tile) #if the tiles have gone too far down on the y insert a new row at #~the begining and fill it with tiles if (top_left.rect.y + self.y_offset > -50): row = [] for i in range(0, len(self.tiles[0])): top = self.tiles[0][i] tile = Tile('resources/terrain/grass0.png', x=top.rect.x, y=top.rect.y - Tile.HEIGHT) tile.in_bin = False row.append(tile) self.tiles.insert(0, row)
def go(): # START # Fairly obvious, initialize the pygame library pygame.init() # Create the world in which all physical things reside. It is our coordinate system and universe. world = World(vector((20.0, 20.0, 2000.0)), vector((-10.0, 0.0, 10.0))) # Create startup cameras and make the world visible to them eyecam = Camera() eyecam.registerObject(world) scopecam = Camera() scopecam.registerObject(world) # Setup the program window (just a special case of a View) screen = Screen(eyecam, vector((1000, 800, 0)), -1.0) screen.display = pygame.display.set_mode((1000, 800)) # Setup the display surfaces to be drawn to screen and list them main_dim = vector((1000, 800, 0.0)) scope_dim = vector((240, 240, 0.0)) mainview = View(eyecam, main_dim, zero_vector, 1.0) scopeview = Scope(scopecam, scope_dim, (main_dim - scope_dim) / 2.0, 4.0) views = [mainview, scopeview] # Hide the cursor mouse.set_visible(False) # Let's set up some useful variables now = pygame.time.get_ticks() # The current program time shot_fired = False # chase_bullet = False hide_scope = True lock_mouse = False power = 100.0 #mainview.reposition(eyecam) scopeview.setPower(power) timescale = 1.0 tot_mag = 0.0 tot_dur = 0.0 frames = 0 if lock_mouse: mouse.set_pos((scope_dim / 2.0).xy()) while 1: last_t = now now = pygame.time.get_ticks() t_length = float(now - last_t) if world.has_hit: shot_end = now #break if world.bullet.position.z() > world.target.position.z() + 1000.0: #world.bullet.position = vector((world.bullet.position.x(), world.bullet.position.y(), world.target.position.z() - 0.01)) shot_end = now break #pass for event in pygame.event.get(): if event.type == pygame.MOUSEBUTTONDOWN: if event.dict['button'] == 4: power += 1.0 if event.dict['button'] == 5: power -= 1.0 power = scopeview.setPower(power) print power if event.type == pygame.QUIT: sys.exit() sx, sy = (0.5 * mainview.dimensions).xy() if lock_mouse: mx, my = mouse.get_rel() mouse.set_pos(sx, sy) r = (1.0 / (10000 * power)) * (vector((sx - mx, sy - my, 0))) scopecam.rotation += r eyecam.rotation += r else: mx, my = mouse.get_pos() r = (1.0 / (1000 * power)) * (vector((sx - mx, sy - my, 0))) scopecam.rotation = r eyecam.rotation = r world.bullet.rotation = scopecam.rotation scopeview.reposition(scopeview.camera) mainview.reposition(mainview.camera) b1, b2, b3 = mouse.get_pressed() if b1 and not shot_fired: shot_fired = True shot_start = now world.bullet.velocity = rotate(vector( (0.0, 0.0, 800.0)), (-world.bullet.rotation - scopeview.tweaks)) print 'Start Velocity: ' + str(world.bullet.velocity.magnitude()) #mainview.camera.rotation = zero_vector mainview.setPower(50.0) #scopeview.reposition(view.camera) #scopeview.overlay = [] # hide scope world.movers += [world.bullet] # Move everything. The world does most of the work once we tell it to, # we just need to handle the less tangible stuff. world.moveObjects(timescale * (t_length / 1000.0)) # Bullet chasing if shot_fired: #scopecam.rotation = world.bullet.rotation if hide_scope: scopeview.visible = False bp = world.bullet.position lbp = world.bullet.last_position diff = bp - lbp mag = diff.magnitude() magxz = sqrt(diff.x()**2 + diff.z()**2) frames += 1 tot_mag += mag tot_dur += t_length * timescale if chase_bullet and mag > 0.0: h = arctan2(diff.x(), diff.z()) e = arcsin(diff.y() / mag) eyecam.rotation = vector((h, -e, 0)) eyecam.position = bp & vector((1, 1, 1)) mainview.setPower(10.0) mainview.reposition(mainview.camera) #print eyecam.position #scopeview.reposition(eyecam) # Grab each view's camera image, then draw them to the program window in sequence for view in views: view.updateDisplay() view.draw(screen.display) # fps = 1000.0 / (t_length + (1 if t_length == 0 else 0)) screen.drawOverlay((fps, )) pygame.display.flip() for view in views: view.updateDisplay() view.draw(screen.display, (screen.dimensions - view.dimensions) / 2.0) pygame.display.flip() print "Flight time: " + str(tot_dur / 1000.0) + " s" print "Impact energy: " + str( .5 * world.bullet.mass * world.bullet.velocity.magnitude()**2) + " J" print 'End Position' print world.bullet.position print 'End Velocity: ' + str(world.bullet.velocity.magnitude()) print "Average frame time: " + str((tot_dur / 1000.0) / float(frames)) print "Average FPS: " + str(float(frames) / tot_dur * 1000) print "Average distance per frame: " + str(tot_mag / float(frames)) pygame.time.delay(1000) pygame.display.quit() pygame.quit()