我尝试运行此程序,但出现以下错误:
Traceback (most recent call last):
File "C:/Python/ComputingCW/ComputingCW.py", line 12, in <module>
Surface = pygame.image.load('Pygame_Background.jpg')((W, H), 0, 32)
TypeError: 'pygame.Surface' object is not callable
过去是:
Surface=pygame.display.set_模式((W,H),0,32),然后使用“Surface.fill(BLACK)”制作黑色背景。
我不明白为什么它不允许我使用图像作为背景。有人能帮我解释一下为什么会这样吗
import pygame, sys, time
from pygame.locals import *
pygame.init()
mainClock = pygame.time.Clock()
# all_fonts = pygame.font.get_fonts()
W = 1280
H = 900
Surface = pygame.image.load('Pygame_Background.jpg')((W, H), 0, 32)
pygame.display.set_caption('Physics in Motion')
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)
game = True
mouse_pos = (0, 0)
mouse_click = (0, 0)
text1B = False
text2B = False
text3B = False
output = ''
while game == True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYUP:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
if event.type == MOUSEMOTION:
mouse_pos = event.pos
if event.type == MOUSEBUTTONUP:
mouse_click = event.pos
color = WHITE
Font = pygame.font.SysFont('arial', 72)
if text1B:
color = RED
text = Font.render('Start Simulation', True, color)
text_rect = text.get_rect()
text_rect.center = (W / 2, H / 5)
if text_rect.collidepoint(mouse_click):
output = 'Simulation()'
if text_rect.collidepoint(mouse_pos):
text1B = True
else:
text1B = False
Surface.blit(text, text_rect)
color = WHITE
if text2B:
color = RED
Font = pygame.font.SysFont('arial', 72)
text = Font.render('Graph Generator', True, color)
text_rect = text.get_rect()
text_rect.center = (W / 2, H * 2 / 5)
if text_rect.collidepoint(mouse_click):
output = 'GraphG()'
if text_rect.collidepoint(mouse_pos):
text2B = True
else:
text2B = False
Surface.blit(text, text_rect)
color = WHITE
if text3B:
color = RED
Font = pygame.font.SysFont('arial', 72)
text = Font.render('Exit', True, color)
text_rect = text.get_rect()
text_rect.center = (W / 2, H * 3 / 5)
if text_rect.collidepoint(mouse_click):
pygame.quit()
sys.exit()
if text_rect.collidepoint(mouse_pos):
text3B = True
else:
text3B = False
Surface.blit(text, text_rect)
Font = pygame.font.SysFont('arial', 72)
text = Font.render(output, True, BLUE)
text_rect = text.get_rect()
text_rect.center = (W / 2, H * 4 / 5)
Surface.blit(text, text_rect)
pygame.display.flip()
mainClock.tick(100000)
此行} 对象。现在您有了这个曲面,并尝试像调用函数一样调用它
pygame.image.load('Pygame_Background.jpg')
从硬盘加载一个映像,并将其转换为^{((W, H), 0, 32)
,这会导致TypeError
,因为曲面对象不是函数(它不可调用)你真正想做的是:
创建显示窗口(我在这里称之为
screen
)加载背景图像(最好称之为
background
,因为Surface
是pygame.Surface
类的名称)另外,调用
pygame.Surface.convert
或convert_alpha
方法来提高性能在主while循环中将
background
和其他所有内容显示在屏幕上这看起来像是名称冲突。Surface是一个模块(或子模块),但此行似乎覆盖了此Surface对象:
我会尝试在左侧使用不同的变量,例如surf_bg
尝试使用
'Pygame_Background.jpg'
文件的绝对路径从驱动器的盘符开始跟踪目录
相关问题 更多 >
编程相关推荐