Python 为什么调用font.render()时程序会返回错误?
我正试图通过以下pdf学习pygame的一些基础知识: 我在第29页停了下来,它告诉我Rect类定义了4个角点、4个中点和1个中心点。但是当我启动程序时,它会给我一个错误,说没有名为'rect'的模块,如果我删除它,它会说font没有定义,我应该添加什么才能使它正常工作Python 为什么调用font.render()时程序会返回错误?,python,pygame,Python,Pygame,我正试图通过以下pdf学习pygame的一些基础知识: 我在第29页停了下来,它告诉我Rect类定义了4个角点、4个中点和1个中心点。但是当我启动程序时,它会给我一个错误,说没有名为'rect'的模块,如果我删除它,它会说font没有定义,我应该添加什么才能使它正常工作 import pygame from pygame.locals import * from rect import * def draw_point(text, pos): img = font.render(tex
import pygame
from pygame.locals import *
from rect import *
def draw_point(text, pos):
img = font.render(text, True, BLACK)
pygame.draw.circle(screen, RED, pos, 3)
screen.blit(img, pos)
pygame.init()
GRAY = (125, 125, 125)
GREEN = (0, 255, 0)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
screen = pygame.display.set_mode((640, 240))
rect = Rect(50, 40, 250, 80)
pts = ('topleft', 'topright', 'bottomleft', 'bottomright', 'midtop', 'midright', 'midbottom', 'midleft', 'center')
running = True
while running:
for event in pygame.event.get():
if event.type == QUIT:
running = False
screen.fill(GRAY)
pygame.draw.rect(screen, GREEN, rect, 4)
for pt in pts:
draw_point(pt, eval('rect.'+pt))
pygame.display.flip()
pygame.quit()
继续读那本书;第
3.9节通用代码包含rect.py
的定义:
import pygame
from pygame.locals import *
from random import randint
width = 500
height = 200
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
MAGENTA = (255, 0, 255)
CYAN = (0, 255, 255)
BLACK = (0, 0, 0)
GRAY = (150, 150, 150)
WHITE = (255, 255, 255)
dir = {K_LEFT: (-5, 0), K_RIGHT: (5, 0), K_UP: (0, -5), K_DOWN: (0, 5)}
pygame.init()
screen = pygame.display.set_mode((width, height))
font = pygame.font.Font(None, 24)
running = True
一旦创建了该文件,您就可以了。您必须定义字体
例如:
font = pygame.font.SysFont("arial", 35)
game_over_text = "Press Space to Start"
game_over_label = font.render(game_over_text, 1, (255,255,255))
window.blit(game_over_label, (WIDTH-550, HEIGHT-400))
您在哪里定义font
?阅读第5章,它说明了如何分配font
。您没有创建对象。e、 g.pygame.font.SysFont(无,30)
。我建议仔细阅读这份文件。