Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

python - How do I rotate a sprite towards the mouse and move it?

I'm confused by the fact that in my code the sprite's coordinates don't seem to change where it is. Putting (200, 200) would be the same as putting (900, 100000). So basically i cannot coordinate the sprite in a designated position. Can you help?

Turning credits to Ann Zen But the sprite problems on me Pygame sprite not turning accordingly to mouse

My code:

import pygame
from math import atan2, degrees
# tank = pygame.image.load('Sprite0.png')
wn = pygame.display.set_mode((400, 400))

class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load('Sprite0.png')
        self.x = 0
        self.y = 0
        self.rect = self.image.get_rect()



    def point_at(self, x, y):
        rotated_image = pygame.transform.rotate(self.image, degrees(atan2(x-self.rect.x, y-self.rect.y)))
        new_rect = rotated_image.get_rect(center=self.rect.center)
        wn.fill((255, 255, 255))
        wn.blit(rotated_image, new_rect.topleft)




player = Player()
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
        elif event.type == pygame.MOUSEMOTION:
            player.point_at(*pygame.mouse.get_pos())

    # wn.blit(tank, Player)
    pygame.display.update()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Remove the elif event.type == pygame.MOUSEMOTION: block, and put the player.point_at(*pygame.mouse.get_pos()) directly into the while loop.

Create a clock so that the sprite won't zip out of the screen.

Finally, add

    keys = pygame.key.get_pressed()
    if keys[pygame.K_w]:
        player.rect.y -= 1
    if keys[pygame.K_s]:
        player.rect.y += 1
    if keys[pygame.K_a]:
        player.rect.x -= 1
    if keys[pygame.K_d]:
        player.rect.x += 1

to control the player.

Example:

import pygame
from math import atan2, degrees

wn = pygame.display.set_mode((400, 400))

class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((30, 40), pygame.SRCALPHA)
        self.image.fill((255, 0, 0))
        self.rect = self.image.get_rect(topleft=(185, 180))

    def point_at(self, x, y):
        rotated_image = pygame.transform.rotate(self.image, degrees(atan2(x-self.rect.x, y-self.rect.y)))
        new_rect = rotated_image.get_rect(center=self.rect.center)
        wn.fill((0, 0, 0))
        wn.blit(rotated_image, new_rect.topleft)

player = Player()
clock = pygame.time.Clock() # Create the clock

while True:
    clock.tick(30) # Use the clock
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()

    keys = pygame.key.get_pressed() # Get al the pressed keys
    if keys[pygame.K_w]:
        player.rect.y -= 1
    if keys[pygame.K_s]:
        player.rect.y += 1
    if keys[pygame.K_a]:
        player.rect.x -= 1
    if keys[pygame.K_d]:
        player.rect.x += 1
        
    player.point_at(*pygame.mouse.get_pos()) # Put this here
    pygame.display.update()

Output:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...