【发布时间】:2019-08-05 17:22:37
【问题描述】:
最初,图像开始向 (0,0) 方向移动。在每一帧中,对象使用pygame.mouse.get_pos() 查看光标的当前位置并将其方向更新为direction = .9*direction + v,其中v 是一个长度为10 的向量,它从图像中心指向鼠标位置。
这就是我所拥有的:
from __future__ import division
import pygame
import sys
import math
from pygame.locals import *
class Cat(object):
def __init__(self):
self.image = pygame.image.load('ball.png')
self.x = 1
self.y = 1
def draw(self, surface):
mosx = 0
mosy = 0
x,y = pygame.mouse.get_pos()
mosx = (x - self.x)
mosy = (y - self.y)
self.x = 0.9*self.x + mosx
self.y = 0.9*self.y + mosy
surface.blit(self.image, (self.x, self.y))
pygame.display.update()
pygame.init()
screen = pygame.display.set_mode((800,600))
cat = Cat()
Clock = pygame.time.Clock()
running = True
while running:
screen.fill((255,255,255))
cat.draw(screen)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
Clock.tick(40)
问题在于,当我运行代码时,图像会随着鼠标光标移动,而不是跟随它。我该如何改进呢?
【问题讨论】:
标签: python pygame mouseevent mouse-cursor