如何解决获取pygame.Surface错误我该如何解决?
我使用Python 3.7.4和Pygame 1.9.6。我正在尝试仅创建一个测试游戏,以便我可以像泡菜一样保存每句话的得分等数据,这是我的第一次。但是一旦完成测试游戏,我就会走这条路。
我分别在ball(ballX[i],ballY[i],i)
和screen.blit(ballImg,(x,y),i)
中遇到错误。这是一个TypeError:参数1必须是pygame.Surface,而不是list。
完整追溯:
Traceback (most recent call last):
File "C:Test/main.py",line 128,in <module>
ball(ballX[i],i)
File "C:/Test/main.py",line 66,in ball
screen.blit(ballImg,i)
代码:
import pygame
import random
# Ball
ballImg = []
ballX = []
ballY = []
num_of_balls = 4
for i in range(num_of_balls):
ballImg.append(pygame.image.load('ball.png'))
ballX.append(random.randint(0,736))
ballY.append(random.randint(50,535))
'''This code is above the while running loop'''
def ball(x,y,i):
screen.blit(ballImg,i)
running = True
while running:
'''The key stuff and what not'''
'''I have the screen color load before the image appears'''
for i in range(num_of_balls):
ball(ballX[i],i)
pygame.display.update()
我不了解自己在功能/列表中做错的事情或晚上使图像变白。我觉得我正在正确地填写参数等,因此我很欣赏建议。所以谢谢你。
解决方法
几件事:
- 如@Ewong所述,您将
ballImg
传递给blit
而不是ballImg[i]
- 您不处理操作系统消息,因此程序将冻结
尝试以下代码:
import pygame
import random
scr_width = 800
scr_height = 600
screen = pygame.display.set_mode((scr_width,scr_height))
# Ball
ballImg = []
ballX = []
ballY = []
num_of_balls = 4
for i in range(num_of_balls):
ballImg.append(pygame.image.load('ball.png'))
ballX.append(random.randint(0,736))
ballY.append(random.randint(50,535))
#'''This code is above the while running loop'''
def ball(x,y,i):
screen.blit(ballImg[i],(x,y))
running = True
while running:
#'''The key stuff and what not'''
#'''I have the screen color load before the image appears'''
for i in range(num_of_balls):
ball(ballX[i],ballY[i],i)
for event in pygame.event.get(): # process OS messages
if event.type == pygame.QUIT:
pygame.quit()
break
pygame.display.update()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。