码迷,mamicode.com
首页 > 移动开发 > 详细

对象跟随鼠标移动(用向量)

时间:2017-08-27 16:15:48      阅读:276      评论:0      收藏:0      [点我收藏+]

标签:quit   ini   set   back   nat   obj   local   filename   ges   

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

 

对象跟随鼠标移动(用向量)

标签:quit   ini   set   back   nat   obj   local   filename   ges   

原文地址:http://www.cnblogs.com/superxuezhazha/p/7440438.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!