2
2
2tieatie2022-04-04 18:42:39
pygame
2tieatie, 2022-04-04 18:42:39

How to make an object rotate towards the cursor in pygame?

below is the code separately for the gun in the game, you need to make it turn towards the cursor, please write specifically for my case

import pygame
import math
from pygame import Vector2
class Gun():
    def __init__(self, screen):
        self.screen = screen
        self.image = pygame.image.load('gunimage.png')
        self.rect = self.image.get_rect()
        self.screen_rect = self.screen.get_rect()
        self.rect.centerx = self.screen_rect.centerx
        self.rect.bottom = self.screen_rect.bottom
        self.mright = False
        self.mleft = False
        self.mdown = False
        self.mup = False
        self.position = Vector2()
    def update_gun(self):
        if self.mright and self.rect.right < self.screen_rect.right:
            self.rect.centerx += 1
        elif self.mleft and self.rect.left > 0:
            self.rect.centerx -= 1
        elif self.mdown and self.rect.bottom < self.screen_rect.bottom:
            self.rect.centery += 1
        elif self.mup and self.rect.top > 0:
            self.rect.centery -= 1

    def output(self):
        self.screen.blit(self.image, self.rect)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question