简体   繁体   中英

Pygame program looking different when debuging from visual studio code versus running from command prompt

I am making chess in python using pygame. The starting menu is composed of two buttons. Their border is renderd to the screen using

pygame.draw.rect(screen, (255, 255, 255), text1rect.inflate(20, 20), 10)

This is the code that I use to do this:

import pygame
from sys import exit

pygame.init()


screen = pygame.display.set_mode((800, 800))
pygame.display.set_caption("Chess")

icon = pygame.image.load("./Images/wking.png").convert_alpha()
pygame.display.set_icon(icon)

font = pygame.font.Font("Roboto-Regular.ttf", 32)
text1 = font.render("Create game", True, (0, 255, 0), (124, 124, 124))
text1_rect = text1.get_rect(center = (400, 300))

text2 = font.render("Join game", True, (0, 255, 0), (124, 124, 124))
text2_rect = text2.get_rect(center = (400, 500))


board = pygame.image.load("./Images/board.png").convert()

def main():
    scene_to_render = "menu"
    clock = pygame.time.Clock()
    while True:
        mouse_pos = pygame.mouse.get_pos()
        is_clicked = pygame.mouse.get_pressed()[0]
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                exit()

        if scene_to_render == "menu":
            screen.fill((0, 0, 0))
            screen.blit(text1, text1_rect)
            screen.blit(text2, text2_rect)
            pygame.draw.rect(screen, (255, 255, 255), text1_rect.inflate(20, 20), 10)
            pygame.draw.rect(screen, (255, 255, 255), text2_rect.inflate(20, 20), 10)

            if text1_rect.collidepoint(mouse_pos) and is_clicked:
                pass
            if text2_rect.collidepoint(mouse_pos) and is_clicked:
                pass

        # Obviously there is more code but that is not important because only the menu buttons are broken when starting from the command prompt

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

if __name__ == "__main__":
    main()

The window and taskbar icon also dosen't appear when starting from the command prompt, being replaced with the standard python application icon. I understand this can be fixed when compiling to .exe with pyinstaller using a command.

The user Zack explains this in the following answer:

https://stackoverflow.com/a/10438300/18072034

With pyinstaller for example, you would call python pyinstaller.py --icon=icon.ico

This is how the menu looks when starting from vscode:

在 vscode 中调试时菜单的外观

This is how the menu looks when running from cmd:

从 cmd 运行时菜单的外观

I actually figured this out while writting the question, the problem was fixed after upgrading pip and pygame to the latest versions by entering in the command prompt the following commands: (Make sure you open command prompt as an administartor when doing this)

pip install --upgrade pip

and

pip install --upgrade pygame

Also, because I fixed the problem before finishing the question I couldn't make a screenshot of a problem. The image that showcases the broken menu is edited but also very simmilar to how it actualy looked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM