[英]Pygame program looking different when debuging from visual studio code versus running from command prompt
I am making chess in python using pygame.我正在使用 pygame 在 python 中下棋。 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.从命令提示符启动时,窗口和任务栏图标也不会出现,被标准的 python 应用程序图标替换。 I understand this can be fixed when compiling to .exe with pyinstaller using a command.
我知道在使用命令使用 pyinstaller 编译为 .exe 时可以解决此问题。
The user Zack explains this in the following answer:用户 Zack 在以下答案中解释了这一点:
https://stackoverflow.com/a/10438300/18072034 https://stackoverflow.com/a/10438300/18072034
With pyinstaller for example, you would call python pyinstaller.py --icon=icon.ico
以 pyinstaller 为例,您可以调用 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 和 pygame 升级到最新版本后,通过在命令提示符中输入以下命令来解决问题:(确保在执行此操作时以管理员身份打开命令提示符)
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.
显示损坏菜单的图像经过编辑,但与实际外观非常相似。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.