获取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()