0

In python pygame I'm trying to make my player shoot a particle in the direction of the mouse, starting at the players centre

class Particle:
    def __init__(self, ..., dx, dy, x, y):
        self.dx = dx
        self.dy = dy
        self.x = x
        self.y = y
        self.Rect = self.img.get_rect()

    def draw(self):
        self.x += self.dx
        self.y += self.dy
        self.Rect.centerx = self.x
        self.Rect.centery = self.y
        screen.blit(self.img, (self.Rect.x, self.Rect.y))

theta is the angle between the centre of the player and the mouse in degrees, ranging from 0 to 360, 0 being the positive x direction

dx = math.cos(theta) 
dy = math.sin(theta)
Particle(..., dx, dy, starting_x, starting_y)

However the particles seem to fire in random directions?

Full code:

import pygame
import math

FPS = 60
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLUE = (0, 0, 255)

pygame.init()

screen = pygame.display.set_mode([300, 300])

clock = pygame.time.Clock()

class Particle:
    def __init__(self, dx, dy, x, y):
        self.dx = dx
        self.dy = dy
        self.x = x
        self.y = y
        self.Rect = pygame.Rect(100, 150, 5, 5)


    def draw(self, screen):
        self.x += self.dx
        self.y += self.dy
        self.Rect.centerx = self.x
        self.Rect.centery = self.y
        pygame.draw.rect(screen, BLUE, self.Rect)


particles = []

done = False
while not done:

    screen.fill(WHITE)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True


        if pygame.mouse.get_pressed()[0]:
            mousex = pygame.mouse.get_pos()[0]
            mousey = pygame.mouse.get_pos()[1]

            theta = math.degrees(math.atan(player_rect.x - mousey/  player_rect.y - mousex))
            dx = math.cos(theta) 
            dy = math.sin(theta)
            particles.append(Particle(dx, dy, player_rect.x, player_rect.y))

    for particle in particles:
        particle.draw(screen)



    player_rect = pygame.Rect(100, 150, 10, 10)
    pygame.draw.rect(screen, RED, player_rect)
    clock.tick(FPS)
    pygame.display.update()
pygame.quit()
k-shar
  • 79
  • 1
  • 8
  • 3
    `math.cos` and `math.sin` use [radians](https://docs.python.org/3/library/math.html); so, you having `theta = math.degrees(...` seems like the issue. If you fix that obvious issue does that take care of the problem? – Random Davis Nov 24 '20 at 19:20
  • 1
    sin and cos take the angle in radians, not in degrees. – mkrieger1 Nov 24 '20 at 19:20
  • 1
    Furthermore be aware that the y-axis needs to be reversed (`-dy`) because the y-axis is generally pointing up but in the PyGame coordinate system the y-axis is pointing down. – Rabbid76 Nov 24 '20 at 19:23
  • 1
    Also be aware of basic arithmetic rules. Division is evaluated before subtraction. – mkrieger1 Nov 24 '20 at 19:29
  • 1
    Also double check that you don't subtract y from x values. – mkrieger1 Nov 24 '20 at 19:30
  • There is no need to compute the angle. Just compute the normalized direction ([Unit vector](https://en.wikipedia.org/wiki/Unit_vector)): `dx=mousex-player_rect.x` `dy=mousey-player_rect.y` `len=math.hypot(dx, dy)` `dx/=len` `dy/=len` – Rabbid76 Nov 24 '20 at 19:42

1 Answers1

3

There is no need to compute the angle. Just compute the normalized direction (Unit vector):

dx, dy = mousex - player_rect.centerx, mousey - player_rect.centery
len = math.hypot(dx, dy)
if len > 0:
    particles.append(Particle(dx/len, dy/len, player_rect.centerx, player_rect.centery))

The center of the rectangle is player_rect.centerx, player_rect.centery respectively player_rect.center rather than player_rect.x, player_rect.y.


Complete example:

import pygame
import math

FPS = 60
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLUE = (0, 0, 255)

pygame.init()
screen = pygame.display.set_mode([300, 300])
clock = pygame.time.Clock()

class Particle:
    def __init__(self, dx, dy, x, y):
        self.dx = dx
        self.dy = dy
        self.x = x
        self.y = y
        self.Rect = pygame.Rect(100, 150, 5, 5)

    def draw(self, screen):
        self.x += self.dx
        self.y += self.dy
        self.Rect.centerx = self.x
        self.Rect.centery = self.y
        pygame.draw.rect(screen, BLUE, self.Rect)

particles = []

done = False
while not done:
    screen.fill(WHITE)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

        if pygame.mouse.get_pressed()[0]:
            mousex = pygame.mouse.get_pos()[0]
            mousey = pygame.mouse.get_pos()[1]

            dx, dy = mousex - player_rect.centerx, mousey - player_rect.centery
            len = math.hypot(dx, dy)
            if len > 0:
                particles.append(Particle(dx/len, dy/len, *player_rect.center))

    for particle in particles:
        particle.draw(screen)

    player_rect = pygame.Rect(100, 150, 10, 10)
    pygame.draw.rect(screen, RED, player_rect)
    pygame.display.flip()
    clock.tick(FPS)

pygame.quit()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174