让精灵在给定的旋转中行走

让精灵在给定的旋转中行走

本文介绍了Pygame 让精灵在给定的旋转中行走的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很久以前我做了一个小脚本,我想用 Pygame 把它转换成 Python.

有很多示例显示图像的旋转,但我想知道如何更改精灵的旋转以使其沿给定方向移动,而无需更改图像.

这是我的 Scratch 代码:

这是我的 Pygame 精灵类:

class Star(pygame.sprite.Sprite):def __init__(self):super().__init__()self.image = img_starself.rect = self.image.get_rect()self.velocity = [0, 0]self.rect.x = random.randint(0, window_x)self.rect.y = random.randint(0, window_y)
解决方案

使用 MOUSEBUTTONDOWN 事件检测鼠标何时被点击:

if event.type == pygame.MOUSEBUTTONDOWN:mouse_x, mouse_y = event.pos

计算从精灵中心到鼠标点击位置的向量:

dx = mouse_x - star.rect.centerxdy = mouse_y - star.rect.centery

计算向量的长度(

import pygame, random, math类星(pygame.sprite.Sprite):def __init__(self):super().__init__()self.image = img_starself.rect = self.image.get_rect()self.velocity = [0, 0]self.rect.x = random.randint(0, window_x)self.rect.y = random.randint(0, window_y)pygame.init()window_x, window_y = 500, 500window = pygame.display.set_mode((window_x, window_y))时钟 = pygame.time.Clock()img_star = pygame.Surface((20, 20), pygame.SRCALPHA)pygame.draw.circle(img_star, (255, 255, 0), (10, 10), 10)星 = 星()group = pygame.sprite.Group(star)运行 = 真运行时:时钟滴答(60)对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:运行 = 错误如果 event.type == pygame.MOUSEBUTTONDOWN:mouse_x, mouse_y = event.posdx = mouse_x - star.rect.centerxdy = mouse_y - star.rect.centerydist = math.sqrt(dx*dx + dy*dy)步骤 = 10如果距离 >0:star.rect.x += 步数 * dx/diststar.rect.y += 步数 * dy/distwindow.fill(0)group.draw(窗口)pygame.display.flip()pygame.quit()出口()

I did a little Scratch script a long time ago, which I would like to convert to Python with Pygame.

There are plenty of examples showing the rotation of the image but I would like to know how to change the sprite's rotation to make it move in a given direction, without changing the image.

Here is my Scratch code:

Here is my Pygame sprite class:

class Star(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.image = img_star
        self.rect = self.image.get_rect()
        self.velocity = [0, 0]
        self.rect.x = random.randint(0, window_x)
        self.rect.y = random.randint(0, window_y)
解决方案

Detect when the mouse is clicked with the MOUSEBUTTONDOWN event:

if event.type == pygame.MOUSEBUTTONDOWN:
    mouse_x, mouse_y = event.pos

Compute the vector from the center of the sprite to the mouse click position:

dx = mouse_x - star.rect.centerx
dy = mouse_y - star.rect.centery

Compute the length of the vector (Euclidean distance):

dist = math.sqrt(dx*dx + dy*dy)

or

dist = math.hypot(dx, dy)

Normalize the vector (Unit vector). A normalized vector has a length of 1:

if dist > 0:
    dx /= dist
    dy /= dist

Move the object a certain number of steps in the direction of the vector:

star.rect.x += steps * dx
star.rect.y += steps * dy

See also Follow target or mouse


Minimal example:

import pygame, random, math

class Star(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.image = img_star
        self.rect = self.image.get_rect()
        self.velocity = [0, 0]
        self.rect.x = random.randint(0, window_x)
        self.rect.y = random.randint(0, window_y)

pygame.init()
window_x, window_y = 500, 500
window = pygame.display.set_mode((window_x, window_y))
clock = pygame.time.Clock()

img_star = pygame.Surface((20, 20), pygame.SRCALPHA)
pygame.draw.circle(img_star, (255, 255, 0), (10, 10), 10)
star = Star()
group = pygame.sprite.Group(star)

run = True
while run:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            mouse_x, mouse_y = event.pos
            dx = mouse_x - star.rect.centerx
            dy = mouse_y - star.rect.centery
            dist = math.sqrt(dx*dx + dy*dy)
            steps = 10
            if dist > 0:
                star.rect.x += steps * dx / dist
                star.rect.y += steps * dy / dist

    window.fill(0)
    group.draw(window)
    pygame.display.flip()

pygame.quit()
exit()

这篇关于Pygame 让精灵在给定的旋转中行走的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 10:19