问题描述
在我的代码中,标题和图标更新不起作用。我有pygame 1.9.6,python 3.8和png文件是32.32。我的代码是:
import pygame
pygame.init()
screen = pygame.display.set_mode((800,600)
pygame.display.set_caption("Space Invader")
icon = pygame.image.load('ufo.png')
pygame.display.set_icon(icon)
playerImg = pygame.image.load('player.png')
playerX = 370
playerY = 480
def player():
screen.blit(playerImg,(playerX,playerY))
running = True
while running:
colour = (255,0)
screen.fill(colour)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.update()
解决方法
缺少)
:
screen = pygame.display.set_mode((800,600)
screen = pygame.display.set_mode((800,600))
您必须在主应用程序循环中而不是循环之后更新显示。更正Indentation:
while running:
# [...]
#-->| INDENTATION
pygame.display.update()
[...]某些系统不允许在显示窗口图标后对其进行更改。可以在
pygame.display.set_mode()
之前调用此函数,以在设置显示模式之前创建图标。
在screen = pygame.display.set_mode((800,600))
之前设置图标:
pygame.display.set_caption("Space Invader")
icon = pygame.image.load('ufo.png')
pygame.display.set_icon(icon)
screen = pygame.display.set_mode((800,600))
工作代码:
import pygame
pygame.init()
pygame.display.set_caption("Space Invader")
icon = pygame.image.load('ufo.png')
pygame.display.set_icon(icon)
screen = pygame.display.set_mode((800,600))
playerImg = pygame.image.load('player.png')
playerX = 370
playerY = 480
def player():
screen.blit(playerImg,(playerX,playerY))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
colour = (255,0)
screen.fill(colour)
player()
pygame.display.update()