Мои спрайты не отображаются должным образом в Pygame - PullRequest
1 голос
/ 09 февраля 2020

Мои спрайты не отображаются должным образом в моей программе. Карта отображается нормально, но спрайт отображается только в течение доли секунды, когда я покидаю программу, нажимая большую X в правом верхнем углу (но не когда я выхожу, нажимая ES C)

Код отображения спрайтов:

# Protagonist Sprites
protagR = [pygame.image.load("ASG2_Graphics/JoArrow_R1_1.png"), pygame.image.load("ASG2_Graphics/JoArrow_R1_2.png"), pygame.image.load("ASG2_Graphics/JoArrow_R1_3.png"), pygame.image.load("ASG2_Graphics/JoArrow_R1_2.png"), pygame.image.load("ASG2_Graphics/JoArrow_R1_1.png")]
protagL = [pygame.image.load("ASG2_Graphics/JoArrow_L1_1.png"), pygame.image.load("ASG2_Graphics/JoArrow_L1_2.png"), pygame.image.load("ASG2_Graphics/JoArrow_L1_3.png"), pygame.image.load("ASG2_Graphics/JoArrow_L1_2.png"), pygame.image.load("ASG2_Graphics/JoArrow_L1_1.png")]
protagU = [pygame.image.load("ASG2_Graphics/JoArrow_U1_1.png"), pygame.image.load("ASG2_Graphics/JoArrow_U1_2.png"), pygame.image.load("ASG2_Graphics/JoArrow_U1_3.png"), pygame.image.load("ASG2_Graphics/JoArrow_U1_2.png"), pygame.image.load("ASG2_Graphics/JoArrow_U1_1.png")]
protagD = [pygame.image.load("ASG2_Graphics/JoArrow_D1_1.png"), pygame.image.load("ASG2_Graphics/JoArrow_D1_2.png"), pygame.image.load("ASG2_Graphics/JoArrow_D1_3.png"), pygame.image.load("ASG2_Graphics/JoArrow_D1_2.png"), pygame.image.load("ASG2_Graphics/JoArrow_D1_1.png")]
# Bullet Sprites
bulletsP = [pygame.image.load("ASG2_Graphics/Bullet_1.png"), pygame.image.load("ASG2_Graphics/Bullet_2.png"), pygame.image.load("ASG2_Graphics/Bullet_3.png"), pygame.image.load("ASG2_Graphics/Bullet_4.png")]
bulletsE = [pygame.image.load("ASG2_Graphics/E_Bullet_1.png"), pygame.image.load("ASG2_Graphics/E_Bullet_2.png"), pygame.image.load("ASG2_Graphics/E_Bullet_3.png")]
# Enemy Sprites
enemyR = [pygame.image.load("ASG2_Graphics/Enemy_R1_1.png"), pygame.image.load("ASG2_Graphics/Enemy_R1_2.png"), pygame.image.load("ASG2_Graphics/Enemy_R1_3.png"), pygame.image.load("ASG2_Graphics/Enemy_R1_4.png"), pygame.image.load("ASG2_Graphics/Enemy_R1_5.png")]
enemyL = [pygame.image.load("ASG2_Graphics/Enemy_L1_1.png"), pygame.image.load("ASG2_Graphics/Enemy_L1_2.png"), pygame.image.load("ASG2_Graphics/Enemy_L1_3.png"), pygame.image.load("ASG2_Graphics/Enemy_L1_4.png"), pygame.image.load("ASG2_Graphics/Enemy_L1_5.png")]
enemyU = [pygame.image.load("ASG2_Graphics/Enemy_U1_1.png"), pygame.image.load("ASG2_Graphics/Enemy_U1_2.png"), pygame.image.load("ASG2_Graphics/Enemy_U1_3.png"), pygame.image.load("ASG2_Graphics/Enemy_U1_4.png"), pygame.image.load("ASG2_Graphics/Enemy_U1_5.png")]
enemyD = [pygame.image.load("ASG2_Graphics/Enemy_D1_1.png"), pygame.image.load("ASG2_Graphics/Enemy_D1_2.png"), pygame.image.load("ASG2_Graphics/Enemy_D1_3.png"), pygame.image.load("ASG2_Graphics/Enemy_D1_4.png"), pygame.image.load("ASG2_Graphics/Enemy_D1_5.png")]

