对象跟随鼠标移动(用向量)
Posted 行尸走肉
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了对象跟随鼠标移动(用向量)相关的知识,希望对你有一定的参考价值。
这里下载一个gameobject的库,封装了向量计算
#_*_coding:utf-8_*_ from gameobjects.vector2 import * background_image_filename=‘./images/sea.jpg‘ sprite_image_filename=‘./images/fish.png‘ import pygame from pygame.locals import * from sys import exit pygame.init() screen=pygame.display.set_mode((640,480),0,32) background=pygame.image.load(background_image_filename).convert() sprit=pygame.image.load(sprite_image_filename) clock=pygame.time.Clock() position=Vector2(100.0,100.0) heading=Vector2() while True: for event in pygame.event.get(): if event.type==QUIT: exit() screen.blit(background,(0,0)) screen.blit(sprit,position) time_passed_seconds=clock.tick()/1000.0 #参数前面加*:把列表或元祖展开 destination=Vector2(*pygame.mouse.get_pos())-Vector2(*sprit.get_size())/2. vector_to_mouse=Vector2.from_points(position,destination) vector_to_mouse.normalize() heading=heading+(vector_to_mouse/6) position+=heading*time_passed_seconds pygame.display.update()
以上是关于对象跟随鼠标移动(用向量)的主要内容,如果未能解决你的问题,请参考以下文章