Python Pygame文本不显示
我是python新手(也不熟悉python本身的编码),当时正试图在pygame中创建一个Hello World脚本。它在python 3.2和pygame 1.9.2中。我有一本书,我直接从中复制,但当我运行它时,我得到的只是一个黑色的窗口。这是我的密码:Python Pygame文本不显示,python,pygame,Python,Pygame,我是python新手(也不熟悉python本身的编码),当时正试图在pygame中创建一个Hello World脚本。它在python 3.2和pygame 1.9.2中。我有一本书,我直接从中复制,但当我运行它时,我得到的只是一个黑色的窗口。这是我的密码: import pygame import sys pygame.init() from pygame.locals import * white = 255,255,255 blue = 0,0,200 screen = pygame.di
import pygame
import sys
pygame.init()
from pygame.locals import *
white = 255,255,255
blue = 0,0,200
screen = pygame.display.set_mode((600,500))
pygame.font.init
myfont = pygame.font.Font(None,60)
textImage = myfont.render("Hello Pygame", True, white)
screen.fill(blue)
screen.blit(textImage, (100,100))
pygame.display.update
这本书使用了完全相同的版本,但我仍然无法让它正常工作。好的,有几个问题 PyGame屏幕更新函数是
update()
,该调用和字体初始化中缺少括号
pygame.display.update()
screen = pygame.display.set_mode((600,500))
pygame.font.init()
第二,你的程序直接退出。您需要实现一个事件循环,并等待窗口关闭消息
这对我很有用:
import sys
import pygame
from pygame.locals import *
white = 255,255,255
blue = 0,0,200
pygame.init()
screen = pygame.display.set_mode((600,500))
pygame.font.init()
myfont = pygame.font.Font(None,60)
textImage = myfont.render("Hello Pygame", True, white)
screen.fill(blue)
screen.blit(textImage, (100,100))
pygame.display.update()
while (True):
event = pygame.event.wait()
if event.type == QUIT:
pygame.quit()
sys.exit()
我知道您才刚刚开始,但有一件事可以节省您以后的时间(并使之更容易),那就是将窗口的宽度和高度放入变量中。然后在屏幕上相对于这些值定位项目。这样,当您以后更改显示大小(或其他)时,只需更改这两个位置的代码
WIDTH = 600
HEIGHT = 500
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
...
text_width = textImage.get_width()
text_height = textImage.get_height()
# Centre text #TODO - handle text being larger than window
screen.blit(textImage, ( (WIDTH-text_width)//2 , (HEIGHT-text_height)//2 ))
注意:/
在python中是整数除法在最后一行的更新调用中缺少一个()
pygame.display.update()