Код класса:

class player():
        def __init__(self, x, y, width, height, walkCount):
            self.x = x
            self.y = y
            self.width = width
            self.height = height
            self.vel = 5
            self.up = False
            self.down = False
            self.left = False
            self.right = False
            self.up = False
            self.down = False
            self.walkCount = walkCount

        def redrawGameWindow(self, window):
            dest = (self.x, self.y)
            if self.walkCount + 1 >= 30:
                self.walkCount = 0
            if self.left:
                window.blit(protagL[self.walkCount//3], dest)
                self.walkCount += 1
            elif self.right:
                window.blit(protagR[self.walkCount//3], dest)
                self.walkCount += 1
            elif self.up:
                window.blit(protagU[self.walkCount//3], dest)
                self.walkCount += 1
            elif self.down:
                window.blit(protagD[self.walkCount//3], dest)
                self.walkCount += 1
            else:
                window.blit(protagL[self.walkCount//3], dest)
            pygame.display.update()

Код отображения карты:

def multilineRender(screen, text, x, y, the_font, colour=(0, 0, 0), justification="left"):
    justification = justification[0].upper()
    # text = text.strip().replace('\r','').split('\n')
    max_width = 0
    text_bitmaps = []
    # Convert all the text into bitmaps, calculate the justification width
    for char in text:
        text_bitmap = the_font.render(char, True, colour)
        text_width = text_bitmap.get_width()
        text_bitmaps.append((text_width, text_bitmap))
        if (max_width < text_width):
            max_width = text_width
    # Paint all the text bitmaps to the screen with justification
    for (width, bitmap) in text_bitmaps:
        xPos = x
        width_diff = max_width - width
        if justification == 'R':  # right-justify
            xPos = x + width_diff
        elif justification == 'C':  # centre-justify
            xPos = x + (width_diff // 2)

        screen.blit(bitmap, (xPos, y))
        y += bitmap.get_height()
    pygame.display.update()

Код отображения символов:

def movePlayer(board):
    clock = pygame.time.Clock()
    run = True
    while run:
        clock.tick(15)

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

        arrowKeys = pygame.key.get_pressed()
        if arrowKeys[pygame.K_LEFT] and board.x > 25 + board.vel:
            board.x -= board.vel
            board.left = True
            board.right = False
            board.up = False
            board.down = False
        elif arrowKeys[pygame.K_RIGHT] and board.x < 510 - board.width - board.vel:
            board.x += board.vel
            board.right = True
            board.left = False
            board.up = False
            board.down = False
        elif arrowKeys[pygame.K_UP] and board.y > 40 + board.vel:
            board.y -= board.vel
            board.right = False
            board.left = False
            board.up = True
            board.down = False
        elif arrowKeys[pygame.K_DOWN] and board.y < 450 - board.height - board.vel:
            board.y += board.vel
            board.right = False
            board.left = False
            board.up = False
            board.down = True
        elif arrowKeys[pygame.K_ESCAPE]:  # ESC key
            stopGame()
        else:
            board.right = False
            board.left = False
            board.up = False
            board.down = False
            board.walkCount = 0

    board.redrawGameWindow(window)
    pygame.display.quit() 

Основной код:

def main():
    inFile = open("map(TEST).txt", "r")
    text = inFile.read().splitlines()  # splitlines is to exclude the '\n'
    inFile.close()
    window.fill((0, 0, 0))

    myfont = pygame.font.SysFont("Calibri", 35)

    window.fill((0,0,0))
    board = player(30, 45, 64, 64, 0)
    multilineRender(window, text, 20, 20, myfont, (255, 255, 255))

    while True:
        # movePlayer(board)
        multilineRender(window, text, 20, 20, myfont, (255, 255, 255))
        movePlayer(board)

Я должен также упомянуть, что при выходе из программы я получаю эту ошибку:

Traceback (most recent call last):
  File "C:/Users/User/PycharmProjects/Python_Projects/Asg2/m5_PYGAME_STYLE.py", line 156, in <module>
    main()
  File "C:/Users/User/PycharmProjects/Python_Projects/Asg2/m5_PYGAME_STYLE.py", line 153, in main
    multilineRender(window, text, 20, 20, myfont, (255, 255, 255))
  File "C:/Users/User/PycharmProjects/Python_Projects/Asg2/m5_PYGAME_STYLE.py", line 83, in multilineRender
    screen.blit(bitmap, (xPos, y))
pygame.error: display Surface quit

... но я сомневаюсь, что это связано.

1 Ответ

0 голосов
/ 09 февраля 2020

Удалить pygame.display.quit() форму movePlayer И повернуть while l oop в movePlayer для выбора if. Обратите внимание, что у вас есть oop в main, поэтому используйте его.
Более того, я рекомендую сделать pygame.display.update() в основном.

Основное приложение l oop должно:

  • обрабатывает события и обновляет объекты, зависящие от входных событий.
  • очищает дисплей
  • рисует сцену
  • обновляет отображение
def main():
    inFile = open("map(TEST).txt", "r")
    text = inFile.read().splitlines()  # splitlines is to exclude the '\n'
    inFile.close()
    myfont = pygame.font.SysFont("Calibri", 35)

    board = player(30, 45, 64, 64, 0)

    clock = pygame.time.Clock()
    run = True
    while run:
        clock.tick(15)

        # clear the display
        window.fill((0, 0, 0))

        # draw the scene
        board.redrawGameWindow(window)
        multilineRender(window, text, 20, 20, myfont, (255, 255, 255))

        # update the display 
        pygame.display.update()

        # handle the events and update the objects
        run = movePlayer(board)

movePlayer должен вернуть true, если игра все еще работает, а false:

def movePlayer(board):

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

    arrowKeys = pygame.key.get_pressed()
    if arrowKeys[pygame.K_LEFT] and board.x > 25 + board.vel:
        board.x -= board.vel
        board.left, board.right, board.up, board.down = True, False, False, False
    elif arrowKeys[pygame.K_RIGHT] and board.x < 510 - board.width - board.vel:
        board.x += board.vel
        board.left, board.right, board.up, board.down = False, True, False, False
    elif arrowKeys[pygame.K_UP] and board.y > 40 + board.vel:
        board.y -= board.vel
        board.left, board.right, board.up, board.down = False, False, True, False
    elif arrowKeys[pygame.K_DOWN] and board.y < 450 - board.height - board.vel:
        board.y += board.vel
        board.left, board.right, board.up, board.down = False, False, False, True
    elif arrowKeys[pygame.K_ESCAPE]:  # ESC key
        stopGame()
    else:
        board.left, board.right, board.up, board.down = False, False, False, False
        board.walkCount = 0

    return run

multilineRender не pygame.display.update():

def multilineRender(screen, text, x, y, the_font, colour=(0, 0, 0), justification="left"):
    justification = justification[0].upper()
    # text = text.strip().replace('\r','').split('\n')
    max_width = 0
    text_bitmaps = []
    # Convert all the text into bitmaps, calculate the justification width
    for char in text:
        text_bitmap = the_font.render(char, True, colour)
        text_width = text_bitmap.get_width()
        text_bitmaps.append((text_width, text_bitmap))
        if (max_width < text_width):
            max_width = text_width
    # Paint all the text bitmaps to the screen with justification
    for (width, bitmap) in text_bitmaps:
        xPos = x
        width_diff = max_width - width
        if justification == 'R':  # right-justify
            xPos = x + width_diff
        elif justification == 'C':  # centre-justify
            xPos = x + (width_diff // 2)

        screen.blit(bitmap, (xPos, y))
        y += bitmap.get_height()
...