日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python梦幻西游鼠标偏移_PYTHONPYGAME如何向鼠标位置移动和旋转多边形?

發布時間:2025/3/20 python 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python梦幻西游鼠标偏移_PYTHONPYGAME如何向鼠标位置移动和旋转多边形? 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

要將對象移向鼠標,可以使用向量。只需從鼠標位置減去位置,將得到的矢量規格化,然后以所需的速度對其進行多次修改。這就給了你一個速度向量,你可以把它加到每一幀的self.pos上(同時更新rect,作為blit位置和碰撞檢測)。在

調用Vector2.as_polar方法(它返回polar coordinates)來獲得向量的角度,然后使用它來旋轉原始圖像。在import pygame as pg

from pygame.math import Vector2

class Entity(pg.sprite.Sprite):

def __init__(self, pos, *groups):

super().__init__(*groups)

self.image = pg.Surface((50, 30), pg.SRCALPHA) # A transparent image.

# Draw a triangle onto the image.

pg.draw.polygon(self.image, pg.Color('dodgerblue2'),

((0, 0), (50, 15), (0, 30)))

# A reference to the original image to preserve the quality.

self.orig_image = self.image

self.rect = self.image.get_rect(center=pos)

self.vel = Vector2(0, 0)

self.pos = Vector2(pos)

def update(self):

# Subtract the pos vector from the mouse pos to get the heading,

# normalize this vector and multiply by the desired speed.

self.vel = (pg.mouse.get_pos() - self.pos).normalize() * 5

# Update the position vector and the rect.

self.pos += self.vel

self.rect.center = self.pos

# Rotate the image.

# `Vector2.as_polar` returns the polar coordinates (radius and angle).

radius, angle = self.vel.as_polar()

self.image = pg.transform.rotozoom(self.orig_image, -angle, 1)

self.rect = self.image.get_rect(center=self.rect.center)

def main():

screen = pg.display.set_mode((640, 480))

clock = pg.time.Clock()

all_sprites = pg.sprite.Group()

entity = Entity((100, 300), all_sprites)

done = False

while not done:

for event in pg.event.get():

if event.type == pg.QUIT:

done = True

all_sprites.update()

screen.fill((30, 30, 30))

all_sprites.draw(screen)

pg.display.flip()

clock.tick(30)

if __name__ == '__main__':

pg.init()

main()

pg.quit()

總結

以上是生活随笔為你收集整理的python梦幻西游鼠标偏移_PYTHONPYGAME如何向鼠标位置移动和旋转多边形?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。