pygame shooting script causing lines

70 Views Asked by At

I have this very basic shooting script made in pygame. The script works fine and the bullets move as intended, but when bullets are fired off the left side of the screen specifically, their image is streeched across the screen, creating a line that follows the bullet.

import pygame, math
pygame.init()

display = pygame.display.set_mode((600,600))
clock = pygame.time.Clock()

class Player(object):
    def __init__(self, x, y, radius):
        self.x = x
        self.y = y
        self.radius = radius

    def draw(self, display):
        pygame.draw.circle(display, (255, 0, 0), (self.x, self. y), self.radius)

class Bullet(object):
    def __init__(self, x, y, mouse_x, mouse_y):
        self.x = x
        self.y = y
        self.mouse_x = mouse_x
        self.mouse_y = mouse_y
        self.lifetime = 50
        self.speed = 15
        self.angle = math.atan2(mouse_y-self.y, mouse_x-self.x)
        self.x_vel = math.cos(self.angle) * self.speed
        self.y_vel = math.sin(self.angle) * self.speed
        
    def draw(self, draw):
        self.x += int(self.x_vel)
        self.y += int(self.y_vel)

        pygame.draw.circle(display, (255, 255, 255), (self.x, self.y), 5)
        self.lifetime-= 1


player = Player(100,100,20)
bullets = []

run = True
#mainloop
while run:
    display.fill((0, 0, 0))

    x, y = pygame.mouse.get_pos()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
            run = False

        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                bullets.append(Bullet(player.x, player. y, x, y))


    keys = pygame.key.get_pressed()

    if keys[pygame.K_w]:
        player.y -= 5
    if keys[pygame.K_s]:
        player.y += 5
    if keys[pygame.K_a]:
        player.x -= 5
    if keys[pygame.K_d]:
        player.x += 5
    
    for bullet in bullets:
        if bullet.lifetime <= 0:
            bullets.pop(bullets.index(bullet))
        bullet.draw(display)
    player.draw(display)

    clock.tick(60)
    pygame.display.update()

I can't find a reason this could be happening, so I'm forced to make bullets disappear when off-screen.

1

There are 1 best solutions below

0
Rabbid76 On

This is a bug in a recent version of pygame. You can fix it by using pygame-ce instead, a better maintained fork of pygame. The patch specifically that fixes what you're facing: https://github.com/pygame-community/pygame-ce/pull/2032