0
import pygame,math

background_colour = (87, 151, 153)
(width, height) = (1920, 1080)
screen = pygame.display.set_mode((1920, 1080))
pygame.display.set_caption('Shooter 2D Game')
screen.fill(background_colour)
global ticks
ticks = 0
pygame.display.flip()


class player:
    global screen,ticks,move_ticker
    def __init__(self,x,y,colour,speed):

        self.x = x
        self.y = y
        self.colour = colour
        self.speed = speed
        self.last = ticks
        self.cooldown = 100
        self.bullet_velocity = 1.9

    def over_boundary(self):

        self.boundary = False
        if self.x < 0:
            self.boundary = True

            self.x = 1920
        elif self.x > 1920:

            self.boundary = True
            self.x = 0
        elif self.y < 0:

            self.boundary = True
            self.y = 1080
        elif self.y > 1080:

            self.boundary = True
            self.y = 0



    def move(self):

        global move_ticker
        move_ticker = 0
        self.over_boundary()
        self.old_x = self.x
        self.old_y = self.y





        keys = pygame.key.get_pressed()




        if keys[pygame.K_a]:
            if self.boundary == False:
                if move_ticker == 0:
                    move_ticker = 30000
                    self.x -= self.speed
        self.over_boundary()
        if keys[pygame.K_d]:
            if self.boundary == False:
                if move_ticker == 0:
                    move_ticker = 30000
                    self.x += self.speed

        self.over_boundary()
        if keys[pygame.K_w]:
            if self.boundary == False:
                if move_ticker == 0:
                    move_ticker = 30000
                    self.y -= self.speed

        self.over_boundary()
        if keys[pygame.K_s]:
            if self.boundary == False:
                if move_ticker == 0:
                    move_ticker = 30000
                    self.y += self.speed

    def check_shoot(self):
        (mouse_y,mouse_x) = pygame.mouse.get_pos()
        if pygame.mouse.get_pressed(3)[0] and self.now - self.last >= self.cooldown:
            angle = math.atan2(mouse_x - self.x,mouse_y - self.y)
            print(angle)
            self.vel_x= self.bullet_velocity * math.cos(angle);
            self.vel_y = self.bullet_velocity * math.sin(angle)

        if self.now - self.last >= self.cooldown and pygame.mouse.get_pressed(3)[0]:
            self.last = self.now
            bullets.append(bullet(self.x,self.y,self.vel_x,self.vel_y,(255,0,0),self))

    def draw(self):
        pygame.draw.rect(screen,self.colour,(self.x,self.y,100,100))
    def update(self):
        self.now = ticks
        self.move()
        pygame.draw.rect(screen,background_colour,(self.old_x,self.old_y,100,100))
        self.draw()
        self.check_shoot()







class bullet():
    global screen,background_colour,ticks
    def __init__(self,x,y,vel_x,vel_y,colour,obj):
        self.new_x = x
        self.new_y = y
        self.colour = colour
        self.last = ticks
        self.obj = obj
        self.vel_x = vel_x
        self.vel_y = vel_y


        self.collide_with_wall()
    def collide_with_wall(self):
        self.wall_collide = False
        if self.new_x < 0:
            self.wall_collide = True
        if self.new_x > 1920:
            self.wall_collide = True
        if self.new_y < 0:
            print("hello")
            self.wall_collide = True
        if self.new_y > 1080:
            self.wall_collide = True




    def draw(self):
        pygame.draw.rect(screen,self.colour,(self.new_x,self.new_y,33,33))
    def clear_up(self,w,h):
        pygame.draw.rect(screen,background_colour,(self.old_x,self.old_y,w,h))
    def update(self):
        self.collide_with_wall()
        self.old_x = self.new_x
        self.old_y = self.new_y
        self.clear_up(33,33)

        # where to change x and y to move the bullet
        self.now = ticks
        self.new_x += self.vel_x * (self.now - self.last)
        self.new_y += self.vel_y * (self.now - self.last)
        self.last = ticks


        self.draw()



global bullets
shooter_one = player(400,400,(255,255,255),2)

bullets = []
running = True

while running:
    ticks += 1
    screen.fill(background_colour)
    for i in bullets:
        if i.wall_collide:
            bullets.remove(i)
        else:
            i.update()
    shooter_one.update()
    pygame.display.update()
    if move_ticker > 0:
        move_ticker -= 1
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
          running = False

My problem lies in the fact that the bullets are only accurately shot from when the position where the player spawns and as the player moves away from this spawning position the it gradually breaks down to the point where the mouse is being clicked on one side and the bullets fly out to another spot. I've tried alternate methods that don't require trig but they don't seem to work at all.

No errors are produced when complied and run.

0 Answers0