PyGame keeps crashing and i dont know why

Viewed 107

Im currently trying to get random movement for the people class, and every time i run the program it crashes and im not sure why.

I thought it was a information overload so i put some delay on it but it still is crashing. Im assuming that im not using pygame correctly or that there is a command im not running/running wrong but i keep looking for solutions and i cant find any. This is also the first project i have done that's this big so i could have been doing more wrong then i thought.

pygame.init()
(width, height) = (600, 600)
screen = pygame.display.set_mode((width, height))
pygame.display.flip()
pygame.display.set_caption('kings Game')
running = True

class King(object):
    #reproduction,Food count, food distributuon, movment
    Status = 1

    def __init__(self,kfood,King_reproduction):
        self.food = kfood
        self.King_reproduction = King_reproduction

    def Charecter():

        x=50
        y=50
        width=60
        highth=60
        vel=5
        Status = 1

        pygame.draw.circle(screen,(0,0,255), (x,y), 10,)
        pygame.display.update()

        #KEYS = pygame.key.get_pressed()

        while Status == 1:
            direction = random.randrange(3)
            time.sleep(2)
            if direction == 0:  #right
                x += vel
                #time.sleep(5)
                print(direction)
            elif direction == 1:#left
                x -= vel
                #time.sleep(5)
                print(direction)
            elif direction == 2:#up
                y += vel
                #time.sleep(5)
                print(direction)
            elif direction == 3:#down
                y -= vel
                #time.sleep(5)
                print(direction)
            else:
                break
            screen.fill(0)

    def reproduction():
        pass

    def food(kfood):
        if kfood == 1:
            print('bet')
        else:
            print('nope')

class People():
    #movment, reproduction, giving king food, finding food
    def reproduction():
        pass
    def movment():
        pass
    def food():
        pass

class Food():
    #spawn
    pass

test = King.Charecter()

test

while running == True:
    pg.display.flip()
    for event in pg.event.get():
        if event.type==pg.event.QUIT:
            running = False
1 Answers

Change the class King. Make x, y, width, height and vel instance attributes.
Character has to be an instance Method, which does a single "step" rather than an endless while loop.

Note, the loop in Character changes the position of the object, but it dose't draw the character or handle the events, so you didn't "see" the changes of the object.
Anyway it is no good idea to create multiple application loops. Use the game loop to handel the events and to draw the scene continuously.

class King(object):
    #reproduction,Food count, food distributuon, movment
    Status = 1

    def __init__(self, kfood=None, King_reproduction=None):
        self.food = kfood
        self.King_reproduction = King_reproduction
        self.x=50
        self.y=50
        self.width=60
        self.highth=60
        self.vel=5

    def Character(self, surface):

        #KEYS = pygame.key.get_pressed()
        if self.Status == 1:
            self.direction = random.randrange(4)
            if self.direction == 0:  #right
                self.x += self.vel
                print(self.direction)
            elif self.direction == 1:#left
                self.x -= self.vel
                print(self.direction)
            elif self.direction == 2:#up
                self.y += self.vel
                print(self.direction)
            elif self.direction == 3:#down
                self.y -= self.vel
                print(self.direction)

        pygame.draw.circle(surface, (0,0,255), (self.x,self.y), 10)

Create an instance of King and use the main application loop, to continuously update respectively draw the object:

test = King()

while running == True:

    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            running = False

    screen.fill(0)
    test.Character(screen)
    pygame.display.update()
Related