'how to run a single python script with multiple threads with pygame

I want to run this game with multiple cores but not on different processes so all the cores use the same pygame window rather than make the same screen many times, as the game (not the demo I included, but is similar) is vary lagging and I would like to run it at more than 20 fps.

This is a player image that you will need.

Change the name to spaceship.png.

Make sure it is in the same folder as the python script.

    import pygame
    snSzX = 800
    snSzY = 600

    # talk to pygame
    pygame.init()

    # creat screen x,y
    screen = pygame.display.set_mode((snSzX, snSzY))

    # title and icon
    pygame.display.set_caption("Space Invaders (DEMO)")

    # player write and set
    playerImg = pygame.image.load('spaceship.png')
    playerX = 370
    playerY = 480
    playerX_change = 4

    def player(x, y):  # draw to game
        screen.blit(playerImg, (x, y))

    # game checks
    running = True
    while running is True:
        # set screen , r,g,b
        screen.fill((0, 0, 0))
        # back ground
        #screen.blit(background, (0, 0))

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    playerX_change = -1
                if event.key == pygame.K_RIGHT:
                    playerX_change = 1
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    playerX_change = 0


        playerX += playerX_change

        if playerX <= 0:
            playerX = 0
        elif playerX >= 736:
            playerX = 736

        player(playerX, playerY)
        pygame.display.update()

And if this is not possible then what will I need/change to get it to work?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source