0

I did not make this code, I copy and pasted it because I wanted to learn how to control a projectile with a vector that is controlled by mouse (I would link the source, but I forgot where I found it, so if you know where it came from, leave it in the comment thing, I'm new to this forum).

import pygame
import math

mousehitbox = pygame.Rect(0, 0, 11, 11)
screen = pygame.display.set_mode((500, 500))


class Player(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.image = pygame.Surface((32, 32))
        self.image.fill((0, 0, 0))
        self.image.set_colorkey((0, 0, 0))
        pygame.draw.polygon(self.image, pygame.Color('dodgerblue'), ((0, 0), (32, 16), (0, 32)))
        self.org_image = self.image.copy()
        self.angle = 0
        self.direction = pygame.Vector2(1, 0)
        self.rect = self.image.get_rect()
        self.x_change = 0
        self.y_change = 0
        self.pos = pygame.Vector2(self.rect.center)

    def update(self, events, dt):
        mousehitbox.center = pygame.mouse.get_pos()
        for e in events:
            if e.type == pygame.KEYDOWN:
                if e.key == pygame.K_SPACE:
                    self.groups()[0].add(Projectile(self.rect.center, self.direction))
        pressed = pygame.key.get_pressed()
        if pressed[pygame.K_q]:
            self.angle += 6
        if pressed[pygame.K_e]:
            self.angle -= 6
        if pressed[pygame.K_w]:
            self.y_change -= 3
        if pressed[pygame.K_a]:
            self.x_change -= 3
        if pressed[pygame.K_s]:
            self.y_change += 3
        if pressed[pygame.K_d]:
            self.x_change += 3

        self.orientation()
        self.image = pygame.transform.rotate(self.org_image, self.angle)
        self.rect = self.image.get_rect(center=self.rect.center)

        self.rect.x += self.x_change
        self.rect.y += self.y_change

        self.x_change = 0
        self.y_change = 0

    def orientation(self):
        self.direction = pygame.Vector2(mousehitbox.x-self.rect.x, mousehitbox.y-self.rect.y).rotate(-self.angle)
        # print(self.direction)
        self.angle = -(math.degrees(math.tan((mousehitbox.y-self.rect.y) / (mousehitbox.x-self.rect.x))))
        print(self.angle)
        print(mousehitbox)


class Projectile(pygame.sprite.Sprite):
    def __init__(self, pos, direction):
        super().__init__()
        self.image = pygame.Surface((8, 8))
        self.image.fill((0, 0, 0))
        self.image.set_colorkey((0, 0, 0))
        pygame.draw.circle(self.image, pygame.Color('orange'), (4, 4), 4)
        self.rect = self.image.get_rect(center=pos)
        self.direction = direction
        self.pos = pygame.Vector2(self.rect.center)

    def update(self, events, dt):
        self.pos += self.direction * dt
        self.rect.center = self.pos
        if not pygame.display.get_surface().get_rect().contains(self.rect):
            self.kill()


def main():
    pygame.init()
    sprites = pygame.sprite.Group(Player())
    clock = pygame.time.Clock()
    dt = 0

    while True:
        events = pygame.event.get()
        for e in events:
            if e.type == pygame.QUIT:
                return
        sprites.update(events, dt)
        screen.fill((30, 30, 30))
        sprites.draw(screen)
        pygame.draw.rect(screen, (0, 255, 0), mousehitbox, 3, 1)
        pygame.display.update()
        dt = clock.tick(60)


if __name__ == '__main__':
    main()

But, there are several things I just can't understand, like, why is the player incredibly unstable when I aim to 5 o'clock from the sprite? I used print to check what's going on with the variables and I saw 2 different outputs from the same equation with same input. Does anyone know what's actually happening?

Mobilizes
  • 9
  • 2

0 Answers0