下载一个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()