0

So I found a few answers online, and upon trying one, I discovered that the "bullets" are always 90 degrees away from the cursor. Any way to fix this? I have very minimal experience with trigonometry. Also, I'm very new to this thing, so don't mind the lack of structure in the code.

import pygame
import math
pygame.init()
win_height=800
win_width=800
win=pygame.display.set_mode((win_width,win_height))
pygame.display.set_caption("Shooter Game")

white=(255,255,255)
black=(0,0,0)

clock=pygame.time.Clock()
bullets=pygame.sprite.Group()

class soldier_class(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image=pygame.image.load("inkscape images for games/soldier2.png")
        self.image=pygame.transform.scale(self.image,(100,150))
        self.rect=self.image.get_rect()
    def update(self):
        new_rect=soldier_rotated.get_rect(center=(soldier.rect.x,soldier.rect.y))
        win.blit(soldier_rotated,(new_rect.x,new_rect.y))
class bullet_class(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image=pygame.Surface((25,25))
        self.image.fill(black)
        self.rect=self.image.get_rect()
        self.angle=0
    def update(self):
        self.rect.x+=5*math.cos(self.angle)
        self.rect.y-=5*math.sin(self.angle)
soldier=soldier_class()
soldier.rect.x=200
soldier.rect.y=200

while True:
    mouse=pygame.mouse.get_pos()
    clock.tick(60)
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            pygame.quit()
    key=pygame.key.get_pressed()
    if key[pygame.K_w]:
        soldier.rect.y-=5
    if key[pygame.K_s]:
        soldier.rect.y+=5
    if key[pygame.K_d]:
        soldier.rect.x+=5
    if key[pygame.K_a]:
        soldier.rect.x-=5
    if key[pygame.K_SPACE]:
        bullet=bullet_class()
        bullet.rect.x=soldier.rect.x
        bullet.rect.y=soldier.rect.y
        bullet.angle=math.atan2(mouse[0]-bullet.rect.x,mouse[1]-bullet.rect.y)
        bullets.add(bullet)
        
    angle=math.atan2(mouse[0]-soldier.rect.x,mouse[1]-soldier.rect.y)/6.28*360
    soldier_rotated=pygame.transform.rotate(soldier.image,angle)
    win.fill(white)
    soldier.update()
    bullets.update()
    bullets.draw(win)
    pygame.display.update()
    
ree
  • 107
  • 6
  • The angle can be computed by `atan2(y, x)` (instead of `atan2(x, y)`). Compensating for this error by swapping `sin` and `cos` compensates for one error with another error. – Rabbid76 Oct 28 '20 at 21:26

1 Answers1

0

Because the calculation of the angle is wrong:

while True:
    # [...]
    
    if key[pygame.K_SPACE]:
       # [...]

        #bullet.angle=math.atan2(mouse[0]-bullet.rect.x,mouse[1]-bullet.rect.y)
        bullet.angle = math.atan2(soldier.rect.y - mouse[1], mouse[0] - soldier.rect.x)
        
    #angle = math.atan2(mouse[0]-soldier.rect.x,mouse[1]-soldier.rect.y)/6.28*360
    angle = math.atan2(soldier.rect.y - mouse[1], mouse[0] - soldier.rect.x) * 180/math.pi

See How to know the angle between two points?
and How to rotate an image(player) to the mouse direction?

Rabbid76
  • 142,694
  • 23
  • 71
  • 112