There is an error in the code that i can't identify

Viewed 54

When i try to run my code, the pygame window is opened but it stays black and unresponsive. In my experience, this only happens when the code that has been run is incorrect. Now the problem is that i cant find any error in the code. Please help me debugg it. This is my code-

import pygame as pg
import player
import pygame 
purple=(132, 66, 245)

class bullet(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((5,5))
        self.image.fill(purple)
        self.rect = self.image.get_rect()
    def move(self,dx,dy):
        self.rect.move_ip(dx,dy)

        
pg.init()

SCREEN=pg.display.set_mode((500,500))
SCREEN.fill((0,0,0))
go=True
clock=pg.time.Clock()
playerlist=pg.sprite.Group()
player1=player.PLAYER()
player1.rect.x=235
player1.rect.y=235
playerlist.add(player1)
bulletlist=pg.sprite.Group()
def Move(fx,fy,coeffy):
    alive=True
    bullet1=bullet()
    bullet1.rect.x=235
    bullet1.rect.y=235
    bulletlist.add(bullet1)
    if bullet1.rect.x==fx and bullet1.rect.y==fy:
        bulletlist.remove(bullet1)
        bullet1.kill()
        alive=False
    while alive:
        bulletlist.draw(SCREEN)
        bullet1.move(5,coeffy*5)
playerlist.draw(SCREEN)
while go:
    for event in pg.event.get():
        if event.type==pg.QUIT:
            go=False
    
    
    p1,p2,p3=pg.mouse.get_pressed()
    mousex,mousey=pg.mouse.get_pos()
    if p1:
        if mousex>235:
            distx=mousex-235
            disty=mousey-235
            coeff_y=disty/distx
            Move(mousex,mousey,coeff_y)
pg.display.flip()

In case this helps, the program is supposed to move a sprite to the place where the mouse is clicked at a constant speed.

1 Answers

The while loop in the Move function is an infinite loop, since the alive status in the loop never changes. You have an application loop, hence there is no need for an extra loop in the loop. The bullet just has to move once per frame. Change the loop in Move to a selection (change while to if)

def Move(fx,fy,coeffy):
    alive=True
    bullet1=bullet()
    bullet1.rect.x=235
    bullet1.rect.y=235
    bulletlist.add(bullet1)
    if bullet1.rect.x==fx and bullet1.rect.y==fy:
        bulletlist.remove(bullet1)
        bullet1.kill()
        alive=False
    
    if alive:                       # <--- if instead of while
        bulletlist.draw(SCREEN)
        bullet1.move(5,coeffy*5)

pg.display.flip() has to be done in the application loop, rather than after the application loop. Care about the Indentation:

while go:
    for event in pg.event.get():
        if event.type==pg.QUIT:
            go=False
    
    
    p1,p2,p3=pg.mouse.get_pressed()
    mousex,mousey=pg.mouse.get_pos()
    if p1:
        if mousex>235:
            distx=mousex-235
            disty=mousey-235
            coeff_y=disty/distx
            Move(mousex,mousey,coeff_y)

    #-->|   INDENTATION

    pg.display.flip()
Related